Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Evaluation2SK {
- static String[] Statement = {"(1+2)*3+1","1+3-2/1","(1+2)-2*1","3+2*2-1","1+2*(1+1)"};
- static String post="";
- static int value;
- public static void main(String[] args){
- for (int i = 0; i < Statement.length; i++){ // no of statements to be evaluated
- post="";
- // infix -> postfix
- System.out.println("infix = " +Statement[i]);
- Stack211<Character> st = new Stack211();
- for (int j = 0; j < Statement[i].length(); j++) {
- char c = Statement[i].charAt(j);
- if (c >= '0' && c <= '9') {
- post += c;
- } // find out the number
- // If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack.
- else if (st.isEmpty() || (char)st.myStack[st.stackTop] == '(') {
- st.push(c);
- } else if (c == '(') {//If the incoming symbol is a left parenthesis, push it on the stack.
- st.push(c);
- } else if (c == ')') {//If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses.
- while ((char) st.myStack[st.stackTop] != '(') {
- post+=(st.pop());
- }
- st.pop();
- } else if (checkprecedence(c) > (char) st.myStack[st.stackTop]) {//If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
- st.push(c);
- }else if (checkprecedence(c)== (char)st.myStack[st.stackTop]){//If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator.
- post+=(st.pop());
- if(c=='*'||c=='+'){
- st.push(c);
- }
- }else if (checkprecedence(c)< (char)st.myStack[st.stackTop]){//If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack
- post+=(st.pop());
- j--;
- }else if(c=='*'||c=='+'){
- post+=(st.pop());
- }
- }
- while(!st.isEmpty()) {
- post += st.pop();// end of first statement(1 + 2 ) * 3. postfix is now produced
- }
- System.out.println("postfix = " +post);
- //postfix = 1+2*3
- Stack211<Integer> intSt = new Stack211();// postfix evaluation
- for (int k = 0; k < post.length(); k++){
- char c= post.charAt(k);
- if (c >='0' && c<='9'){
- int v = c-'0';
- intSt.push(v);
- value+=v; //just test
- } else {
- int x = intSt.pop();
- int y = intSt.pop();
- int value= calculator(x,y,c);
- intSt.push(value);//push the value back to stack top
- }
- }
- value=intSt.pop();//pop the value out
- System.out.println("value= " + value);
- System.out.println();
- }
- }
- public static int checkprecedence(char c){// method for precedence
- int num;
- if (c=='*'||c=='/'){
- num=2;
- return num;
- }else if(c== '+'||c=='-'||c=='('){
- num= 1;
- return num;
- }
- return 0;
- }
- static int calculator(int x,int y, char c){
- if (c == '+') {// + pop calcu then push
- return (y+x);
- } else if (c == '-') {
- return(y-x);
- } else if (c == '*') {
- return(y*x);
- } else {
- return(y/x);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement