Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Calculator
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.  
  13.         float prev_value;
  14.         Keys curr_operator = Keys.None;
  15.         bool wait_for_new = false;
  16.  
  17.         void Calc()
  18.         {
  19.             float curr_value = float.Parse(textBox1.Text);
  20.             float new_value = 0;
  21.             switch(curr_operator)
  22.             {
  23.                 case Keys.Add:
  24.                     new_value = prev_value + curr_value;
  25.                     textBox1.Text = new_value.ToString();
  26.                     prev_value = new_value;
  27.                     wait_for_new = true;
  28.                     break;
  29.                 case Keys.Subtract:
  30.                     new_value = prev_value - curr_value;
  31.                     textBox1.Text = new_value.ToString();
  32.                     prev_value = new_value;
  33.                     wait_for_new = true;
  34.                     break;
  35.                 case Keys.Multiply:
  36.                     new_value = prev_value * curr_value;
  37.                     textBox1.Text = new_value.ToString();
  38.                     prev_value = new_value;
  39.                     wait_for_new = true;
  40.                     break;
  41.                 case Keys.Divide:
  42.                     new_value = prev_value / curr_value;
  43.                     textBox1.Text = new_value.ToString();
  44.                     prev_value = new_value;
  45.                     wait_for_new = true;
  46.                     break;
  47.                 default: break;
  48.             }
  49.             curr_operator = Keys.None;
  50.         }
  51.  
  52.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  53.         {
  54.             switch(e.KeyCode)
  55.             {
  56.                 case Keys.Add:
  57.                     Calc();
  58.                     curr_operator = Keys.Add;
  59.                     wait_for_new = true;
  60.                     e.SuppressKeyPress = true;
  61.                     break;
  62.                 case Keys.Subtract:
  63.                     Calc();
  64.                     curr_operator = Keys.Subtract;
  65.                     wait_for_new = true;
  66.                     e.SuppressKeyPress = true;
  67.                     break;
  68.                 case Keys.Multiply:
  69.                     Calc();
  70.                     curr_operator = Keys.Multiply;
  71.                     wait_for_new = true;
  72.                     e.SuppressKeyPress = true;
  73.                     break;
  74.                 case Keys.Divide:
  75.                     Calc();
  76.                     curr_operator = Keys.Divide;
  77.                     wait_for_new = true;
  78.                     e.SuppressKeyPress = true;
  79.                     break;
  80.                 case Keys.Enter:
  81.                     Calc();
  82.                     wait_for_new = true;
  83.                     e.SuppressKeyPress = true;
  84.                     break;
  85.                 case Keys.Escape:
  86.                     e.SuppressKeyPress = true;
  87.                     wait_for_new = true;
  88.                     textBox1.Text = string.Empty;
  89.                     break;
  90.                 default:
  91.                     if(wait_for_new)
  92.                     {
  93.                         prev_value = (textBox1.Text.Length > 0) ? float.Parse(textBox1.Text) : 0;
  94.                         textBox1.Text = string.Empty;
  95.                         wait_for_new = false;
  96.                     }
  97.                     break;
  98.             };
  99.         }
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement