Home > Back-end >  Making the Calculator
Making the Calculator

Time:10-31

I know there's tons of way of solving this issue, i know a "dumb" way using if statements, however i've been on the path of trying to use less code, but more use of it.

     if(ope == " ")
        {
            result = userInput   userInput2;
            Console.WriteLine(result);
        }

and just copy past this junk for all the operators.

I did a list, with all the operators, and thought i could check the list, if input does not equal anything in the list, repeat, until an operator is picked. (This might lead to a bug() what happens if the user writes more than 1 correct answer, will it take more from the list or first one. An example: What if user inputs - /(haven't even come this far), both of them are in the list.

Here's an example of a code that i wrote for taking double: an example would be "while (!double.TryParse(Console.ReadLine(), out userInput));" From the code snippet, where the code won't take any string values, and repeat the code over and over until userInput is a number.

I don't know how to loop the code with a do while loop to check if the userInput has the same value as the list contain: the math operators ,-,*,/;^,% and if the user input isn't correct, repeat the code until it's correct.

My question is, How do i loop through the list, check if the input is correct, pick out the correct input and solve the equation on userInput X userInput2. Should the user pick 2 operators such as / and - Repeat loop. Pick only 1 operator.

    private static void Main(string[] args)
      {
        List<string> OperatorToUse = new List<string>();
        OperatorToUse.Add(" ");
        OperatorToUse.Add("-");
        OperatorToUse.Add("/");
        OperatorToUse.Add("*");
        OperatorToUse.Add("%");
        OperatorToUse.Add("^");
       
        double userInput;
        double userInput2;
        var result;
        var ope;
             
        do
        {
            Console.WriteLine("Enter Your first number: ");
        } while (!double.TryParse(Console.ReadLine(), out userInput));
           #If the answer is X, and user Input is also X. stop the Loop
               #Not working
       do
       {
            Console.WriteLine("Enter your Operator: ");
        } while (!OperatorToUse.Contains(Console.ReadLine(), out OperatorToUse));
                #Not working
        do
        {
            Console.WriteLine("Enter your Operator: ");
        } while (Console.Readline(ope) == " " || ope == "-" || ope == "/");    
         
        do
        {
            Console.WriteLine("Enter Your Second number: ");
        } while (!double.TryParse(Console.ReadLine(), out userInput2));

        Console.WriteLine($"{result}");
     }

CodePudding user response:

Your while loop should look like this:

do
{
    Console.WriteLine("Enter your Operator: ");
    ope = Console.ReadLine()?.Trim();
} while (!OperatorToUse.ContainsKey(ope));

In the example below, instead of List I'm using Dictionary but the idea is the same.

In order to avoid if-statements for each operator, you should use a Dictionary and add each operator as a Key and the respective math function as a Value. You can do this with a delegate as in the example. Then you just call the respective function by retrieving it from the dictionary with the selected operator like this:

OperatorToUse[ope](userInput, userInput2)

internal class Program
{
    delegate double MathOperation(double a, double b);
    static void Main(string[] args)
    {
        Dictionary<string, MathOperation> OperatorToUse = new Dictionary<string, MathOperation>();
        OperatorToUse.Add(" ", (a, b) => a   b);
        OperatorToUse.Add("-", (a, b) => a - b);
        OperatorToUse.Add("/", (a, b) => a / b);
        OperatorToUse.Add("*", (a, b) => a * b);
        OperatorToUse.Add("%", (a, b) => a % b);
        OperatorToUse.Add("^", Math.Pow);

        double userInput;
        double userInput2;
        string ope = String.Empty;
        do
        {
            Console.WriteLine("Enter Your first number: ");
        } while (!double.TryParse(Console.ReadLine(), out userInput));
                    
        do
        {
            Console.WriteLine("Enter your Operator: ");
            ope = Console.ReadLine()?.Trim();
        } while (!OperatorToUse.ContainsKey(ope));

        do
        {
            Console.WriteLine("Enter Your Second number: ");
        } while (!double.TryParse(Console.ReadLine(), out userInput2));

        Console.WriteLine($"{OperatorToUse[ope](userInput, userInput2)}");
    }
}
  • Related