用C#实现类似于微软的一个计算器!

最近选修了c#,这个小计算器是我的期末作业,用做记录,也希望能给大家带来帮助。先展示效果

目录

一,计算器的实现

二,日期计算实现


一,计算器的实现

计算器的实现主要是按钮效果的实现,最总要的是num1和num2的计算区分,什么时候为num1赋值,什么时候为num2赋值。用字符串来实现是最为简单的!

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();
        }
    }
}

结尾之所以写退出的代码,是因为在打开日期计算后,Form1将会隐藏,再次打开Form1时,再点击退出的时候,Form1将成为不是控制程序退出的主程序,无法实现退出效果,所以绑定了退出事件!

二,日期计算实现

想要达成和微软同样的效果,需要一个动态的效果,设计了Combox和label的隐藏和显示

这是我设计的dataForm界面,后面就是处理事件

不是很复杂,很容易实现!

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();
        }
    }
}

需要效果的话可以私信我要!

猜你喜欢

转载自blog.csdn.net/weixin_73733267/article/details/134842547