So i am attempting to make a calculator and I am unsure of how to take the equation and split it up into operators and operands. I have chosen to do this by using the string.split function to turn it into an array of numbers and operators to which i can then compare and output the result. However I want to throw and error if the user enters " + 100" as it will expect a operand first. This is my code to check the above condition
String equation = txt_equation.Text;
String[] equationArray= new String [30];
equationArray = equation.Split(' ');
if (equationArray[0] == "+" || equationArray[0] == "-" || equationArray[0] == "/" || equationArray[0] == "*")
{
System.Windows.Forms.MessageBox.Show("The equation entered is incorrect");
}
else
{...}
The problem is when I enter "100 + 100" it does get split up as:
equationArray[0] = "100"
equationArray[1] = "+"
equationArray[2] = "100"
but if I enter " + 100" the result is:
equationArray[0] = ""
equationArray[1] = "+"
equationArray[2] = "100"
Side note users enter in the numbers / operands with buttons numbers are entered into the equation as "n" operands as " operator " allowing the spaces to break it up