自己写的计算器,请大家给点意见
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JSQ
{
public partial class Form1 : Form
{
private string _op;//运算符
private float _result;//数字
private bool _setop = false;//是否按下运算符
public Form1()
{
InitializeComponent();
}
//数字键
private void btn0_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (_setop || this.textBox1.Text == "0" && this.textBox1.Text.Length == 1)
this.textBox1.Clear();
Button but = (Button)sender;
this.textBox1.Text += but.Text;
_setop = false;
}
//小数点
private void btnspot_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Length > 0 && this.textBox1.Text.IndexOf(".") == -1)
this.textBox1.Text += ".";
}
//清空
private void btnclean_Click(object sender, EventArgs e)
{
this.textBox1.Clear();
}
//退格
private void btn10_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Length > 1)
this.textBox1.Text = this.textBox1.Text.Remove(this.textBox1.Text.Length - 1);
else
this.textBox1.Text = "0";
}
//运算符
private void btnadd_Click(object sender, EventArgs e)
{
Calculate();
_result =float.Parse( this.textBox1.Text);
Button btn=(Button)sender;
_op =btn.Text;
_setop = true;
}
//运算
private void Calculate()
{
float f1 = float.Parse(this.textBox1.Text);
switch (_op)
{
case "+":
_result = _result + f1;
//this.textBox1.Text = (_result + f1).ToString();
break;
case "-":
//this.textBox1.Text = (_result + f1).ToString();
_result = _result - f1;
break;
case "*":
//this.textBox1.Text = (_result + f1).ToString();
_result = _result * f1;
break;
case "/":
//this.textBox1.Text = (_result + f1).ToString();
_result = _result * 1.0f / f1;
break;
case null:
_result = f1;
break;
}
this.textBox1.Text = _result.ToString();
}
//等于
private void btnamount_Click(object sender, EventArgs e)
{
Calculate();
_op=null;
_setop=false;
}
}
}