2013-10-31 50 views
1

所以,这里是主要想法,我试图做一个计算器。节约计算的操作数

private void _1_button_Click(object sender, RoutedEventArgs e) 
    { 
     resultBoxText += "1"; 
     resultBox.Text = resultBoxText; 
    } 
    private void _2_button_Click(object sender, RoutedEventArgs e) 
    { 
     resultBoxText += "2"; 
     resultBox.Text = resultBoxText; 
    } 
    private void _3_button_Click(object sender, RoutedEventArgs e) 
    { 
     resultBoxText += "3"; 
     resultBox.Text = resultBoxText; 
    } 
    private void plus_button_Click(object sender, RoutedEventArgs e) 
    { 
     resultBoxText += "+"; 
     resultBox.Text = resultBoxText; 
    } 

    private void minus_button_Click(object sender, RoutedEventArgs e) 
    { 
     resultBoxText += "-"; 
     resultBox.Text = resultBoxText; 
    } 

上面的代码是供用户输入数字或操作数时文本框中的文本发生变化的。 现在,我如何进行实际计算?在一个单独的函数(带参数)? 是一个变量足够所有的操作数,或者我应该做一个数组? 如何将它们添加到一起,作为输入? 例如:我按“2”,然后“+”,然后“3”,我如何将它们收集在结果中? 我真的不知道如何开始:/

+0

哦,伙计。你在这里打开一罐蠕虫。提示:限制自己的一小部分有效输入开始(一个小文法)。 – AndyG

+0

你的意思是,我使这个代码更短? – Athomield

+0

我的意思是你应该首先限制你的计算器的一小部分操作。 a + b,a-b,a * b,a/b也许。之后,您可以担心使用圆括号和操作顺序解析更复杂的表达式。 – AndyG

回答

0

如果你打算只添加/ 1,2,3,4。减去然后一个变量应该是足够的。只需在您的事件处理程序中进行所有必要的计算。

+0

好吧,我不仅为那些,这些只是首发。 – Athomield

0

我最近尝试过这个完全一样的东西。我提出了以下令人惊讶的简单解决方案。首先,添加对COM库的引用MSScriptControlhow to add and remove references in VS)。然后,所有的数字按钮点击事件之后:

private void _1_button_Click(object sender, RoutedEventArgs e) 
{ 
    resultBoxText += "1"; 
    resultBox.Text = resultBoxText; 
} 
private void _2_button_Click(object sender, RoutedEventArgs e) 
{ 
    resultBoxText += "2"; 
    resultBox.Text = resultBoxText; 
} 
private void _3_button_Click(object sender, RoutedEventArgs e) 
{ 
    resultBoxText += "3"; 
    resultBox.Text = resultBoxText; 
} 
private void plus_button_Click(object sender, RoutedEventArgs e) 
{ 
    resultBoxText += "+"; 
    resultBox.Text = resultBoxText; 
} 

private void minus_button_Click(object sender, RoutedEventArgs e) 
{ 
    resultBoxText += "-"; 
    resultBox.Text = resultBoxText; 
} 

把一些代码为你等于按钮:

private void equals_button_Click(object sender, RoutedEventArgs e) 
{ 
    MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl(); 
    sc.Language = "VBScript"; 
    //Using try catch statement just in case a user enters an invalid expression. 
    //This becomes more useful when the application gets more advanced. 
    try 
    { 
     object result = sc.Eval(resultBoxText); 
     //Output the result somehow. E.g.: resultBox.Text = result.ToString(); 
    } 
    catch 
    { 
     MessageBox.Show("Invalid expression!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 

只是这个代码量小,可以采取一个字符串,如“7 * 9 - 6 + 3/6“,并返回”57.5“的正确答案。 但是,我必须提到的一件事是,如何不能正确处理像“1(1)”这样的字符串。 从技术上讲,它应该将其视为“1 * 1”,但它只会引发错误。所以,我不得不做一些编码来解决这个问题。但我只是让你船到桥头当你来到它... :)

有一两件事我想补充:如果你的数字键都包含的“1”或“2”或“简单的文字+ “,那么你可以通过一个较大的来压缩你的按钮点击量。

private void num_button_click(object sender, RoutedEventArgs e) 
{ 
    Button b = (Button)sender; 
    resultBoxText += b.Text; 
    resultBox.Text = resultBoxText; 
} 

那么对于所有您的数字按键,它们指的是num_button_click代码时,他们被点击。这也适用于所有的操作员按钮(“+”)。

如果在另一方面,你的按钮包括类似“点击一个”文本,那么就坚持你原来的代码;不理我。

我只是意识到,你的所有点击事件都RoutedEventArgs。你在使用WPF吗?我只是假设它是winforms。那么,无论如何,代码应该仍然有效......我希望这就是你正在寻找的。