简体   繁体   English

C#字符串计算器

[英]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" 功能思想是充当计算器,输入是带有符号(* / +-)和数字的字符串,输出是计算值示例:输入:“ 5 * 7 + 32-2 * 5”输出:“ 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 问题是variale的“ left”一直都变为0,请帮助

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. 注意for循环内的for循环。 You are using left as the loop variable when instead you should use a different variable as the conditions to your loop. 您使用left作为循环变量,而应该使用其他变量作为循环条件。 Try assigning a variable such as x = i - 1; 尝试分配一个变量,例如x = i-1; If you need the left to change to i - 1 then do that before the loop starts. 如果您需要将左侧更改为i-1,请在循环开始之前执行此操作。 I hope this helps. 我希望这有帮助。

Your calculator algorithm is not following proper PEMDAS rules. 您的计算器算法未遵循正确的PEMDAS规则。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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