C#计算器连按等号实现连加功能?
大家好,我想用C#语言写一个计算器的程序,实现如下功能:第一次输入2+1= 得到结果3,之后按等号就可以一直执行+1操作,显示结果4,5,6……如果输入的是2+3= 得到结果5,之后按等号就可以一直执行+3操作,显示结果8,11,14……
以下是已经写好的没有连加功能的程序,该如何改动呢?
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
double sum = 0;//记录部分和
bool blnClear = false; //表示如果再输入数字或小数点,先清除编辑框中显示的前一个加数
string strOper = "+"; //记录输入的运算符
public Form1()
{
InitializeComponent();
}
private void button10_Click(object sender, EventArgs e) //0数字键
{
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
}
private void button1_Click(object sender, EventArgs e) //1数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button2_Click(object sender, EventArgs e) //2数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button3_Click(object sender, EventArgs e) //3数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button4_Click(object sender, EventArgs e) //4数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button5_Click(object sender, EventArgs e) //5数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button6_Click(object sender, EventArgs e) //6数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button7_Click(object sender, EventArgs e) //7数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button8_Click(object sender, EventArgs e) //8数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button9_Click(object sender, EventArgs e) //9数字键
{
if (blnClear)
{
textBox1.Text = "0";
blnClear = false;
}
Button b1 = (Button)sender;
if (textBox1.Text != "0")
textBox1.Text += b1.Text;
else
textBox1.Text = b1.Text;
}
private void button11_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 button13_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 == button13) //记录此次键入的运算符
strOper = "+";
if (sender == button12)
strOper = "=";
textBox1.Text = Convert.ToString(sum ); // 显示部分和
blnClear = true; // 设置标记使键入另一个加数前,先清除显示的前个加数
}
private void button12_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 == button13)
strOper = "+";
if (sender == button12)
{
strOper = "=";
}
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
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 == button12)
strOper = "=";
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
private void button15_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 == button15)
strOper = "*";
if (sender == button12)
strOper = "=";
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
private void button16_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 == button16)
strOper = "/";
if (sender == button12)
strOper = "=";
textBox1.Text = Convert.ToString(sum);
blnClear = true;
}
private void button17_Click(object sender, EventArgs e) //清除键的代码
{
textBox1.Text = "0";
sum = 0;
blnClear = false;
strOper = "+";
}
}
}
[ 本帖最后由 mosiying 于 2011-4-6 21:53 编辑 ]