标签:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//点击运算符为true 点完数字为false
private string ysf;//记录上一次运算符
private string bds;//记录已经算完的表达式
private decimal sum;//记录上一步结果
private bool isok = true;//用来判断是否刚刚点过运算符
private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;//sender为事件主题 强制转换为button类型
if (isok)
{
textBox2.Text = btn.Text;//添加新的btn.text的字符串
isok = false;
}
else
{
textBox2.Text += btn.Text; //添加btn.text的字符串
}
}
private void buttonC_Click(object sender, EventArgs e)//清除所有数据从新开始
{
textBox1.Text="";
textBox2.Text = "0";
isok = true;
ysf = null;
sum = 0;
}
private void button加_Click(object sender, EventArgs e) //运算符
{
Button btn = (Button)sender;
string yunsuanfu = btn.Text; //定义一个string类型的yunsuanfu 来接收按的运算符
if (ysf== null) //if判断的是 是不是第一次点击运算符
{
sum = decimal.Parse(textBox2.Text); //第一次点击运算符计算出的结果转换类型并显示在box2里面
bds = textBox2.Text; //记录表达式
}
else //不是第一次点击运算符
{
if (!isok)
{
if (ysf == "+")//判断点击的运算符
{
sum = sum + decimal.Parse(textBox2.Text);//第二次点击运算符计算出的结果转换类型并显示在box2里面
}
if (ysf == "-")
{
sum = sum - decimal.Parse(textBox2.Text);
}
if (ysf == "*")
{
sum = sum * decimal.Parse(textBox2.Text);
}
if (ysf == "/")
{
sum = sum / decimal.Parse(textBox2.Text);
}
if (ysf == "%")
{
sum = sum % decimal.Parse(textBox2.Text);
}
bds = bds + ysf + textBox2.Text;//现在的表达式=原来的表达式+第二次的运算符+刚点进去的数字
}
}
textBox1.Text = bds + yunsuanfu;//上面的显示表达式+你刚点过的运算符
ysf = yunsuanfu;//保存上一次点击的运算符并赋到ysf变量中
isok = true;
textBox2.Text = sum.ToString(); //显示结果
}
private void button等于_Click(object sender, EventArgs e)//等于
{
textBox1.Text = "";
if (ysf == "+")
{
sum = sum + decimal.Parse(textBox2.Text);
}
if (ysf == "-")
{
sum = sum - decimal.Parse(textBox2.Text);
}
if (ysf == "*")
{
sum = sum * decimal.Parse(textBox2.Text);
}
if (ysf == "/")
{
//if (decimal.Parse(textBox2.Text) == 0)
//{
// textBox2.Text = "除数不可为0";
//}
sum = sum / decimal.Parse(textBox2.Text);
}
if (ysf == "%")
{
sum = sum % decimal.Parse(textBox2.Text);
}
textBox2.Text = sum.ToString();
sum = 0;
bds = "";
ysf = null;
isok = true;
}
private void button加减_Click(object sender, EventArgs e)//正负
{
if (!isok)
{
if (decimal.Parse(textBox2.Text) > 0)
{
textBox2.Text = "-" + textBox2.Text;
}
else
{
textBox2.Text = textBox2.Text.Substring(1);
}
}
}
private void button点_Click(object sender, EventArgs e)//小数点
{
if (textBox2.Text == "0")
{
textBox2.Text = "0.";
}
if (!isok && !textBox2.Text.Contains("."))
{
textBox2.Text += ".";
}
isok = false;
}
private void button箭头_Click(object sender, EventArgs e)//撤销
{
if (!isok)//禁止撤销得出的结果
{
if (textBox2.Text.Length == 1)
{
textBox2.Text = "0";
isok = true;
}
else
{
textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/Mr-xue/p/4542303.html