0
我已經在Stackoverflow上找到了不同的解決方案,但有些事情我沒有理解。 ((1+(4 *(2 + 3)))+((2 + 3)*(4 * 5)))的最佳計算方法是什麼?如何通過在C#中使用堆棧來計算Infix表達式的輸出
我的方法看起來如下,但我知道有很多錯誤的吧:
public static int ComputeInfix(string infix) {
Stack<char> operatorstack = new Stack<char>();
Stack<int> operandstack = new Stack<int>();
for(int j = 0; j < infix.Length; j++) {
char c = infix[j];
if (c => 0 && c <= 9) {
operandstack.Push(c);
}
else if ((c == '+' || c == '*' || c == '/' || c == '-')) {
if (operatorstack.IsEmpty()) {
operatorstack.Push(c);
}
else {
if (operatorstack.Peek() != '*' || operatorstack.Peek() != '/') {
operatorstack.Push(c);
}
}
}
else if (c == '(') {
operatorstack.Push(c);
}
else if (c == ')') {
operatorstack.Pop();
}
}
return infix;
}
現在改爲:
Stack<char> operatorstack = new Stack<char>();
Stack<char> operandstack = new Stack<char>();
for(int j = 0; j < infix.Length; j++) {
char c = infix[j];
if (c => '0' && c <= '9') {
operandstack.Push(c);
}
,但得到的錯誤:
Infix.cs(16,8): error CS0136: A local variable named
c' cannot be declared in this scope because it would give a different meaning to
c', which is already used in a `parent or current' scope to denote something else
[Calculator.net?](http://weblogs.asp.net/pwelter34/archive/2007/05/05/calculator-net-calculator-that-evaluates-math-expressions.aspx) – PoweredByOrange
現在改變了它會:'(c =>'0'&& c <='9')' – Patzi0207
'c => 0 && c <= 9'這行是錯誤的。 'c'變量是一個字符。如果你想要這個char的整數值,你應該解析它,就像這樣'int number = int.Parse(c.ToString());' –