Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Evaluate {
  5. private Stack number;
  6. private Stack operator;
  7.  
  8. Scanner scan = new Scanner(System.in);
  9. public Evaluate()
  10. {number = new Stack(); operator = new Stack();}
  11.  
  12.  
  13. public void menu()
  14. {
  15. System.out.println("Enter an expression to evaluate :");
  16.  
  17.  
  18. String to_evaluate = scan.next();
  19. String numbr = "";
  20. for(int i =0; i<to_evaluate.length(); i++)
  21. {
  22. String toAdd = "";
  23.  
  24.  
  25.  
  26.  
  27. if((to_evaluate.charAt(i) == '*') || (to_evaluate.charAt(i) == '+') || (to_evaluate.charAt(i) == '/') || (to_evaluate.charAt(i)=='-')){
  28. operator.push("" + to_evaluate.charAt(i));
  29.  
  30. String a = number.pop();
  31. String b = number.pop();
  32.  
  33. String sign = operator.pop();
  34.  
  35. numbr = "("+b+sign+a+")";
  36.  
  37. number.push(numbr);
  38.  
  39. }else{number.push(""+to_evaluate.charAt(i));}
  40.  
  41. }
  42.  
  43. System.out.println(numbr);
  44.  
  45. }
  46.  
  47.  
  48. public static void main (String[] args){
  49. Evaluate n = new Evaluate();
  50. n.menu();
  51. }
  52.  
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement