C# 简单计算器红河学院.docx
《C# 简单计算器红河学院.docx》由会员分享,可在线阅读,更多相关《C# 简单计算器红河学院.docx(19页珍藏版)》请在冰豆网上搜索。
C#简单计算器红河学院
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacecaculator
{
publicpartialclassCalculator:
Form
{
publicCalculator()
{
InitializeComponent();
}
inti=0;
boolshownNull=false;
boolover=false;
Numbern1=newNumber();
Numbern2=newNumber();
publicdelegatedoubleDualfunction(Numbera,Numberb);
publicDualfunctionfun;
publicvoidsetvalue()
{
if(Normal.count21==0)//这里要求在调用GetDualfunction(inti)之后才对count21进行加加;
n1.Getvalue(textBox1.Text);
else
n2.Getvalue(textBox1.Text);
Normal.count11=0;
Normal.count12=0;
Normal.count13=0;
}
publicboolGetDualfunction(inti)//具体方法操作____委托处理
{
try
{
if(i==1)
{
setvalue();
fun=Number.Add;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
if(i==2)
{
setvalue();
fun=Number.Sub;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
if(i==3)
{
setvalue();
fun=Number.Mul;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
if(i==4)
{
setvalue();
if(n2.numeber==0)
{
textBox1.Text="除数不能为0!
请重新输入一个除数或者直接按CE键重新开始";
shownNull=true;
Normal.repeat=true;//防止未正常运算而按下其他操作符导致出现程序异常;
returnfalse;
}
else
{
fun=Number.Div;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
}
if(i==5)
{
setvalue();
if(n2.numeber==0)
{
textBox1.Text="不能用0对一个数求余!
请重新输入一个求余数或者直接按CE键重新开始";
shownNull=true;
Normal.repeat=true;//防止未正常运算而按下其他操作符导致出现程序异常;
returnfalse;
}
else
{
fun=Number.Model;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
}
if(i==6)
{
n1.Getvalue(textBox1.Text);//一元操作无需调用setvalue();
if(n1.numeber<=0)
{
textBox1.Text="非正数不能开方!
请重新输入一个开方数或者直接按CE键重新开始";
shownNull=true;
Normal.repeat=true;//防止未正常运算而按下其他操作符导致出现程序异常;
returnfalse;
}
else
{
fun=Number.Sqrt;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;//这里的Normal.repeat=false语句不能省略,因为紧接着的可能直接是二元操作符;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
}
if(i==7)
{
n1.Getvalue(textBox1.Text);
if(n1.numeber==0)
{
textBox1.Text="除数不能为0!
请重新输入一个求倒数或者直接按CE键重新开始";
shownNull=true;
Normal.repeat=true;//防止未正常运算而按下其他操作符导致出现程序异常;
returnfalse;
}
else
{
fun=Number.Dao;
textBox1.Text=fun(n1,n2).ToString();
Normal.repeat=false;//这里的Normal.repeat=false语句不能省略,因为紧接着的可能直接是二元操作符;
shownNull=true;
Normal.count21=0;
Normal.count22=0;
returntrue;
}
}
else
returnfalse;
//else
//textBox1.Text="您还没输入任何操作数!
";
}
catch(Exceptione)
{
textBox1.Text=e.Message+"=>"+"请按CE键重新开始!
";
returnfalse;
}
}
publicvoidsettext(strings)//文本输入
{
Normal.repeat=false;
if(shownNull)textBox1.Text=null;
if(Normal.count11==0)textBox1.Text=s;
elsetextBox1.Text+=s;
shownNull=false;
}
//以下的是按钮,全都是在正常情况下的输入和响应!
!
!
privatevoidbtn0_Click(objectsender,EventArgse)
{
if(Normal.oneQuestion())
{
settext("0");
Normal.count11++;
Normal.count13++;
Normal.count14++;
}
}
privatevoidbtn1_Click(objectsender,EventArgse)
{
settext("1");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn2_Click(objectsender,EventArgse)
{
settext("2");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn3_Click(objectsender,EventArgse)
{
settext("3");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn4_Click(objectsender,EventArgse)
{
settext("4");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn5_Click(objectsender,EventArgse)
{
settext("5");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn6_Click(objectsender,EventArgse)
{
settext("6");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn7_Click(objectsender,EventArgse)
{
settext("7");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn8_Click(objectsender,EventArgse)
{
settext("8");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtn9_Click(objectsender,EventArgse)
{
settext("9");
Normal.count11++;
Normal.count12++;
Normal.count14++;
}
privatevoidbtnAdd_Click(objectsender,EventArgse)
{
if(Normal.twoQuestion())
{
if(Normal.count21==0)
{
setvalue();
i=1;
}
elseif(Normal.count21==1)
{
if(GetDualfunction(i))
i=1;
}
Normal.count21=1;
Normal.repeat=true;
shownNull=true;
}
}
privatevoidbtnSub_Click(objectsender,EventArgse)
{
if(Normal.twoQuestion())
{
if(Normal.count21==0)
{
setvalue();
i=2;
}
elseif(Normal.count21==1)
{
if(GetDualfunction(i))
i=2;
}
Normal.count21=1;
Normal.repeat=true;
shownNull=true;
}
}
privatevoidbtnMul_Click(objectsender,EventArgse)
{
if(Normal.twoQuestion())
{
if(Normal.count21==0)
{
setvalue();
i=3;
}
elseif(Normal.count21==1)
{
if(GetDualfunction(i))
i=3;
}
Normal.count21=1;
Normal.repeat=true;
shownNull=true;
}
}
privatevoidbtnDiv_Click(objectsender,EventArgse)
{
if(Normal.twoQuestion())
{
if(Normal.count21==0)
{
setvalue();
i=4;
}
elseif(Normal.count21==1)
{
if(GetDualfunction(i))
i=4;
}
Normal.count21=1;
Normal.repeat=true;
shownNull=true;
}
}
privatevoidbtnMod_Click(objectsender,EventArgse)
{
if(Normal.twoQuestion())
{
if(Normal.count21==0)
{
setvalue();
i=5;
}
elseif(Normal.count21==1)
{
if(GetDualfunction(i))
i=5;
}
Normal.count21=1;
Normal.repeat=true;
shownNull=true;
}
}
privatevoidbtnSqr_Click(objectsender,EventArgse)
{
if(Normal.threeQuestion())
{
if(Normal.count21==1)GetDualfunction(i);
GetDualfunction(6);
}
}
privatevoidbtnDao_Click(objectsender,EventArgse)
{
if(Normal.threeQuestion())
{
if(Normal.count21==1)GetDualfunction(i);
GetDualfunction(7);
}
}
privatevoidbtnPoint_Click(objectsender,EventArgse)
{
if(Normal.fourQuestion())
{
textBox1.Text+=".";
Normal.count11++;
}
}
privatevoidbtnExc_Click(objectsender,EventArgse)
{
if(Normal.fiveQuestion())
{
textBox1.Text=(-double.Parse(textBox1.Text)).ToString();
}
}
privatevoidbtnEqu_Click(objectsender,EventArgse)
{
if(Normal.sixQuestion())
{
GetDualfunction(i);
}
}
privatevoidbtnBac_Click(objectsender,EventArgse)
{
if(Normal.sevenQuestion())
{
strings=textBox1.Text.Substring(textBox1.Text.Length-1,1);
if(s!
=".")//如果不是小数点则转为数值;
{
doublen=double.Parse(s);
if(0elseif(n==0)Normal.count13--;
}
textBox1.Text=textBox1.Text.Substring(0,textBox1.Text.Length-1);
if(Normal.count11==1)//当画面上最后一个字符退掉后文本框里应置为初始化;
textBox1.Text="0";
Normal.count11--;
}
}
privatevoidbtnCle_Click(objectsender,EventArgse)
{
i=0;
textBox1.Text="0";
n1.numeber=0;
n2.numeber=0;
Normal.count11=0;
Normal.count12=0;
Normal.count13=0;
Normal.count14=0;
Normal.count21=0;
Normal.count22=0;
Normal.repeat=false;
}
privatevoidCalculator_Load(objectsender,EventArgse)
{
}
}
publicclassNumber
{
publicdoublenumeber=0;
publicvoidGetvalue(strings)
{
numeber=double.Parse(s);
}
publicstaticdoubleAdd(Numbera,Numberb)
{
doubleresult;
result=a.numeber+b.numeber;
a.numeber=result;
b.numeber=0;
returnresult;
}
publicstaticdoubleSub(Numbera,Numberb)
{
doubleresult;
result=a.numeber-b.numeber;
a.numeber=result;
b.numeber=0;
returnresult;
}
publicstaticdoubleMul(Numbera,Numberb)
{
doubleresult;
result=a.numeber*b.numeber;
a.numeber=result;
b.numeber=0;
returnresult;
}
publicstaticdoubleDiv(Numbera,Numberb)
{
doubleresult;
result=a.numeber/b.numeber;
a.numeber=result;
b.numeber=0;
returnresult;
}
publicstaticdoubleModel(Numbera,Numberb)
{
doubleresult;
result=a.numeber%b.numeber;
a.numeber=result;
b.numeber=0;
returnresult;
}
publicstaticdoubleSqrt(Numbera,Numberb)
{
doubleresult;
result=Math.Sqrt(a.numeber);
a.numeber=result;
b.numeber=0;
returnresult;
}
publicstaticdoubleDao(Numbera,Numberb)
{
doubleresult;
result=1.0/a.numeber;
a.numeber=result;
b.numeber=0;
returnresult;
}
}
publicclassNormal
{
publicstaticintcount11=0;//总的输入次数(一次赋值);
publicstaticintcount12=0;//非0的输入次数(一次赋值);
publicstaticintcount13=0;//0的输入次数(一次赋值);
publicstaticintcount14=0;//一轮运算总的输入次数;
publicstaticintcount21=0;//二元操作符的存储值(一轮运算),不是等于0就是等于1;
publicstaticintcount22=0;//等于操作符的存储值(一轮运算)
publicstaticboolrepeat=false;
publicstaticbooloneQuestion()//0的个数问题
{
if(count12>0||count13==0||(count13>0&&count11-(count12+count13)==1))
returntrue;
else
returnfalse;
}
publicstaticbooltwoQuestion()//二元运算问题
{
if(!
repeat&&count14>0)
retur