问题描述
如何进行连续运算?就像1+3-2这样……这个只能实现两位数的运算……是不是要把运算那部分改为switchcase形式?求大神帮忙OJZZZZZZZZZZzusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespacecalculator{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}booloperatorFlag=false;publicvoidInputNumber(stringnumber1){if(tBanswer.Text=="0"){tBanswer.Text="";}tBanswer.Text=tBanswer.Text+number1;}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidbonDec_Click(objectsender,EventArgse){if(tBanswer.Text.IndexOf(".")==-1)InputNumber(".");}privatevoidbon00_Click(objectsender,EventArgse){InputNumber("0");}privatevoidbon01_Click(objectsender,EventArgse){InputNumber("1");}privatevoidbon02_Click(objectsender,EventArgse){InputNumber("2");}privatevoidbon03_Click(objectsender,EventArgse){InputNumber("3");}privatevoidbon04_Click(objectsender,EventArgse){InputNumber("4");}privatevoidbon05_Click(objectsender,EventArgse){InputNumber("5");}privatevoidbon06_Click(objectsender,EventArgse){InputNumber("6");}privatevoidbon07_Click(objectsender,EventArgse){InputNumber("7");}privatevoidbon08_Click(objectsender,EventArgse){InputNumber("8");}privatevoidbon09_Click(objectsender,EventArgse){InputNumber("9");}publicstaticstringnumbersaved;publicstaticstringsignsaved;publicvoidSaveInputed(stringnum1,stringsign1){if(num1=="0"|num1=="")return;numbersaved=num1;signsaved=sign1;tBanswer.Text="0";}privatevoidbonAdd_Click(objectsender,EventArgse){SaveInputed(tBanswer.Text,"加");}privatevoidbonSub_Click(objectsender,EventArgse){SaveInputed(tBanswer.Text,"减");}privatevoidbonMul_Click(objectsender,EventArgse){SaveInputed(tBanswer.Text,"乘");}privatevoidbonDiv_Click(objectsender,EventArgse){SaveInputed(tBanswer.Text,"除");}privatevoidbonEqula_Click(objectsender,EventArgse){stringnumbernow=tBanswer.Text;if(signsaved==""|numbersaved==""|numbernow==""|numbernow=="0")return;tBanswer.Text=Calcuration(numbersaved,signsaved,numbernow);signsaved="";numbersaved="";}publicstringCalcuration(stringnum1,stringsign1,stringnum2){floatnumber1,number2,sum1=0,result=0,a=0;try{number1=(float)(Double.Parse(num1));number2=(float)(Double.Parse(num2));if(sign1=="加"){sum1=number1+number2;}if(sign1=="减"){sum1=number1-number2;}if(sign1=="乘"){sum1=number1*number2;}if(sign1=="除"){sum1=number1/number2;}result=sum1;returnresult.ToString();}catch{return"";}}privatevoidbonC_Click(objectsender,EventArgse){tBanswer.Text="0";numbersaved="";signsaved="";}}}
解决方案
解决方案二:
这段代码包含的各种诡异的写法真是让我打开眼界你现在是在按=时后进行计算的你可以看下windows自带的计算机,按其他符号的时候,如果前面已经有一个式子,也会计算出结果
解决方案三:
google完整输入site:download.csdn.net计算器C#winforms结果就有了。
解决方案四:
你这个写法也太拖沓了,数字部分可以合并成一个方法,然后通过tag来获取当前点击对应数字,加减乘除也是的
解决方案五:
设置一个成员变量存结果就行了
解决方案六:
引用3楼starfd的回复:
你这个写法也太拖沓了,数字部分可以合并成一个方法,然后通过tag来获取当前点击对应数字,加减乘除也是的
+1
解决方案七:
搜索一下逆波兰表达式,可以解决你的问题。
解决方案八:
用栈就可以解决