Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do trivial algorithm for GUI calculator on C#?
    primarykey
    data
    text
    <p>For example I doing calculator for WP7. </p> <p><img src="https://i.stack.imgur.com/4qyN0.png" alt="enter image description here"></p> <p>This is my simple logic class:</p> <pre><code>public class CalcLogic { public static double Calculate(double a, double b, string operation) { double res = 0; switch (operation) { case ("+"): res = a + b; break; case ("-"): res = a - b; break; case ("*"): res = a * b; break; case ("/"): res = a / b; break; default: break; } return res; } } </code></pre> <p>And this is in my MainPage.xaml.cs:</p> <pre><code>private const int Rows = 4, Cols = 4; private string[,] buttonTags = { { "7", "8", "9", "/" }, { "4", "5", "6", "*" }, { "1", "2", "3", "-" }, { "0", "C", "=", "+" } }; private Button[,] buttons; // Constructor public MainPage() { InitializeComponent(); CreateButtons(); } public void CreateButtons() { buttons = new Button[Rows, Cols]; for (int i = 0; i &lt; Rows; i++) { for (int j = 0; j &lt; Cols; j++) { buttons[i, j] = new Button(); buttons[i, j].Content = buttonTags[j, i]; buttons[i, j].Width = 120; buttons[i, j].Height = 110; buttons[i, j].Margin = new Thickness(i * 110 + 5, j * 110 + 100, 0, 0); buttons[i, j].VerticalAlignment = System.Windows.VerticalAlignment.Top; buttons[i, j].HorizontalAlignment = System.Windows.HorizontalAlignment.Left; buttons[i, j].Name = "button" + buttonTags[j, i]; buttons[i, j].Click += new System.Windows.RoutedEventHandler(Button_Click); ContentPanel.Children.Add(buttons[i, j]); ContentPanel.UpdateLayout(); } } } </code></pre> <p>And question is - how correct to do Button_Click method?</p> <pre><code>private void Button_Click(object sender, EventArgs e) { Button button = sender as Button; tbResult.Text += button.Content.ToString(); string operation = ""; double a = 0; double b = 0; if (button.Content.ToString() == "=") { tbResult.Text = CalcLogic.Calculate(a, b, operation).ToString(); } if (button.Content.ToString() == "C") { tbResult.Text = ""; } ... } </code></pre> <p>Maybe I should just write to my TextBox digits and sign and after that parse? But I think it is not correct algorithm.</p> <p><strong>UPD:</strong></p> <p>That's what I did, it's working good. But I not sure about correct realization:</p> <pre><code>private void Button_Click(object sender, EventArgs e) { try { Button button = sender as Button; double res; if (Double.TryParse(button.Content.ToString(), out res)) { DigitClick(button.Content.ToString()); } else { OperatorClick(button.Content.ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message); ClearAll(); } } private void ClearAll() { first = second = result = 0; operStr = ""; newNum = true; tbResult.Text = ""; } private void DigitClick(string dig) { if (newNum) { tbResult.Text = ""; } tbResult.Text += dig; newNum = false; } private void OperatorClick(string oper) { if (oper == "=") { if (operStr == "=" || operStr == "") { return; } second = Convert.ToInt32(tbResult.Text); result = CalcLogic.Calculate(first, second, operStr); tbResult.Text = result.ToString(); operStr = oper; } else if (oper == "C") { ClearAll(); } else { operStr = oper; first = Convert.ToInt32(tbResult.Text); } newNum = true; } </code></pre> <p>I just want to know best realization for GUI calculator, thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload