Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在满足'|';_C#_String_Algorithm - Fatal编程技术网

C# 在满足'|';

C# 在满足'|';,c#,string,algorithm,C#,String,Algorithm,我有一个问题可能有点愚蠢,但我想不出来 我从.xml中读取,这不是问题,但我有一个字符串,看起来像这样: string str = "3|+5|*4/2+8*32|+7"; { "(3+5)", "|*", "4", "/", "2", "+", "8", "*", "32", "|+", "7" } { "((3+5)*4)", "/", "2", "+", "8", "*", "32", "|+", "7" } { "((3+5)*4)", "/", "2", "+", "8", "*",

我有一个问题可能有点愚蠢,但我想不出来

我从.xml中读取,这不是问题,但我有一个字符串,看起来像这样:

string str = "3|+5|*4/2+8*32|+7";
{ "(3+5)", "|*", "4", "/", "2", "+", "8", "*", "32", "|+", "7" }
{ "((3+5)*4)", "/", "2", "+", "8", "*", "32", "|+", "7" }
{ "((3+5)*4)", "/", "2", "+", "8", "*", "(32+7)" }
我必须把它从这个状态改成-

(3+5)*4/2+8*(32+7).
基本上,当在+,-,/,*之前有“|”时,我必须去掉“|”并加上(),然后我必须计算总和,这不再是问题了。 我试着拆开绳子或是检查他的所有特征,但没有一个像我想要的那样有效

我只是需要一些方向,从哪里开始。如果你有任何想法,请写下来,让我试试。


当有
3 |+5
时,该方法应看到|,并在第一个数字前添加开放括号,如
(3+5
之后,它必须检查数字5包含
|
之后的操作,如果不是,它必须在5之后加上右括号,否则继续下一个数字,当找到一个没有
|
的操作时,它必须在操作之前加上右括号。

此解决方案的先决条件是输入表达式将我从来没有括号。如果输入中有括号,那么这不是解决方案,需要其他解决方案

将输入字符串拆分为令牌列表:

string expression = "3|+5|*4/2+8*32|+7";
List<string> tokens = TokenizeExpression(expression); // You code this
重复此操作,直到更换所有
“| op”
运算符


结果是列表中所有元素的串联,即:
“((3+5)*4)/2+8*(32+7)”

这里有一种方法可以处理您的输入。但我担心有很多方法可能会失败。也许它能给您一个想法:

string str = "3|+5|*4/2+8*32|+7";
char[] operators = {'+', '-', '/', '*'};
StringBuilder sb = new StringBuilder();
int index = str.IndexOf('|');
if (index > 0)
{
    for (int i = 0; i < str.Length; i++)
    {
        Char c = str[i];
        if (c != '|')
        {
            sb.Append(c);
            continue;
        }
        Char nextChar = str[i + 1];
        if (operators.Contains(nextChar))
        { 
            // next char is operator, find index before last number
            int countLeft = 1;
            while (i - ++countLeft > 0 && Char.IsDigit(str[i - countLeft]))
                ;
            int countRight = 2; // skip operator
            while (i + ++countRight < str.Length && Char.IsDigit(str[i + countRight]))
                ;
            countLeft--;
            countRight--;
            int start = i - countLeft;
            string numLeft = str.Substring(start, countLeft);
            string numRight = str.Substring(i + 2, countRight - 1);
            sb.Remove(start, countLeft);
            string expr = string.Format("({0}{1}{2})", numLeft, nextChar, numRight);
            sb.Append(expr);
            i = i + countRight + 1;
        }
    }
}
else
    sb.Append(str);

可以粘贴用于计算和的代码吗?修改此代码以适应附加运算符可能会更容易。您可能需要创建一个相当大且复杂的字符串操作函数来处理所有可能的组合,即处理以下内容:
“3 |+5 |*4 |+5 |*4 |+3”=“(((((3+5)*4)+5)*4)+3”
应该是(3+5)(4/2+8)(32+7)?答案应该是什么?3120?这在我看来似乎是一个模棱两可的语法。例如,
1+2*3+4的预期输出是什么?
应该是
(1+2)*(3+4)
还是
(1+(2*3)+4)
?结果不应该是:
((((3)+5)*+4+8*32)
。否则,我很难理解逻辑优先级…非常感谢,这将非常有帮助。我知道
数据表.Compute的诀窍。
DataTable calculator = new DataTable();
object value = calculator.Compute(sb.ToString(), null);
double result = System.Convert.ToDouble(value); // 328.0