Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- namespace Calculator
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- float prev_value;
- Keys curr_operator = Keys.None;
- bool wait_for_new = false;
- void Calc()
- {
- float curr_value = float.Parse(textBox1.Text);
- float new_value = 0;
- switch(curr_operator)
- {
- case Keys.Add:
- new_value = prev_value + curr_value;
- textBox1.Text = new_value.ToString();
- prev_value = new_value;
- wait_for_new = true;
- break;
- case Keys.Subtract:
- new_value = prev_value - curr_value;
- textBox1.Text = new_value.ToString();
- prev_value = new_value;
- wait_for_new = true;
- break;
- case Keys.Multiply:
- new_value = prev_value * curr_value;
- textBox1.Text = new_value.ToString();
- prev_value = new_value;
- wait_for_new = true;
- break;
- case Keys.Divide:
- new_value = prev_value / curr_value;
- textBox1.Text = new_value.ToString();
- prev_value = new_value;
- wait_for_new = true;
- break;
- default: break;
- }
- curr_operator = Keys.None;
- }
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- switch(e.KeyCode)
- {
- case Keys.Add:
- Calc();
- curr_operator = Keys.Add;
- wait_for_new = true;
- e.SuppressKeyPress = true;
- break;
- case Keys.Subtract:
- Calc();
- curr_operator = Keys.Subtract;
- wait_for_new = true;
- e.SuppressKeyPress = true;
- break;
- case Keys.Multiply:
- Calc();
- curr_operator = Keys.Multiply;
- wait_for_new = true;
- e.SuppressKeyPress = true;
- break;
- case Keys.Divide:
- Calc();
- curr_operator = Keys.Divide;
- wait_for_new = true;
- e.SuppressKeyPress = true;
- break;
- case Keys.Enter:
- Calc();
- wait_for_new = true;
- e.SuppressKeyPress = true;
- break;
- case Keys.Escape:
- e.SuppressKeyPress = true;
- wait_for_new = true;
- textBox1.Text = string.Empty;
- break;
- default:
- if(wait_for_new)
- {
- prev_value = (textBox1.Text.Length > 0) ? float.Parse(textBox1.Text) : 0;
- textBox1.Text = string.Empty;
- wait_for_new = false;
- }
- break;
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement