Advertisement
Guest User

Untitled

a guest
Feb 10th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1.     class RPN
  2.     {
  3.         public static int Evaluate(List<TreeNode> nodes) // Input is a list of TreeNodes given in POSTFIX traversal order of the tree
  4.         {
  5.             Stack<TreeNode> nodeStack = new Stack<TreeNode>(); // Create stack to use for RPN, type TreeNode as it could be a BinOp or Num
  6.  
  7.             foreach (TreeNode treeNode in nodes)
  8.             {
  9.                 if (treeNode is Num) nodeStack.Push(treeNode); // If it is a Num (Integer) then just push onto stack
  10.                 else if (treeNode is BinOp) // If it is a BinOp (operator, root of leaf) then pop last two operands and calculate:
  11.                 {
  12.                     if (treeNode.value.Equals("_")) // unary minus
  13.                     {
  14.                         Num arg = (Num)nodeStack.Pop(); // Only pop one off the stack, unary minus only operates on one operand
  15.  
  16.                         nodeStack.Push(new Num(arg.IntValue() * (-1))); // unary minus, so negate the operand
  17.                     }
  18.                     else
  19.                     {
  20.                         // Pop last two
  21.                         Num arg2 = (Num)nodeStack.Pop();
  22.                         Num arg1 = (Num)nodeStack.Pop(); // Note they are reversed, the first one to be popped is the second argument in the expression.
  23.  
  24.                         nodeStack.Push(new Num(Calculate(arg1.IntValue(), arg2.IntValue(), treeNode.value))); // Create new Num (Integer) with result of calc
  25.                     }
  26.                 }
  27.             }
  28.             Num result = (Num)nodeStack.Pop(); // Stack should be left with just one Num (Integer) as the final result
  29.             return result.IntValue();
  30.         }
  31.  
  32.         public static int Calculate(int arg1, int arg2, string operation) // Simplest way to 'act out' operators that are in string-form
  33.         {
  34.             int result = 0; // Default is 0
  35.             switch (operation)
  36.             {
  37.                 case "+":
  38.                     result = arg1 + arg2;
  39.                     break;
  40.                 case "-":
  41.                     result = arg1 - arg2;
  42.                     break;
  43.                 case "*":
  44.                     result = arg1 * arg2;
  45.                     break;
  46.                 case "/":
  47.                     result = arg1 / arg2;
  48.                     break;
  49.                 case "^":
  50.                     result = (int)Math.Pow(arg1, arg2);
  51.                     break;
  52.                 default:
  53.                     break; // do nothing as result = 0 already
  54.             }
  55.             return result;
  56.         }
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement