0

I'm trying to build a calculator in C# for practice. My first step is to parse the user-input equation. For troubleshooting purposes, I'm trying to see if my parser can differentiate between numbers and operation symbol (e.g. "+", "-", "/", etc). The idea is to compare the user-input operation to a character array containing the allowed operations (+, -, /, *). If one of these allowed operation is inputted by the user, the code should say it is an allowed operation. The code to do that is shown below:

        public static void ProcessEq(string equation)
        {
            //Parse the equation using regular expression
            equation = equation.Replace(" ", "");
            char[] operations = { '+', '-', '*', '/' };
            string[] parts = Regex.Split(equation, @"(?<=[-+*/()])|(?=[-+*/()])");
            foreach (string item in parts)
                //Console.WriteLine(item + " is being tested...");
                try
                {
                    float num = Int32.Parse(item);
                    Console.WriteLine(item + " is a number");
                }
                catch(FormatException)
                {
                    if ( Array.IndexOf(operations, item) > -1 )
                    {
                        Console.WriteLine(item + " is a valid operation");
                    }
                    else
                    {
                        Console.WriteLine(item + " is NOT a valid operation");
                    }

                }

        }

The if-condition is not picking up on any of the allowed operation symbols. If I type in 2+2, it keeps just passing into the else-condition and saying that the user inputted operation is not valid. Is my if-condition flawed?

Thanks

ssthor22
  • 55
  • 4
  • There are many different ways of doing this. Which way is best for you depends on too many things you haven't explained. If you are determined to create an expression-evaluator by hand instead of using one of the [many existing parsers](https://stackoverflow.com/questions/2607798/) you are already going in the wrong direction. You need to [break your problem into simpler steps](https://ericlippert.com/2014/03/21/find-a-simpler-problem/); for example get rid of that Regex and build a function that returns whether a string is an integer or not. – Dour High Arch Jan 26 '20 at 23:48
  • I started out with the regex because I knew eventually I wanted the parser to be able to handle complicated expressions. I'm completely new to C# so I'm still learning as I go. I knew I probably wasn't building an efficient parser at all. Regardless, I wanted to just try for the sake of learning by tinkering around. Thank you for the pointers though. – ssthor22 Jan 27 '20 at 00:08

1 Answers1

0

The Array.IndexOf is looking for a string in an array of characters. You can simply change it to

if ( Array.IndexOf(operations, item[0]) > -1 )

or

if ( Array.IndexOf(operations, Convert.ToChar(item)) > -1 )

to fix it.

BrianM
  • 836
  • 9
  • 17