资源描述:
《简易计算器源代码C#》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、简易计算器源代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace简易计算器{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}doublenum1,num2,result
2、;//操作数及运算结果booldecimalFlag=false;//判断输入的是否为小数stringmyOperator;//操作类型#region调用NumClick方法publicvoidNumClick(intmyNumber){if(decimalFlag)//输入小数{txtReadout.Text=txtReadout.Text+myNumber;}else{if(txtReadout.Text=="0.")//输入整数{//输入的是整数的第一个数字txtReadout.Text=myNumber+".";}else{//输入的不是整数的
3、第一个数字txtReadout.Text=txtReadout.Text.Substring(0,txtReadout.Text.Length-1)+myNumber+".";}}}#endregion#region窗体载入事件代码privatevoidForm1_Load(objectsender,EventArgse){txtReadout.Text="0.";//指定文本框的对齐方式为右对齐txtReadout.TextAlign=HorizontalAlignment.Right;}#endregion#region数字按钮【0】~【9】的单击
4、事件代码privatevoidbtnNum0_Click(objectsender,EventArgse){NumClick(0);}privatevoidbtnNum1_Click(objectsender,EventArgse){NumClick(1);}privatevoidbtnNum2_Click(objectsender,EventArgse){NumClick(2);}privatevoidbtnNum3_Click(objectsender,EventArgse){NumClick(3);}privatevoidbtnNum4_Clic
5、k(objectsender,EventArgse){NumClick(4);}privatevoidbtnNum5_Click(objectsender,EventArgse){NumClick(5);}privatevoidbtnNum6_Click(objectsender,EventArgse){NumClick(6);}privatevoidbtnNum7_Click(objectsender,EventArgse){NumClick(7);}privatevoidbtnNum8_Click(objectsender,EventArgse){
6、NumClick(8);}privatevoidbtnNum9_Click(objectsender,EventArgse){NumClick(9);}#endregion#region【.】按钮的单击事件代码privatevoidbtnDot_Click(objectsender,EventArgse){decimalFlag=true;//输入小数点}#endregion#region【C】按钮的单击事件代码privatevoidbtnC_Click(objectsender,EventArgse){num1=0;num2=0;txtReadout
7、.Text="0.";decimalFlag=false;}#endregion#region【+】、【-】、【*】、【/】按钮的单击事件代码privatevoidbtnAdd_Click(objectsender,EventArgse){myOperator="add";num1=double.Parse(txtReadout.Text);txtReadout.Text="0.";decimalFlag=false;}privatevoidbtnSub_Click(objectsender,EventArgse){myOperator="Sub";n
8、um1=double.Parse(txtReadout.Text);txtReadout.Te