• 一个适合新手的简易计算器


    翻出 初学C#时候写的一个计算器:初学者可以看看

    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 计算器
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string Op1, Op2;
            bool HasDecimal;
            int NumCount;
            string LastInput;
            int OpFlag;
            String tempReadout;
            private void Form1_Load(object sender, EventArgs e)
            {
                HasDecimal = false;
                NumCount = 0;
                LastInput = "NONE";
                OpFlag = 0;
                textBox1.Text = "0.";
            }


            public void InputNum(int num)
            {
                if (LastInput != "NUMS")
                {
                    textBox1.Text = "0";
                    HasDecimal = false;
                }
                if (HasDecimal)
                {
                    textBox1.Text =textBox1.Text + num;
                }
                else
                {
                    if (textBox1.Text == "0")
                    {
                        textBox1.Text = num+"." ;
                    }
                    else
                    {
                        textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + num+".";
                    }
                }
                if (LastInput == "NEG")
                {
                    textBox1.Text ="-"+textBox1.Text;
                }
                LastInput = "NUMS";
            }

            private void Decimal_Click(object sender, EventArgs e)
            {
                if (LastInput == "NEG")
                {
                    textBox1.Text = "-0.";
                }
                else if (LastInput != "NUMS")
                {
                    textBox1.Text = "0.";
                }
                HasDecimal = true;
                LastInput = "NUMS";
            }

            private void button1_Click(object sender, EventArgs e)
            {
                InputNum(1);
            }

            private void button2_Click(object sender, EventArgs e)
            {
                InputNum(2);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                InputNum(3);
            }

            private void button4_Click(object sender, EventArgs e)
            {
                InputNum(4);
            }

            private void button5_Click(object sender, EventArgs e)
            {
                InputNum(5);
            }

            private void button6_Click(object sender, EventArgs e)
            {
                InputNum(6);
            }

            private void button7_Click(object sender, EventArgs e)
            {
                InputNum(7);
            }

            private void button8_Click(object sender, EventArgs e)
            {
                InputNum(8);
            }

            private void button9_Click(object sender, EventArgs e)
            {
                InputNum(9);
            }

            private void button0_Click(object sender, EventArgs e)
            {
                InputNum(0);
            }

            private void Operator(int op)
            {
                tempReadout = textBox1.Text;
                if (LastInput == "NUMS")
                {
                    NumCount += 1;
                }
                string Myop;
                Myop = NumCount.ToString();
               
                switch (NumCount)
                {
                    case 0:
                        if (op == 2 && LastInput != "NEG")
                        {
                            textBox1.Text = "-" + textBox1.Text+".";
                            LastInput = "NEG";
                        }
                        break;
                    case 1:
                        Op1 = textBox1.Text;
                        if (op == 2 && LastInput != "NUMS"&&OpFlag !=5)
                        {
                            textBox1.Text = "-" ;
                            LastInput = "NEG";
                        }
                        break;
                    case 2:
                        Op2=tempReadout ;
                        switch (OpFlag )
                        {
                            case 1:
                                Op1=((Double.Parse (Op1)+Double.Parse (Op2 )).ToString ());
                                break;
                            case 2:
                                Op1=((Double.Parse (Op1)-Double.Parse (Op2 )).ToString ());
                                break;
                            case 3:
                                Op1=((Double.Parse (Op1)*Double.Parse (Op2 )).ToString ());
                                break;
                            case 4:
                                if (Double.Parse(Op2) != 0)
                                Op1=((Double.Parse (Op1)/Double.Parse (Op2 )).ToString ());
                                break;
                             case 5:
                                Op1 = Op2;
                                break;    
                        }
                        textBox1.Text = Op1;
                        NumCount = 1;
                        break;
                }
                if(LastInput !="NGS")
                {
                    LastInput = "OPS";
                    OpFlag = op;
                }
            }

            private void button13_Click(object sender, EventArgs e)
            {
               
                Operator(1);
            }

            private void button14_Click(object sender, EventArgs e)
            {
               
                Operator(2);
            }

            private void button15_Click(object sender, EventArgs e)
            {
              
                Operator(3);
            }

            private void button16_Click(object sender, EventArgs e)
            {
               
                Operator(4);
            }

            private void button17_Click(object sender, EventArgs e)
            {
               
                Operator(5);
            }

            private void buttonCLS_Click(object sender, EventArgs e)
            {
                textBox1.Text = "0.";
                Op1 = "";
                Op2 = "";
                Form1_Load(this,new EventArgs ());
            }

            private void buttonBack_Click(object sender, EventArgs e)
            {
                textBox1.Text = "0.";
                HasDecimal = false;
                LastInput = "CE";
            }

            private void button10_Click(object sender, EventArgs e)
            {
             
                    if (LastInput == "NUMS"&&textBox1.Text != "" && textBox1.Text != "0." && textBox1.Text != string.Empty && textBox1.Text != null && textBox1.Text.Length >1)
                    {
                        textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 2) + ".";
                        if (textBox1.Text.Length == 1)
                        {
                            textBox1.Text = "0.";
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        return;
                    }
                    LastInput = "NUMS";
              
            }

          

        }
    }

  • 相关阅读:
    sql分页 sql server,oracle,db2,mysql
    部分SQL优化
    javascript &&和||的其他用法
    socket 套接字
    网络协议篇
    异常处理
    元类type 反射 函数与方法 双下方法
    私有 实例方法 类方法 静态方法 属性 issubclass isinstance区别
    面向对象的三大特性 鸭子类型 类的约束 super的深度剖析
    继承
  • 原文地址:https://www.cnblogs.com/gfjin/p/8150716.html
Copyright © 2020-2023  润新知