Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Evaluate {
- private Stack number;
- private Stack operator;
- Scanner scan = new Scanner(System.in);
- public Evaluate()
- {number = new Stack(); operator = new Stack();}
- public void menu()
- {
- System.out.println("Enter an expression to evaluate :");
- String to_evaluate = scan.next();
- String numbr = "";
- for(int i =0; i<to_evaluate.length(); i++)
- {
- String toAdd = "";
- if((to_evaluate.charAt(i) == '*') || (to_evaluate.charAt(i) == '+') || (to_evaluate.charAt(i) == '/') || (to_evaluate.charAt(i)=='-')){
- operator.push("" + to_evaluate.charAt(i));
- String a = number.pop();
- String b = number.pop();
- String sign = operator.pop();
- numbr = "("+b+sign+a+")";
- number.push(numbr);
- }else{number.push(""+to_evaluate.charAt(i));}
- }
- System.out.println(numbr);
- }
- public static void main (String[] args){
- Evaluate n = new Evaluate();
- n.menu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement