fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using ExpressionComputer.Utility;
  7.  
  8. namespace ExpressionComputer
  9. {  
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("6 == {0}\n", Exp.Compute ("1 + 2 * 3 - 4 + 5 * 6 / 2 - 2 * 3 ^ 2 + ( 5 - 2 ) * 2"));
  15.  
  16.  
  17.             Console.WriteLine("19 == {0}\n", Exp.Compute("1 + 2 * 3 ^ 2"));
  18.  
  19.  
  20.             Console.WriteLine("{0}\n", Exp.ConvertToRpnString("1 + 2 + 3 + 4 + 5"));
  21.  
  22.             string orig = "1 + 2 * 3 + 4";
  23.             Console.WriteLine(orig);
  24.             IList<string> rpn = Exp.ConvertToRpn(orig);
  25.             Console.WriteLine(Exp.ConvertRpnToString(rpn));
  26.             int n = Exp.ComputeRpn(rpn);
  27.             Console.WriteLine("11 == {0}\n", n);
  28.  
  29.  
  30.  
  31.             string orig2 = "1 + 2 * ( 3 + 4 )";
  32.             Console.WriteLine(orig2);
  33.             IList<string> rpn2 = Exp.ConvertToRpn(orig2);
  34.             Console.WriteLine("{0}", Exp.ConvertRpnToString(rpn2));
  35.             Console.WriteLine("15 == {0}\n", Exp.ComputeRpn(rpn2));
  36.  
  37.  
  38.             Console.WriteLine("23 == {0}", Exp.Compute("1 + 2 * 3 + 4 + 2 * 6"));
  39.             Console.WriteLine("17 == {0}\n", Exp.Compute("1 + 2 * 3 + 4 + 2 * 6 / 2"));
  40.  
  41.  
  42.             string orig3 = "1 + 2 * 3 + 4 + 2 * 6 / 2 - 1";
  43.             Console.WriteLine ("{0}",orig3);
  44.             string exp2 = Exp.ConvertToRpnString(orig3);
  45.             Console.WriteLine(exp2);
  46.             Console.WriteLine("16 == {0}", Exp.Compute(orig3));
  47.  
  48.              
  49.              
  50.  
  51.  
  52.  
  53.  
  54.             Console.ReadLine();
  55.         }
  56.     }
  57.  
  58.  
  59.     public static class Exp
  60.     {
  61.  
  62.         public static string ConvertRpnToString(IList<string> rpn)
  63.         {
  64.             return string.Join(" ", rpn.ToArray());
  65.         }
  66.  
  67.         public static string ConvertToRpnString(string exp)
  68.         {
  69.             var rpn = Exp.ConvertToRpn(exp);
  70.  
  71.             return string.Join(" ", rpn.ToArray());
  72.         }
  73.  
  74.         public static IList<string> ConvertToRpn(string exp)
  75.         {
  76.             IList<string> output = new List<string>();
  77.             Stack<string> opStack = new Stack<string>();
  78.  
  79.             string[] tokens = exp.Split(' ');
  80.  
  81.             foreach (var token in tokens)
  82.             {
  83.                 bool isOperator = token.IsOperator();
  84.  
  85.                 if (token == "(" || token == ")")
  86.                 {
  87.                     if (token == "(")
  88.                     {
  89.                         opStack.Push(token);
  90.                     }
  91.                     else if (token == ")")
  92.                     {
  93.                         string op = "";
  94.                         while ((op = opStack.Pop()) != "(")
  95.                         {
  96.                             output.Add(op);
  97.                         }
  98.  
  99.                     }
  100.  
  101.                 }
  102.                 else if (!isOperator)
  103.                 {
  104.                     output.Add(token);
  105.                 }
  106.                 else
  107.                 {
  108.                     while (opStack.Count > 0 &&
  109.                                 (
  110.                                     (token.IsLeftAssociative() && token.OperatorPrecedence() <= opStack.Peek().OperatorPrecedence())
  111.                                     ||
  112.                                     (token.OperatorPrecedence() < opStack.Peek().OperatorPrecedence())
  113.                                 )                      
  114.                           )
  115.                     {
  116.                         output.Add(opStack.Pop());
  117.                     }
  118.                     opStack.Push(token);
  119.                 }
  120.             }
  121.  
  122.             while (opStack.Count > 0)
  123.                 output.Add(opStack.Pop());
  124.  
  125.  
  126.             return output;
  127.         }
  128.  
  129.         public static int Compute(string expr)
  130.         {
  131.             return ComputeRpn(ConvertToRpn(expr));
  132.         }
  133.  
  134.         public static int ComputeRpn(IList<string> rpn)
  135.         {
  136.             Stack<string> values = new Stack<string>();
  137.  
  138.             int n = 0;
  139.             foreach (string token in rpn)
  140.             {
  141.                 bool isOperator = token.IsOperator();
  142.  
  143.                 if (isOperator)
  144.                 {
  145.                     int opB = int.Parse(values.Pop());
  146.                     int opA = int.Parse(values.Pop());
  147.                     int result = 0;
  148.                     switch (token)
  149.                     {
  150.                         case "*":
  151.                             result = opA * opB;
  152.                             break;
  153.                         case "/":
  154.                             result = opA / opB;
  155.                             break;
  156.                         case "+":
  157.                             result = opA + opB;
  158.                             break;
  159.                         case "-":
  160.                             result = opA - opB;
  161.                             break;
  162.                         case "^":
  163.                             result = (int)Math.Pow((double)opA, (double)opB);
  164.                             break;
  165.                     }
  166.                     values.Push(result.ToString());
  167.                 }
  168.                 else
  169.                 {
  170.                     values.Push(token);
  171.                 }
  172.             }
  173.  
  174.             return int.Parse(values.Pop());
  175.         }
  176.     }
  177.  
  178. }
  179.  
  180. namespace ExpressionComputer.Utility
  181. {
  182.     public static class Helper
  183.     {
  184.  
  185.         public static int OperatorPrecedence(this string op)
  186.         {
  187.             switch (op)
  188.             {
  189.                 case "^":
  190.                     return 3;
  191.                 case "*":
  192.                 case "/":
  193.                     return 2;
  194.                 case "+":
  195.                 case "-":
  196.                     return 1;
  197.             }
  198.             return 0;
  199.         }
  200.  
  201.         public static bool HasLowerOrEqualPrecedenceThan(this string opA, string opB)
  202.         {
  203.             return opA.OperatorPrecedence() <= opB.OperatorPrecedence();
  204.         }
  205.  
  206.         public static bool IsOperator(this string token)
  207.         {
  208.             return new[] { "*", "/", "+", "-", "^" }.Contains(token);
  209.         }
  210.  
  211.         public static bool IsRightAssociative(this string token)
  212.         {
  213.             return new[] { "^" }.Contains(token);
  214.         }
  215.  
  216.         public static bool IsLeftAssociative(this string token)
  217.         {
  218.             return !token.IsRightAssociative();
  219.         }
  220.     }
  221.  
  222.  
  223. }
Success #stdin #stdout 0.05s 34064KB
stdin
Standard input is empty
stdout
6 == 6

19 == 19

1 2 + 3 + 4 + 5 +

1 + 2 * 3 + 4
1 2 3 * + 4 +
11 == 11

1 + 2 * ( 3 + 4 )
1 2 3 4 + * +
15 == 15

23 == 23
17 == 17

1 + 2 * 3 + 4 + 2 * 6 / 2 - 1
1 2 3 * + 4 + 2 6 * 2 / + 1 -
16 == 16