当前位置: 代码迷 >> 综合 >> 《Visual C# 程序设计》课程学习(19)——简单计算器代码
  详细解决方案

《Visual C# 程序设计》课程学习(19)——简单计算器代码

热度:135   发布时间:2023-10-01 12:25:43.0

《Visual C# 程序设计》课程学习(19)——简单计算器代码

namespace compterExample
{public partial class Form1 : Form{public string tempstr1="";public string tempstr2="";public string oper="";public Form1(){InitializeComponent();}
//其他从2到9以及0 的代码均调用btn1_clickprivate void btn1_Click(object sender, EventArgs e){Button btn = (Button)sender;if (tempstr1==""){if (textBox1.Text == "0"){textBox1.Text = btn.Text;}else{textBox1.Text += btn.Text;}}else {tempstr2 += btn.Text;textBox1.Text =tempstr2;}}
//此为清空代码private void button1_Click(object sender, EventArgs e){textBox1.Text = "0";tempstr1 = "";tempstr2 = "";oper = "";}
//此为点号代码private void btndot_Click(object sender, EventArgs e){if (tempstr1 == ""){textBox1.Text += btndot.Text;}else{if (tempstr2 == ""){tempstr2 = "0.";}else{tempstr2 += btndot.Text;} textBox1.Text = tempstr2;}}private void btnadd_Click(object sender, EventArgs e){oper = "+";tempstr1 = textBox1.Text;}private void btnsub_Click(object sender, EventArgs e){oper = "-";tempstr1 = textBox1.Text;}private void btnmul_Click(object sender, EventArgs e){oper = "*";tempstr1 = textBox1.Text;}private void btndiv_Click(object sender, EventArgs e){oper = "/";tempstr1 = textBox1.Text;}private void btnequal_Click(object sender, EventArgs e){double result=0.0;switch (oper){case "+":result = Convert.ToDouble(tempstr1) + Convert.ToDouble(tempstr2);break;case "-":result = Convert.ToDouble(tempstr1) - Convert.ToDouble(tempstr2);break;case "*":result = Convert.ToDouble(tempstr1) *Convert.ToDouble(tempstr2);break;case "/":result = Convert.ToDouble(tempstr1) / Convert.ToDouble(tempstr2);break;}textBox1.Text = result.ToString();}}
}

 

  相关解决方案