Making a simple calculator with arrays in C#

混江龙づ霸主 提交于 2019-12-12 02:16:33

问题


I am trying to make a simple calculator with arrays in C#. Firstly I tried making it using two integers and one operator only and it worked well. Now I am trying to do so that the user can make the expression as long as they like. For example 7 * 7 + 1 / 50 instead of a simple 9 + 8 which includes only one operator and two integers. The problem is that whenever I type a long expression with multiple numbers and operators it only calculates the first 2 numbers with the first operator. What is a good fix for this problem ? Thanks in advance.

static void Main()
    {
        while (true)
        {
            Console.WriteLine("Write an expression with two numbers and an operator with space in-between, for example, 4 + 2");
            string expression;
            string[] array;
            string[] array1;
            expression = Console.ReadLine();
            array = expression.Split();
            array1 = Calculation(array);
            Console.WriteLine("Press ENTER to write a new expression.");
            Console.ReadLine();
            Console.Clear();
        }

    }
    static string[] Calculation(string[] arr)
    {
        double numLeft= 0.0;
        double numRight = 0.0;
        string sign = "";
        double result = 0.0;
        int index = 1;

        while (true)
        {
            numLeft = Convert.ToDouble(arr[0]);
            sign = Convert.ToString(arr[index]);
            numRight = Convert.ToDouble(arr[index + 1]);
            index = index + 2;
            if (sign == "+")
            {
                Console.Clear();
                Console.WriteLine();
                result= result + numLeft;
            }
            else if (sign == "-")
            {
                Console.Clear();
                result = result + numLeft;
                numLeft = 0 - numRight;
            }
            else if (sign == "*")
            {
                Console.Clear();
                numLeft = numLeft * numRight;
            }
            else if (sign == "/")
            {
                Console.Clear();
                numLeft = numLeft / numRight;
            }
            else
            {
                break;
            }
            result = result + numLeft;
            Console.WriteLine("Answer: {0}", result);
            return arr;
        }
        return arr;
    }

回答1:


because you return the array at the end of the "while true", so only first 2 get calculated.

Also, this will not be correct. for example: 2 + 3 * 4 = 14, and not 20, like your calculator will calculate.




回答2:


How about this?

Split your operation string into pieces and fill a list with them;

7 * 7 + 1 / 50 => [7][*][7][+][1][/][50]

Then go through the list and solve the * and / operations. When you encounter one of the operators remove the operator element, the one before it and the one after it, and replace them with the result. Keep in mind that the list length changes.

first iteration => [49][+][1][/][50] second iteration => [49][+][0.02]

Then do the same for + and - operators first iteration => [49.02]

When you have only one element remaining in the list, that is your result.




回答3:


For simple computations there exists a method in .net:

public static int Compute(string operation)
{
    return new DataTable().Compute("7 * 7 + 1 / 50", null);
}

Source: C# Math calculator

If you really want to implement this I would suggest a recursive binary tree algorithm.

Pseudo-code: 1. Split incoming string on places where there is a mathematical operation symbol and turn in to a binary tree 2. Go through this tree and resolve operations that have a greater priority (* comes before +) 3. Recursion should return value of two child leafs and so on until it yields only one value to the main program



来源:https://stackoverflow.com/questions/40174345/making-a-simple-calculator-with-arrays-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!