I have just written this small calculator, and it kinda works.
2+2 = 4
4+4 = 8
but
2+2+2 = 12
4-2 = -8
So there is obviosly some glitches, and i suspect the "recurrsion". I have tried lots of adjustments but none solves the problem. Maybe you see something that i dont:
Code:using System; using System.Collections.Generic; using System.Text; namespace minicalc { class Program { public static String input, tmpexp; // Expression public static int length; // Lenght of input public static int curPos; // Current token public static int value, temp = 0; // value holders public static String Op; static void Main(string[] args) { System.Console.WriteLine("Please enter expression to calculate: "); input = System.Console.ReadLine(); length = input.Length; curPos = 0; while (curPos < length) { if (input[curPos] == '0' || input[curPos] == '1' || input[curPos] == '2' || input[curPos] == '3' || input[curPos] == '4' || input[curPos] == '5' || input[curPos] == '6' || input[curPos] == '7' || input[curPos] == '8' || input[curPos] == '9') { tmpexp += input[curPos]; } temp += Convert.ToInt32(tmpexp); Op = Convert.ToString(input[curPos ]); // Determine which operator we have and use it on the result switch (Op) { case "+": value += temp; break; case "-": value -= temp; break; case "*": value *= temp; break; } curPos++; } System.Console.WriteLine("Result of calculation: " + value); System.Console.ReadLine(); } } }




Reply With Quote