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. asp.net get server control id from javascript

    var WhateverValue = document.getElementById('<%= saveValue.ClientID %>').value

  2. Spark Streaming揭秘 Day22 架构源码图解

    Spark Streaming揭秘 Day22 架构源码图解 今天主要是通过图解的方式,对SparkStreaming的架构进行一下回顾. 下面这个是其官方标准的流程描述. SparkStreamin ...

  3. Centos6.4 安装NLTK

    NLTK 安装链接 http://www.nltk.org/install.html 不知道什么原因打不开pypi 的网站  http://pypi.python.org/pypi/setuptool ...

  4. ubuntu下安装ffmpeg和X264

    第一步:安装必要的库 $:-dev libtheora-dev libx11-dev zlib1g-dev 第二步:安装SDL(否则可能编译不出ffplay) $:-dev $:-dev libsdl ...

  5. table如何在过宽的时候添加滚动条

    在页面中,往往由于一个table的列过于多,导致页面放不下,然后内容各种挤变形. 这里说一个解决方法,.先用一个DIV把TABLE包围起来.然后给这个DIV设置宽度,并且设置overflow:auto ...

  6. C# asp Aspose.Cells 教程,包含增加勾选框,单元格,字体设置

    1,引用Aspose.Cells  dll 2,using Aspose.Cells; 3, Workbook excel = new Workbook(); string strFilePath = ...

  7. EXTJS 资料 combo 点一次触发一次

    { xtype: 'combo', id: 'ApplyToModel', name: 'ApplyToModel', store: comStoreApplyToModel, valueField: ...

  8. 懒惰的JY--关于遍历

    先上题: [问题描述] 众所周知,JY的百度搜索算法已经练的炉火纯青,任何搜索题都能0.000ms出解. 不幸的是,JY遇到了一道百度搜索算法解决不了的题目,题目是这样的: 给定N个数A[1] A[2 ...

  9. [SQL SERVER系列]之常用函数和开窗函数介绍及实例

    本文主要介绍SQL SERVER数据库中一些常用的系统函数及其SQL SERVER 2005以上支持的开窗函数. 1.常用函数 --从字符串右边截取指定字符数 select RIGHT('HELLO' ...

  10. Mac - 更新 Ruby

    因为准备在项目中使用bootstrap,在安装bootstrap过程中提示需要Ruby的版本在1.9.2以上,而目前使用的Ruby版本是Mac系统自带的1.8.7.所以需要对Ruby进行升级.这里使用 ...