//名字空间MyCalClass
//类名: CalClass
//无参的构造函数CalClass()
//6个用到的私有变量
//void PointClick() 按小数点
//string CE() 是计算器复位用的,clear函数
//string BackSpace(string Txt) 倒退键,参数是计算过程中现在显示的计算结果
//string NumClick(string Txt,int NumKey) 0-9数字键按下的响应,Txt是计算的当前结果,NumKey是按下的数字值
//string OpClick(string Txt,int OpKey) +,-,*,/,=几个键按下时的处理,Txt是计算的当前结果,OpKey是按下操作键的数字IDnamespace MyCalClass
{
public class CalClass
{
private string ResultTxt; //Get and Show The Result of the TextBox in application
private bool PointIsDown; //If the radix point was clicked;
private double MidResult; //To Store middle results
private double Results; //To store finally result
private int LastClick; //To sign last clicking button class,is number(1) or operator(2)
private int LastOperator; //To sign last clicked button of operator is who?0 is Null,1 is +,2 is - ,3 is *,4 is /,5 is =;
public CalClass()
{
ResultTxt = "0.";
PointIsDown = false;
MidResult = 0;
Results = 0;
LastClick = 0;
LastOperator = 0;
} public void PointClick() //按下小数点后的函数处理
{
PointIsDown = true;
return;
} public string CE() //按下CE键,清理计算器
{
ResultTxt = "0.";
MidResult = 0;
Results = 0;
PointIsDown = false;
LastClick = 0;
LastOperator = 0;
return ResultTxt;
}
public string BackSpace(string Txt) //后退键按下后的处理函数
{
if(LastClick == 2)return Txt; //假如上次的按键是操作,不是数字,直接返回,不能再退回 if (Txt[Txt.Length - 1] == '.') //计算器文本里最后一位是小数点时的情况:
{
if (PointIsDown == true) { PointIsDown = false; return Txt; } //假如小数点按下了,则退去小数点
else
{
if (Txt.Length == 2 && Txt[0] != '0') return "0."; //假如现在只有一位整数,"X."的情况,直接返回"0."
if (Txt.Length > 2) { ResultTxt = Txt.Remove(Txt.Length - 2, 2) + "."; return ResultTxt; }//否则,把最后一位去掉;
}
}
else//假如最后一们不是小数点,很简单,直接把最后一们去掉就行了!
{
ResultTxt = Txt.Remove(Txt.Length - 1, 1);
return ResultTxt;
}
return ResultTxt;//最后还要返回一个东西,程序里要求的,虽然根本运行不到这里!
} public string NumClick(string Txt, int NumKey)//按下数字键(0-9)后的处理函数
{
//ResultTxt=Txt;
LastClick = 1;//先标志一下,最后一次按键是"数字键"
if (Txt[Txt.Length - 1] == '.')//最后一位是小数点的情况:
{
if (PointIsDown == true)//小数点已经被按下
{
ResultTxt = Txt + NumKey.ToString();//直接把按下的数字加到计算结果字符串上
}
else//小数点没被按下的话
{
if (Txt.Length == 2 && Txt[0] == '0')//假如现在的数值是0.的话,则把现在的数值直接变成按下的数字
{
ResultTxt = Txt.Replace("0", NumKey.ToString());
}
else
{
ResultTxt = Txt.Insert(Txt.Length - 2, NumKey.ToString());否则,就在小数点前加上当前按下的数字
}
}
}
else//最后一位不是小数点的情况:直接把数字加到最后
{
ResultTxt = Txt + NumKey.ToString();
}
return ResultTxt;
}
public string OpClick(string Txt, int OpKey)//运算操作键按下的处理
{
bool HASPoint=false;//用来标志已有的运算结果中,是否有小数点
PointIsDown = false;//按下任意运算键,小数点都设为未按下状态
if (LastClick == 2 && LastOperator != 5)//假如上一次按下的是操作运算键,并且不是等于号,则把上一次按下的内容覆盖,然后直接返回
{
LastClick = 2;
LastOperator = OpKey;
return Txt;
}
Results=double.Parse(Txt);//把现有的计算结果从string转为double型 switch (LastOperator)//根据上一次按下的操作按键来确定这次要做的运算
{
case 0:
MidResult=Results;//假如从未按过运算符键,则把当前的数值做为第一操作数
break;
//以下1-4是计算加,减,乘,除运算
case 1:
Results += MidResult;
break;
case 2:
Results = MidResult - Results;
break;
case 3:
Results *= MidResult;
break;
case 4:
if (Results == 0.0) { throw new System.Exception("Can Div with 0!"); }//除数为零的异常
Results = MidResult / Results;
break;
case 5:
MidResult=Results;//假如上一次按下等于号,同样要把当前的数值做为第一操作数
break;
}
ResultTxt=Results.ToString();
for(int i=0;i<ResultTxt.Length;i++)//整数计算时,最后结尾会没有小数点,这里要加上计算结果里有没有小数点的标志
{
if(ResultTxt[i]=='.'){HasPoint=true;break;}
}
if (HasPoint) return ResultTxt;
else return ResultTxt + ".";
}
}
}