📄 calvalue.java
字号:
/******************************************************************
/
/队列中存放后缀表达式求值
/
*******************************************************************/
import java.io.*;
public class CalValue
{
public static void main(String args[])throws IOException
{
midCal mid=new midCal();
int aa[]=new int[10]; //定义数组存放数据项
int temp=0; //存放数组的下标
int pos=0;
//获取表达式
System.out.println("Please input the Expression: ");
String Expression;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
Expression=in.readLine();
while(true)
{
int ch=(int)Expression.charAt(pos);
if(mid.IsOperator(ch))
{
System.out.println("---------------Operator:-----------");
System.out.println("the Item is: "+ch);
int T1=aa[temp-1];
System.out.println("T1="+T1);
int T2=aa[temp];
System.out.println("T2="+T2);
temp=temp-1;
aa[temp]=mid.TwoResult(ch,T1,T2);
System.out.println("中间值为: "+aa[temp]);
pos++;
System.out.println("pos= "+pos);
System.out.println("------------------------------------");
}
else
{
System.out.println("---------Opreand:-----------");
temp++;
aa[temp]=ch-48;
System.out.println("the Item is: aa["+temp+"]="+aa[temp]);
pos++;
System.out.println("pos= "+pos);
System.out.println("------------------------------------");
}
System.out.println("Expression's length is: "+Expression.length());
System.out.println("temp: "+temp);
if(pos>=Expression.length())
break;
}
System.out.print("The Expression's value is: "+aa[temp]);
}
}
class midCal
{
public boolean IsOperator(int item)
{
if(item==43||item==45||item==42||item==47)
return true;
else
return false;
}
public int TwoResult(int operator, int operand1,int operand2)
{
switch(operator)
{
case 43:
return (operand1+operand2);
case 45:
return (operand1-operand2);
case 42:
return (operand1*operand2);
case 47:
return (operand1/operand2);
}
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -