简体   繁体   中英

C# string calculator

The function idea is to act as a calculator, input is a string with signs(*/+-) and numbers, output is calculated value example: input:"5*7+32-2*5" output: "57"

static string Calculate(ref string s) {
        int left, right, leftOp, rightOp;
        string calculated, signPool = "*/+-";
        char sign;
        for (int j = 0; j < 4; j++) {
            sign = signPool[j];
            for (int i = s.IndexOf(sign); i != -1; i = s.IndexOf(sign)) {
                for (left = i-1; Char.IsDigit(s[left]); left--) { };
                left++;
                for (right = i+1; Char.IsDigit(s[right]); right++) { };
                right--;
                leftOp = Convert.ToInt32(s.Substring(left, i - left));
                rightOp = Convert.ToInt32(s.Substring(right, right - i));
                switch (sign) {
                    case '*':
                        calculated = Convert.ToString(leftOp * rightOp);
                        break;
                    case '/':
                        calculated = Convert.ToString(leftOp / rightOp);
                        break;
                    case '+':
                        calculated = Convert.ToString(leftOp + rightOp);
                        break;
                    case '-':
                        calculated = Convert.ToString(leftOp - rightOp);
                        break;
                }

                calculated = Convert.ToString(leftOp * rightOp);
                s = s.Replace(s.Substring(left, right - left + 1), calculated);
            }
        }
        return s;
    }

the problem is that variale "left" becomes 0 all the time, pls help

for (int i = s.IndexOf(sign); i != -1; i = s.IndexOf(sign)) {
            for (left = i-1; Char.IsDigit(s[left]); left--) { };
            left++;

Notice the for loop inside the for loop. You are using left as the loop variable when instead you should use a different variable as the conditions to your loop. Try assigning a variable such as x = i - 1; If you need the left to change to i - 1 then do that before the loop starts. I hope this helps.

Your calculator algorithm is not following proper PEMDAS rules. Multiplication and Division are equivalent operations, and should be performed from left to right as they are encountered . This means you need to be searching for multiplication AND division operators at the same time as you move from left to right. Currently you are performing all multiplication from left to right, and then afterwards performing all division from left to right.

For example, consider this problem:

5 * 7 / 5 * 2

Solving left to right with multiplication and division as equivalent operations gives us:

// Correct
35 / 5 * 2
7 * 2
14

Solving with your current algorithm, however, would yield a different answer:

// Incorrect
35 / 5 * 2
35 / 10
3.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM