【发布时间】:2015-03-03 12:10:13
【问题描述】:
按钮代码: 我的按钮中的所有代码都是用数字的字符串表示填充文本框。
private void button9_Click(object sender, EventArgs e)
{
if ((screenBox.Text == "0") || (opr))
{
screenBox.Clear();
}
opr = false;
screenBox.Text = screenBox.Text + button9.Text;
}
运营商代码: 操作员代码做同样的事情。
private void buttonAdd_Click(object sender, EventArgs e)
{
operation = buttonAdd.Text;
screenBox.Text =screenBox.Text +" "+ operation +" ";
}
等于: 现在,当我按下等于时。
public void buttonEq_Click(object sender, EventArgs e)
{
operation = buttonEq.Text;
screenBox.Text = " " + screenBox.Text + " " + operation;
string splitUp; //stores elements of the original string
string expr = screenBox.Text; //makes expr whatever is in textbox
string[] ops;//makes array called ops
ops = expr.Split( '(', ')', '^', '/', '*', '+', '-', '=');
//splits expr which contains the string input to the screenbox
//using any operator as a token (unless i'm confused and you don't
//all them tokens with this method)
foreach(string op in ops)
{
splitUp = op;
Console.WriteLine(op);
}
//string split up holds whatever is in op, which is a string that
//contains an element of ops, which is the array that indexes the
//elements of the original string
//eg 5+5= in the text box would appear as in the console.
//5
//5
我现在应该如何解决这个问题,以便我的程序知道它在添加时已经拆分,所以它需要在 + 之前和之后添加两个数字(或 '*'、'/'、'-')但是在执行计算并收集该序列的总和之前,首先要检查运算符右侧的“(”“)”括号
我只想了解我应该采用哪种逻辑。
【问题讨论】:
-
最好使用表达式解析器而不是自己构建。 Search for C# Expression parser
-
我知道,但这是为大学工作,我认为我们不能包含图书馆。一直在寻找要实现什么样的逻辑,但还没有真正走多远……仍然,早期。
标签: c# visual-studio calculator