Use C# to implement a calculator similar to Microsoft!

I recently took C# as an elective. This small calculator is my final homework for recording. I hope it can be helpful to everyone. Show the effect first

Table of contents

1. Implementation of calculator

2. Date calculation implementation


1. Implementation of calculator

The implementation of the calculator is mainly the implementation of the button effect. The most important thing is the calculation distinction between num1 and num2, when to assign a value to num1 and when to assign a value to num2. It is easiest to implement it with strings!

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 CaculatorFrm1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool flag = true;//是否能计算
        string sign = "";//计算符号
        string x = "";
        string y = "";
        bool dot = false;//是否位小数

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
        }


        private void Clear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            x = y = "";
            sign = "";
            flag = true;
            dot = false;
            label1.Text = "";
        }

        private void number_1_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "1";
            }
            else
            {
                y += "1";
            }

            textBox1.Text = flag ? x : y; // 只显示当前操作数

            label1.Text = textBox1.Text + sign + y; // 显示操作和执行的操作

        }

        private void plus_Click(object sender, EventArgs e)
        {
            flag = false;
            sign = "+";

            textBox1.Text = x + sign; // 只显示当前操作数和操作符

            label1.Text = textBox1.Text + y; // 显示操作和执行的操作
        }

        private void equal_Click(object sender, EventArgs e)
        {
            label1.Text += "=";
            flag = true;
            switch (sign)
            {
                case "+":
                    textBox1.Text = (Convert.ToDouble(x) + Convert.ToDouble(y)).ToString();
                    x = (Convert.ToDouble(x) + Convert.ToDouble(y)).ToString();
                    y = "";
                    sign = "";
                    break;
                case "-":
                    textBox1.Text = (Convert.ToDouble(x) - Convert.ToDouble(y)).ToString();
                    x = (Convert.ToDouble(x) + Convert.ToDouble(y)).ToString();
                    y = "";
                    sign = "";
                    break;
                case "*":
                    textBox1.Text = (Convert.ToDouble(x) * Convert.ToDouble(y)).ToString();
                    x = (Convert.ToDouble(x) * Convert.ToDouble(y)).ToString();
                    y = "";
                    sign = "";
                    break;
                case "÷":
                    {
                        // 除法逻辑
                        if (Convert.ToDouble(y) != 0)
                        {
                            double result = Convert.ToDouble(x) / Convert.ToDouble(y);
                            textBox1.Text = result.ToString("F5"); // 保留五位小数
                            x = result.ToString();
                        }
                        else
                        {
                            textBox1.Text = "Error: Divide by zero";
                        }
                        y = "";
                        sign = "";
                        break;
                    }
                    case "%":
                    {
                        if (Convert.ToDouble(y) != 0)
                        {
                            double result = Convert.ToDouble(x) % Convert.ToDouble(y);
                            textBox1.Text = result.ToString();
                            x = result.ToString();
                        }
                        else
                        {
                            textBox1.Text = "Error: Modulo by zero";
                        }
                        y = "";
                        sign = "";
                        break;
                    }

            }
        }

        private void number_2_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "2";
            }
            else
            {
                y += "2";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_3_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "3";
            }
            else
            {
                y += "3";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_4_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "4";
            }
            else
            {
                y += "4";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_5_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "5";
            }
            else
            {
                y += "5";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_6_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "6";
            }
            else
            {
                y += "6";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_7_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "7";
            }
            else
            {
                y += "7";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_8_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "8";
            }
            else
            {
                y += "8";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_9_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "9";
            }
            else
            {
                y += "9";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void number_0_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += "0";
            }
            else
            {
                y += "0";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void point_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x += ".";
            }
            else
            {
                y += ".";
            }

            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;

        }

        private void symbol_Click(object sender, EventArgs e)
        {


            if (flag == true)
            {
                if (!x.Equals("0"))
                {
                    if (x.StartsWith("-"))
                    {
                        x = x.Substring(1);
                    }
                    else
                    {
                        x = "-" + x;
                    }
                }
            }
            else
            {
                if (!y.Equals("0"))
                {

                    if (y.StartsWith("-"))
                    {
                        y = y.Substring(1);
                    }
                    else
                    {
                        y = "-" + y;
                    }
                }
            }

            textBox1.Text = x + sign + y;
            label1.Text = x + sign + y;

        }

        private void reduce_Click(object sender, EventArgs e)
        {
            flag = false;
            sign = "-";
            textBox1.Text += "-";

        }

        private void multiply_Click(object sender, EventArgs e)
        {

            flag = false;
            sign = "*";
            textBox1.Text += "*";
        }

        private void division_Click(object sender, EventArgs e)
        {
            flag = false;
            sign = "÷";
            textBox1.Text += "÷";
        }

        private void redical_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                double result = Math.Sqrt(Convert.ToDouble(x));
                textBox1.Text = result.ToString();
                x = result.ToString();
            }
            else
            {
                double result = Math.Sqrt(Convert.ToDouble(y));
                textBox1.Text = result.ToString();
                y = result.ToString();
            }
        }

        private void square_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                double result = Math.Pow(Convert.ToDouble(x), 2);
                textBox1.Text = result.ToString();
                x = result.ToString();
            }
            else
            {
                double result = Math.Pow(Convert.ToDouble(y), 2);
                textBox1.Text = result.ToString();
                y = result.ToString();
            }
        }

        private void fenzhiyi_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                if (Convert.ToDouble(x) != 0)
                {
                    double result = 1 / Convert.ToDouble(x);
                    textBox1.Text = result.ToString("F5"); // 保留五位小数
                    x = result.ToString();
                }
                else
                {
                    textBox1.Text = "Error: Divide by zero";
                }
            }
            else
            {
                if (Convert.ToDouble(y) != 0)
                {
                    double result = 1 / Convert.ToDouble(y);
                    textBox1.Text = result.ToString("F5"); // 保留五位小数
                    y = result.ToString();
                }
                else
                {
                    textBox1.Text = "Error: Divide by zero";
                }
            }
        }

        private void ClearEntry_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                x = "";
            }
            else
            {
                y = "";
            }
            textBox1.Text = x + sign + y;
            label1.Text = textBox1.Text;
        }

        private void backspace_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                if (x.Length > 0)
                {
                    x = x.Substring(0, x.Length - 1);
                }
                textBox1.Text = x;
            }
            else
            {
                if (y.Length > 0 && y != sign)
                {
                    y = y.Substring(0, y.Length - 1);
                }
                textBox1.Text = y;
            }
        }

        private void remainder_Click(object sender, EventArgs e)
        {
            flag = false;
            sign = "%";

            textBox1.Text = x + sign; // 只显示当前操作数和操作符

            label1.Text = textBox1.Text + y; // 显示操作和执行的操作
        }

        private void 日期计算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataForm data = new dataForm();
            data.Show();
            this.Hide();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}

