用visual_studio制作的简单的计算器。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
首先利用visual studio里面的button和textbox模块建立上图模块。然后就是编写程序:
该程序最重要的是要理解清楚计算器的运算逻辑。
public partial class Form1 : Form
{
string a, b, q, ac;
int c = 0, d = 0, n = 0, p = 0, m = 0;
double temp1 = -1;
int pos = 0;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void addNum(int num)
{
textBox1.Text = textBox1.Text +
num.ToString();
if (c > 0) // 管¨¹理¤¨ª加¨®减?乘?除y运?算?
{
textBox1.Text = "";
a = num.ToString();
b = b + a;
textBox1.Text = b;
}
}
private void button1_Click(object sender, EventArgs e)
{
addNum(7); //向¨°显?示º?屏¨¢加¨®字Á? n++;
}
private void button2_Click(object sender, EventArgs e)
{
addNum(8);
}
private void button3_Click(object sender, EventArgs e)
{
addNum(9);
}
EventArgs e)
{
addNum(4);
}
private void button6_Click(object sender, EventArgs e)
{
addNum(5);
}
private void button7_Click(object sender, EventArgs e)
{
addNum(6);
}
EventArgs e)
{
addNum(1);
}
private void button10_Click(object sender, EventArgs e)
{
addNum(2);
}
private void button11_Click(object sender, EventArgs e)
{
addNum(3);
}
private void button14_Click(object sender, EventArgs e)
{
addNum(0);
}
private void button15_Click(object sender, EventArgs e)
{
if (p==0)
textBox1.Text = textBox1.Text + ".";//加¨®上¦?小?数ºy点Ì?
p++;
m++;
if (c > 0) // 管¨¹理¤¨ª加¨®减?乘?除y运?算?
{
b = b + ".";
}
}
private void button4_Click(object sender, EventArgs e)
{
pos = 1;
temp1 = Convert .ToDouble (textBox1.Text); textBox1.Text =temp1+"+";
d++;
c++;
p = 0;
}
private void button8_Click(object sender, EventArgs e)
{
pos = 2;
temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = temp1 + "-";
c++; p = 0;
}
private void button12_Click(object sender, EventArgs e)
{
pos = 3;
temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = temp1 + "*";
c++; p = 0;
}
private void button16_Click(object sender, EventArgs e)
{
pos = 4;
temp1 = Convert.ToDouble(textBox1.Text); textBox1.Text = temp1 + "/";
c++; p = 0;
}
private void button13_Click(object sender, EventArgs e)
{
double temp2 = Convert.ToDouble(b);