using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
 
namespace caculator
{
    public partial class caculator1 : Form
    {
        String op1,op2;           //定义两个操作数
        bool DecimalFlag;         //标识小数点是否存在
        int NumOps;               //记录操作数个数
        String LastInput;         //指示上一次按键事件的类型
        int OpFlag;               //指示将要进行的操作类型
        bool OperaterFlag;        //指示是否已经进行过操作
        String TempReadout;
 
        public caculator1()
        {
            InitializeComponent();//初始化界面控件
        }
 
        private void caculator1_Load(object sender, EventArgs e)   //对变量进行初始化赋值
        {
            DecimalFlag = false;                 //小数点标志赋为false
            OperaterFlag = false;                //尚未进行过操作
            NumOps = 0;                          //操作数个数为
            LastInput = "NONE";
            OpFlag = 0;
            ReadOut.Text = "0.";                 //文本框中初始值为“.”
        }
 
        public void calculate(int MyNumber)            //接收操作数
        {
            if (!OperaterFlag)                         //判断是否点击过操作符,初始值为false  如果没有点击操作符
            {
                if (DecimalFlag)                       //判断是否点击过小数点,初始值为false   如果已经点击过小数点
                {
                    ReadOut.Text = ReadOut.Text + MyNumber;   //直接把点击的数字加在框中现有数据的后面  小数点后点击的数字直接追加
                }
                else                                       //未曾点击过小数点则进行如下操作 
                {
                    if (ReadOut.Text == "0.")         
                    {
                        ReadOut.Text = MyNumber + ".";      //如果输入框中此时为“0.”,则在输入数字的后面加上小数点,如此时输入,则显示“.”
                    }
                    Else  
                    {
                        ReadOut.Text = ReadOut.Text.Substring(0, ReadOut.Text.Length - 1) + MyNumber + ".";  //框中数据后面加上小数点
                    }
                }
            }
            else
            {
                ReadOut.Text = MyNumber + ".";     //点击过操作符之后,输入的数字后面仍然加个小数点(为了显示的一致性,即任何时候框中的数据都是*.)
            }
           // DecimalFlag = false;
            OperaterFlag = false;
        }
 
 
        private void sign_Click(object sender, EventArgs e)        //正负切换(+/-)
        {
            if (ReadOut.Text != "0.")          //此时框中的数值不为“0.”
            {
                if (LastInput == "NUMS")       //上次输入的数为正数
                {
                    LastInput = "NEG";             //把LastInput置为“NEG”,表明最后一次输入的数是负数
                    ReadOut.Text = "-" + ReadOut.Text;    //给文本框中的数值前添上“-”号
                }
                else if (LastInput == "NEG")     //如果上次输入为负数
                {
                    LastInput = "NUMS";         //把LastInput置为“NUMS”,表明最后一次输入的数是正数
                    ReadOut.Text = ReadOut.Text.Substring(1, ReadOut.Text.Length - 1);  //把框中的数据最前面的符号去掉(重新变成正数)
                }
                else                     //正常情况下默认此时文本框中的数值是正数,点击+/-后,变成负数
                {
                    LastInput = "NEG";
                    ReadOut.Text = "-" + ReadOut.Text;
                }
            } 
            else
            {
                LastInput = "NUMS";      //为“0.”时的情况
            }
        }
 
 
        private void DecimalDot_Click(object sender, EventArgs e)      //小数点(.)
        {
            if (OperaterFlag == true)
            {
                ReadOut.Text = "0.";
            }
               DecimalFlag = true;
        }
 
        public void StandardizationShow(String s)                     //规范化显示
        {
            if (s.IndexOf(".") == -1)
            {
                ReadOut.Text = s + ".";
            }
            else
            {
                ReadOut.Text=s;
            }
        }
 
        private void Number0_Click(object sender, EventArgs e)    //点击对应的数字键时调用方法
        {
            calculate(0);
        }
 
        private void Number1_Click(object sender, EventArgs e)
        {
            calculate(1);
        }
 
        private void Number2_Click(object sender, EventArgs e)
        {
            calculate(2);
        }
 
        private void Number3_Click(object sender, EventArgs e)
        {
            calculate(3);
        }
 
        private void Number4_Click(object sender, EventArgs e)
        {
            calculate(4);
        }
 
        private void Number5_Click(object sender, EventArgs e)
        {
            calculate(5);
        }
 
        private void Number6_Click(object sender, EventArgs e)
        {
            calculate(6);
        }
 
