您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > C# 编程 科学计算器
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceCalculator{publicpartialclassCounter:Form{publicCounter(){InitializeComponent();}doublenum1,num2;doubleresult=0;intop;intflag=0;//开始标志intsp=0;//判断除数为零时移除监听intmax=0;//范围标识inteq=0;#region功能键privatevoidon_Click(objectsender,EventArgse)//开始{flag=1;sp=0;max=0;this.numInput.Text=0;this.txtInput.Text=;}privatevoidoff_Click(objectsender,EventArgse)//关闭{flag=0;sp=0;max=0;this.numInput.Text=;this.txtInput.Text=;}privatevoidbutton9_Click(objectsender,EventArgse)//删除{max=0;if(flag==1&&sp==0){stringnumstring=this.numInput.Text;if(numstring.Length0){stringtemp=numstring.Substring(0,numstring.Length-1);this.numInput.Text=temp.ToString();}else{this.numInput.Text=0;}}}privatevoidclear_Click(objectsender,EventArgse)//c清零{max=0;if(flag==1){sp=0;this.numInput.Text=0;this.txtInput.Text=;}}privatevoiddelete_Click(objectsender,EventArgse)//清零{max=0;if(flag==1){sp=0;this.numInput.Text=0;}}#endregion#region运算符privatevoidequal_Click(objectsender,EventArgse)//等号{eq=1;if(flag==1&&sp==0){max=0;num2=Convert.ToDouble(this.numInput.Text);this.txtInput.Text+=num2.ToString();this.txtInput.Text+==;txtInput.Text=null;switch(op){case1:result=num1+num2;break;case2:result=num1-num2;break;case3:result=num1*num2;break;case4:result=num1/num2;break;case5:result=Math.Pow(num1,num2);break;case6:result=Mod(num1,num2);break;default:return;}if(op==4&&num2==0){this.numInput.Text=除数不能为0;sp=1;}else//this.txtInput.Text+=result.ToString();this.numInput.Text=result.ToString();}}privatevoidsub_Click(objectsender,EventArgse)//减法{max=0;if(flag==1&&sp==0){num1=Convert.ToDouble(this.numInput.Text);this.txtInput.Text+=num1.ToString();numInput.Text=;this.txtInput.Text+=-;op=2;}}privatevoidmul_Click(objectsender,EventArgse)//乘法{max=0;if(flag==1&&sp==0){num1=Convert.ToDouble(this.numInput.Text);this.txtInput.Text+=num1.ToString();this.numInput.Text=;this.txtInput.Text+=*;op=3;}}privatevoiddivide_Click(objectsender,EventArgse)//除法{max=0;if(flag==1&&sp==0){num1=Convert.ToDouble(this.numInput.Text);this.txtInput.Text+=num1.ToString();this.numInput.Text=;this.txtInput.Text+=/;op=4;}}privatevoidplus_Click(objectsender,EventArgse)//加法{max=0;if(flag==1&&sp==0){num1=Convert.ToDouble(this.numInput.Text);this.txtInput.Text+=num1.ToString();numInput.Text=;this.txtInput.Text+=+;op=1;}}publicdoubleMod(doublex,doubley)//取模运算{max=0;inttemp=Convert.ToInt32(x/y);return(x-y*temp);}privatevoidpower_Click(objectsender,EventArgse)//幂运算{max=0;if(flag==1&&sp==0){num1=Convert.ToDouble(numInput.Text);this.txtInput.Text+=num1.ToString();this.numInput.Text=;this.txtInput.Text+=^;op=5;}}privatevoidrecip_Click(objectsender,EventArgse)//倒数{max=0;if(flag==1&&sp==0){num1=Convert.ToDouble(this.numInput.Text);doubletemp=0;temp=1/num1;this.txtInput.Text=1/+num1.ToString()+=;this.numInput.Text=temp.ToString();}}privatevoidpercent_Click(objectsender,EventArgse){max=0;if(flag==1&&sp==0){//doubletemp=Convert.ToDouble(this.numInput.Text);//this.txtInput.Text+=temp.ToString();//temp=temp/100;//this.numInput.Text=temp.ToString();//this.txtInput.Text+=%=;num1=Convert.ToDouble(this.numInput.Text);this.txtInput.Text+=num1.ToString();this.numInput.Text=;this.txtInput.Text+=%;op=6;}}#endregion#region数字privatevoidone_Click(objectsender,EventArgse)//1{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=1;eq=0;}elsethis.numInput.Text+=1;}}privatevoidtwo_Click(objectsender,EventArgse)//2{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=2;eq=0;}elsethis.numInput.Text+=2;//this.txtInput.Text+=2;}}privatevoidthree_Click(objectsender,EventArgse)//3{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=3;eq=0;}elsethis.numInput.Text+=3;//this.txtInput.Text+=3;}}privatevoidfour_Click(objectsender,EventArgse)//4{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=4;eq=0;}elsethis.numInput.Text+=4;//this.txtInput.Text+=4;}}privatevoidfive_Click(objectsender,EventArgse)//5{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=5;eq=0;}elsethis.numInput.Text+=5;//this.txtInput.Text+=5;}}privatevoidsix_Click(objectsender,EventArgse)//6{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=6;eq=0;}elsethis.numInput.Text+=6;//this.txtInput.Text+=6;}}privatevoidseven_Click(objectsender,EventArgse)//7{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=7;eq=0;}elsethis.numInput.Text+=7;//this.txtInput.Text+=7;}}privatevoideight_Click(objectsender,EventArgse)//8{if(flag==1&&sp==0&&max==0){if(this.numInput.Text==0){this.numInput.Text=;}if(eq==1){this.numInput.Text=8;eq=0;}elsethis.numInput.Text+=8;//this.txtInput.Text+=8;}}privatev
本文标题:C# 编程 科学计算器
链接地址:https://www.777doc.com/doc-7028722 .html