用C#代码写的计算器,怎么样实现联系运算。

我用C#写了个类似xp里自带的计算器,但是无法实现联系相加连续相减连续相乘连续相除,还有三角函数怎么写,希望能帮我解答!

连续运算
你可以定义三个全局变量A=0,B=0和C=0
检查是否为数字:
private int IsNumeric(string number)
{
try
{
return int.Parse(number);
}
catch
{
MessageBox("输入有误!");
}
}
各运算符按钮代码:
switch(C)
{
case 0:A=IsNumeric(TextBox1.Text);C=当前运算的代号;break;//1代表+,2代表-,3是*……这个自己定义
case 1:B=IsNumeric(TextBox1.Text);TextBox1.Text=(A+B).ToString();C=当前运算的代号;break;
case 2:B=IsNumeric(TextBox1.Text);TextBox1.Text=(A-B).ToString();C=当前运算的代号;break;
……//根据自己需要写
}
数字键代码就自己写吧,思路是这样的当按过运算或等于按钮时,第一次按数字键要先清空TextBox的值

三角函数
public static double tricorn(double n,string str)
{
double pi=System.Math.PI;
switch(str)
{
case "sin": return System.Math.Sin(n*pi/180);
case "cos":return System.Math.Cos(n*pi/180);
case "tan":return System.Math.Tan(n*pi/180);
case "cot":return 1.0/System.Math.Tan(n*pi/180);
case "asin":return System.Math.Asin(n);
case "acos":return System.Math.Acos(n);
case "atan":return System.Math.Atan(n);
case "acot":return System.Math.Atan(1.0/n);
default:
return 0;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-26
用以下方法来实现按钮的动作

例: bDot.click+=net EventHandler(btn_clk);;//EventHandler类是事件代表类,用来注册事件的处理方法.

?/第一个参数是object类型,指向发出事件的对象;

?/第二个参数是EventArgs类型,包含了关于这个事件的数据

3,用以下方法来判断运算以及运算操作

private void calc(){
switch(strOper){
case "+":
dblAcc+=dblSec;;//加法运算
break;
case "-":
dblAcc-=dblSec;;//减法运算
break;
case "*":
dblAcc*=dblSec;;//乘法运算
break;
case "/":
dblAcc/=dblSec;;//除法运算
break;
}
strOper="=";;//等号运算
blnFrstOpen=true;

txtCalc.Text=Convert.ToString(dblAcc);;//将运算结果转换成字符型,并输出结果

dblSec=dblAcc;
}
第2个回答  2013-10-26
用一个中间变量存储运算结果阿。用高等级的数据类型在做如double类型
上一次运算的结果保存在中间变量里,下一次运算用中间变量与本次的输入进行计算。
另外不要用Try来判断用户的输入是否是数字,Try只用来捕获不可预料的异常,否则效率会很低。
给你个判断数字的方法
public bool IsNumber(string strNumber)
{
Regex objNotNumberPattern = new Regex(@"[^0-9.-]");
Regex objTwoDotPattern = new Regex(@"[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex(@"[0-9]*[-][0-9]*[-][0-9]*");
string strValidRealPattern = @"^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
string strValidIntegerPattern = @"^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
相似回答