在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 简易计算器 入门级示例

C# 简易计算器 入门级示例

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.05M
  • 下载次数:1
  • 浏览次数:46
  • 发布时间:2018-05-16
  • 实例类别:C#语言基础
  • 发 布 人:konger
  • 所需积分:2
 相关标签: 计算器 计算

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 简易计算器
{
    public partial class calc : Form
    {
        private bool ClearDisplay = true;
        private bool Spequal = true;  //判断等号是否按下
        private bool Spresult = true;  //控制result时不能<-
        private bool Spfirst = true; //控制初始为0
        private bool Spdis=false;//Operand1解决
        private bool Spcomma=true;
       // private bool Spoperator1;
      //  private bool Spoperator = false;//判断是否 -*/连续出现
        private string Operator="=",Operator1="=";
        private double Operand1;
        private double Operand2;
        private double result;
        private System.Windows.Forms.Button btn;

    
        public calc()
        {
            InitializeComponent();
            if (Spfirst)
            {
                txtDisplay.Text = "0";
                ClearDisplay = true;
                Operand1 =Operand2 = 0;

                Spfirst = false;
            }
        }

        private void calc_Load(object sender, EventArgs e)
        {
            
        }

        
        private void button_11_Click(object sender, EventArgs e) //C 清所有
        {
            Operand1 = 0;
            Operand2 = 0;
            result = 0;
            txtDisplay.Text = "0";
            Spequal = true;
            ClearDisplay = true;
            Spresult = false;
            Spdis = false;
            Operator1 = "=";
            Operator = "=";

        }

        private void handleDigits(object sender, EventArgs e)
        {
                           /* if ((Operator!="=")||(Operator1!="="))
                            {
                                Spoperator = true;
                            }
                            if (Spoperator)
                            {
                                //double result = 0;
                                Operand2 = System.Convert.ToDouble(txtDisplay.Text);
                                switch (Operator)
                                {
                                    case " ":
                                        Operand1 = Operand1   Operand2;
                                        break;
                                    case "-":
                                        Operand1 = Operand1 - Operand2;
                                        break;
                                    case "*":
                                        Operand1 = Operand1 * Operand2;
                                        break;
                                    case "/":
                                        Operand1 = Operand1 / Operand2;
                                        break;
                                    case "^":
                                        Operand1 = System.Math.Pow(Operand1, Operand2);
                                        break;
                                }
                                txtDisplay.Text = Operand1.ToString();
                                ClearDisplay = true;
                                Spoperator = false;
                               // Spequal = false;  //等号是否按下
                               // Spresult = false;  //为结果时不能执行<-

                            }
                            else{
                                  btn = (Button)sender;
                                if (ClearDisplay)
                                {
                                    txtDisplay.Text = "";
                                    ClearDisplay = false;
                                }
                                 txtDisplay.Text = txtDisplay.Text   btn.Text;
                                 Spresult = true;
                            }*/
            btn = (Button)sender;
            if (ClearDisplay)
            {
                txtDisplay.Text = "";
                ClearDisplay = false;
            }
            //////改     
            if (txtDisplay.Text.IndexOf(".") == -1)
            {
                txtDisplay.Text = txtDisplay.Text   btn.Text;
                Spresult = true; //控制result时不能 < -
                Spdis = true;//Operand1解决
                Spequal = true;
            }
            else
            {   
                if(btn.Text ==".")
                {
                    txtDisplay.Text = txtDisplay.Text;
                }
                else
                {
                    txtDisplay.Text = txtDisplay.Text btn.Text ;
                    Spresult = true; //控制result时不能 < -
                    Spdis = true;//Operand1解决
                    Spequal = true;
                }
            }
            /*原始
            txtDisplay.Text = txtDisplay.Text   btn.Text;
            Spresult = true; //控制result时不能 < -
            Spdis = true;//Operand1解决
            Spequal = true;*/
           // Operator1 = Operator;
        }

        private void handleoperator(object sender, EventArgs e)//操作符
        {
           // if (Spequal)  //判断符号是否按下
           // {
                btn = (Button)sender;
                Operator = btn.Text;
                if ((Operator != "=") && (Operator1 != "=") && (Spdis))//Operand1解决
                {
                    //txtDisplay.Text = "进了1";
                    switch (Operator1)
                    {    
                        case " ":
                            Operand1 = Operand1   System.Convert.ToDouble(txtDisplay.Text);//txtDisplay.Text = "进了2";
                            txtDisplay.Text = Operand1.ToString();
                            break;
                        case "-":
                            Operand1 = Operand1 - System.Convert.ToDouble(txtDisplay.Text);//txtDisplay.Text = "进了3";
                            txtDisplay.Text = Operand1.ToString();
                            break;
                        case "*":
                            Operand1 = Operand1 * System.Convert.ToDouble(txtDisplay.Text);
                            txtDisplay.Text = Operand1.ToString();
                            break;
                        case "/":
                            Operand1 = Operand1 / System.Convert.ToDouble(txtDisplay.Text);
                            txtDisplay.Text = Operand1.ToString();
                            break;
                        case "^":
                            Operand1 = System.Math.Pow(Operand1, System.Convert.ToDouble(txtDisplay.Text));
                            txtDisplay.Text = Operand1.ToString();
                            break;
                    }
                    ClearDisplay = true;
                    // ***Spequal = true;
                    Spdis = false;  //Operand1解决
                    Spresult = false; //等号没有按下,等号可使用
                    Operator1 = Operator;

                    Spequal = true;
                    Spfirst = false;
                }
                else
                {
                    Operand1 = System.Convert.ToDouble(txtDisplay.Text);
                    ClearDisplay = true;
                    
                    //***Spequal = true;
                    Spdis = false; //Operand1解决
                    Operator1 = Operator;

                    Spequal = true;
                    Spresult = false;
                    Spfirst = false;
                }           
            //}
           
            /*原始
            btn = (Button)sender;
            Operator = btn.Text;
            Operand1 = System.Convert.ToDouble(txtDisplay.Text);
            //txtDisplay.Text = "";
            ClearDisplay = true;
            Spequal = true;*/
        }

        private void button_20_Click(object sender, EventArgs e)  //=
        {
            if (Spequal && Spdis) //Spdis -Operand1解决
            {
                double result = 0;
                Operand2 = System.Convert.ToDouble(txtDisplay.Text);

                switch (Operator)
                {
                    case " ":
                        result = Operand1   Operand2;
                        break;
                    case "-":
                        result = Operand1 - Operand2;
                        break;
                    case "*":
                        result = Operand1 * Operand2;
                        break;
                    case "/":
                        result = Operand1 / Operand2;
                        break;
                    case "^":
                        result = System.Math.Pow(Operand1, Operand2);
                        break;
                }
                txtDisplay.Text = result.ToString();
                ClearDisplay = true;
                Spequal = false;   //判断等号是否按下
                Spresult = false;   //控制result时不能<-
                Operand1 = Operand2 = 0;
                //Operator1 = "=";

            }
            Operator = "=";
            Operator1 = "=";
        }

        private void button_18_Click(object sender, EventArgs e)   //   -/ 
        {
            result = -Convert.ToDouble(txtDisplay.Text);
            txtDisplay.Text = result.ToString();
        }

        private void button_19_Click(object sender, EventArgs e)   //  1/x
        {
            if (txtDisplay.Text != "0")
            {
                result = 1.0 / Convert.ToDouble(txtDisplay.Text);
                txtDisplay.Text = result.ToString();
                ClearDisplay = true;
            }
        }

        private void button_22_Click(object sender, EventArgs e)    //CE   清除目前
        {
            txtDisplay.Text = "0";
            ClearDisplay = true;
            Spequal = true;
            Spdis = false;

        }

        private void button_21_Click(object sender, EventArgs e)   //  <-
        {   
            if(Spresult)
            {
                if (txtDisplay.Text.Length > 0)
                {
                    txtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1);
                    if (txtDisplay.Text.Length == 0)
                    {
                        txtDisplay.Text = "0";
                        ClearDisplay = true;             
                    }
                }
            }
            
        }

       
    }
}

标签: 计算器 计算

实例下载地址

C# 简易计算器 入门级示例

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_4847253'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s13.cnzz.com/stat.php%3Fid%3D4847253' type='text/javascript'%3E%3C/script%3E"));