System function description
Windows calculator is a simple calculator program written by C# program design. It can carry out simple addition, subtraction, multiplication and division operations. It can also realize the functions of advanced operations such as trigonometric function, logarithm, exponent and so on.
The functional structure diagram of the whole system is shown in the figure:
According to the function structure diagram, users can add calculator forms in form applications and establish the framework of windows applications. Therefore, two form controls Form1 and Form2 are added to represent scientific calculator and ordinary calculator respectively. Then the user adds a Button control to establish the main interface of the program. Finally, in the code segment of each Button, add the event triggered processing code respectively. Control textBox1, which displays the input information and the processed results.
System function analysis and Implementation
Control represents a graphical connection between a user and a program. Control can provide or process data, accept user input, respond to events, or perform other functions that connect users and applications. There are many controls in the form. The windows form in the toolbox contains all windows standard controls. You can change the appearance and properties of a control by changing its properties in the properties window. The following controls are required for this project:
(1) Button control: in the Form1 form, add 30 button controls, including 10 numeric buttons, 16 operation buttons, an equal sign button for calculation and 2 buttons for emptying. In Form2 form, add 17 button controls, including 10 number buttons, 4 operation buttons, an equal sign button for calculation and a button for emptying.
(2) Text box control (TextBox): add a TextBox control to Form1 and Form2 respectively to display the calculation results.
(3) Menu bar control (MenuStrip): add MenuStrip controls in Form1 and Form2 respectively, and modify the Text property of the ToolStripMenuItem menu item in the control to model conversion. This control can realize the mutual conversion between two forms.
(1) Code in the main form Form1.cs file
using System; using System.Windows.Forms; namespace Windows Calculator { public partial class Form1 : Form { private string Op; private double Op1; private double Op2; private double result; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } //Number key 1 private void BtNumber1_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "1"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 2 private void BtNumber2_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "2"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 3 private void BtNumber3_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "3"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 4 private void BtNumber4_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "4"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 5 private void BtNumber5_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "5"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 6 private void BtNumber6_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "6"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 7 private void BtNumber7_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "7"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 8 private void BtNumber8_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "8"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 9 private void BtNumber9_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "9"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Number key 0 private void BtNumber0_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "0"; Op1 = System.Convert.ToDouble(textBox1.Text); } //Add function private void BtAdd_Click(object sender, EventArgs e) { Op = "+"; Op2 = Op1; textBox1.Text = ""; } //Subtractive function private void BtSubtract_Click(object sender, EventArgs e) { Op = "-"; Op2 = Op1; textBox1.Text = ""; } //Multiplication function private void BtMult_Click(object sender, EventArgs e) { Op = "*"; Op2 = Op1; textBox1.Text = ""; } //Except function private void BtDivision_Click(object sender, EventArgs e) { Op = "/"; Op2 = Op1; textBox1.Text = ""; } //% private void BtRemedial_Click(object sender, EventArgs e) { Op = "%"; Op2 = Op1; textBox1.Text = ""; } //Del function private void BtDel_Click(object sender, EventArgs e) { if (textBox1.Text.Length >= 1) { textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); } if (textBox1.Text.Length == 0) { textBox1.Text = ""; } } //Decimal point function private void BtComma_Click(object sender, EventArgs e) { if (textBox1.Text.IndexOf(".") < 0) textBox1.Text += ((Button)sender).Text; else { } } //Equal function private void BtDebgyu_Click(object sender, EventArgs e) { switch (Op) { case "+": result = Op2 + Op1; break; case "-": result = Op2 - Op1; break; case "*": result = Op2 * Op1; break; case "/": if (Op2 == 0) { Console.WriteLine("Error, divisor cannot be 0!"); } else { result = Op2 / Op1; } break; case "%": result = Op2 % Op1; break; case "^": int m; double n; m = 1; n = Op2; while (m + 1 <= Op1) { n = n * Op2; m++; } result = n; break; } textBox1.Text = System.Convert.ToString(result); } //sin function private void Btsin_Click(object sender, EventArgs e) { Convert.ToDouble(Op1); textBox1.Text = System.Convert.ToString(Math.Sin(Op1 * Math.PI / 180)); } //con function private void Btcos_Click(object sender, EventArgs e) { Convert.ToDouble(Op1); textBox1.Text = System.Convert.ToString(Math.Cos(Op1 * Math.PI / 180)); } //tan function private void Bttan_Click(object sender, EventArgs e) { Convert.ToDouble(Op1); textBox1.Text = System.Convert.ToString(Math.Tan(Op1 * Math.PI / 180)); } //Factorial function private void BtFactorial_Click(object sender, EventArgs e) { int i, j; i = 1; j = 1; while (i <= Op1) { j *= i; i++; } textBox1.Text = System.Convert.ToString(j); } //Square function private void BtSquare_Click(object sender, EventArgs e) { textBox1.Text = System.Convert.ToString(Op1 * Op1); } //Cubic function private void BtCube_Click(object sender, EventArgs e) { textBox1.Text = System.Convert.ToString(Op1 * Op1 * Op1); } //π private void BtPI_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + Math.PI; Op1 = System.Convert.ToDouble(textBox1.Text); } //Screen clearing function private void BtClear_Click(object sender, EventArgs e) { textBox1.Text = ""; Op = ""; Op1 = 0; Op2 = 0; result = 0; } //exponentiation private void BtPower_Click(object sender, EventArgs e) { Op = "^"; Op2 = Op1; textBox1.Text = ""; } //10^x private void BtIndex_Click(object sender, EventArgs e) { int a, b; a = 1; b = 1; while (a <= Op1) { b *= 10; a++; } textBox1.Text = System.Convert.ToString(b); } //Log private void BtLog_Click(object sender, EventArgs e) { textBox1.Text = System.Convert.ToString(Math.Log10(Op1)); } //ln private void BtLn_Click(object sender, EventArgs e) { textBox1.Text = System.Convert.ToString(Math.Log(Op1)); } private void Model conversion ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 stu = new Form2(); this.Hide(); stu.ShowDialog(); this.Close(); } } }
(2) Code in the main form Form2.cs file
using System; using System.Windows.Forms; namespace Windows Calculator { public partial class Form2 : Form { private string Op; private double Op1; private double Op2; private double result; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { } private void BtNumber1_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "1"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber2_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "2"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber3_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "3"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber4_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "4"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber5_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "5"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber6_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "6"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber7_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "7"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber8_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "8"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber9_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "9"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtNumber0_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "0"; Op1 = System.Convert.ToDouble(textBox1.Text); } private void BtComma_Click(object sender, EventArgs e) { if (textBox1.Text.IndexOf(".") < 0) textBox1.Text += ((Button)sender).Text; else { } } private void BtDebgyu_Click(object sender, EventArgs e) { switch (Op) { case "+": result = Op2 + Op1; break; case "-": result = Op2 - Op1; break; case "*": result = Op2 * Op1; break; case "/": result = Op2 / Op1; break; } textBox1.Text = System.Convert.ToString(result); } private void BtAdd_Click(object sender, EventArgs e) { Op = "+"; Op2 = Op1; textBox1.Text = ""; } private void BtSubtract_Click(object sender, EventArgs e) { Op = "-"; Op2 = Op1; textBox1.Text = ""; } private void BtMult_Click(object sender, EventArgs e) { Op = "*"; Op2 = Op1; textBox1.Text = ""; } private void BtDivision_Click(object sender, EventArgs e) { Op = "/"; Op2 = Op1; textBox1.Text = ""; } private void BtClear_Click(object sender, EventArgs e) { textBox1.Text = ""; Op = ""; Op1 = 0; Op2 = 0; result = 0; } private void Model conversion ToolStripMenuItem_Click(object sender, EventArgs e) { Form1 stu = new Form1(); this.Hide(); stu.ShowDialog(); this.Close(); } } }
Program.cs
using System; using System.Windows.Forms; namespace Windows Calculator { static class Program { /// <summary> ///The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }