首页上一页 1 下一页尾页 4 条记录 1/1页
简单计算器怎么实现连续运算?
发表在C#图书答疑
2016-03-23
是否精华
是
否
版块置顶:
是
否
我参照网络上的方法,编写了一个简单计算器。但是只能正确实现一个数加一个数,一个数减一个数的运算。如果连续按等于号,会使得运算结果出错。例如:计算2+1,按键以后显示结果为3,再按等号则会出现5,7,9,……我明明想要的是2+1=3,再按等号以后结果应该为4,5,6,……,该怎么改程序才能实现?减法、乘法、出发也实现这种连续运算的功能?编程代码如下:
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 calculator
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
double temp1 = -1;//记录第一个数字
int pos = 0;
private void addnum(int num)
{
textBox1.Text = textBox1.Text + num.ToString();
}
private void btn0_Click(object sender, EventArgs e)//输入数字0
{
addnum(0);
}
private void btn1_Click(object sender, EventArgs e)//输入数字1
{
addnum(1);
}
private void btn2_Click(object sender, EventArgs e)
{
addnum(2);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn3_Click(object sender, EventArgs e)
{
addnum(3);
}
private void btn4_Click(object sender, EventArgs e)
{
addnum(4);
}
private void btn5_Click(object sender, EventArgs e)
{
addnum(5);
}
private void btn6_Click(object sender, EventArgs e)
{
addnum(6);
}
private void btn7_Click(object sender, EventArgs e)
{
addnum(7);
}
private void btn8_Click(object sender, EventArgs e)
{
addnum(8);
}
private void btn9_Click(object sender, EventArgs e)
{
addnum(9);
}
private void btnadd_Click(object sender, EventArgs e)//加法运算
{
pos = 1;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btnsub_Click(object sender, EventArgs e)//减法运算
{
pos = 2;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btnmul_Click(object sender, EventArgs e)//乘法运算
{
pos = 3;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btndiv_Click(object sender, EventArgs e)//除法运算
{
pos = 4;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btnclear_Click(object sender, EventArgs e)//清空
{
textBox1.Text = "0";
temp1 = 0;
pos = 0;
}
private void btnnum_Click(object sender, EventArgs e)//按等号键
{
double temp2 = Convert.ToDouble(textBox1.Text);
switch (pos)
{
case 1:
textBox1.Text = (temp1 + temp2).ToString();
break;
case 2:
textBox1.Text = (temp1 - temp2).ToString();
break;
case 3:
textBox1.Text = (temp1 * temp2).ToString();
break;
case 4:
textBox1.Text = (temp1 / temp2).ToString();
break;
}
}
private void btnpoint_Click(object sender, EventArgs e)//输入小数点
{
if (textBox1.Text == "")
textBox1.Text = "0.";
else if (textBox1.Text.IndexOf(".") >= 0)
MessageBox.Show("已经添加了小数点!", "提示");
else
textBox1.Text = textBox1.Text + ".";
}
}
}
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 calculator
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
double temp1 = -1;//记录第一个数字
int pos = 0;
private void addnum(int num)
{
textBox1.Text = textBox1.Text + num.ToString();
}
private void btn0_Click(object sender, EventArgs e)//输入数字0
{
addnum(0);
}
private void btn1_Click(object sender, EventArgs e)//输入数字1
{
addnum(1);
}
private void btn2_Click(object sender, EventArgs e)
{
addnum(2);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn3_Click(object sender, EventArgs e)
{
addnum(3);
}
private void btn4_Click(object sender, EventArgs e)
{
addnum(4);
}
private void btn5_Click(object sender, EventArgs e)
{
addnum(5);
}
private void btn6_Click(object sender, EventArgs e)
{
addnum(6);
}
private void btn7_Click(object sender, EventArgs e)
{
addnum(7);
}
private void btn8_Click(object sender, EventArgs e)
{
addnum(8);
}
private void btn9_Click(object sender, EventArgs e)
{
addnum(9);
}
private void btnadd_Click(object sender, EventArgs e)//加法运算
{
pos = 1;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btnsub_Click(object sender, EventArgs e)//减法运算
{
pos = 2;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btnmul_Click(object sender, EventArgs e)//乘法运算
{
pos = 3;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btndiv_Click(object sender, EventArgs e)//除法运算
{
pos = 4;
temp1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
private void btnclear_Click(object sender, EventArgs e)//清空
{
textBox1.Text = "0";
temp1 = 0;
pos = 0;
}
private void btnnum_Click(object sender, EventArgs e)//按等号键
{
double temp2 = Convert.ToDouble(textBox1.Text);
switch (pos)
{
case 1:
textBox1.Text = (temp1 + temp2).ToString();
break;
case 2:
textBox1.Text = (temp1 - temp2).ToString();
break;
case 3:
textBox1.Text = (temp1 * temp2).ToString();
break;
case 4:
textBox1.Text = (temp1 / temp2).ToString();
break;
}
}
private void btnpoint_Click(object sender, EventArgs e)//输入小数点
{
if (textBox1.Text == "")
textBox1.Text = "0.";
else if (textBox1.Text.IndexOf(".") >= 0)
MessageBox.Show("已经添加了小数点!", "提示");
else
textBox1.Text = textBox1.Text + ".";
}
}
}