Visual Studio (C#)Create calculator

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignment https://bbs.csdn.net/topics/617332156
The Aim of This AssignmentMake a calculator
MU STU ID and FZU STU ID21126046_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 StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate4040
Development
• Analysis3050
• Design Spec6060
• Design Review6060
• Coding Standard9090
• Design90100
• Coding120150
• Code Review6070
• Test2035
Reporting
• Test Repor3030
• Size Measurement1020
• Postmortem & Process Improvement Plan4050
Sum650750

3.Solution idea description

  1. Create Windows forms application projects, achieve simple interface design with the help of common buttons and other controls in the toolbox.

  2. Bind data to each control and implement the functions required by each click event.

  3. 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

  1. Create a new Windows Forms application project in Visual Studio.

  2. 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.

  3. 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;//当前运算符
  1. 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);
                }
              
  1. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值