C#编写简单窗体计算器

    说起来话长......

    大一学习了一年C++了,一直想做一个 GUI的简单计算器,但是在经历各种折磨之后决定放弃。。。。。

    因为偶然的机会开始接触学习C#,或许因为有C++的基础,感觉上手还算容易!

    在自学窗体后,又突然想起了曾经这个美妙的想法!话不多说,说做就做,在借鉴几篇博客之后,结果如下。

    由于本人水平有限,大神莫见笑。


效果图:

   


C#实现简单计算器,代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        private double num1 = 0; // 第一操作数
        private double num2 = 0; // 第二操作数
        private double num3 = 0; // 两操作数结果
        private string sign; // 运算符号
        private bool flag = true; // 检查是否为第一操作数
        public void Input_1(string s)
        {
            textBox1.Text += s;
            num1 = Convert.ToDouble(textBox1.Text);
        }
        public void Input_2(string s)
        {
            textBox1.Text += s;
            num2 = Convert.ToDouble(textBox1.Text);
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "是的!这只是一个垃圾计算器,他还有BUG!";
            linkLabel1.Links[0].LinkData = "http://www.lhldev.cn";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if(flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if(flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if(flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if(flag)
            {
                Button b = (Button)sender;
                Input_1(b.Text);
            }
            else
            {
                Button b = (Button)sender;
                Input_2(b.Text);
            }
        }

        private void button10_Click(object sender, EventArgs e) // 0
        {
            if(textBox1.Text!="")
            {
                if(flag)
                {
                    Button b = (Button)sender;
                    Input_1(b.Text);
                }
                else
                {
                    Button b = (Button)sender;
                    Input_2(b.Text);
                }
            }
        }

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

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

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

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

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

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

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

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

        private void button18_Click(object sender, EventArgs e) // 相反数
        {
            sign = "bw";
            textBox1.Text = "";
            flag = false;
        }

        private void button11_Click(object sender, EventArgs e)  //小数点
        {
            if(textBox1.Text=="")
            {
                textBox1.Text = "0.";
            }
            else if(textBox1.Text.IndexOf(".")>=0)
            {
                MessageBox.Show("已经添加小数点l!", "提示");
            }
            else
            {
                textBox1.Text += ".";
            }
        }

        private void button12_Click(object sender, EventArgs e)  // =
        {
            flag = true;
            switch(sign)
            {
                case "+":
                    num3 = num1 + num2;
                    break;
                case "-":
                    num3 = num1 - num2;
                    break;
                case "*":
                    num3 = num1 * num2;
                    break;
                case "/":
                    {
                        textBox1.Text = "";
                        if(num2==0)
                        {
                            textBox1.Clear();
                            MessageBox.Show("除数不能为0", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            break;
                        }
                        else
                        {
                            num3 = num1 / num2;
                            break;
                        }
                    }
                case "%":
                    {
                        textBox1.Text = "";
                        if (num2 == 0)
                        {
                            textBox1.Clear();
                            MessageBox.Show("除数不能为0", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            break;
                        }
                        else
                        {
                            num3 = num1 % num2;
                            break;
                        }
                    }
                case "bw":
                    num3 = 1 / num1;
                    break;
                case "pow":
                    num3 = Math.Pow(num1, 2);
                    break;
                case "ln":
                    num3 = Math.Log(num1,Math.E);
                    break;
                case "sqrt":
                    num3 = Math.Sqrt(num1);
                    break;
            }
            textBox1.Text = num3.ToString();
            num1 = num3;
        }

        private void button17_Click(object sender, EventArgs e)  // C 
        { 
            textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); //字符串剪切
            if(flag)
            {
                num1 = double.Parse(textBox1.Text);
            }
            else
            {
                num2 = double.Parse(textBox1.Text);
            }
        }

        private void button16_Click(object sender, EventArgs e)  // CE
        {
            textBox1.Text = "";
            num1 = 0;
            num2 = 0;
            num3 = 0;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }
    }
}
如有bug  还望指教!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页