问题描述

  输入一个只包含加减乖除和括号的合法表达式,求表达式的值。其中除表示整除。

输入格式

  输入一行,包含一个表达式。

输出格式

  输出这个表达式的值。

样例输入

1-2+3*(4-5)

样例输出

-4

数据规模和约定

  表达式长度不超过100,表达式运算合法且运算过程都在int内进行。

package com.liuzhen.systemExe;

import java.util.Scanner;
import java.util.Stack; public class Main{
//计算表达式的值
public void getExpressionValue(String A){
char[] arrayA = A.toCharArray();
Stack<Integer> Value = new Stack<Integer>(); //存放运算数字及表达式计算结果
Stack<Character> Operator = new Stack<Character>(); //存放运算符
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,char operator){
int result;
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;
}
return result;
}
//判断运算符a和b的优先级
public int comparePriority(char a,char b){
//使用二维数组表达运算符之间的优先级,行用字符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;
return Value[i][j];
} public static void main(String[] args){
Main test = new Main();
Scanner in = new Scanner(System.in);
System.out.println("请输入一个算法表达式:");
String A = in.nextLine();
test.getExpressionValue(A); }
}

运行结果:

请输入一个算法表达式:
1-2+3*(4-5)
-4 请输入一个算法表达式:
1-2*((2+3)*2-(2+3))
-9 请输入一个算法表达式:
1-2*((2+3)*(2+3))
-49

最新文章

  1. 前端开发week2
  2. Tigase集群设置
  3. push notification获取device token
  4. Languages
  5. SVN经常使用命令说明
  6. mysql ERROR 1064 (42000): Erreur de syntaxe pr&#232;s de &#39;order)
  7. iphone开发 IOS 组织架构图
  8. css 文本域textarea显示成label标签
  9. UIScrollView的基本使用和一些常用代理方法
  10. Day4_代码重用与函数
  11. gcc 的编译过程
  12. java的JCombobox实现中国省市区三级联动
  13. 仿百度壁纸客户端(二)——主页自定义ViewPager广告定时轮播图
  14. 中国交建 WAF 基础平台 http://waf.ccccltd.cn/
  15. 在JSP中使用el函数标签获取默认值(男女性别选项)
  16. app已损坏,打不开。你应该将它移到废纸篓
  17. MySQL与宿主Linux之间交互式执行命令
  18. Java如何使套接字向单个客户端显示消息?
  19. C# 3.0 / C# 3.5 扩展方法
  20. Android 运行时报错Error running app: Instant Run requires &#39;Tools | Android | Enable ADB integration&#39; to be enabled. 的解决办法

热门文章

  1. Luogu P3846 BSGS算法
  2. Python 简明教程 --- 3,Python 基础概念
  3. vscode+eslint自动格式化vue代码的方法
  4. AXI总线slave模式下接收数据---verilog代码
  5. spring MVC--WebApplicationContext做了什么
  6. Elasticsearch URI search 查询语法整理
  7. eslint插件开发教程
  8. SQL拦截器
  9. orcle报错:ORA-12737:Instant Client Light:unsupported server character set ZHS16GBK
  10. mac OS和win7笔记本实现文件共享