简单计算器

tengye19840704 2009-09-22 02:06:37
+-*/的计算器.C#写的
例如:1+2=3
我在"+"按钮里写如下代码
private void button13_Click(object sender, System.EventArgs e)
{
string strNo = "";
string strNo1= "";
strNo = this.textBox1.Text;//得到TEXTBOX中的值
strNo1 = strNo;
strSum = int.Parse(strNo1)+int.Parse(strNo);//两个值相加

}
应该怎么修改一下可以实现1+2=3
现在的情况是,只能加相同的值,因为都是从TEXTBOX中得到的值.
谢谢.
...全文
2237 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zhengjie322 2010-05-13
  • 打赏
  • 举报
回复
我 菜鸟一个 请大家多指教
zhuifengguxin 2009-10-23
  • 打赏
  • 举报
回复
我也在做
lsd123 2009-09-22
  • 打赏
  • 举报
回复
.
ralhander 2009-09-22
  • 打赏
  • 举报
回复
第二个函数a+=result
ralhander 2009-09-22
  • 打赏
  • 举报
回复
最后那个函数为buttonEqual_Click
ralhander 2009-09-22
  • 打赏
  • 举报
回复
想那么多干什么,一个textBox就一个呗,关键要你设两个类的变量

string a,b,c,result;//a为上一次显示的,b为这次显示的,c为运算符号
private void button1_Click(object sender, System.EventArgs e)
{
b+="1";
textBox1.Text=b;
GetResult();

}
//按照上面的复制2~0和.

private void buttonPlus_Click(object sender, System.EventArgs e)
{
textBox1.Text=result;
c="+";
a="";
a+=b;
b="";
}
//按照上面的复制-~/

private GetResult()
{
try
{
switch(c)
{
case"+":
result=(Double.Parse(a)+Double.Parse(b)).ToString;
break;
case"-":
result=(Double.Parse(a)-Double.Parse(b)).ToString;
break;
case"*":
result=(Double.Parse(a)*Double.Parse(b)).ToString;
break;
case"/":
result=(Double.Parse(a)/Double.Parse(b)).ToString;
break;
default:
result=""+b;
}
}
catch(DividedByZeroException e1)
{
Console.WriteLine("除数不能为0");
}

}
private void buttonPlus_Click(object sender, System.EventArgs e)
{
textBox1.Text=result;
}

cadtian 2009-09-22
  • 打赏
  • 举报
回复
专家写的就是详细
wuyq11 2009-09-22
  • 打赏
  • 举报
回复
使用堆栈stack保存
public class Operation
{
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get
{
return _numberA;
}
set
{
_numberA = value;
}
}
public double NumberB
{
get
{
return _numberB;
}
set
{
_numberB = value;
}
}
public virtual double GetResult()
{
double result = 0;
return result;
}
public static string checkNumberInput(string currentNumber, string inputString)
{
string result = "";
if (inputString == ".")
{
if (currentNumber.IndexOf(".") < 0)
{
if (currentNumber.Length == 0)
result = "0" + inputString;
else
result = currentNumber + inputString;
}
}
else if (currentNumber == "0")
{
result = inputString;
}
else
{
result = currentNumber + inputString;
}

return result;
}


}
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception("除数不能为0。");
result = NumberA / NumberB;
return result;
}
}
class OperationSqr : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberB * NumberB;
return result;
}
}
class OperationSqrt : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB < 0)
throw new Exception("负数不能开平方根。");
result = Math.Sqrt(NumberB);
return result;
}
}
class OperationReverse : Operation
{
public override double GetResult()
{
double result = 0;
result = -NumberB;
return result;
}
}
public class OperationFactory
{
public static Operation createOperate(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":
{
oper = new OperationAdd();
break;
}
case "-":
{
oper = new OperationSub();
break;
}
case "*":
{
oper = new OperationMul();
break;
}
case "/":
{
oper = new OperationDiv();
break;
}
case "sqr":
{
oper = new OperationSqr();
break;
}
case "sqrt":
{
oper = new OperationSqrt();
break;
}
case "+/-":
{
oper = new OperationReverse();
break;
}
}

return oper;
}


