package sherry.tyut;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
/*
/*
* 中序表达式求值(见Infix2.java)
* 操作数和运算符两个栈若都是Integer类型,则'+','-','*','/'等无法用char表示出,只能用其相应的ASCII
* 表示。'+','-','*','/'分别是43,45,42,47。 其次,buf.readLine()输出的是String类型。
* str.charAt(i)输出的是char类型,类似于char ch = '/'或是char n1='3';。
* 如何将单字符类型转换为int型。采用Integer.parseInt(Character.toString(str.charAt(i)))。
* 这样对于n1='3'可以把n1转换为int型。而对于ch = '/'则不能转换成功。
*
* 把操作数,运算符都定义为Character类型,不便于操作数的入栈和出栈操作,不能够实现。
* 把操作数,运算符都定义为Integer类型,只要注意运算符用相应的ASCII表示即可,可以实现的。
* Infix2.java中操作数被定义为Integer类型,运算符被定义为Character类型。得以实现。
*
* 注意这里的运算,只能是个位数的加减乘除运算。操作数若大于一位,或是运算符带有括号,其他运算符等等,
* 算法不能够实现。CharTest.java是一个专为Infix.java设计的一个测试检验类。
*/
public class Infix2 {
public static void main(String[] args) throws IOException {
Stack<Integer> operandstack = new Stack<Integer>();//操作数栈
Stack<Character> operatorstack = new Stack<Character>();//运算符栈
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入中序表达式:");
String str = buf.readLine();
int len = str.length();
int i = 0;
while(i<len) {
if (operator(str.charAt(i))){//读取的是运算符
if(operatorstack.empty()) {//运算符栈空
System.out.println("运算符栈空中:"+str.charAt(i));
operatorstack.push(str.charAt(i));
}
else {//运算符栈非空
if(priority(str.charAt(i),operatorstack.peek())) {//准备入栈的运算符优先级大于栈顶运算符
System.out.println("运算符栈非空中:"+str.charAt(i));
operatorstack.push(str.charAt(i));//入栈
}
else {
char oper = operatorstack.pop();//取运算符栈顶元素
int num1 = operandstack.pop();//取操作数栈顶中两个操作数
int num2 = operandstack.pop();
System.out.println("oper="+oper+",num1="+num1+",num2="+num2+",str.charAt(i)="+str.charAt(i));
System.out.println("准备入栈的运算符优先级小于栈顶运算符"+result(num2,num1,oper));
operandstack.push(result(num2,num1,oper));//计算结果存入操作数栈
operatorstack.push(str.charAt(i));//将准备进入的运算符入栈
}
}
}
else {//读取的是操作数
int num = Integer.parseInt(Character.toString(str.charAt(i)));
System.out.println("num="+num);
operandstack.push(num);
}
i ++;
}
while(!operatorstack.empty()) {//运算符栈非空,这里要用到循环while,而不是条件if。
char oper = operatorstack.pop();//取运算符栈顶元素
int num1 = operandstack.pop();//取操作数栈顶中两个操作数
int num2 = operandstack.pop();
System.out.println("运算符栈非空''"+result(num2,num1,oper));
operandstack.push(result(num2,num1,oper));//计算结果存入操作数栈
}
while(!operandstack.empty()) {
System.out.print(operandstack.pop());
}
}
private static boolean operator(char c) {
if(c=='+' || c=='-' || c=='*' || c=='/') {
return true;
}
else return false;
}
private static boolean priority(char c1, char c2) {//运算符的比较:c1的优先级大于c2。
if(c1=='*'||c1=='/' && c2=='+'||c2=='-')
return true;
else return false;
}
private static int result(int operand1, int operand2, char operator) {
System.out.println("-----");
System.out.println("operand1="+operand1);
System.out.println("operand2="+operand2);
System.out.println("~~~~~~~~~~~~~~~~");
switch(operator) {
case '+': return (operand1 + operand2);
case '-': return (operand1 - operand2);
case '*': return (operand1 * operand2);
case '/': return (operand1 / operand2);
}
return 0;
}
}