public void ProcessInputTestGetFirstInputNumber() { int expected = 12345; string input = "12345 + 54321"; ProcessInput PI = new ProcessInput(input); Assert.AreEqual(expected, PI.GetFirstInputNumber()); }
public void EvaluateTestOperateWithSubtract() { string input = "2 - 1"; ProcessInput PI = new ProcessInput(input); Evaluate ev = new Evaluate(PI.GetFirstInputNumber(), PI.GetOperator(), PI.GetSecondInputNumber()); int expected = 1; Assert.AreEqual(expected, ev.Operate()); }
static void Main(string[] args) { Console.WriteLine("Calculator"); int count = 0; string input; Constant constant = new Constant(); while (true) { Console.Write("[{0}]> ", count); input = Console.ReadLine(); if(input == "exit" || input == "quit") { Console.WriteLine("loggin out"); break; } ProcessInput PI = new ProcessInput(input); try { Evaluate ev = new Evaluate(PI.GetFirstInputNumber(), PI.GetOperator(), PI.GetSecondInputNumber()); Console.WriteLine(" = " + ev.Operate()); } catch { constant.ChangeExpression(input); try { Console.WriteLine(constant.GetExpression()); } catch { Console.WriteLine("Error! Try again . . ."); } } count++; } }
public int GetExpression() { if(Input.Trim().Length == 1) { if (Char.IsLetter(char.Parse(Input.Trim()))) { if (IsInList(char.Parse(Input.Trim()))) { return GetValueOf(char.Parse(Input.Trim())); } else { throw new FormatException(); } } else { throw new FormatException(); } } else { var leftside = Input.Split('=')[0].Trim(); if(leftside.Length != 1 || Input.Split('=').Length != 2) { throw new FormatException(); } else { if (IsInList(char.Parse(leftside))) { throw new FormatException(); } var rightside = Input.Split('=')[1].Trim(); if(rightside.Length == 0) { throw new FormatException(); } try { for (int i = 0; i < rightside.Length; i++) { if (Char.IsLetter(rightside[i])) { if (IsInList(rightside[i])) { //this line has the error //string newRightside = rightside.Insert(i, GetValueOf(rightside[i]).ToString()); //rightside.Replace(i, 1); //s.Remove(3, 2).Insert(3, "ZX"); rightside = rightside.Remove(i, 1).Insert(i, GetValueOf(rightside[i]).ToString()); } else { throw new FormatException(); } } } ProcessInput PI = new ProcessInput(rightside); try { PI.GetOperator(); Evaluate ev = new Evaluate(PI.GetFirstInputNumber(), PI.GetOperator(), PI.GetSecondInputNumber()); int rightsideResult = ev.Operate(); AddToConstants(char.Parse(leftside), rightsideResult); return rightsideResult; } catch { AddToConstants(char.Parse(leftside), int.Parse(rightside)); return int.Parse(rightside); } } catch { throw new FormatException(); } } } }