winform 计算器

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 计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void 科学型ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {  
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
        }
        public bool down;
        private void dd_Click(object sender, EventArgs e)
        {
           if(down)
           {
               if (this.textBox1.Text.Contains("+") && this.textBox1.Text.Substring(textBox1.Text.Length - 1) == "0")
               {
                   this.textBox1.Text += ".";
               }
               else
               { this.textBox1.Text = "输入错误"; }
               down = false;
               
           }
           else 
           {
               this.textBox1.Text += ".";
               down = true;
           }
        }
        public double fuhao;
        private void and_Click(object sender, EventArgs e)
        {
            fuhao = 1;
            this.textBox1.Text += "+";
           
        }
        private void dengyu_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Substring(0, 1) == "-")
            {
                this.textBox1.Text = this.textBox1.Text.Substring(1);
                double sum;
                char[] chr = new char[] { '+', '-', '*', '/', '%' };
                string[] no = this.textBox1.Text.Split(chr);

                double a = -Convert.ToDouble(no[0]);
                double b = Convert.ToDouble(no[1]);
                if (fuhao == 1)
                {
                    sum = a + b;
                    textBox1.Text = Convert.ToString(sum);
                }
                else if (fuhao == 2)
                {
                    sum = a - b;
                    textBox1.Text = Convert.ToString(sum);
                }
                else if (fuhao == 3)
                {
                    sum = a * b;
                    textBox1.Text = Convert.ToString(sum);
                }
                else if (fuhao == 4)
                {
                    sum = a / b;
                    textBox1.Text = Convert.ToString(sum);
                }

            }
            else
            {
               
                double sum;
                char[] chr = new char[] { '+', '-', '*', '/', '%' };
                for (int i = 0; i < chr.Length; i++)
                {
                    if (!this.textBox1.Text.Contains(chr[i]))
                    {
                        this.textBox1.Text = this.textBox1.Text;
                    }
                    else
                    {
                        string[] no = this.textBox1.Text.Split(chr);

                        double a = Convert.ToDouble(no[0]);
                        double b = Convert.ToDouble(no[1]);
                        if (fuhao == 1)
                        {
                            sum = a + b;
                            textBox1.Text = Convert.ToString(sum);
                        }
                        else if (fuhao == 2)
                        {
                            sum = a - b;
                            textBox1.Text = Convert.ToString(sum);
                        }
                        else if (fuhao == 3)
                        {
                            sum = a * b;
                            textBox1.Text = Convert.ToString(sum);
                        }
                        else if (fuhao == 4)
                        {
                            sum = a / b;
                            textBox1.Text = Convert.ToString(sum);
                        }
                        else if (fuhao == 5)
                        {
                            sum = a % b;
                            textBox1.Text = Convert.ToString(sum);
                        }
                    }
                }
            }
            
        }

        private void jian_Click(object sender, EventArgs e)
        {
            fuhao = 2;
            this.textBox1.Text += "-";
        }

        private void cheng_Click(object sender, EventArgs e)
        {
            fuhao = 3;
            this.textBox1.Text += "*";
        }

        private void chu_Click(object sender, EventArgs e)
        {
            fuhao = 4;
            this.textBox1.Text += "/";
            
        }


        private void num6_Click(object sender, EventArgs e)
        {
            Button btr = (Button)sender;
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = btr.Text ;
            }
            else
            {
                this.textBox1.Text += btr.Text ;
            }
        }

        private void back_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length == 1)
            {
                this.textBox1.Text = "0";
            }
            else
            {
                this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
            }
        }

        private void yu_Click(object sender, EventArgs e)
        {
            fuhao = 5;
            this.textBox1.Text += "%";
        }

        private void clear_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "0";
            
        }

        private void reverst_Click(object sender, EventArgs e)
        {
            this.textBox1.Text =( 1 / (Convert.ToDouble( this.textBox1.Text.Trim()))).ToString();
        }

        private void mi_Click(object sender, EventArgs e)
        {
            if (Convert.ToDouble(this.textBox1.Text )<=0)
            {
                this.textBox1.Text = "无效输入";
            }
            else
            {
                this.textBox1.Text = Math.Sqrt(Convert.ToDouble(this.textBox1.Text.Trim())).ToString();
            }
        }

        private void zhengfu_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = this.textBox1.Text;
            }
            else if (Convert.ToDouble(this.textBox1.Text) < 0)
            {
                this.textBox1.Text = this.textBox1.Text.Substring(1);
            }
            else
            {
                this.textBox1.Text = "-" + this.textBox1.Text;
            }
        }


    }
}
原文地址:https://www.cnblogs.com/lushixiong/p/4536075.html