如何使用Unity制作一个简易的计算器(unity,开发技术)

时间:2024-05-03 07:53:57 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    一、前言

    Hello,又见面了,今天分享如何使用Unity制作计算器,难度中等,可以用来学习,或者当成其他项目的小组件导入。

    当然,也可以导出来,发布到网页端,来做一个嵌入式工具也可以。

    二、效果图及源工程

    效果图:

    如何使用Unity制作一个简易的计算器

    源工程

    三、实现

    1.界面搭建

    如何使用Unity制作一个简易的计算器

    如何使用Unity制作一个简易的计算器

    所有的按钮摆放到Background下面。

    2.代码实现

    首先找到所有的按钮,添加到事件:

     //结果显示TextComputeProcess=GameObject.Find("Canvas/Background/Image/TextComputeProcess").GetComponent<Text>();TextComputeResult=GameObject.Find("Canvas/Background/Image/TextComputeResult").GetComponent<Text>();TextComputeResult.text="0";RUNSTATE=0;//操作BtnReset=GameObject.Find("Canvas/Background/重置").GetComponent<Button>();BtnReset.onClick.AddListener(()=>OperationDispose("CE"));BtnDelete=GameObject.Find("Canvas/Background/删除").GetComponent<Button>();BtnDelete.onClick.AddListener(()=>OperationDispose("Del"));//加减乘除BtnAdd=GameObject.Find("Canvas/Background/加").GetComponent<Button>();BtnAdd.onClick.AddListener(()=>OperationDispose("+"));BtnSub=GameObject.Find("Canvas/Background/减").GetComponent<Button>();BtnSub.onClick.AddListener(()=>OperationDispose("-"));BtnMul=GameObject.Find("Canvas/Background/乘").GetComponent<Button>();BtnMul.onClick.AddListener(()=>OperationDispose("*"));BtnDiv=GameObject.Find("Canvas/Background/除").GetComponent<Button>();BtnDiv.onClick.AddListener(()=>OperationDispose("/"));BtnEqual=GameObject.Find("Canvas/Background/等于").GetComponent<Button>();BtnEqual.onClick.AddListener(()=>OperationDispose("="));//数字Btn0=GameObject.Find("Canvas/Background/0").GetComponent<Button>();Btn0.onClick.AddListener(()=>NumDispose("0"));Btn1=GameObject.Find("Canvas/Background/1").GetComponent<Button>();Btn1.onClick.AddListener(()=>NumDispose("1"));Btn2=GameObject.Find("Canvas/Background/2").GetComponent<Button>();Btn2.onClick.AddListener(()=>NumDispose("2"));Btn3=GameObject.Find("Canvas/Background/3").GetComponent<Button>();Btn3.onClick.AddListener(()=>NumDispose("3"));Btn4=GameObject.Find("Canvas/Background/4").GetComponent<Button>();Btn4.onClick.AddListener(()=>NumDispose("4"));Btn5=GameObject.Find("Canvas/Background/5").GetComponent<Button>();Btn5.onClick.AddListener(()=>NumDispose("5"));Btn6=GameObject.Find("Canvas/Background/6").GetComponent<Button>();Btn6.onClick.AddListener(()=>NumDispose("6"));Btn7=GameObject.Find("Canvas/Background/7").GetComponent<Button>();Btn7.onClick.AddListener(()=>NumDispose("7"));Btn8=GameObject.Find("Canvas/Background/8").GetComponent<Button>();Btn8.onClick.AddListener(()=>NumDispose("8"));Btn9=GameObject.Find("Canvas/Background/9").GetComponent<Button>();Btn9.onClick.AddListener(()=>NumDispose("9"));BtnPoint=GameObject.Find("Canvas/Background/点").GetComponent<Button>();BtnPoint.onClick.AddListener(()=>NumDispose("."));BtnPm=GameObject.Find("Canvas/Background/正负").GetComponent<Button>();BtnPm.onClick.AddListener(()=>NumDispose("-"));

    按钮操作:

     //操作点击处理privatevoidOperationDispose(stringoperation){switch(operation){case"+":if(RUNSTATE==0)TextComputeProcess.text="0+";else{TextComputeProcess.text=TextComputeResult.text+"+";m_operation="+";RUNSTATE=2;}break;case"-":if(RUNSTATE==0)TextComputeProcess.text="0-";else{TextComputeProcess.text=TextComputeResult.text+"-";m_operation="-";RUNSTATE=2;}break;case"*":if(RUNSTATE==0)TextComputeProcess.text="0*";else{TextComputeProcess.text=TextComputeResult.text+"*";m_operation="*";RUNSTATE=2;}break;case"/":if(RUNSTATE==0)TextComputeProcess.text="0/";else{TextComputeProcess.text=TextComputeResult.text+"/";m_operation="/";RUNSTATE=2;}break;case"=":if(RUNSTATE==0)TextComputeProcess.text="0=";else{if(RUNSTATE==3){doubleresult;switch(m_operation){case"+":result=double.Parse(calculateString)+double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"+"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;case"-":result=double.Parse(calculateString)-double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"-"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;case"*":result=double.Parse(calculateString)*double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"*"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;case"/":result=double.Parse(calculateString)/double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"/"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;default:break;}}else{TextComputeProcess.text=TextComputeResult.text+"=";}}break;case"CE":TextComputeProcess.text="";TextComputeResult.text="0";RUNSTATE=0;break;case"Del":if(RUNSTATE==0)return;else{if(TextComputeResult.text.Length==1){TextComputeResult.text="0";}else{TextComputeResult.text=TextComputeResult.text.Remove(TextComputeResult.text.Length-1,1);}}break;default:break;}}

    数字点击处理:

     //数字点击处理privatevoidNumDispose(stringnum){switch(num){case".":if(RUNSTATE==0)TextComputeResult.text="0";elseTextComputeResult.text+=num;break;case"-":if(RUNSTATE==0)TextComputeResult.text="0";else{if(RUNSTATE==1){if(pmState){TextComputeResult.text=TextComputeResult.text.Remove(0,1);pmState=false;}else{TextComputeResult.text=num+TextComputeResult.text;pmState=true;}}elseif(RUNSTATE==2){pmState=false;}elseif(RUNSTATE==3){if(pmState){TextComputeResult.text=TextComputeResult.text.Remove(0,1);pmState=false;}else{TextComputeResult.text=num+TextComputeResult.text;pmState=true;}}elseif(RUNSTATE==4){pmState=false;OperationDispose("CE");}}break;default:if(RUNSTATE==0){TextComputeResult.text=num;RUNSTATE=1;}elseif(RUNSTATE==1){pmState=false;TextComputeResult.text+=num;}elseif(RUNSTATE==2){calculateString=TextComputeResult.text;TextComputeResult.text="";TextComputeResult.text+=num;RUNSTATE=3;}elseif(RUNSTATE==3){TextComputeResult.text+=num;}elseif(RUNSTATE==4){OperationDispose("CE");TextComputeResult.text=num;RUNSTATE=1;}break;}}

    完整代码:

    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassCalculatorControl:MonoBehaviour{privateTextTextComputeProcess;//计算过程privateTextTextComputeResult;//计算结果privateButtonBtnReset;privateButtonBtnDelete;privateButtonBtnAdd;privateButtonBtnSub;privateButtonBtnMul;privateButtonBtnDiv;privateButtonBtnEqual;privateButtonBtn0,Btn1,Btn2,Btn3,Btn4,Btn5,Btn6,Btn7,Btn8,Btn9;privateButtonBtnPoint,BtnPm;privatestringcalculateString="";//计算数privatestringm_operation="";//操作数privateboolpmState=false;//正负状态privateintRUNSTATE=0;//0默认1输入数字2输入操作符3输入操作符再输入数字4计算结果后voidStart(){//结果显示TextComputeProcess=GameObject.Find("Canvas/Background/Image/TextComputeProcess").GetComponent<Text>();TextComputeResult=GameObject.Find("Canvas/Background/Image/TextComputeResult").GetComponent<Text>();TextComputeResult.text="0";RUNSTATE=0;//操作BtnReset=GameObject.Find("Canvas/Background/重置").GetComponent<Button>();BtnReset.onClick.AddListener(()=>OperationDispose("CE"));BtnDelete=GameObject.Find("Canvas/Background/删除").GetComponent<Button>();BtnDelete.onClick.AddListener(()=>OperationDispose("Del"));//加减乘除BtnAdd=GameObject.Find("Canvas/Background/加").GetComponent<Button>();BtnAdd.onClick.AddListener(()=>OperationDispose("+"));BtnSub=GameObject.Find("Canvas/Background/减").GetComponent<Button>();BtnSub.onClick.AddListener(()=>OperationDispose("-"));BtnMul=GameObject.Find("Canvas/Background/乘").GetComponent<Button>();BtnMul.onClick.AddListener(()=>OperationDispose("*"));BtnDiv=GameObject.Find("Canvas/Background/除").GetComponent<Button>();BtnDiv.onClick.AddListener(()=>OperationDispose("/"));BtnEqual=GameObject.Find("Canvas/Background/等于").GetComponent<Button>();BtnEqual.onClick.AddListener(()=>OperationDispose("="));//数字Btn0=GameObject.Find("Canvas/Background/0").GetComponent<Button>();Btn0.onClick.AddListener(()=>NumDispose("0"));Btn1=GameObject.Find("Canvas/Background/1").GetComponent<Button>();Btn1.onClick.AddListener(()=>NumDispose("1"));Btn2=GameObject.Find("Canvas/Background/2").GetComponent<Button>();Btn2.onClick.AddListener(()=>NumDispose("2"));Btn3=GameObject.Find("Canvas/Background/3").GetComponent<Button>();Btn3.onClick.AddListener(()=>NumDispose("3"));Btn4=GameObject.Find("Canvas/Background/4").GetComponent<Button>();Btn4.onClick.AddListener(()=>NumDispose("4"));Btn5=GameObject.Find("Canvas/Background/5").GetComponent<Button>();Btn5.onClick.AddListener(()=>NumDispose("5"));Btn6=GameObject.Find("Canvas/Background/6").GetComponent<Button>();Btn6.onClick.AddListener(()=>NumDispose("6"));Btn7=GameObject.Find("Canvas/Background/7").GetComponent<Button>();Btn7.onClick.AddListener(()=>NumDispose("7"));Btn8=GameObject.Find("Canvas/Background/8").GetComponent<Button>();Btn8.onClick.AddListener(()=>NumDispose("8"));Btn9=GameObject.Find("Canvas/Background/9").GetComponent<Button>();Btn9.onClick.AddListener(()=>NumDispose("9"));BtnPoint=GameObject.Find("Canvas/Background/点").GetComponent<Button>();BtnPoint.onClick.AddListener(()=>NumDispose("."));BtnPm=GameObject.Find("Canvas/Background/正负").GetComponent<Button>();BtnPm.onClick.AddListener(()=>NumDispose("-"));}//操作点击处理privatevoidOperationDispose(stringoperation){switch(operation){case"+":if(RUNSTATE==0)TextComputeProcess.text="0+";else{TextComputeProcess.text=TextComputeResult.text+"+";m_operation="+";RUNSTATE=2;}break;case"-":if(RUNSTATE==0)TextComputeProcess.text="0-";else{TextComputeProcess.text=TextComputeResult.text+"-";m_operation="-";RUNSTATE=2;}break;case"*":if(RUNSTATE==0)TextComputeProcess.text="0*";else{TextComputeProcess.text=TextComputeResult.text+"*";m_operation="*";RUNSTATE=2;}break;case"/":if(RUNSTATE==0)TextComputeProcess.text="0/";else{TextComputeProcess.text=TextComputeResult.text+"/";m_operation="/";RUNSTATE=2;}break;case"=":if(RUNSTATE==0)TextComputeProcess.text="0=";else{if(RUNSTATE==3){doubleresult;switch(m_operation){case"+":result=double.Parse(calculateString)+double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"+"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;case"-":result=double.Parse(calculateString)-double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"-"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;case"*":result=double.Parse(calculateString)*double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"*"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;case"/":result=double.Parse(calculateString)/double.Parse(TextComputeResult.text);TextComputeProcess.text=calculateString+"/"+TextComputeResult.text+"=";TextComputeResult.text=result.ToString();RUNSTATE=4;break;default:break;}}else{TextComputeProcess.text=TextComputeResult.text+"=";}}break;case"CE":TextComputeProcess.text="";TextComputeResult.text="0";RUNSTATE=0;break;case"Del":if(RUNSTATE==0)return;else{if(TextComputeResult.text.Length==1){TextComputeResult.text="0";}else{TextComputeResult.text=TextComputeResult.text.Remove(TextComputeResult.text.Length-1,1);}}break;default:break;}}//数字点击处理privatevoidNumDispose(stringnum){switch(num){case".":if(RUNSTATE==0)TextComputeResult.text="0";elseTextComputeResult.text+=num;break;case"-":if(RUNSTATE==0)TextComputeResult.text="0";else{if(RUNSTATE==1){if(pmState){TextComputeResult.text=TextComputeResult.text.Remove(0,1);pmState=false;}else{TextComputeResult.text=num+TextComputeResult.text;pmState=true;}}elseif(RUNSTATE==2){pmState=false;}elseif(RUNSTATE==3){if(pmState){TextComputeResult.text=TextComputeResult.text.Remove(0,1);pmState=false;}else{TextComputeResult.text=num+TextComputeResult.text;pmState=true;}}elseif(RUNSTATE==4){pmState=false;OperationDispose("CE");}}break;default:if(RUNSTATE==0){TextComputeResult.text=num;RUNSTATE=1;}elseif(RUNSTATE==1){pmState=false;TextComputeResult.text+=num;}elseif(RUNSTATE==2){calculateString=TextComputeResult.text;TextComputeResult.text="";TextComputeResult.text+=num;RUNSTATE=3;}elseif(RUNSTATE==3){TextComputeResult.text+=num;}elseif(RUNSTATE==4){OperationDispose("CE");TextComputeResult.text=num;RUNSTATE=1;}break;}}}

    效果图如下:

    如何使用Unity制作一个简易的计算器

    四、后记

    完整代码278行,还是依旧那么简练,整体代码难度不大,主要是状态之间的切换:

    1、输入数字的状态

    2、输入操作符状态

    3、输入操作符后再输入数字状态

    4、计算结果后状态

    理解这些状态后,代码就容易理解了。

    最后,拓展一下,将其他大佬写的代码给大家看一下,大家如果觉得上面的代码太简单,可以看一下:

    代码使用OnGUI搭建界面,直接拖到任意对象上就可以看到效果了:

    usingUnityEngine;usingSystem.Text.RegularExpressions;usingSystem;publicclassCalculator2:MonoBehaviour{publicstaticboolIsNumeric(stringvalue){returnRegex.IsMatch(value,@"^[+-]?\d*[.]?\d*$");}publicstringresult="";//用来显示结果publicstaticstringstr1="";//第一个操作数publicstaticboolhaveDot=false;//第二个操作数publicstaticboolisCaclutate=false;voidOnGUI(){//对数字进行处理if(GUI.Button(newRect(100,100,100,60),"CE")){result="";str1="";haveDot=false;}if(GUI.Button(newRect(210,100,100,60),"×")&&str1.Substring(str1.Length-1,1)!="-"&&str1.Substring(str1.Length-1,1)!="+"&&str1.Substring(str1.Length-1,1)!="."){if(IsNumeric(str1.Substring(str1.Length-1,1))){Debug.Log(str1.Substring(str1.Length-1,1));str1+="*";haveDot=false;isCaclutate=false;}result=str1;}if(GUI.Button(newRect(320,100,100,60),"÷")&&str1.Substring(str1.Length-1,1)!="-"&&str1.Substring(str1.Length-1,1)!="+"&&str1.Substring(str1.Length-1,1)!="."){if(IsNumeric(str1.Substring(str1.Length-1,1))){str1+="/";haveDot=false;isCaclutate=false;}result=str1;}if(GUI.Button(newRect(430,100,100,60),"←")){if(isCaclutate==true){str1="";isCaclutate=false;}elseif(result.Length!=0){str1=str1.Substring(0,str1.Length-1);}result=str1;}if(GUI.Button(newRect(100,170,100,60),"1")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="1";result=str1;}if(GUI.Button(newRect(210,170,100,60),"2")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="2";result=str1;}if(GUI.Button(newRect(320,170,100,60),"3")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="3";result=str1;}if(GUI.Button(newRect(430,170,100,60),"-")){if(IsNumeric(str1.Substring(str1.Length-1,1))&&str1.Substring(str1.Length-1,1)!="-"&&str1.Substring(str1.Length-1,1)!="+"&&str1.Substring(str1.Length-1,1)!="."){str1+="-";haveDot=false;isCaclutate=false;}result=str1;}if(GUI.Button(newRect(100,240,100,60),"4")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="4";result=str1;}if(GUI.Button(newRect(210,240,100,60),"5")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="5";result=str1;}if(GUI.Button(newRect(320,240,100,60),"6")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="6";result=str1;}if(GUI.Button(newRect(430,240,100,60),"+")&&str1.Substring(str1.Length-1,1)!="+"&&str1.Substring(str1.Length-1,1)!="-"&&str1.Substring(str1.Length-1,1)!="."){if(IsNumeric(str1.Substring(str1.Length-1,1))){str1+="+";haveDot=false;isCaclutate=false;}result=str1;}if(GUI.Button(newRect(100,310,100,60),"7")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="7";result=str1;}if(GUI.Button(newRect(210,310,100,60),"8")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="8";result=str1;}if(GUI.Button(newRect(320,310,100,60),"9")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="9";result=str1;}if(GUI.Button(newRect(430,310,100,130),"=")){vartmp=Evaluator.Eval(result);Debug.Log(tmp.ToString());result=tmp.ToString();str1=result;isCaclutate=true;if(result.Contains(".")){haveDot=true;}}if(GUI.Button(newRect(100,380,210,60),"0")){if(isCaclutate==true){str1="";isCaclutate=false;haveDot=false;}str1+="0";result=str1;}if(GUI.Button(newRect(320,380,100,60),".")){if(isCaclutate==true){str1="0.";isCaclutate=false;}if(IsNumeric(str1.Substring(str1.Length-1,1))&&str1.Substring(str1.Length-1,1)!="."&&haveDot==false){Debug.Log(str1.Substring(str1.Length-1,1));str1+=".";haveDot=true;isCaclutate=false;}result=str1;}GUI.TextArea(newRect(100,20,430,60),result);}/**////<summary>///动态求值///</summary>publicclassEvaluator{/**////<summary>///计算结果,如果表达式出错则抛出异常///</summary>///<paramname="statement">表达式,如"1+2+3+4"</param>///<returns>结果</returns>publicstaticobjectEval(stringstatement){if(statement.Trim()!=string.Empty){Evaluatorevaluator=newEvaluator();returnevaluator.GetFormulaResult(statement);}else{returnnull;}}privateobjectGetFormulaResult(strings){if(s==""){returnnull;}stringS=BuildingRPN(s);stringtmp="";System.Collections.Stacksk=newSystem.Collections.Stack();charc='';System.Text.StringBuilderOperand=newSystem.Text.StringBuilder();doublex,y;for(inti=0;i<S.Length;i++){c=S[i];//addedc==','forgermanycultureif(char.IsDigit(c)||c=='.'||c==','){//数据值收集.Operand.Append(c);}elseif(c==''&&Operand.Length>0){#region运算数转换try{tmp=Operand.ToString();if(tmp.StartsWith("-"))//负数的转换一定要小心...它不被直接支持.{//现在我的算法里这个分支可能永远不会被执行.sk.Push(-((double)Convert.ToDouble(tmp.Substring(1,tmp.Length-1))));}else{sk.Push(Convert.ToDouble(tmp));}}catch{returnnull;//}Operand=newSystem.Text.StringBuilder();#endregion}elseif(c=='+'//运算符处理.双目运算处理.||c=='-'||c=='*'||c=='/'||c=='%'||c=='^'){#region双目运算if(sk.Count>0)/*如果输入的表达式根本没有包含运算符.或是根本就是空串.这里的逻辑就有意义了.*/{y=(double)sk.Pop();}else{sk.Push(0);break;}if(sk.Count>0)x=(double)sk.Pop();else{sk.Push(y);break;}switch(c){case'+':sk.Push(x+y);break;case'-':sk.Push(x-y);break;case'*':if(y==0){sk.Push(x*1);}else{sk.Push(x*y);}break;case'/':if(y==0){sk.Push(x/0);}else{sk.Push(x/y);}break;case'%':sk.Push(x%y);break;case'^'://if(x>0)//{//我原本还想,如果被计算的数是负数,又要开真分数次方时如何处理的问题.后来我想还是算了吧.sk.Push(System.Math.Pow(x,y));//}//else//{//doublet=y;//stringts="";//t=1/(2*t);//ts=t.ToString();//if(ts.ToUpper().LastIndexOf('E')>0)//{//;//}//}break;}#endregion}elseif(c=='!')//单目取反.){sk.Push(-((double)sk.Pop()));}}if(sk.Count>1){returnnull;//;}if(sk.Count==0){returnnull;//;}returnsk.Pop();}/**////<summary>//////</summary>privatestringBuildingRPN(strings){System.Text.StringBuildersb=newSystem.Text.StringBuilder(s);System.Collections.Stacksk=newSystem.Collections.Stack();System.Text.StringBuilderre=newSystem.Text.StringBuilder();charc='';//sb.Replace("","");//一开始,我只去掉了空格.后来我不想不支持函数和常量能滤掉的全OUT掉.for(inti=0;i<sb.Length;i++){c=sb[i];//addedc==','forgermancultureif(char.IsDigit(c)||c==',')//数字当然要了.re.Append(c);//if(char.IsWhiteSpace(c)||char.IsLetter(c);//如果是空白,那么不要.现在字母也不要.//continue;switch(c)//如果是其它字符...列出的要,没有列出的不要.{case'+':case'-':case'*':case'/':case'%':case'^':case'!':case'(':case')':case'.':re.Append(c);break;default:continue;}}sb=newSystem.Text.StringBuilder(re.ToString());#region对负号进行预转义处理.负号变单目运算符求反.for(inti=0;i<sb.Length-1;i++)if(sb[i]=='-'&&(i==0||sb[i-1]=='('))sb[i]='!';//字符转义.#endregion#region将中缀表达式变为后缀表达式.re=newSystem.Text.StringBuilder();for(inti=0;i<sb.Length;i++){if(char.IsDigit(sb[i])||sb[i]=='.')//如果是数值.{re.Append(sb[i]);//加入后缀式}elseif(sb[i]=='+'||sb[i]=='-'||sb[i]=='*'||sb[i]=='/'||sb[i]=='%'||sb[i]=='^'||sb[i]=='!')//.{#region运算符处理while(sk.Count>0)//栈不为空时{c=(char)sk.Pop();//将栈中的操作符弹出.if(c=='(')//如果发现左括号.停.{sk.Push(c);//将弹出的左括号压回.因为还有右括号要和它匹配.break;//中断.}else{if(Power(c)<Power(sb[i]))//如果优先级比上次的高,则压栈.{sk.Push(c);break;}else{re.Append('');re.Append(c);}//如果不是左括号,那么将操作符加入后缀式中.}}sk.Push(sb[i]);//把新操作符入栈.re.Append('');#endregion}elseif(sb[i]=='(')//基本优先级提升{sk.Push('(');re.Append('');}elseif(sb[i]==')')//基本优先级下调{while(sk.Count>0)//栈不为空时{c=(char)sk.Pop();//popOperatorif(c!='('){re.Append('');re.Append(c);//加入空格主要是为了防止不相干的数据相临产生解析错误.re.Append('');}elsebreak;}}elsere.Append(sb[i]);}while(sk.Count>0)//这是最后一个弹栈啦.{re.Append('');re.Append(sk.Pop());}#endregionre.Append('');returnFormatSpace(re.ToString());//在这里进行一次表达式格式化.这里就是后缀式了.}///<summary>///优先级别测试函数.///</summary>///<paramname="opr"></param>///<returns></returns>privatestaticintPower(charopr){switch(opr){case'+':case'-':return1;case'*':case'/':return2;case'%':case'^':case'!':return3;default:return0;}}///<summary>///规范化逆波兰表达式.///</summary>///<paramname="s"></param>///<returns></returns>privatestaticstringFormatSpace(strings){System.Text.StringBuilderret=newSystem.Text.StringBuilder();for(inti=0;i<s.Length;i++){if(!(s.Length>i+1&&s[i]==''&&s[i+1]==''))ret.Append(s[i]);elseret.Append(s[i]);}returnret.ToString();//.Replace('!','-');}}}
     </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
    本文:如何使用Unity制作一个简易的计算器的详细内容,希望对您有所帮助,信息来源于网络。
    上一篇:C语言实现图书管理系统的示例分析下一篇:

    17 人围观 / 0 条评论 ↓快速评论↓

    (必须)

    (必须,保密)

    阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18