郁闷的C小加(三)
2017-09-19 07:01
176 查看
郁闷的C小加(三)
时间限制:1000 ms | 内存限制:65535 KB难度:4
描述聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很高兴。但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值。即先把表达式转换为前缀和后缀表达式,再求值。这时又要考虑操作数是小数和多位数的情况。
输入第一行输入一个整数T,共有T组测试数据(T<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数并且小于1000000。
数据保证除数不会为0。
输出对于每组测试数据输出结果包括三行,先输出转换后的前缀和后缀表达式,再输出计算结果,结果保留两位小数。
样例输入
2 1+2= (19+21)*3-4/5=
样例输出
+ 1 2 = 1 2 + = 3.00 - * + 19 21 3 / 4 5 = 19 21 + 3 * 4 5 / - = 119.20
import java.util.Scanner;
import java.util.Stack;
public class Main {
static String string;
public static int compare(char fh) {
switch (fh) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
private static void changePrefix(String str) {
Stack<Character> sign = new Stack<Character>();
StringBuilder all = new StringBuilder("");
for (int i = str.length() - 2; i >= 0; i--) {
char top = str.charAt(i);
if ((top >= '0' && top <= '9') || top == '.') {
all.append(top);
} else {
if (top == '+' || top == '-' || top == '*' || top == '/') {
all.append(" ");
while (!sign.isEmpty() && sign.peek() != ')'
&& (compare(sign.peek()) > compare(top))) {
all.append(sign.pop());
all.append(" ");
}
sign.push(top);
} else if (top == '(') {
while (sign.peek() != ')') {
all.append(" ");
all.append(sign.pop());
}
sign.pop();
} else if (top == ')') {
sign.push(top);
}
}
}
while (!sign.isEmpty()) {
all.append(" ");
all.append(sign.pop());
}
System.out.println(all.reverse() + " =");
}
private static void changeSuffix(String str) {
Stack<Character> sign = new Stack<Character>();
StringBuilder all = new StringBuilder("");
for (int i = 0; i < str.length(); i++) {
char top = str.charAt(i);
if ((top >= '0' && top <= '9') || top == '.') {
all.append(top);
} else {
if (top == '+' || top == '-' || top == '*' || top == '/') {
all.append(" ");
while (!sign.isEmpty()
&& compare(sign.peek()) >= compare(top)) {
all.append(sign.pop());
all.append(" ");
}
sign.push(top);
} else if (top == '(') {
sign.push(top);
} else if (top == ')') {
while (sign.peek() != '(') {
all.append(" ");
all.append(sign.pop());
}
sign.pop();
} else if (top == '=') {
while (!sign.isEmpty()) {
all.append(" ");
all.append(sign.pop());
}
all.append(" =");
}
}
}
string = all.toString();
System.out.println(all);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int times = scanner.nextInt();
String cc = scanner.nextLine();
while (times-- != 0) {
String str = scanner.nextLine();
changePrefix(str);
changeSuffix(str);
Stack<Double> result = new Stack<Double>();
String arr[] = string.split(" ");
for (int i = 0; i < arr.length; i++) {
char top = arr[i].charAt(0);
double value = 0;
if (top >= '0' && top <= '9') {
value = Double.valueOf(arr[i]);
result.push(value);
} else if (top == '+') {
double one = result.pop();
double two = result.pop();
double three = one + two;
result.push(three);
} else if (top == '-') {
double one = result.pop();
double two = result.pop();
double three = two - one;
result.push(three);
} else if (top == '*') {
double one = result.pop();
double two = result.pop();
double three = one * two;
result.push(three);
} else if (top == '/') {
double one = result.pop();
double two = result.pop();
double three = two / one;
result.push(three);
} else if (top == '=') {
System.out.printf("%.2f\n", result.pop());
}
}
}
}
}
相关文章推荐
- NYOJ 257 郁闷的C小加(一)(中缀式变后缀式)
- nyoj257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- 郁闷的C小加(二)
- NYOJ 257 郁闷的C小加(一)
- NYOJ 257 郁闷的C小加(一)
- nyoj267 郁闷的C小加(二)
- NYOJ 267 郁闷的C小加(二)
- NYOJ - 郁闷的C小加(一)
- nyoj-409 郁闷的C小加(三) (表达式求值,中缀式转前缀式,中缀式转后缀式)
- NYOJ--郁闷的C小加(一)
- 南阳oj 郁闷的c小加(一) 题目257
- 郁闷的C小加(一)
- nyoj-郁闷的C小加
- NYOJ 267 郁闷的C小加(二)
- NYOJ - 郁闷的C小加(二)
- 郁闷的C小加(三)(nyoj 409)
- 郁闷的C小加(一)
- 南阳oj 郁闷的c小加(二) 题目267