【发布时间】:2014-02-03 10:32:58
【问题描述】:
下一个例子:
用户输入示例:“1 + 3 - 2 * 4”
string input = Console.ReadLine();
string[] getElement = input.split(' ');
//So I can have
//getElement[0] = 1
//getElement[1] = +
//getElement[2] = 3
//...
//I have a "for()" cicle here and at one moment I have the following instruction:
if(getElement[i] == "+")
int add = Convert.ToInt32(getElement[i - 1]) + Convert.ToInt32(getElement[i + 1]);
//In this example it means: add = 1 + 3
我的问题是如何删除位置 [0],[1],[2] 的 string[] getElement 并将它们替换为“添加”值?
在这种情况下,我不知道如何使用Replace()。我需要调整数组大小吗?需要建议。
【问题讨论】:
-
在这种情况下你应该使用堆栈
-
也许使用
List<string>而不是string[]? -
您能否说清楚,您是想用文本替换 OP 还是想评估结果。
-
如果你想评估表达式,你可以参考:stackoverflow.com/questions/5838918/…
-
我想替换一些计算的结果。因为我总是有 2 个数字和 1 个操作数,所以我想替换这 3 个位置作为计算结果。