예제 #1
0
        static void Main(string[] args)
        {
            int count = 0;
            Constant currentConstant = new Constant();
            Evaluate expression;

            while (true)
            {
                Console.Write("[{0}]> ", count);
                string input = Console.ReadLine();
                expression = new Evaluate(input, currentConstant);
                if (input.ToLower() == "exit" || input.ToLower() == "quit")
                {
                    break;
                }
                else if (input.IndexOf("=")> -1)
                {
                    //set variable

                    Parse addConstant = new Parse(input, currentConstant);
                    addConstant.setOperatorIndex();
                    currentConstant.addConst(addConstant.StringFirst(), addConstant.secondNum());
                    // = saved 'x' as '3'
                    Console.WriteLine("= saved '" + addConstant.StringFirst() + "' as '" + addConstant.secondNum() + "'");

                }
                else
                {
                    int answer = expression.doMath();
                    Console.WriteLine("   = {0}", answer);
                    count++;

                }
            }
        }
예제 #2
0
 public void ParseOnlyAllowsSimpleExpressions()
 {
     Parse twoPlusThree = new Parse("2 - 5 + 3");
     twoPlusThree.setOperatorIndex();
     twoPlusThree.firstNum();
     twoPlusThree.secondNum();
 }
예제 #3
0
 public void ParseOnlyAllowsNumbersSecond()
 {
     Parse twoPlusThree = new Parse("2 + b");
     twoPlusThree.setOperatorIndex();
     twoPlusThree.firstNum();
     twoPlusThree.secondNum();
 }
예제 #4
0
 public void ParseCanFindSecondNum()
 {
     Parse twoPlusThree = new Parse("2 + 3");
     twoPlusThree.setOperatorIndex();
     twoPlusThree.firstNum();
     Assert.AreEqual(3, twoPlusThree.secondNum());
 }
예제 #5
0
 public void ParseAllowsConstantsFirst()
 {
     Constant newConstant = new Constant();
     Parse aplus3 = new Parse("a + 3", newConstant);
     aplus3.addConst("a", 5);
     aplus3.setOperatorIndex();
     Assert.AreEqual(5, aplus3.firstNum());
 }
예제 #6
0
 public void ParseCanFindOperation()
 {
     Parse twoPlusThree = new Parse("2 + 3");
     twoPlusThree.setOperatorIndex();
     Assert.AreEqual("+", twoPlusThree.mathOperation());
 }