The reason why the exit code is written at the end is that after opening the date calculation, Form1 will be hidden. When you open Form1 again and click to exit, Form1 will become the main program that does not control the exit of the program, and the exit effect cannot be achieved, so bind it. The exit event is scheduled!

2. Date calculation implementation

To achieve the same effect as Microsoft, a dynamic effect is needed, and the hiding and display of Combbox and label are designed.

This is the dataForm interface I designed, followed by event processing

Not very complicated and easy to implement!

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 CaculatorFrm1
{
    public partial class dataForm : Form
    {
        public dataForm()
        {
            InitializeComponent();
        }

        private void dataForm_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedItem = "日期之间的相隔时间";

        }

        private void dataForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String method = comboBox1.SelectedItem.ToString();
            switch (method)
            {
                case "日期之间的相隔时间":
                    DateTime startDate = dateTimePicker1.Value;
                    DateTime endDate = dateTimePicker2.Value;

                    // 计算相隔时间
                    TimeSpan duration = endDate - startDate;
                    label4.Text = $"相隔时间:{duration.Days}天 ";
                    break;
                case "添加或减去天数":

                    int years = int.Parse(comboBox2.SelectedItem.ToString());
                    int months = int.Parse(comboBox3.SelectedItem.ToString());
                    int days = int.Parse(comboBox4.SelectedItem.ToString());
                    DateTime newDate;
                    if (radioButton1.Checked)
                    {
                        newDate = dateTimePicker1.Value.AddYears(years).AddMonths(months).AddDays(days);
                    }
                    else if (radioButton2.Checked)
                    {
                        newDate = dateTimePicker1.Value.AddYears(-years).AddMonths(-months).AddDays(-days);
                    }
                    else
                    {
                        MessageBox.Show("请选择计算的方式");
                        // 其他操作,如报错或显示提示信息
                        return;
                    }
                    label4.Text = $"结果:{newDate.ToString("yyyy年MM月dd日")}";
                    break;                

            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String method = comboBox1.SelectedItem.ToString();
            switch (method)
            {
                case "日期之间的相隔时间":
                    label4.Text = "";
                    label3.Text = "差值";
                    label2.Show();
                    dateTimePicker2.Show();
                    radioButton1.Hide();
                    radioButton2.Hide();
                    comboBox2.Hide();
                    comboBox3.Hide();
                    comboBox4.Hide();
                    label5.Hide();
                    label6.Hide();
                    label7.Hide();
                    break;
                case "添加或减去天数":
                    label4.Text = "";
                    comboBox2.SelectedIndex = 0;
                    comboBox3.SelectedIndex = 0;
                    comboBox4.SelectedIndex = 0;
                    label3.Text = "日期";
                    label2.Hide();
                    dateTimePicker2.Hide();
                    radioButton1.Show();
                    radioButton2.Show();
                    comboBox2.Show();
                    comboBox3.Show();
                    comboBox4.Show();
                    label5.Show();
                    label6.Show();
                    label7.Show();
                    break;
            }
        }

        private void 标准ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Form1().Show();
            this.Hide();
        }
    }
}

If you need the effect, you can send me a private message!

Guess you like

Origin blog.csdn.net/weixin_73733267/article/details/134842547