private void buttonAdd_Click(object sender, EventArgs e)
{
if (txtShow.Text != "")
{
oper = OperationFactory.createOperate(((Button)sender).Text);

oper.NumberA = Convert.ToDouble(txtShow.Text);

bOperate = true;
}
}
private void buttonEqual_Click(object sender, EventArgs e)
{
if (txtShow.Text != "")
{
if (((Button)sender).Text != "=")
{
oper = OperationFactory.createOperate(((Button)sender).Text);
}
oper.NumberB = Convert.ToDouble(txtShow.Text);
txtShow.Text = oper.GetResult().ToString();
bOperate = true;
}
}
cadtian 2009-09-22
  • 打赏
  • 举报
回复
http://download.csdn.net/source/607633
i466834553 2009-09-22
  • 打赏
  • 举报
回复
int total=0;
//加的事件
private void Add_Click(object sender, System.EventArgs e)
{
total+= int.Parse(textBox1.Text.Trim());
textBox1.Text = "";

}
//等的事件
private void Equals_Click(object sender, System.EventArgs e)
{
textBox1.Text = (aa + int.Parse(textBox1.Text.Trim())).ToString();
}

i466834553 2009-09-22
  • 打赏
  • 举报
回复
int total=0;
//加的事件
private void Add_Click(object sender, System.EventArgs e)
{
total+= int.Parse(textBox1.Text.Trim());
textBox1.Text = "";

}
//等的事件
private void Equals_Click(object sender, System.EventArgs e)
{
textBox1.Text = (aa + int.Parse(textBox1.Text.Trim())).ToString();
}

cadtian 2009-09-22
  • 打赏
  • 举报
回复
以前刚开始学的时候,我写了一个计算器,一个texbox的,能显示计算式子的,数字输入时用很多Button的,当时厚颜居然传上去了,你实在不行可以去bs下我的
后来看到《大话设计模式》中写的一个计算器,感叹自己写的实在太sb了
heping173 2009-09-22
  • 打赏
  • 举报
回复
多定义一个变量不就可以了
cadtian 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tengye19840704 的回复:]
是..= 号是有的...
我就是把关键的相加写上来了,我把计算出的值定义成全局的了..在=号事件里给TEXTBOX赋值的.
[/Quote]

只有一个TEXTBOX,想进行计算,那计算的代码应该是在“=”代码里,这样的话,两个待处理的数字应该设为全局
huming_h 2009-09-22
  • 打赏
  • 举报
回复
定义一个全局的string strNo = ""; 变量
在加号事件里
strNo = this.textBox1.Text;
this.textBox1.Text = "";
在等号事件里
int result = int.Parse(this.no1) + int.Parse(this.textBox1.Text);
cadtian 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tengye19840704 的回复:]
放多个是可以实现的.现在要求就放一个TEXTBOX,- -!唉..每次获得TEXTBOX的值,我开始想是,第一次获得的保存起来,跟第二次获得的相加.可是没成功.
[/Quote]

在"+"“=”按钮外面定义2个操作数啊

string strNo = "";
string strNo1= "";

加号代码里面写:strNo = this.textBox1.Text; 同时可以清空textBox1;
=号代码里面写strNo1 = this.textBox1.Text;


但是这样必须把操作符号+-*/传到等号代码里进行处理得出结果

xuenzhen123 2009-09-22
  • 打赏
  • 举报
回复
你只想放一个textbox除非你用很多button 就想普通计算器上的数字按钮 点一个button就在textbox上面显示相应的值 并赋给一个变量相应的值 然后再点击第二个值 做相同处理 然后点“=”号 计算结果并把结果赋值给textbox....
tengye19840704 2009-09-22
  • 打赏
  • 举报
回复
是..= 号是有的...
我就是把关键的相加写上来了,我把计算出的值定义成全局的了..在=号事件里给TEXTBOX赋值的.
cadtian 2009-09-22
  • 打赏
  • 举报
回复
要看你咋样实现
1.放两个textBox
strSum = int.Parse(this.textBox1.Text)+int.Parse(this.textBox2.Text);//两个值相加

2.一个textBox1,按下+-*/后熟第二个数,但是这样应该还要个=号吧,不然怎么计算
tengye19840704 2009-09-22
  • 打赏
  • 举报
回复
放多个是可以实现的.现在要求就放一个TEXTBOX,- -!唉..每次获得TEXTBOX的值,我开始想是,第一次获得的保存起来,跟第二次获得的相加.可是没成功.
加载更多回复(5)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