In: Computer Science
Hi it's the java stack to convert from infix to postfix
If I want to fix this code to calculate each numbers not just displaying the numbers what should I do?
Would you help me to fix the result?
import java.util.Stack;
import java.util.Scanner;
class Main
{
//Function to return precedence
static int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static String infixToPostfix(String exp)
{
// initializing empty String for result
String result = new String("");
Stack<Character> stack = new Stack<>();
int i=0;
int len=exp.length();
for ( i = 0; i<len; ++i)
{
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c))
result =result+ c;
else if (c == '(')
stack.push(c);
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result =result+ stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression";
else
stack.pop();
}
else
{
//for operators
while (!stack.isEmpty() && Prec(c) <=
Prec(stack.peek()))
result =result+ stack.pop();
stack.push(c);
}
}
while (!stack.isEmpty())
result =result+ stack.pop();
return result;
}
public static void main(String[] args)
{
String exp = "(5+8)*(7-3)";
String exp2="6+7*(3+9)/2";
System.out.println("Infix "+exp);
System.out.println("Postfix "+infixToPostfix(exp));
System.out.println("Infix "+exp2);
System.out.println("Postfix "+infixToPostfix(exp2));
}
}
import java.util.Stack;
import java.util.Scanner;
class Main
{
//Function to return precedence
static int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// Method to evaluate value of a postfix expression
static int evaluatePostfix(String exp)
{
//create a stack
Stack<Integer> stack=new Stack<>();
int val3,val4,val5,val6;
// Scan all characters one by one
for(int i=0;i<exp.length();i++)
{
char c=exp.charAt(i);
// If the scanned character is an operand (number here),
// push it to the stack.
if(Character.isDigit(c))
stack.push(c - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = stack.pop();
int val2 = stack.pop();
switch(c)
{
case '+':
val3=val1+val2;
stack.push(val3);
pos(stack,i+1,val3,exp);
break;
case '-':
val4=val2-val1;
stack.push(val4);
pos(stack,i+1,val4,exp);
break;
case '/':
val5=val2/val1;
stack.push(val5);
pos(stack,i+1,val5,exp);
break;
case '*':
val6=val1*val2;
stack.push(val6);
pos(stack,i+1,val6,exp);
break;
}
}
}
return stack.pop();
}
static int pos(Stack<Integer> stack,int m ,int val3,String exp){
int r=0;
int[] arr= new int[100];
System.out.printf("\n");
while(!stack.isEmpty())
{
arr[r]=stack.pop();
r++;
}
for(int j=r-1;j>=0;j--)
{
System.out.print(" "+arr[j]);
}
for(int k=r-1;k>=0;k--)
{
stack.push(arr[k]);
arr[k]=0;
}
//System.out.println(val3);
for(int l=m;l<exp.length();l++) {
char c=exp.charAt(l);
System.out.print(" "+c);
}
System.out.printf("\n");
return 0;
}
static String infixToPostfix(String exp)
{
// initializing empty String for result
String result = new String("");
Stack<Character> stack = new Stack<>();
int i=0;
int len=exp.length();
for ( i = 0; i<len; ++i)
{
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c))
result =result+ c;
else if (c == '(')
stack.push(c);
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result =result+ stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression";
else
stack.pop();
}
else
{
//for operators
while (!stack.isEmpty() && Prec(c) <=
Prec(stack.peek()))
result =result+ stack.pop();
stack.push(c);
}
}
while (!stack.isEmpty())
result =result+ stack.pop();
return result;
}
public static void main(String[] args)
{
String exp = "(5+8)*(7-3)";
String exp2="6+7*(3+9)/2";
System.out.println("Infix:"+exp);
System.out.println("Postfix
expression:"+infixToPostfix(exp));
System.out.println("postfix evaluation:
"+evaluatePostfix(infixToPostfix(exp)));
System.out.printf("\n");
System.out.println("Infix:"+exp2);
System.out.println("Postfix
expression:"+infixToPostfix(exp2));
System.out.println("postfix evaluation:
"+evaluatePostfix(infixToPostfix(exp2)));
}
}
OUTPUT:
Infix:(5+8)*(7-3)
Postfix expression:58+73-*
13 7 3 - *
13 4 *
52
postfix evaluation: 52
Infix:6+7*(3+9)/2
Postfix expression:6739+*2/+
6 7 12 * 2 / +
6 84 2 / +
6 42 +
48
postfix evaluation: 48