static void Compute(Calc calc) { calc.compute (); if (calc.CurrentState is CompState) { Console.WriteLine ("({0}) {1} ({2}) = {3}", calc.Opperand1.ToString (), calc.pending_op.ToString (), calc.Opperand2.ToString (), calc.Total.ToString ()); } }
static void Compute(Calc calc) { calc.compute (); if (calc.CurrentState is CompState) { Console.WriteLine ("({0}) {1} ({2}) = ({3})", calc.Opperand1, calc.pending_op, calc.Opperand2, calc.Total); Console.WriteLine("operand1: {0}", calc.Total); } }
public override void Calculate(Calc calc) { //calc.CurrentState=ErrorState.Singleton; //throw new Exception("called calculate in compute state????"); //just redo the calc just finished calc.Total=calc.pending_op.compute(calc.Opperand1, calc.Opperand2); calc.CurrentState=CompState.Singleton; }
public override void Calculate(Calc calc) { Complex total = calc.pending_op.compute(calc.Opperand1, calc.Opperand2); calc.Opperand1=total; calc.Total = total; calc.CurrentState = CompState.Singleton; //calc.CurrentState=ErrorState.Singleton; //throw new Exception("you cant do a calculation right after you add an opperator, need another opperand"); }
static void ConsoleReadOperator(Calc calc) { while (!(calc.CurrentState is OpperatorEntredState)) { Console.Write ("Enter operator (+ - * /): "); String opperator = Console.ReadLine (); HandleMenuInterupt (opperator); calc.enterOp (opperator); } }
static void ConsoleReadOperand(Calc calc) { while (!(calc.CurrentState is OpperandEnteredState)) { Console.Write ("Enter Operand: "); String input = Console.ReadLine (); HandleMenuInterupt (input); calc.enterOperand (input); if (calc.CurrentState is ErrorState) { Console.WriteLine ("There was an input error"); } } }
public override void Calculate(Calc calc) { if (calc.pending_op != null) { Complex total = calc.pending_op.compute(calc.Opperand1, calc.Opperand2); //calc.Opperand1=total; calc.Total = total; calc.CurrentState = CompState.Singleton; } else { calc.CurrentState=ErrorState.Singleton; // throw new Exception("cannot do calculation w/o pending opperator"); } }
public override void addOpperand(Calc calc, Complex c) { calc.Opperand1=calc.Total; calc.Opperand2=c; // Complex result = calc.pending_op.compute(calc.Total, calc.Opperand2); // calc.Total=result; // calc.Opperand1=result; //why did I do this??? //calc.pending_op=null; calc.CurrentState=OpperandEnteredState.Singleton; //calc.CurrentState=CompState.Singleton; }
public static void runCalculator() { calc = new Calc (); PrintMenu (); bootstrap (calc); while (true) { try { ConsoleReadOperator (calc); ConsoleReadOperand (calc); Compute (calc); } catch (ClearException) { calc.Clear (); bootstrap (calc); } }//end while }
static void ConsoleReadOperand(Calc calc) { Complex new_opp=null; //get rid of duplicate code!!!! switch (Complex.mode) { case ComplexNumbers.MODE.Polar: while (!(calc.CurrentState is OpperandEnteredState)) { Console.WriteLine ("Enter operand: "); Console.Write("\tmagnitude: "); String magnitude = Console.ReadLine ().Trim(); Console.Write("\tangle: "); String angle=Console.ReadLine().Trim(); String opp_str=String.Format("{0} {1}", magnitude, angle); new_opp=calc.enterPolarOperand (opp_str); if (calc.CurrentState is ErrorState) { Console.WriteLine ("There was an input error"); } } break; case ComplexNumbers.MODE.Rectangular: while (!(calc.CurrentState is OpperandEnteredState)) { Console.WriteLine ("Enter operand: "); Console.Write("\treal part: "); String real = Console.ReadLine ().Trim(); Console.Write("\timaginary part: "); String imag=Console.ReadLine().Trim(); String opp_str=String.Format("{0} {1}", real, imag); new_opp=calc.enterRectOperand (opp_str); if (calc.CurrentState is ErrorState) { Console.WriteLine ("There was an input error"); } } break; } // if(new_opp!=null) // Console.WriteLine("operand: {0}", new_opp); }
public abstract void Calculate(Calc calc);
public abstract void addOpperand(Calc calc, Complex c);
public abstract void addOpperator(Calc calc, IBinOp op);
public override void addOpperator(Calc calc, IBinOp op) { calc.CurrentState=ErrorState.Singleton; throw new Exception("cant add opperator in opperator entered state"); }
public override void addOpperand(Calc calc, System.Numerics.Complex c) { calc.Opperand1=calc.Total; calc.Opperand2=c; calc.CurrentState=OpperandEnteredState.Singleton; }
public override void addOpperator(Calc calc, IBinOp op) { calc.CurrentState = ErrorState.Singleton; throw new Exception ("cannot add opperator when an opperand has not been entered yet!"); }
public override void addOpperand(Calc calc, Complex c) { calc.Total=new Complex(0,0); calc.Opperand2 = c; calc.CurrentState = OpperandEnteredState.Singleton; }
public override void addOpperand(Calc calc, Complex c) { calc.CurrentState=ErrorState.Singleton; throw new Exception("Cannot add opperand in opperand entered state"); }
public override void addOpperator(Calc calc, IBinOp op) { calc.pending_op=op; calc.CurrentState=OpperatorEntredState.Singleton; }
public override void addOpperand(Calc calc, Complex c) { calc.Opperand1=c; calc.CurrentState=OpperandEnteredState.Singleton; }
public override void Calculate(Calc calc) { throw new NotImplementedException (); }
public void TestCase() { Calc c=new Calc(); Assert.True(c.CurrentState is StartState); c.enterOperand("3 4"); Assert.True(c.CurrentState is OpperandEnteredState); Assert.AreEqual(c.Total, new Complex(0,0)); Assert.AreEqual(new Complex(3,4), c.Opperand2); //should fail b/c there's no operator to use! c.compute(); Assert.IsTrue(c.CurrentState is ErrorState); //add operator c.enterOp("+"); c.compute(); Complex answer=new Complex(3,4); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(answer, c.Total); Assert.AreEqual(answer, c.Opperand1); //now let's subtract and check if we get 0+0i c.enterOperand("3 4"); c.enterOp("-"); c.compute(); answer=new Complex(0,0); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(answer, c.Total); Assert.AreEqual(answer, c.Opperand1); //now lets add 5-6i Complex old_lhs=c.Opperand1; c.enterOp("+"); Assert.IsTrue(c.CurrentState is OpperatorEntredState); c.enterOperand("5 -6"); Assert.AreEqual(new Complex(5,-6), c.Opperand2); c.compute(); answer=new Complex(5,-6); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(answer.ToString(), c.Total.ToString()); Assert.AreEqual(old_lhs.ToString(), c.Opperand1.ToString()); //test multplication (5-6i) * (2+3i) = (28+3i) c.enterOp("*"); Assert.AreEqual(c.Total.ToString(), c.Opperand1.ToString()); c.enterOperand("2 3"); c.compute(); answer=new Complex(28,3); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(answer.ToString(), c.Total.ToString()); //test division (28+3i) / (3-2i)=(6+5i) c.enterOp("/"); c.enterOperand("3 -2"); c.compute(); answer=new Complex(6,5); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(answer.ToString(), c.Total.ToString()); //test error handling Complex opp1=c.Opperand1; Complex opp2=c.Opperand2; c.enterOperand("3"); Assert.IsTrue(c.CurrentState is ErrorState); //ensure no opperands are changed Assert.AreEqual(opp1, c.Opperand1); Assert.AreEqual(opp2, c.Opperand2); Assert.AreEqual(answer.ToString(), c.Total.ToString()); //ensure previous sum remains //enter bad input again! c.enterOperand("3 3 3 3 3"); Assert.IsTrue(c.CurrentState is ErrorState); //ensure no opperands are changed Assert.AreEqual(opp1, c.Opperand1); Assert.AreEqual(opp2, c.Opperand2); Assert.AreEqual(answer.ToString(), c.Total.ToString()); //ensure previous sum remains //test bad operatOR inputs IBinOp old_op=c.pending_op; c.enterOp("a"); Assert.IsTrue(c.CurrentState is ErrorState); //ensure no opperands are changed Assert.AreEqual(opp1, c.Opperand1); Assert.AreEqual(opp2, c.Opperand2); Assert.AreEqual(old_op, c.pending_op); Assert.AreEqual(answer.ToString(), c.Total.ToString()); //ensure previous sum remains //check that op1 and op2 and opp are same from before errors started Assert.AreEqual(opp1, c.Opperand1); Assert.AreEqual(opp2, c.Opperand2); Assert.AreEqual(old_op, c.pending_op); c.enterOp("+"); c.enterOperand("6 6"); c.compute(); answer=new Complex(12,11); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(answer.ToString(), c.Total.ToString()); //reset calc c.Clear(); Assert.IsTrue(c.CurrentState is StartState); Assert.AreEqual(new Complex(0,0), c.Opperand1); Assert.AreEqual(new Complex(0,0), c.Opperand2); //initialize for console calc c.enterOperand("3 4"); c.enterOp("+"); c.compute(); //now it's ready for the while loop c.enterOperand("1 2"); c.compute(); Assert.IsTrue(c.CurrentState is CompState); Assert.AreEqual(new Complex(4,6), c.Total); }
public override void Calculate(Calc calc) { calc.CurrentState = (ErrorState.Singleton); throw new Exception ("cannot do calculation when no expressions have been entered --- in the start state"); }
static void bootstrap(Calc calc) { ConsoleReadOperand (calc); //initial calc.enterOp ("+"); calc.compute (); }
static void ConsoleReadOperator(Calc calc) { while (!(calc.CurrentState is OpperatorEntredState)) { Console.Write ("Operation: "); String opperator = Console.ReadLine (); if (HandleMenuInterupt (opperator, calc)) { continue; //restart while loop } calc.enterOp (opperator); if (calc.CurrentState is ErrorState) { Console.WriteLine ("ERROR! Enter an operation."); Console.WriteLine ("Options are: c, +, -, *, /, p, r, m, q, M, A, R, I"); } } }
static Boolean HandleMenuInterupt(string input, Calc calc) { switch (input) { case "q": //throw new QuitException (); Environment.Exit (1); break; case "m": //throw new ShowMenuException (); PrintMenu (); break; case "c": throw new ClearException (); case "p": Complex.mode = ComplexNumbers.MODE.Polar; Console.WriteLine ("Calc set to polar mode"); Console.WriteLine("{0}", calc.Total); break; case "r": Complex.mode = ComplexNumbers.MODE.Rectangular; Console.WriteLine ("Calc set to rectagular mode"); Console.WriteLine("{0}", calc.Total); break; case "M": ShowEditMagnitude (); Console.WriteLine("{0}", calc.Total); break; case "A": ShowEditAngle (); Console.WriteLine("{0}", calc.Total); break; case "R": ShowEditReal (); Console.WriteLine("{0}", calc.Total); break; case "I": ShowEditImag (); Console.WriteLine("{0}", calc.Total); break; default: return false; } return true; }