Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.41 KB | None | 0 0
  1. double fst;
  2.         double snd;
  3.         double exp;
  4.         Char action;
  5.         Calc solver;
  6.         bool pnt;
  7.         bool neg;
  8.         public CalcForm()
  9.         {
  10.             InitializeComponent();
  11.             pnt = false;
  12.             neg = false;
  13.             action = '@';
  14.             solver = new Calc();
  15.         }
  16.  
  17.         private void button_0_Click(object sender, EventArgs e)
  18.         {
  19.             exprEdit.Text = exprEdit.Text + "0";
  20.         }
  21.  
  22.         private void button_1_Click(object sender, EventArgs e)
  23.         {
  24.             exprEdit.Text = exprEdit.Text + "1";
  25.         }
  26.  
  27.         private void button_point_Click(object sender, EventArgs e)
  28.         {
  29.             if (!pnt)
  30.             {
  31.                 exprEdit.Text = exprEdit.Text + ",";
  32.                 pnt = true;
  33.             }
  34.             else MessageBox.Show("Point is already placed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  35.         }
  36.  
  37.         private void button_2_Click(object sender, EventArgs e)
  38.         {
  39.             exprEdit.Text = exprEdit.Text + "2";
  40.         }
  41.  
  42.         private void button_3_Click(object sender, EventArgs e)
  43.         {
  44.             exprEdit.Text = exprEdit.Text + "3";
  45.         }
  46.  
  47.         private void button_4_Click(object sender, EventArgs e)
  48.         {
  49.             exprEdit.Text = exprEdit.Text + "4";
  50.         }
  51.  
  52.         private void button_5_Click(object sender, EventArgs e)
  53.         {
  54.             exprEdit.Text = exprEdit.Text + "5";
  55.         }
  56.  
  57.         private void button_6_Click(object sender, EventArgs e)
  58.         {
  59.             exprEdit.Text = exprEdit.Text + "6";
  60.         }
  61.  
  62.         private void button_7_Click(object sender, EventArgs e)
  63.         {
  64.             exprEdit.Text = exprEdit.Text + "7";
  65.         }
  66.  
  67.         private void button_8_Click(object sender, EventArgs e)
  68.         {
  69.             exprEdit.Text = exprEdit.Text + "8";
  70.         }
  71.  
  72.         private void button_9_Click(object sender, EventArgs e)
  73.         {
  74.             exprEdit.Text = exprEdit.Text + "9";
  75.         }
  76.  
  77.         private void button_summ_Click(object sender, EventArgs e)
  78.         {
  79.             if(!action.Equals('@'))
  80.             {
  81.                 String[] tokens = exprEdit.Text.Split(action);
  82.                 if(!double.TryParse(tokens[tokens.Length - 1], out snd))
  83.                 {
  84.                     MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  85.                     return;
  86.                 }
  87.                 if (neg) snd *= -1;
  88.                 fst = compute();
  89.                 exprEdit.Text = System.Convert.ToString(fst);
  90.             }
  91.  
  92.             if(exprEdit.Text.Equals(""))
  93.                 MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  94.             else
  95.             {
  96.                 double.TryParse(exprEdit.Text, out fst);
  97.                 if (neg) fst *= -1;
  98.                 exprEdit.Text = exprEdit.Text + " + ";
  99.                 action = '+';
  100.                 pnt = false;
  101.                 neg = false;
  102.             }
  103.         }
  104.  
  105.         private void button_sub_Click(object sender, EventArgs e)
  106.         {
  107.             if (!action.Equals('@'))
  108.             {
  109.                 String[] tokens = exprEdit.Text.Split(action);
  110.                 if (!double.TryParse(tokens[tokens.Length - 1], out snd))
  111.                 {
  112.                     MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  113.                     return;
  114.                 }
  115.                 if (neg) snd *= -1;
  116.                 fst = compute();
  117.                 exprEdit.Text = System.Convert.ToString(fst);
  118.             }
  119.  
  120.             if (exprEdit.Text.Equals(""))
  121.                 MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  122.             else
  123.             {
  124.                 double.TryParse(exprEdit.Text, out fst);
  125.                 exprEdit.Text = exprEdit.Text + " - ";
  126.                 action = '-';
  127.                 pnt = false;
  128.                 neg = false;
  129.             }
  130.         }
  131.  
  132.         private void button_mult_Click(object sender, EventArgs e)
  133.         {
  134.             if (!action.Equals('@'))
  135.             {
  136.                 String[] tokens = exprEdit.Text.Split(action);
  137.                 if (!double.TryParse(tokens[tokens.Length - 1], out snd))
  138.                 {
  139.                     MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  140.                     return;
  141.                 }
  142.                 if (neg) snd *= -1;
  143.                 fst = compute();
  144.                 exprEdit.Text = System.Convert.ToString(fst);
  145.             }
  146.  
  147.             if (exprEdit.Text.Equals(""))
  148.                 MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  149.             else
  150.             {
  151.                 double.TryParse(exprEdit.Text, out fst);
  152.                 exprEdit.Text = exprEdit.Text + " * ";
  153.                 action = '*';
  154.                 pnt = false;
  155.                 neg = false;
  156.             }
  157.         }
  158.  
  159.         private void button_div_Click(object sender, EventArgs e)
  160.         {
  161.             if (!action.Equals('@'))
  162.             {
  163.                 String[] tokens = exprEdit.Text.Split(action);
  164.                 if (!double.TryParse(tokens[tokens.Length - 1], out snd))
  165.                 {
  166.                     MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  167.                     return;
  168.                 }
  169.                 if (neg) snd *= -1;
  170.                 fst = compute();
  171.                 exprEdit.Text = System.Convert.ToString(fst);
  172.             }
  173.  
  174.             if (exprEdit.Text.Equals(""))
  175.                 MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  176.             else
  177.             {
  178.                 double.TryParse(exprEdit.Text, out fst);
  179.                 exprEdit.Text = exprEdit.Text + " / ";
  180.                 action = '/';
  181.                 pnt = false;
  182.                 neg = false;
  183.             }
  184.         }
  185.  
  186.         private void button_compute_Click(object sender, EventArgs e)
  187.         {
  188.             if(action.Equals('@'))
  189.             {
  190.                 MessageBox.Show("No operations to compute", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  191.                 return;
  192.             }
  193.  
  194.             String[] tokens = exprEdit.Text.Split(action);
  195.             double.TryParse(tokens[tokens.Length-1], out snd);
  196.             fst = compute();
  197.             exprEdit.Text = System.Convert.ToString(fst);
  198.             neg = false;
  199.             pnt = false;
  200.             action = '@';
  201.         }
  202.  
  203.         double compute()
  204.         {
  205.             switch(action)
  206.             {
  207.                 case '+': return solver.Summ(fst, snd);
  208.                 case '-': return solver.Substract(fst, snd);
  209.                 case '/': return solver.divide(fst, snd);
  210.                 case '*': return solver.Multiple(fst, snd);
  211.                 case '^': return solver.pow(fst, snd);
  212.                 default: return double.NaN;
  213.             }
  214.         }
  215.  
  216.         private void button_negate_Click(object sender, EventArgs e)
  217.         {
  218.             if (!neg)
  219.             {
  220.                 if(action.Equals('@') && !exprEdit.Text.Equals(""))
  221.                 {
  222.                     MessageBox.Show("Can't negate half of number", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  223.                     return;
  224.                 }
  225.  
  226.                 String[] tokens = exprEdit.Text.Split(action);
  227.                 if (double.TryParse(tokens[tokens.Length - 1], out snd))
  228.                 {
  229.                     MessageBox.Show("Can't negate half of number", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  230.                     return;
  231.                 }
  232.  
  233.                 exprEdit.Text = exprEdit.Text + "-";
  234.                 neg = true;
  235.             }
  236.             else
  237.             {
  238.                 MessageBox.Show("Number is already negated", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  239.                 return;
  240.             }
  241.         }
  242.  
  243.         private void button_pow_Click(object sender, EventArgs e)
  244.         {
  245.             if (!action.Equals('@'))
  246.             {
  247.                 String[] tokens = exprEdit.Text.Split(action);
  248.                 if (!double.TryParse(tokens[tokens.Length - 1], out snd))
  249.                 {
  250.                     MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  251.                     return;
  252.                 }
  253.                 if (neg) snd *= -1;
  254.                 fst = compute();
  255.                 exprEdit.Text = System.Convert.ToString(fst);
  256.             }
  257.  
  258.             if (exprEdit.Text.Equals(""))
  259.                 MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  260.             else
  261.             {
  262.                 double.TryParse(exprEdit.Text, out fst);
  263.                 exprEdit.Text = exprEdit.Text + "^";
  264.                 action = '^';
  265.                 pnt = false;
  266.                 neg = false;
  267.             }
  268.         }
  269.  
  270.         private void button_clear_Click(object sender, EventArgs e)
  271.         {
  272.             pnt = false;
  273.             neg = false;
  274.             action = '@';
  275.             exprEdit.Text = "";
  276.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement