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

2
  • 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 you are already going in the wrong direction. You need to break your problem into simpler steps; for example get rid of that Regex and build a function that returns whether a string is an integer or not. Commented Jan 26, 2020 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. Commented Jan 27, 2020 at 0:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok, that makes sense. Fixed now and I can move on! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.