        private void Number7_Click(object sender, EventArgs e)
        {
            calculate(7);
        }
 
        private void Number8_Click(object sender, EventArgs e)
        {
            calculate(8);
        }
 
        private void Number9_Click(object sender, EventArgs e)
        {
            calculate(9);
        }
 
 
        public void OperatorCal(int MyOperator)                  //接收操作符
        {
            if(!OperaterFlag)             //判断以前是否单击过操作符,避免连击操作符错误
            {
                NumOps = NumOps + 1; 
            }
 
            switch (NumOps)
            {
                case 0:
                    OperaterFlag = true;
                    break;
                case 1:
                    op1 = ReadOut.Text;
                    OperaterFlag = true;
                    break;
                case 2:
                    op2 = ReadOut.Text;
                    switch (OpFlag)
                    {
                        case 1:
                            op1 = (Double.Parse(op1) + Double.Parse(op2)).ToString();  //加
                            break;
                        case 3:
                            op1 = (Double.Parse(op1) - Double.Parse(op2)).ToString();  //减
                            break;
                        case 2:
                            op1 = (Double.Parse(op1) * Double.Parse(op2)).ToString();//乘
                            break;
                        case 0:
                            if (Double.Parse(op2) == 0)                                //除
                            {
                                MessageBox.Show("除数不能为", "计算器");
                            }
                            else
                            {
                                try
                                {
                                    op1 = (Double.Parse(op1) / Double.Parse(op2)).ToString();
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("Generic Exception Handler: {0}", e.ToString());
                                }
 
                            }
                            break;
                        case 4:
                            op1 = op2;                        //等                                              
                            break;
                    }
                    TempReadout = op1;
                    this.StandardizationShow(TempReadout);
                    NumOps = 1;
                    break;
            }
            if(NumOps!=0)
            {
                OpFlag = MyOperator;
            }
            OperaterFlag = true;
            DecimalFlag = false;                             
           // LastInput = "NUMS";
        }
 
        private void operator1_Click(object sender, EventArgs e)
        {
            OperatorCal(1);
        }
 
        private void operator3_Click(object sender, EventArgs e)
        {
            OperatorCal(3);
        }
 
        private void operator2_Click(object sender, EventArgs e)
        {
            OperatorCal(2);
        }
 
        private void operator0_Click(object sender, EventArgs e)
        {
            OperatorCal(0);
        }
 
        private void operator4_Click(object sender, EventArgs e)
        {
            OperatorCal(4);
        }
 
        private void Percent_Click(object sender, EventArgs e)       //%
        {
            ReadOut.Text = (Double.Parse(ReadOut.Text) / 100).ToString();
            NumOps = NumOps + 1;
            DecimalFlag = true;
        }
 
        private void Cancel_Click(object sender, EventArgs e)        //C
        {
            ReadOut.Text = "0.";
            op1 = "";
            op2 = "";
            caculator1_Load(this, new System.EventArgs());
        }
 
        private void CancelEntry_Click(object sender, EventArgs e)   //CE
        {
            ReadOut.Text = "0.";
            DecimalFlag = false;
            LastInput = "CE";
        }
 
        private void reciprocal_Click(object sender, EventArgs e)    //求倒数(/x)
        {
            ReadOut.Text = (1/Double.Parse(ReadOut.Text)).ToString();
        }
 
        private void Copy_Click(object sender, EventArgs e)     //复制
        {
           if(ReadOut.Text.Equals(""))
           {
               return;
           }
           Clipboard.SetDataObject(ReadOut.Text,true);
        }
 
        private void paste_Click(object sender, EventArgs e)   //粘贴
        {
            if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
            {
                this.paste.Enabled = true;
                ReadOut.Text = "";
                this.ReadOut.Paste();
            }
            else
            {
                this.paste.Enabled = false;
            }
        }
 
        private void Delete_Click(object sender, EventArgs e)   //删除(BackSpace)
        {
            if (ReadOut.Text != "0.")
            {
                try
                {
                    ReadOut.Text = ReadOut.Text.Substring(0, ReadOut.Text.Length - 2) + ".";
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Generic Exception Handler: {0}", ex.ToString());
                }
               
                if(ReadOut.Text==".")
                {
                    ReadOut.Text = "0.";
                }
            }
           
        }
 
        private void sqrt_Click(object sender, EventArgs e)
        {
            TempReadout = this.ReadOut.Text;
            this.ReadOut.Text = System.Convert.ToString(Math.Sqrt(System.Convert.ToDouble(TempReadout)));
        }
    }
 
}

