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 | Make a calculator |
MU STU ID and FZU STU ID | 21126046_832101204 |
Github project address:https://github.com/qing20030103/calculator1
1. Introduction
This task requires the realization of a simple calculator, to realize and understand some basic functions of the calculator addition, subtraction, multiplication, division, simple trigonometric operations of the visual calculator
2.PSP
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | ||
• Estimate | 40 | 40 |
Development | ||
• Analysis | 30 | 50 |
• Design Spec | 60 | 60 |
• Design Review | 60 | 60 |
• Coding Standard | 90 | 90 |
• Design | 90 | 100 |
• Coding | 120 | 150 |
• Code Review | 60 | 70 |
• Test | 20 | 35 |
Reporting | ||
• Test Repor | 30 | 30 |
• Size Measurement | 10 | 20 |
• Postmortem & Process Improvement Plan | 40 | 50 |
Sum | 650 | 750 |
3.Solution idea description
-
Create Windows forms application projects, achieve simple interface design with the help of common buttons and other controls in the toolbox.
-
Bind data to each control and implement the functions required by each click event.
-
Write the code of various operations of the calculator to realize the functions of the computer. The basic functions of addition, subtraction, multiplication, division, and zeroclearing, plus trigonometric functions, are realized with the functions that come with the Math library.
4.Design and implementation process
-
Create a new Windows Forms application project in Visual Studio.
-
You can drag the required component from the toolbox to the interface, here we use: Button, TextBox, and then according to the need we can right-click the component to open its properties panel, here to change its size, text and other properties, such as the Button name can be changed from “button” to “+”, “÷” and so on.
-
Double-click the component to see the code page, program the key ‘1’ so that when button1 is pressed, the text will appear as 1.To complete these operations, I think the key is to record the left and right numbers and determine what operations are performed, so I first set up variables representing the left and right numbers and results globally.
double num1;
string optType;//当前运算符
- Use arithmetic operators in C# to implement basic mathematical operation logic, and call corresponding methods in the Math class to implement other advanced functions, such as trigonometric functions.
Once the key code is complete, a simple calculator is ready to use
if (optType == "+")
{
result = num1 + num2;
}
else if (optType == "-")
{
result = num1 - num2;
}
else if (optType == "*")
{
result = num1 * num2;
}
else if (optType == "/")
{
result = num1 / num2;
}
else if (txt == "sin")
{
double rad = num2 * (Math.PI / 180);
result = (double)(Math.Sin(rad));
}
else if (optType == "cos")
{
double rad = num2 * (Math.PI / 180);
result = Math.Cos(rad);
}
else if (optType == "tan")
{
double rad = num2 * (Math.PI / 180);
result = Math.Tan(rad);
}
- Debug and test the application to ensure that it can perform various calculation operations correctly and give correct results.
5.Program flow chart of basic operation
6.Code Description
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
double num1;
string optType;//当前运算符
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
Console.WriteLine(btn.Text);
string txt = btn.Text;
if (txt == "+" || txt == "-" || txt == "*" || txt == "/")
{
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
optType = txt;
}
else if (txt == "=")
{
double num2 = double.Parse(textBox1.Text);
double result = 0;
if (optType == "+")
{
result = num1 + num2;
}
else if (optType == "-")
{
result = num1 - num2;
}
else if (optType == "*")
{
result = num1 * num2;
}
else if (optType == "/")
{
result = num1 / num2;
}
else if (txt == "sin")
{
double rad = num2 * (Math.PI / 180);
result = (double)(Math.Sin(rad));
}
else if (optType == "cos")
{
double rad = num2 * (Math.PI / 180);
result = Math.Cos(rad);
}
else if (optType == "tan")
{
double rad = num2 * (Math.PI / 180);
result = Math.Tan(rad);
}
textBox1.Text = result.ToString();
}
else if (txt == "delete")
{
string tempStr = textBox1.Text;
tempStr = tempStr.Substring(0, tempStr.Length - 1);
textBox1.Text = tempStr;
}
else if (txt == "clear")
{
textBox1.Text = "";
}
else
{//输入拼接
string tempStr = textBox1.Text;
tempStr = tempStr + txt;
textBox1.Text = tempStr;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.button2.Click += new System.EventHandler(this.button1_Click);//为button设置初始点击事件,回调
this.button3.Click += new System.EventHandler(this.button1_Click);
this.button4.Click += new System.EventHandler(this.button1_Click);
this.button5.Click += new System.EventHandler(this.button1_Click);
this.button6.Click += new System.EventHandler(this.button1_Click);
this.button7.Click += new System.EventHandler(this.button1_Click);
this.button8.Click += new System.EventHandler(this.button1_Click);
this.button9.Click += new System.EventHandler(this.button1_Click);
this.button10.Click += new System.EventHandler(this.button1_Click);
this.button11.Click += new System.EventHandler(this.button1_Click);
this.button12.Click += new System.EventHandler(this.button1_Click);
this.button13.Click += new System.EventHandler(this.button1_Click);
this.button14.Click += new System.EventHandler(this.button1_Click);
this.button15.Click += new System.EventHandler(this.button1_Click);
this.button16.Click += new System.EventHandler(this.button1_Click);
this.button17.Click += new System.EventHandler(this.button1_Click);
this.button18.Click += new System.EventHandler(this.button1_Click);
this.button19.Click += new System.EventHandler(this.button1_Click);
this.button20.Click += new System.EventHandler(this.button1_Click);
this.button21.Click += new System.EventHandler(this.button1_Click);
}
}
}
7. Results display
gif animation, showing some of the functions of the calculator addition, multiplication operations.
8.Summary
Create a simple calculator using Visual Studio (C#).In this process, various controls are used to build the user interface, control and code interaction controls can help us interact with users more easily and achieve various functions. The basic operation logic of addition, subtraction, multiplication and division is written. Learn more about visualization programs