Write a calculator in C# language



Mimic windows operating computing system attachments is style design , to design the basic menu items (shortcut key, hot key, etc. and the dividing line )

        

 

 

Function requirements : realize the function keys as shown in the figure below

 



The method of setting the hotkey is as follows:

 

First use the form item to make the form as shown in the figure

Right-click the drop-down menu item

 

Click edit item to set hot key

 

Click on the DropdownItems collection

Set the hotkey here, and so on;

 

 

 

After all settings are complete, as shown in the figure

 

 

Then click on the calculator form

The initial display text box is empty; add operand monitoring for 0-9 and decimal point,

Add operator monitoring for operators and add listeners for delete, backspace, and empty respectively;

The entire code is as follows:

<span style="font-family:Comic Sans MS;font-size:14px;">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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //保存用户所按的运算符
        private string s;
        //保存用户输入的运算数
        private double x, y;
        //运算符按钮对象
        private Button btn;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        private void buttond_Click(object sender, EventArgs e)
        {
            btn = (Button)sender;
            textBox1.Text += btn.Text;
        }
        private void buttonop_Click(object sender, EventArgs e)
        {
            btn = (Button)sender;
            if(btn.Name!="btn_Equal")
            {
                x = Convert.ToDouble(textBox1.Text);
                textBox1.Text = "";
                s = btn.Name;
                switch (s)
                {
                    case "btn_Recip":
                        textBox1.Text = "1/" + x.ToString() + "=" + (1/x).ToString();
                        break;
                    case "btn_Sqrt":
                        textBox1.Text = "√" + x.ToString() + "=" + Math.Sqrt(x).ToString();
                        break;
                }
            }
            else
            {
                y = Convert.ToDouble(textBox1.Text);
                switch(s)
                {
                    case"btn_Add":
                        textBox1.Text = x.ToString()+"+"+y.ToString()+"="+(x + y).ToString();
                        break;
                    case "btn_Sub":
                        textBox1.Text = x.ToString() + "-" + y.ToString() + "=" + (x - y).ToString();
                        break;
                    case "btn_Mul":
                        textBox1.Text = x.ToString() + "*" + y.ToString() + "=" + (x * y).ToString();
                        break;
                    case "btn_Div":
                        if (y == 0)
                        {
                            MessageBox.Show("除零错误!!!", "信息提示", MessageBoxButtons.OK);
                        }
                        else
                            textBox1.Text = x.ToString() + "/" + y.ToString() + "=" + (x / y).ToString();
                        break;
                    case "btn_Mod":
                        textBox1.Text = x.ToString() + "%" + y.ToString() + "=" + (x % y).ToString();
                        break;
                }
            }
        }
        private void changeSign(object sender, EventArgs e)        
        {            
            double storNum;             
            if (textBox1.Text.Length > 0)  
            {
                storNum = double.Parse(textBox1.Text);                 
                storNum *= -1;               
                textBox1.Text = storNum.ToString();           
            }            
            btn_Equal.Select();       
        }
        private void btnDeleteSign_Clicked(object sender, EventArgs e)
        {
             
        }
        private void btnDeleteAll_Clicked(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        private void btnDelete1_Clicked(object sender, EventArgs e)
        {
            this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
            if(this.textBox1.Text=="")
            {
                this.textBox1.Text = "";
            }
        }
    }
}</span>

The final execution result is as follows:

 


Guess you like

Origin blog.csdn.net/dream_18/article/details/51713400
Recommended