Ask the user for prefix expressions and evaluate each expression. If the result of an expression is zero, then end. i have this code so far: import java.util.*; class QueueUtils{ static Boolean Operand(char c){ if(c>=48 && c<=57) return true; else return false; } static double Prefix_evaluation(String exp){ Stack Stack = new Stack(); for(int j=exp.length()-1;j>=0;j--){ if(Operand(exp.charAt(j))) Stack.push((double)(exp.charAt(j)-48)); else{ if(exp.charAt(j)==' '){ } else{ double o1 = Stack.peek(); Stack.pop(); double o2 = Stack.peek(); Stack.pop(); switch(exp.charAt(j)){ case '+': Stack.push(o1 + o2); break; case '-': Stack.push(o1 - o2); break; case '*': Stack.push(o1 * o2); break; case '/': Stack.push(o1 / o2); break; case '%': Stack.push(o1%o2); break; } } } } return Stack.peek(); } public static void main(String[] args){ Scanner sc= new Scanner(System.in); String exp; int flag; flag=0; double d; do{ System.out.println("Enter an expression in prefix form (operator comes first)"); exp = sc.nextLine(); d = (Prefix_evaluation(exp)); System.out.println(d); if (d==0){ System.out.println("Exiting"); flag=1; } }while(flag!=1); } } however, it is not providing the correct ouput that i need it is supposed to be: Enter an expression in prefix form (operator comes first)\n 12ENTER 12\n Enter an expression in prefix form (operator comes first)\n + 2 33ENTER 35\n Enter an expression in prefix form (operator comes first)\n + 5 8ENTER 13\n Enter an expression in prefix form (operator comes first)\n - * 17 5 + 4 6ENTER 75\n Enter an expression in prefix form (operator comes first)\n - 5 5ENTER 0\n Exiting\n but it is outputting Enter an expression in prefix form (operator comes first)\n 12ENTER 1.0\n Enter an expression in prefix form (operator comes first)\n + 2 33ENTER 5.0\n Enter an expression in prefix form (operator comes first)\n + 5 8ENTER 13.0\n Enter an expression in prefix form (operator comes first)\n - * 17 5 + 4 6ENTER 2.0\n Enter an expression in prefix form (operator comes first)\n - 5 5ENTER 0.0\n Exiting\n can an expert help fix my code so it provides the correct ouput
Ask the user for prefix expressions and evaluate each expression. If the result of an expression is zero, then end.
i have this code so far:
import java.util.*;
class QueueUtils{
static Boolean Operand(char c){
if(c>=48 && c<=57)
return true;
else
return false;
}
static double Prefix_evaluation(String exp){
Stack<Double> Stack = new Stack<Double>();
for(int j=exp.length()-1;j>=0;j--){
if(Operand(exp.charAt(j)))
Stack.push((double)(exp.charAt(j)-48));
else{
if(exp.charAt(j)==' '){
}
else{
double o1 = Stack.peek();
Stack.pop();
double o2 = Stack.peek();
Stack.pop();
switch(exp.charAt(j)){
case '+':
Stack.push(o1 + o2);
break;
case '-':
Stack.push(o1 - o2);
break;
case '*':
Stack.push(o1 * o2);
break;
case '/':
Stack.push(o1 / o2);
break;
case '%':
Stack.push(o1%o2);
break;
}
}
}
}
return Stack.peek();
}
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
String exp;
int flag;
flag=0;
double d;
do{
System.out.println("Enter an expression in prefix form (operator comes first)");
exp = sc.nextLine();
d = (Prefix_evaluation(exp));
System.out.println(d);
if (d==0){
System.out.println("Exiting");
flag=1;
}
}while(flag!=1);
}
}
however, it is not providing the correct ouput that i need
it is supposed to be:
Enter an expression in prefix form (operator comes first)\n
12ENTER
12\n
Enter an expression in prefix form (operator comes first)\n
+ 2 33ENTER
35\n
Enter an expression in prefix form (operator comes first)\n
+ 5 8ENTER
13\n
Enter an expression in prefix form (operator comes first)\n
- * 17 5 + 4 6ENTER
75\n
Enter an expression in prefix form (operator comes first)\n
- 5 5ENTER
0\n
Exiting\n
but it is outputting
Enter an expression in prefix form (operator comes first)\n
12ENTER
1.0\n
Enter an expression in prefix form (operator comes first)\n
+ 2 33ENTER
5.0\n
Enter an expression in prefix form (operator comes first)\n
+ 5 8ENTER
13.0\n
Enter an expression in prefix form (operator comes first)\n
- * 17 5 + 4 6ENTER
2.0\n
Enter an expression in prefix form (operator comes first)\n
- 5 5ENTER
0.0\n
Exiting\n
can an expert help fix my code so it provides the correct ouput
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images