Hi all,
I am developing calculator app using C#.I have successfuly implemented its standard arithmetic functions but a bit trapped in implementing scientific functions such as sin,cos.tan etc.
I take an operand1,then operator and finally second opernad to workout arithmetic calculations.In the very first attempt to do scientific calculation i for the sake of programme format first takes input followed by sin/cos/tan etc and then out result.
Here is my code:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Calculator{ public partial class Form1 : Form { string input = string.Empty; string operand1 = string.Empty; string operand2 = string.Empty; char operaton; double result = 0.0; public Form1() { InitializeComponent(); } private void one_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "1"; this.textBox1.Text += input; } private void two_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "2"; this.textBox1.Text += input; } private void three_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "3"; this.textBox1.Text += input; } private void four_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "4"; this.textBox1.Text += input; } private void five_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "5"; this.textBox1.Text += input; } private void six_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "6"; this.textBox1.Text += input; } private void seven_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "7"; this.textBox1.Text += input; } private void eight_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "8"; this.textBox1.Text += input; } private void nine_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "9"; this.textBox1.Text += input; } private void dot_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "."; this.textBox1.Text += input; } private void zero_Click(object sender, EventArgs e) { this.textBox1.Text = " "; input += "0"; this.textBox1.Text += input; } private void clear_Click(object sender, EventArgs e) { this.textBox1.Text = "0"; this.input = string.Empty; this.operand1 = string.Empty; this.operand2 = string.Empty; } private void plus_Click(object sender, EventArgs e) { operand1 = input; operaton = '+'; input = string.Empty; } private void minus_Click(object sender, EventArgs e) { operand1 = input; operaton = '-'; input = string.Empty; } private void mul_Click(object sender, EventArgs e) { operand1 = input; operaton = '*'; input = string.Empty; } private void div_Click(object sender, EventArgs e) { operand1 = input; operaton = '/'; input = string.Empty; } private void mod_Click(object sender, EventArgs e) { operand1 = input; operaton = '%'; input = string.Empty; } private void sine_Click(object sender, EventArgs e) { operand1 = input; operaton = 's'; input = string.Empty; } private void cosine_Click(object sender, EventArgs e) { operand1 = input; operaton = 'c'; input = string.Empty; } private void tan_Click(object sender, EventArgs e) { operand1 = input; operaton = 't'; input = string.Empty; } private void equals_Click(object sender, EventArgs e) { operand2 = input; double num1, num2; double.TryParse(operand1, out num1); double.TryParse(operand2, out num2); if (operaton == '+') { result = num1 + num2; textBox1.Text = result.ToString(); } else if (operaton == '-') { result = num1 - num2; textBox1.Text = result.ToString(); } else if (operaton == '*') { result = num1 * num2; textBox1.Text = result.ToString(); } else if (operaton == '/') { if (num2 != 0) { result = num1 / num2; textBox1.Text = result.ToString(); } else { textBox1.Text = "DIV/Zero!"; } } else if (operaton == '%') { result = num1 % num2; textBox1.Text = result.ToString(); } else if (operaton == 's') { int inp; int.TryParse(operand1, out inp); result = Math.Sin(inp); textBox1.Text = result.ToString(); } else if (operaton == 'c') { int inp; int.TryParse(operand1, out inp); result = Math.Cos(num1); textBox1.Text = result.ToString(); } else if (operaton == 't') { int inp; int.TryParse(operand1, out inp); result = Math.Tan(num1); textBox1.Text = result.ToString(); } } } }
I want to mention here that my output for scientific functions is not correct.Also,kindly tell me error in this format as in second attempt i will convert it to standard scientific mode i.e. pressing sin/cos/tan and then operand.