制作计算器的代码(C#)的更多相关文章

  1. c#制作计算器全过程

    前言: 网上看的计算器制作只有代码,没有为全过程下面贴图,所以我在下面主要是贴图,让大家零基础制作计算器. 我的环境是visual studio 2010,其他版本例如2008,2012 都可以 1. ...

  2. Wxpython零基础制作计算器

    本文关于Wxpython零基础利用python3.6在pycharm下制作计算器,文章末尾有免费源代码供下载 以后同步更新到博客园和这个网站,www.empirefree.top, 这个网站备案号没有 ...

  3. Qt简单项目--加法计算器(详细代码注释)

    Qt的简单案例--加法计算器(详细代码注释) 一.项目结构 二.项目代码 widget.h #ifndef WIDGET_H #define WIDGET_H //预编译指令, 为了避免头文件被重复包 ...

  4. jquery侧边折叠导航栏制作,两行代码搞定

    jquery侧边折叠导航栏制作,两行代码搞定 //CSS*{margin: 0;padding: 0} ul{list-style: none} .menu li ul{display: none} ...

  5. WPF中桌面屏保的制作(主要代码)

    原文:WPF中桌面屏保的制作(主要代码) 制作要点:(1) 使用System.Windows.Threading.DispatcherTimer;(2) 将Window属性设置为:      this ...

  6. 用简单的JS代码制作计算器

    代码+注释一共不到200行,是练习交流的必备良药 主界面如下: 操作示意图: 以下是代码部分 HTML: <div> <table class="window"& ...

  7. C#网页版计算器程序代码

    calculator.aspx.cs代码 using System; using System.Collections.Generic; using System.Linq; using System ...

  8. Python实现的复杂的计算器的代码

    用Python实现复杂的计算器,可以按照“()”.乘除.加减的优先级进行混合运算.主旨是对正则表达式进行学习. 设计思路: 1.在计算式中搜索最后一个“(”,再搜索和它匹配的“)”,截取两个括号间的表 ...

  9. Java实现GUI计算器【代码】

    这几天用java写了一个计算器程序,开始写的时候原本只是想实现一下GUI(这个是直接读三个字母还是什么“固椅”的发音)界面,熟悉一下Java GUI 编程,为Java期末大作业练练手,本以为代码不会很 ...

随机推荐

  1. 《APUE》第6章练习1

    问题:编写一个程序,它调用uname并输出utsname结构中的所有字段,并将输出与uname(1)命令的输出结构做比较. 我本来以为utsname结构也跟passwd结构一样,也有那三个函数来获取整 ...

  2. RestFul && HATEOAS && Spring-Data-Rest介绍

    1.什么是RestFul 经常上网的同学会发现,现代软件的一个重要趋势就是互联网化,几乎没有一款软件是纯粹的单机版了.通常的情况下,软件管理着服务器的资源以及这些资源的状态变化,用户通过在浏览器输入h ...

  3. String inputStream file转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  4. 利用cglib生成动态java bean

    cglib详细学习 http://blog.csdn.net/u010150082/article/details/10901641 cglib-nodep jar报下载 http://grepcod ...

  5. 执行插入语句时直接返回插入信息的自增id,判断是否为空

    insert into userinfo(UserName,UserPass,RegTime,email)values('a','b',GETDATE(),'123@qq.com');select @ ...

  6. Kakfa揭秘 Day6 Consumer源码解密

    Kakfa揭秘 Day6 Consumer源码解密 今天主要分析下Consumer是怎么来工作的,今天主要是例子出发,对整个过程进行刨析. 简单例子 Example中Consumer.java是一个简 ...

  7. 1037. Magic Coupon (25)

    #include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...

  8. fedora 20 PIL

    今天安装PIL花了我好多的时间. 刚开始,我手动下载PIL原码,编译安装. 启动我的django项目,报下面的错误,完全不懂是么意思. CommandError: One or more models ...

  9. oracle游标小试

    有时候需要大面积的修改数据,这个时候用循环语句效率不高.而临时表又不能满足点对点修改的时候,游标似一种不错的选择(PS:好像游标也是为循环而生的吧) 现在有两张表 t1(ryid number,nam ...

  10. 内置对象之Cookie

    if (!this.IsPostBack) { try { HttpCookie MyCookie = new HttpCookie("MyCookie"); MyCookie.V ...