谁帮写一个c#实现计算器加减乘除的类,winForms程序的,谢谢。。 本人新手,最好加注释,谢谢

如题所述

string str,opp,opp1;//定义三个字符串变量:数字、二元运算符、一元运算符
double num1, num2,result;//定义操作数、被操作数、结果
//数字键点击事件,统一绑定number函数
private void number(object sender, EventArgs e)
{
Button b = (Button)(sender);
str = b.Text;
if (textBox1.Text == "0")
{
textBox1.Text = str;
}
else
textBox1.Text = textBox1.Text + str;
}
//二元操作符“+ - * /”点击事件,统一绑定operational函数
private void operational(object sender, EventArgs e)
{
Button b = (Button)(sender);
if (b.Text == "+")
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
opp = "+";
opp1 = "";
}
else if (b.Text == "-")
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
opp = "-";
opp1 = "";
}
else if (b.Text == "*")
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
opp = "*";
opp1 = "";
}
else if (b.Text == "/")
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
opp = "/";
opp1 = "";
}
else if (b.Text == "=")
{
if (opp1 != "=")
{
num2 = double.Parse(textBox1.Text);
}
if (opp == "+")
{
num1 = num1 + num2;
textBox1.Text = "" + num1.ToString();
}
else if (opp == "-")
{
num1 = num1 - num2;
textBox1.Text = "" + num1.ToString();
}
else if (opp == "*")
{
num1 = num1 * num2;
textBox1.Text = "" + num1.ToString();
}
else if (opp == "/")
{
if (num2 == 0)
{
textBox1.Text = "除数不能为零";
}
else
{
num1 = num1 / num2;
textBox1.Text = "" + num1.ToString();
}
}
opp1 = "=";
}
}

//一元操作符点击事件,统一绑定operation方法(里面的if判断,相信你都能看明白)
private void operation(object sender, EventArgs e)
{
Button b = (Button)(sender);
if (b.Text == "+/-")
{
num1 = double.Parse(textBox1.Text);
result = num1 * (-1);
textBox1.Text = result.ToString();
}
else if (b.Text == ".")
{
str= textBox1.Text;
int index = str.IndexOf(".");
if (index == -1)//一个数字中只能有一个小数点
{
textBox1.Text = str + ".";
}
}
else if (b.Text == "backspace")
{
str = textBox1.Text;
str = str.Substring(0, str.Length - 1);
textBox1.Text = str;
}
else if (b.Text == "CE")
{
textBox1.Text = "0";
}
else if (b.Text == "C")
{
result=num1 = num2 = 0;
str = null;
opp = null;
textBox1.Text = "0";
}
else if (b.Text == "sqrt")
{
num1 = double.Parse(textBox1.Text);
result = Math.Sqrt(num1);
textBox1.Text = result.ToString();
}
else if (b.Text == "1/x")
{
num1 = double.Parse(textBox1.Text);
result = 1/num1;
textBox1.Text = result.ToString();
}
else if (b.Text == "%")
{
num1 = double.Parse(textBox1.Text);
result = num1/100;
textBox1.Text = result.ToString();
}
opp1 = "";
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-13
简单的说一下
1.定义一个运算抽象类
abstract class Opreation
{
public abstract decimal OpreationNum(decimal NumA,decimal NumB);
}
2.定义一个负责运算逻辑处理的工厂类
class Handler
{
public decimal OperationHandler(string OperationMethod,decimal NumA,decimal NumB)
{
Opreation operation=null;
swtich(OperationMethod)
{
case"+":operation=new AdditionOperation();
case "-":operation=new SubtractionOperation();
...很多自己扩展
}
return operation.OpreationNum(NumA,NumB);
}
}
3.实现加法逻辑运算类
class AdditionOperation:Opreation
{
public decimal OpreationNum(decimal NumA,decimal NumB)
{
return NumA+NumB;
}
}
4.自己去实现减法吧,相信你一定会了,
这样程序的扩展性比较好,新增一种运算,只需要新增子类.修改下工厂类.
如果不想修改工厂类,则可以用C#反射,自己慢慢研究吧.
第2个回答  推荐于2016-11-11
public class CALC
{
public static double add(double a, double b)//加
{
return a + b;
}
public static double minus(double a, double b)//减
{
return a - b;
}
public static double mult(double a, double b)//乘
{
return a * b;
}
public static double divide(double a, double b)//除
{
return a / b;
}
}

调用的时候直接写:

double result=CALC.add(1,2);//返回3
其它类同.追问

调用 Add的时候总说非静态的字段、方法或属性“jisuanqi.methods.Add(double, double)”要求对象引用,该怎么办啊

追答

你怎么调的?在哪调的?

追问

我创建一个新的类 方法放里面 调用就显示错误

追答

没问题啊.
贴代码.

本回答被提问者和网友采纳
相似回答