java 求值算法_算法笔记_044:表达式计算求值(Java)

packagecom.liuzhen.systemExe;importjava.util.Scanner;importjava.util.Stack;public classMain{//计算表达式的值

public voidgetExpressionValue(String A){char[] arrayA =A.toCharArray();

Stack Value = new Stack(); //存放运算数字及表达式计算结果

Stack Operator = new Stack(); //存放运算符for(int i = 0;i < A.length();i++){int temp = 0;if(arrayA[i] >= '0' && arrayA[i] <= '9'){

temp= arrayA[i] - '0';

i= i + 1;while(i < A.length() && arrayA[i] >= '0' && arrayA[i] <= '9'){

temp= temp * 10 + (arrayA[i] - '0');

i++;

}

i--; //对应上面一句i = i+1;因为在for循环中有i++自增操作,若不执行此句,会导致i自增两次

Value.push(temp);

}else{if(Operator.empty()){

Operator.push(arrayA[i]);

}else{char temp1 = Operator.pop(); //进栈前,存放运算符栈中栈顶存放字符

int judge = comparePriority(temp1,arrayA[i]); //比较当前字符与栈顶字符优先级

if(judge == 1){ //当前字符优先级小于栈顶字符

int tempA =Value.pop();int tempB =Value.pop();int result =computeNumber(tempB,tempA,temp1);

Value.push(result);

Operator.push(arrayA[i]);

}if(judge == 0){ //当前字符优先级大于栈顶字符

Operator.push(temp1);

Operator.push(arrayA[i]);

}if(judge == 2){ //字符')'遇到'(',刚好使得'('出栈

System.out.println("'('刚好遇到')'"); //这种情况也应该不会出现,按照给定优先级,')'一般会先遇到+、-、*、/字符

}if(judge == 3){ //此时')'刚好准备进栈

while(temp1 != '('){ //')'字符要等到第一个'('出栈才能结束循环//System.out.println(temp1);

int tempA =Value.pop();int tempB =Value.pop();int result =computeNumber(tempB,tempA,temp1);

Value.push(result);

temp1=Operator.pop();

}

}if(judge == -1){ //此时,说明当前栈顶字符为')',这是不存在的,因为遇到')',按要求不让进栈

System.out.println("出现栈顶有')'错误!!!");

}

}

}

}while(!Operator.empty() && !Value.empty()){ //此时,字符栈中还存在运算符的情况

char temp1 =Operator.pop();int tempA =Value.pop();int tempB =Value.pop();int result =computeNumber(tempB,tempA,temp1);

Value.push(result);

}

System.out.println(Value.pop());//此时运算符栈为空,数字栈中只存在表达式计算最终结果

}//计算a operator b的值,operator = {+,-,*,/}

public int computeNumber(int a,int b,charoperator){intresult;switch(operator){case '+':

result= a+b;break;case '-':

result= a-b;break;case '*':

result= a*b;break;case '/':

result= a/b;break;default:

result= 0;break;

}returnresult;

}//判断运算符a和b的优先级

public int comparePriority(char a,charb){//使用二维数组表达运算符之间的优先级,行用字符a表示,列用字符b表示

int[][] Value = {{1,1,0,0,0,3},

{1,1,0,0,0,3},

{1,1,1,1,0,3},

{1,1,1,1,0,3},

{0,0,0,0,0,2},

{-1,-1,-1,-1,-1,-1}};int i = 0;int j = 0;if(a == '+')

i= 0;if(a == '-')

i= 1;if(a == '*')

i= 2;if(a == '/')

i= 3;if(a == '(')

i= 4;if(a == ')')

i= 5;if(b == '+')

j= 0;if(b == '-')

j= 1;if(b == '*')

j= 2;if(b == '/')

j= 3;if(b == '(')

j= 4;if(b == ')')

j= 5;returnValue[i][j];

}public static voidmain(String[] args){

Main test= newMain();

Scanner in= newScanner(System.in);

System.out.println("请输入一个算法表达式:");

String A=in.nextLine();

test.getExpressionValue(A);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值