catalogue
1.Project introduction
1.1Project introduction
In this assignment, I implemented a simple calculator using c++.
The Table about personal information:
The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | Simple Web Calculator |
MU STU ID and FZU STU ID | 21125261_832102218 |
1.2psp Table
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | 60 | 35 |
• Estimate | ||
Development | 10 | 10 |
• Analysis | ||
• Design Spec | 10 | 15 |
• Design Review | 5 | 10 |
• Coding Standard | 20 | 20 |
• Design | 5 | 2 |
• Coding | 60 | 60 |
• Code Review | 5 | 20 |
• Test | 10 | 10 |
Reporting | ||
• Test Repor | 10 | 10 |
• Size Measurement | 10 | 10 |
• Postmortem & Process Improvement Plan | 10 | 10 |
Sum | 220 | 176 |
2.Project introduction
First, to make a calculator, first design a calculator interface. Secondly, to achieve the basic functions of the calculator: zero, basic operations, results display.
3.Project design and flow chart
The process of the calculator is shown in the figure below:
In addition to the principle of the calculator, I also need to design the interface of the calculator. Here I am using the traditional calculator layout, as shown below:
This calculator is unique in that its display section is divided into 3 sections, showing the first number, the calculation symbol, and the second number respectively. The advantage of this is that it is easy to find out the previous input.
4.Code description
The code is divided into two parts, which are the interface design part and the function implementation part.
Here’s a link to my code on github:https://github.com/wzxslcwdg/SimpleCalculator.git
4.1Interface Design
namespace Calculate {
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
button1 = new Button();
button2 = new Button();
button3 = new Button();
button4 = new Button();
button5 = new Button();
button6 = new Button();
button7 = new Button();
button8 = new Button();
button9 = new Button();
button10 = new Button();
plus = new Button();
multiply = new Button();
minus = new Button();
division = new Button();
button16 = new Button();
label1 = new Label();
label2 = new Label();
label3 = new Label();
label4 = new Label();
labelout = new Label();
labelmode = new Label();
labelbefore = new Label();
button11 = new Button();
SuspendLayout();
//
// button1
//
button1.Location = new Point(12, 174);
button1.Name = "button1";
button1.Size = new Size(94, 29);
button1.TabIndex = 0;
button1.Text = "1";
button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
//
// button2
//
button2.Location = new Point(112, 174);
button2.Name = "button2";
button2.Size = new Size(94, 29);
button2.TabIndex = 1;
button2.Text = "2";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// button3
//
button3.Location = new Point(224, 174);
button3.Name = "button3";
button3.Size = new Size(94, 29);
button3.TabIndex = 2;
button3.Text = "3";
button3.UseVisualStyleBackColor = true;
button3.Click += button3_Click;
//
// button4
//
button4.Location = new Point(12, 240);
button4.Name = "button4";
button4.Size = new Size(94, 29);
button4.TabIndex = 3;
button4.Text = "4";
button4.UseVisualStyleBackColor = true;
button4.Click += button4_Click;
//
// button5
//
button5.Location = new Point(112, 240);
button5.Name = "button5";
button5.Size = new Size(94, 29);
button5.TabIndex = 4;
button5.Text = "5";
button5.UseVisualStyleBackColor = true;
button5.Click += button5_Click;
//
// button6
//
button6.Location = new Point(224, 240);
button6.Name = "button6";
button6.Size = new Size(94, 29);
button6.TabIndex = 5;
button6.Text = "6";
button6.UseVisualStyleBackColor = true;
button6.Click += button6_Click;
//
// button7
//
button7.Location = new Point(12, 292);
button7.Name = "button7";
button7.Size = new Size(94, 29);
button7.TabIndex = 6;
button7.Text = "7";
button7.UseVisualStyleBackColor = true;
button7.Click += button7_Click;
//
// button8
//
button8.Location = new Point(112, 292);
button8.Name = "button8";
button8.Size = new Size(94, 29);
button8.TabIndex = 7;
button8.Text = "8";
button8.UseVisualStyleBackColor = true;
button8.Click += button8_Click;
//
// button9
//
button9.Location = new Point(224, 292);
button9.Name = "button9";
button9.Size = new Size(94, 29);
button9.TabIndex = 8;
button9.Text = "9";
button9.UseVisualStyleBackColor = true;
button9.Click += button9_Click;
//
// button10
//
button10.Location = new Point(12, 349);
button10.Name = "button10";
button10.Size = new Size(94, 29);
button10.TabIndex = 9;
button10.Text = "0";
button10.UseVisualStyleBackColor = true;
button10.Click += button10_Click;
//
// plus
//
plus.Location = new Point(324, 174);
plus.Name = "plus";
plus.Size = new Size(94, 29);
plus.TabIndex = 10;
plus.Text = "+";
plus.UseVisualStyleBackColor = true;
plus.Click += plus_Click;
//
// multiply
//
multiply.Location = new Point(324, 292);
multiply.Name = "multiply";
multiply.Size = new Size(94, 29);
multiply.TabIndex = 11;
multiply.Text = "*";
multiply.UseVisualStyleBackColor = true;
multiply.Click += multiply_Click;
//
// minus
//
minus.Location = new Point(324, 240);
minus.Name = "minus";
minus.Size = new Size(94, 29);
minus.TabIndex = 12;
minus.Text = "-";
minus.UseVisualStyleBackColor = true;
minus.Click += minus_Click;
//
// division
//
division.Location = new Point(324, 349);
division.Name = "division";
division.Size = new Size(94, 29);
division.TabIndex = 13;
division.Text = "/";
division.UseVisualStyleBackColor = true;
division.Click += division_Click;
//
// button16
//
button16.Location = new Point(224, 349);
button16.Name = "button16";
button16.Size = new Size(94, 29);
button16.TabIndex = 15;
button16.Text = "ce";
button16.UseVisualStyleBackColor = true;
button16.Click += button16_Click;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(111, 12);
label1.Name = "label1";
label1.Size = new Size(0, 20);
label1.TabIndex = 16;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(111, 52);
label2.Name = "label2";
label2.Size = new Size(0, 20);
label2.TabIndex = 17;
//
// label3
//
label3.AutoSize = true;
label3.Location = new Point(111, 94);
label3.Name = "label3";
label3.Size = new Size(0, 20);
label3.TabIndex = 18;
//
// label4
//
label4.AutoSize = true;
label4.Location = new Point(111, 135);
label4.Name = "label4";
label4.Size = new Size(0, 20);
label4.TabIndex = 19;
//
// labelout
//
labelout.AutoSize = true;
labelout.Font = new Font("Microsoft YaHei UI", 22.2F, FontStyle.Regular, GraphicsUnit.Point);
labelout.Location = new Point(12, 71);
labelout.Name = "labelout";
labelout.Size = new Size(42, 48);
labelout.TabIndex = 26;
labelout.Text = "0";
//
// labelmode
//
labelmode.AutoSize = true;
labelmode.Font = new Font("Microsoft YaHei UI", 10.8F, FontStyle.Regular, GraphicsUnit.Point);
labelmode.Location = new Point(16, 41);
labelmode.Name = "labelmode";
labelmode.Size = new Size(0, 24);
labelmode.TabIndex = 27;
//
// labelbefore
//
labelbefore.AutoSize = true;
labelbefore.Location = new Point(1, 9);
labelbefore.Name = "labelbefore";
labelbefore.Size = new Size(0, 20);
labelbefore.TabIndex = 28;
//
// button11
//
button11.Location = new Point(118, 349);
button11.Name = "button11";
button11.Size = new Size(88, 29);
button11.TabIndex = 29;
button11.Text = "=";
button11.UseVisualStyleBackColor = true;
button11.Click += button11_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(415, 436);
Controls.Add(button11);
Controls.Add(labelbefore);
Controls.Add(labelmode);
Controls.Add(labelout);
Controls.Add(label4);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(button16);
Controls.Add(division);
Controls.Add(minus);
Controls.Add(multiply);
Controls.Add(plus);
Controls.Add(button10);
Controls.Add(button9);
Controls.Add(button8);
Controls.Add(button7);
Controls.Add(button6);
Controls.Add(button5);
Controls.Add(button4);
Controls.Add(button3);
Controls.Add(button2);
Controls.Add(button1);
Name = "Form1";
Text = "Form1";
Load += Form1_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button button10;
private Button plus;
private Button multiply;
private Button minus;
private Button division;
private Button button16;
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label labelout;
private Label labelmode;
private Label labelbefore;
private Button button11;
}
}
Each button class contains several key factors: the position of the button, its size, the size of the text, and the font of the text. You also need to set the code to click the button. It is worth mentioning that you can add a button in Form.cs through the toolbox, and you can automatically add the corresponding button code. Information about the button can also be changed in Form.cs.
4.2Function Implementation
public Form1()
{
InitializeComponent();
}
// Reset all variables and labels when the "C" button is clicked
private void button16_Click(object sender, EventArgs e)
{
number1 = 0;
number2 = 0;
number3 = 0;
isminus = false;
ismult = false;
isdivi = false;
isplus = false;
ismode = false;
labelout.Text = Convert.ToString(number2); // Set the display to show number2
}
// Handle the addition operator button click
private void plus_Click(object sender, EventArgs e)
{
mode = Operator.plus;
labelmode.Text = "+";
labelout.Text = Convert.ToString(number2);
labelbefore.Text = Convert.ToString(number1);
ismode = true;
isplus = true;
}
// Handle the subtraction operator button click
private void minus_Click(object sender, EventArgs e)
{
mode = Operator.minus;
labelmode.Text = "-";
labelout.Text = Convert.ToString(number2);
labelbefore.Text = Convert.ToString(number1);
ismode = true;
isminus = true;
}
// Handle the multiplication operator button click
private void multiply_Click(object sender, EventArgs e)
{
mode = Operator.multiply;
labelmode.Text = "*";
labelout.Text = Convert.ToString(number2);
labelbefore.Text = Convert.ToString(number1);
ismode = true;
ismult = true;
}
// Handle the division operator button click
private void division_Click(object sender, EventArgs e)
{
mode = Operator.division;
labelmode.Text = "/";
labelout.Text = Convert.ToString(number2);
labelbefore.Text = Convert.ToString(number1);
ismode = true;
isdivi = true;
}
// Handle numeric button clicks
private void button10_Click(object sender, EventArgs e)
{
inputnumber = 0;
Calculate(inputnumber);
}
private void button9_Click(object sender, EventArgs e)
{
inputnumber = 9;
Calculate(inputnumber);
}
// ... Similarly, handle other numeric buttons
// Calculate method to process user input
public void Calculate(int ck)
{
if (ismode == false)
{
number1 = number1 * 10 + ck;
labelout.Text = Convert.ToString(number1);
// Perform the first number calculation
}
else
{
number2 = number2 * 10 + ck;
labelout.Text = Convert.ToString(number2);
}
}
private void Form1_Load(object sender, EventArgs e)
{
labelout.Text = Convert.ToString(number1);
}
// Handle the "=" button click to perform calculations
private void button11_Click(object sender, EventArgs e)
{
if (isplus == true)
{
number3 = number1 + number2;
labelbefore.Text = Convert.ToString("");
labelmode.Text = Convert.ToString("");
labelout.Text = Convert.ToString(number3);
number1 = number3;
number2 = 0;
number3 = 0;
isplus = false;
ismode = false;
}
// Similar code for subtraction, multiplication, and division
// Handle division by zero error
else if (isdivi == true && number2 == 0)
{
labelbefore.Text = Convert.ToString("");
labelmode.Text = Convert.ToString("");
labelout.Text = Convert.ToString("error");
number1 = 0;
number2 = 0;
isdivi = false;
ismode = false;
}
}
}
}
Comments have been added to each module in the code. They are defined variables, set button functions, set modes, and display Settings. Finally comes the most complicated outcome judgment.
4.3Demo
As shown in the figure, the calculator has four basic functions of addition, subtraction, multiplication and division, and has continuous calculation function. It can still be calculated after clearing.
5.Summarize
In general, the program I wrote completed the basic functions of the calculator, but there is still a lot of room for improvement:
Improve the user interface: I can make the calculator more beautiful by beautifying the interface. For example, align buttons at the code level
Error handling: Enhanced error handling to provide more detailed error messages.
Add features: As needed, you can consider adding more features, such as support for more complex calculations and retrieval of calculation history.
Testing: I can also do more detailed testing, such as testing the boundaries of the data