Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double fst;
- double snd;
- double exp;
- Char action;
- Calc solver;
- bool pnt;
- bool neg;
- public CalcForm()
- {
- InitializeComponent();
- pnt = false;
- neg = false;
- action = '@';
- solver = new Calc();
- }
- private void button_0_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "0";
- }
- private void button_1_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "1";
- }
- private void button_point_Click(object sender, EventArgs e)
- {
- if (!pnt)
- {
- exprEdit.Text = exprEdit.Text + ",";
- pnt = true;
- }
- else MessageBox.Show("Point is already placed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- private void button_2_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "2";
- }
- private void button_3_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "3";
- }
- private void button_4_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "4";
- }
- private void button_5_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "5";
- }
- private void button_6_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "6";
- }
- private void button_7_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "7";
- }
- private void button_8_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "8";
- }
- private void button_9_Click(object sender, EventArgs e)
- {
- exprEdit.Text = exprEdit.Text + "9";
- }
- private void button_summ_Click(object sender, EventArgs e)
- {
- if(!action.Equals('@'))
- {
- String[] tokens = exprEdit.Text.Split(action);
- if(!double.TryParse(tokens[tokens.Length - 1], out snd))
- {
- MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (neg) snd *= -1;
- fst = compute();
- exprEdit.Text = System.Convert.ToString(fst);
- }
- if(exprEdit.Text.Equals(""))
- MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- else
- {
- double.TryParse(exprEdit.Text, out fst);
- if (neg) fst *= -1;
- exprEdit.Text = exprEdit.Text + " + ";
- action = '+';
- pnt = false;
- neg = false;
- }
- }
- private void button_sub_Click(object sender, EventArgs e)
- {
- if (!action.Equals('@'))
- {
- String[] tokens = exprEdit.Text.Split(action);
- if (!double.TryParse(tokens[tokens.Length - 1], out snd))
- {
- MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (neg) snd *= -1;
- fst = compute();
- exprEdit.Text = System.Convert.ToString(fst);
- }
- if (exprEdit.Text.Equals(""))
- MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- else
- {
- double.TryParse(exprEdit.Text, out fst);
- exprEdit.Text = exprEdit.Text + " - ";
- action = '-';
- pnt = false;
- neg = false;
- }
- }
- private void button_mult_Click(object sender, EventArgs e)
- {
- if (!action.Equals('@'))
- {
- String[] tokens = exprEdit.Text.Split(action);
- if (!double.TryParse(tokens[tokens.Length - 1], out snd))
- {
- MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (neg) snd *= -1;
- fst = compute();
- exprEdit.Text = System.Convert.ToString(fst);
- }
- if (exprEdit.Text.Equals(""))
- MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- else
- {
- double.TryParse(exprEdit.Text, out fst);
- exprEdit.Text = exprEdit.Text + " * ";
- action = '*';
- pnt = false;
- neg = false;
- }
- }
- private void button_div_Click(object sender, EventArgs e)
- {
- if (!action.Equals('@'))
- {
- String[] tokens = exprEdit.Text.Split(action);
- if (!double.TryParse(tokens[tokens.Length - 1], out snd))
- {
- MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (neg) snd *= -1;
- fst = compute();
- exprEdit.Text = System.Convert.ToString(fst);
- }
- if (exprEdit.Text.Equals(""))
- MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- else
- {
- double.TryParse(exprEdit.Text, out fst);
- exprEdit.Text = exprEdit.Text + " / ";
- action = '/';
- pnt = false;
- neg = false;
- }
- }
- private void button_compute_Click(object sender, EventArgs e)
- {
- if(action.Equals('@'))
- {
- MessageBox.Show("No operations to compute", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- String[] tokens = exprEdit.Text.Split(action);
- double.TryParse(tokens[tokens.Length-1], out snd);
- fst = compute();
- exprEdit.Text = System.Convert.ToString(fst);
- neg = false;
- pnt = false;
- action = '@';
- }
- double compute()
- {
- switch(action)
- {
- case '+': return solver.Summ(fst, snd);
- case '-': return solver.Substract(fst, snd);
- case '/': return solver.divide(fst, snd);
- case '*': return solver.Multiple(fst, snd);
- case '^': return solver.pow(fst, snd);
- default: return double.NaN;
- }
- }
- private void button_negate_Click(object sender, EventArgs e)
- {
- if (!neg)
- {
- if(action.Equals('@') && !exprEdit.Text.Equals(""))
- {
- MessageBox.Show("Can't negate half of number", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- String[] tokens = exprEdit.Text.Split(action);
- if (double.TryParse(tokens[tokens.Length - 1], out snd))
- {
- MessageBox.Show("Can't negate half of number", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- exprEdit.Text = exprEdit.Text + "-";
- neg = true;
- }
- else
- {
- MessageBox.Show("Number is already negated", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- }
- private void button_pow_Click(object sender, EventArgs e)
- {
- if (!action.Equals('@'))
- {
- String[] tokens = exprEdit.Text.Split(action);
- if (!double.TryParse(tokens[tokens.Length - 1], out snd))
- {
- MessageBox.Show("Second number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (neg) snd *= -1;
- fst = compute();
- exprEdit.Text = System.Convert.ToString(fst);
- }
- if (exprEdit.Text.Equals(""))
- MessageBox.Show("First number is not inputed", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- else
- {
- double.TryParse(exprEdit.Text, out fst);
- exprEdit.Text = exprEdit.Text + "^";
- action = '^';
- pnt = false;
- neg = false;
- }
- }
- private void button_clear_Click(object sender, EventArgs e)
- {
- pnt = false;
- neg = false;
- action = '@';
- exprEdit.Text = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement