题目描述
对于一个不存在括号的表达式进行计算
输入描述:
存在多种数据,每组数据一行,表达式不存在空格
输出描述:
输出结果
示例1
输入
复制
6/2+3+3*4
输出
复制
18
import java.util.*;
import java.io.*;
import java.text.* ;
public class Main
{
public static int[] pos = {0};
public static char[] ch;
public static HashMap<String, Integer> mon = new HashMap<>();
public static int len;
public static int i;
public static void main(String[] args) throws ParseException{
try {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
ch = br.readLine().toCharArray();
System.out.println((int)compute());
} catch (IOException e) {
e.printStackTrace();
}
}
public static double translation() {
double integer = 0.0;
double remainder = 0.0;
while(i < len && ch[i] >= '0' && ch[i] <= '9') {
integer *= 10;
integer += (ch[i] - '0');
i++;
}
if(i < len && ch[i] == '.') {
i++;
double c = 0.1;
while(ch[i] >= '0' && ch[i] <= '9') {
double t = ch[i] -'0';
t *= c;
c *= 0.1;
remainder += t;
i++;
}
}
return integer + remainder;
}
public static int getLevel(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
return 0;
};
return -1;
}
public static double operate(double a1, char op, double a2)
{
switch (op)
{
case '+':
return a1 + a2;
case '-':
return a1 - a2;
case '*':
return a1 * a2;
case '/':
return a1 / a2;
};
return 0;
}
public static double compute() {
Stack<Character> optr = new Stack<>();
Stack<Double> opnd = new Stack<>();
len = ch.length;
boolean isMinus = true; // 判断'-'是减号还是负号, true表示负号
for(i = 0; i < len;) {
if(ch[i] == '-' && isMinus) {
opnd.push(0.0);
optr.push('-');
i++;
}
else if(ch[i] == ')') {
isMinus = false;
i++;
while(optr.peek() != '(') {
double a2 = opnd.pop();
double a1 = opnd.pop();
char op = optr.pop();
double result = operate(a1, op, a2);
opnd.push(result);
}
optr.pop();
}
else if(ch[i] >= '0' && ch[i] <= '9') {
isMinus = false;
opnd.push(translation());
}
else if(ch[i] == '(') {
isMinus = true;
optr.push(ch[i]);
i++;
}
else {
while(!optr.empty() && getLevel(ch[i]) <= getLevel(optr.peek())) {
double a2 = opnd.pop();
double a1 = opnd.pop();
char op = optr.pop();
double result = operate(a1, op, a2);
opnd.push(result);
}
optr.push(ch[i]);
i++;
}
}
while(!optr.empty()) {
double a2 = opnd.pop();
double a1 = opnd.pop();
char op = optr.pop();
double result = operate(a1, op, a2);
opnd.push(result);
}
return opnd.pop();
}
}
附加:中缀转后缀
import java.util.*;
import java.io.*;
import java.text.* ;
public class Main
{
public static int[] pos = {0};
public static char[] ch;
public static HashMap<String, Integer> mon = new HashMap<>();
public static int len;
public static int i;
public static StringBuilder s;
public static void main(String[] args) throws ParseException{
try {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
ch = br.readLine().toCharArray();
s = new StringBuilder();
compute();
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String translation() {
StringBuilder tmp = new StringBuilder();
while(i < len && ch[i] >= '0' && ch[i] <= '9') {
tmp.append(ch[i]);
i++;
}
if(i < len && ch[i] == '.') {
tmp.append(ch[i]);
i++;
double c = 0.1;
while(ch[i] >= '0' && ch[i] <= '9') {
tmp.append(ch[i]);
i++;
}
}
return tmp.toString();
}
public static int getLevel(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
return 0;
};
return -1;
}
public static double operate(double a1, char op, double a2)
{
switch (op)
{
case '+':
return a1 + a2;
case '-':
return a1 - a2;
case '*':
return a1 * a2;
case '/':
return a1 / a2;
};
return 0;
}
public static void compute() {
Stack<Character> optr = new Stack<>();
Stack<Double> opnd = new Stack<>();
len = ch.length;
boolean isMinus = true; // 判断'-'是减号还是负号, true表示负号
for(i = 0; i < len;) {
if(ch[i] == '-' && isMinus) {
opnd.push(0.0);
optr.push('-');
i++;
}
else if(ch[i] == ')') {
isMinus = false;
i++;
while(optr.peek() != '(') {
char op = optr.pop();
s.append(op);
}
optr.pop();
}
else if(ch[i] >= '0' && ch[i] <= '9') {
isMinus = false;
s.append(translation());
}
else if(ch[i] == '(') {
isMinus = true;
optr.push(ch[i]);
i++;
}
else {
while(!optr.empty() && getLevel(ch[i]) <= getLevel(optr.peek())) {
char op = optr.pop();
s.append(op);
}
optr.push(ch[i]);
i++;
}
}
while(!optr.empty()) {
char op = optr.pop();
s.append(op);
}
}
}