zoukankan      html  css  js  c++  java
  • 一个适合新手的简易计算器

    翻出 初学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";
              
            }

          

        }
    }

  • 相关阅读:
    spy++工具
    select 设置发送超时发送注意事项
    C++ Socket超时设置
    linux下的find文件查找命令与grep文件内容查找命令
    C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别
    vc6.0出现“cannot add new member”解决办法
    Nginx虚拟主机配置(20200202)
    Centos7内核版安装nginx环境问题及解决方法
    Nginx架构分析(20200202)
    软链接和硬链接——Linux中的文件共享
  • 原文地址:https://www.cnblogs.com/gfjin/p/8150716.html
Copyright © 2011-2022 走看看