该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
double sum = 0;//记录部分和
bool blnClear = false; //表示如果再输入数字或小数点,先清除编辑框中显示的前一个加数
string strOper = "+"; //记录输入的运算符
double dbSencond;//保存第二个数据
public void addNum(int num)//添加数字
{
textBox1.Text = textBox1.Text + num.ToString();
//如果显示框的第一个字符为0,而且第二个字符不是小数点,则自动去掉第一个字符‘0’
if(textBox1.Text.Substring(0,1)=="0"&&textBox1.Text.Substring(1,1)!=".")
{
textBox1.Text=textBox1.Text.Substring(1);
}
}
//0
private void button28_Click(object sender, EventArgs e)
{
if (blnClear) //如果为真,输入下一个加数前,先清除textBox1显示内容
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0") //如果前面已经输入非零数字,如:12
textBox1.Text += b1.Text; //此时键入0,应为120
else
textBox1.Text = b1.Text; //如已输入的数字为0,显示的数字应为0
//dbSencond = 0;
}
//1
private void button22_Click(object sender, EventArgs e)
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
//dbSencond = 1;
}
//除法
private void button14_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
{
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
if (sender == button14)
{
strOper = "/";
}
if (sender == button27)
{
strOper = "=";
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//乘法
private void button20_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
{
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
if (sender == button20)
{
strOper = "*";
}
if (sender == button27)
{
strOper = "=";
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//减法
private void button26_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//加法
private void button32_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear) //如果未输入第二个操作符,不运算,例如连续输入+-*/或=
{
switch (strOper) //按上次记录的运算符号运算
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
textBox1.Text = Convert.ToString(sum); // 显示部分和
blnClear = true; // 设置标记使键入另一个加数前,先清除显示的前个加数
}
//等于
private void button27_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
if (!blnClear)
{
switch (strOper)
{
case "+":
sum += dbSencond;
break;
case "-":
sum -= dbSencond;
break;
case "*":
sum *= dbSencond;
break;
case "/":
sum /= dbSencond;
break;
}
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
//清除当前显示数字
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = "0";//显示屏清空
sum = 0; //临时计算结果归零
blnClear = false;
strOper = "+";
}
//清除当前计算
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = "0";
sum = 0;
blnClear = false;
strOper = "+";
}
//小数点
private void button30_Click(object sender, EventArgs e)
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
int n = textBox1.Text.IndexOf(".");
if (n == -1) //如果没有小数点,增加小数点,否则不增加
textBox1.Text = textBox1.Text + ".";
}
//正负符号
private void button8_Click(object sender, EventArgs e)
{
double dbSencond = Convert.ToDouble(textBox1.Text);
textBox1.Text = Convert.ToString(-dbSencond);
}
//√
private void button9_Click(object sender, EventArgs e)
{
}
}
}