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

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

      

    }
}

转载于:https://www.cnblogs.com/gfjin/p/8150716.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值