コード例 #1
0
        public RPNToken Calculate(RPNToken x, RPNToken y, RPNToken fn)
        {
            RPNToken result;

            if (!(x.TokenType == Outputs.Double && y.TokenType == Outputs.Double &&
                  fn.TokenType == Outputs.Operator))
            {
                return(new RPNToken("ERROR"));
            }

            switch (fn.Token)
            {
            case "-":
                result = new RPNToken((x.Value - y.Value).ToString());
                break;

            case "+":
                result = new RPNToken((x.Value + y.Value).ToString());
                break;

            case "*":
                result = new RPNToken((x.Value * y.Value).ToString());
                break;

            case "/":
                result = new RPNToken((x.Value / y.Value).ToString());
                break;

            default:
                result = new RPNToken(Math.Pow(x.Value, y.Value).ToString());
                break;
            }

            return(result);
        }
コード例 #2
0
        // **********
        // Entrypoint
        // **********
        static void Main(string[] args)
        {
            // ***********************************************************************************
            // Fields :
            // calculator - RPNArithmetic handles math / management of the stored RPN expressions
            // previousCalculation - RPNToken storing previously the previously reduced expression
            // userInput - ConsoleKey storing current userInput
            // ***********************************************************************************
            RPNArithmetic calculator          = new RPNArithmetic();
            RPNToken      previousCalculation = new RPNToken("0");
            ConsoleKey    userInput;

            // **********************
            // Console User Interface
            // **********************
            do
            {
                DisplayMenu();
                userInput = Console.ReadKey().Key;
                switch (userInput)
                {
                case ConsoleKey.A:
                    Console.WriteLine();
                    GetRPNExpression(calculator);
                    break;

                case ConsoleKey.L:
                    Console.WriteLine(
                        "\n\nThe previous calculation was {0}.", previousCalculation);
                    break;

                case ConsoleKey.C:
                    CalculateAndDisplay(calculator, out previousCalculation);
                    break;

                case ConsoleKey.S:
                    calculator.Display();
                    break;

                case ConsoleKey.X:
                    calculator.Clear();
                    break;
                }
                Console.Write("\nPress any key to continue...  ");
                Console.ReadKey();
            }while (userInput != ConsoleKey.Q);
        }
コード例 #3
0
        protected static List <RPNToken> TryParseRPN(string line)
        {
            List <RPNToken> parsed = new List <RPNToken>();

            foreach (string s in line.Split(' '))
            {
                RPNToken temp = new RPNToken(s);
                parsed.Add(temp);

                if (!(temp.TokenType == Outputs.Double || temp.TokenType == Outputs.Operator))
                {
                    Console.WriteLine("\n\nLine could not be parsed: {0}\n\n", line);
                    parsed.Clear();
                    break;
                }
            }

            return(parsed);
        }
コード例 #4
0
 private static void CalculateAndDisplay(RPNArithmetic calculator, out RPNToken previousCalculation)
 {
     previousCalculation = calculator.ReduceStack();
     Console.WriteLine("\n\nThe calculated value is {0}.", previousCalculation);
 }
コード例 #5
0
 public void Push(RPNToken token)
 {
     stk.Push(token);
 }