谁能帮我看看这段代码怎么实现不了小数点功能????

来源:互联网 发布:4g网络车载大屏机 编辑:程序博客网 时间:2024/06/09 18:54

 namespace Calc
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.Div = new System.Windows.Forms.Button();
            this.close = new System.Windows.Forms.Button();
            this.Open = new System.Windows.Forms.Button();
            this.Add = new System.Windows.Forms.Button();
            this.Sub = new System.Windows.Forms.Button();
            this.Mult = new System.Windows.Forms.Button();
            this.Equt = new System.Windows.Forms.Button();
            this.Three = new System.Windows.Forms.Button();
            this.Four = new System.Windows.Forms.Button();
            this.Five = new System.Windows.Forms.Button();
            this.Six = new System.Windows.Forms.Button();
            this.Seven = new System.Windows.Forms.Button();
            this.Eight = new System.Windows.Forms.Button();
            this.Nine = new System.Windows.Forms.Button();
            this.Zoer = new System.Windows.Forms.Button();
            this.Two = new System.Windows.Forms.Button();
            this.Point = new System.Windows.Forms.Button();
            this.One = new System.Windows.Forms.Button();
            this.textShow = new System.Windows.Forms.TextBox();
            this.linkLabel1 = new System.Windows.Forms.LinkLabel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
            this.SuspendLayout();
            //
            // groupBox1
            //
            this.groupBox1.BackColor = System.Drawing.Color.Lime;
            this.groupBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("groupBox1.BackgroundImage")));
            this.groupBox1.Controls.Add(this.Div);
            this.groupBox1.Controls.Add(this.close);
            this.groupBox1.Controls.Add(this.Open);
            this.groupBox1.Controls.Add(this.Add);
            this.groupBox1.Controls.Add(this.Sub);
            this.groupBox1.Controls.Add(this.Mult);
            this.groupBox1.Controls.Add(this.Equt);
            this.groupBox1.Controls.Add(this.Three);
            this.groupBox1.Controls.Add(this.Four);
            this.groupBox1.Controls.Add(this.Five);
            this.groupBox1.Controls.Add(this.Six);
            this.groupBox1.Controls.Add(this.Seven);
            this.groupBox1.Controls.Add(this.Eight);
            this.groupBox1.Controls.Add(this.Nine);
            this.groupBox1.Controls.Add(this.Zoer);
            this.groupBox1.Controls.Add(this.Two);
            this.groupBox1.Controls.Add(this.Point);
            this.groupBox1.Controls.Add(this.One);
            this.groupBox1.Location = new System.Drawing.Point(1, 61);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(218, 202);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = " ";
            //
            // Div
            //
            this.Div.ForeColor = System.Drawing.Color.Red;
            this.Div.Location = new System.Drawing.Point(178, 109);
            this.Div.Name = "Div";
            this.Div.Size = new System.Drawing.Size(35, 23);
            this.Div.TabIndex = 17;
            this.Div.Text = "/";
            this.Div.UseVisualStyleBackColor = true;
            this.Div.Click += new System.EventHandler(this.btn_Oper);
            //
            // close
            //
            this.close.ForeColor = System.Drawing.Color.Red;
            this.close.Location = new System.Drawing.Point(116, 20);
            this.close.Name = "close";
            this.close.Size = new System.Drawing.Size(46, 23);
            this.close.TabIndex = 16;
            this.close.Text = "NO";
            this.close.UseVisualStyleBackColor = true;
            this.close.Click += new System.EventHandler(this.btn_Oper);
            //
            // Open
            //
            this.Open.ForeColor = System.Drawing.Color.Red;
            this.Open.Location = new System.Drawing.Point(178, 20);
            this.Open.Name = "Open";
            this.Open.Size = new System.Drawing.Size(40, 23);
            this.Open.TabIndex = 15;
            this.Open.Text = "OFF";
            this.Open.UseVisualStyleBackColor = true;
            this.Open.Click += new System.EventHandler(this.btn_Oper);
            //
            // Add
            //
            this.Add.ForeColor = System.Drawing.Color.Red;
            this.Add.Location = new System.Drawing.Point(131, 62);
            this.Add.Name = "Add";
            this.Add.Size = new System.Drawing.Size(31, 23);
            this.Add.TabIndex = 14;
            this.Add.Text = "+";
            this.Add.UseVisualStyleBackColor = true;
            this.Add.Click += new System.EventHandler(this.btn_Oper);
            //
            // Sub
            //
            this.Sub.ForeColor = System.Drawing.Color.Red;
            this.Sub.Location = new System.Drawing.Point(178, 62);
            this.Sub.Name = "Sub";
            this.Sub.Size = new System.Drawing.Size(35, 23);
            this.Sub.TabIndex = 13;
            this.Sub.Text = "-";
            this.Sub.UseVisualStyleBackColor = true;
            this.Sub.Click += new System.EventHandler(this.btn_Oper);
            //
            // Mult
            //
            this.Mult.ForeColor = System.Drawing.Color.Red;
            this.Mult.Location = new System.Drawing.Point(131, 109);
            this.Mult.Name = "Mult";
            this.Mult.Size = new System.Drawing.Size(31, 23);
            this.Mult.TabIndex = 12;
            this.Mult.Text = "*";
            this.Mult.UseVisualStyleBackColor = true;
            this.Mult.Click += new System.EventHandler(this.btn_Oper);
            //
            // Equt
            //
            this.Equt.ForeColor = System.Drawing.Color.Red;
            this.Equt.Location = new System.Drawing.Point(72, 139);
            this.Equt.Name = "Equt";
            this.Equt.Size = new System.Drawing.Size(39, 23);
            this.Equt.TabIndex = 11;
            this.Equt.Text = "=";
            this.Equt.UseVisualStyleBackColor = true;
            this.Equt.Click += new System.EventHandler(this.Equt_Click);
            //
            // Three
            //
            this.Three.BackColor = System.Drawing.Color.Wheat;
            this.Three.ForeColor = System.Drawing.Color.Red;
            this.Three.Location = new System.Drawing.Point(77, 20);
            this.Three.Name = "Three";
            this.Three.Size = new System.Drawing.Size(22, 23);
            this.Three.TabIndex = 10;
            this.Three.Text = "3";
            this.Three.UseVisualStyleBackColor = false;
            this.Three.Click += new System.EventHandler(this.Open1_Click);
            //
            // Four
            //
            this.Four.ForeColor = System.Drawing.Color.Red;
            this.Four.Location = new System.Drawing.Point(6, 62);
            this.Four.Name = "Four";
            this.Four.Size = new System.Drawing.Size(22, 23);
            this.Four.TabIndex = 9;
            this.Four.Text = "4";
            this.Four.UseVisualStyleBackColor = true;
            this.Four.Click += new System.EventHandler(this.Open1_Click);
            //
            // Five
            //
            this.Five.ForeColor = System.Drawing.Color.Red;
            this.Five.Location = new System.Drawing.Point(44, 62);
            this.Five.Name = "Five";
            this.Five.Size = new System.Drawing.Size(22, 23);
            this.Five.TabIndex = 8;
            this.Five.Text = "5";
            this.Five.UseVisualStyleBackColor = true;
            this.Five.Click += new System.EventHandler(this.Open1_Click);
            //
            // Six
            //
            this.Six.ForeColor = System.Drawing.Color.Red;
            this.Six.Location = new System.Drawing.Point(77, 62);
            this.Six.Name = "Six";
            this.Six.Size = new System.Drawing.Size(22, 23);
            this.Six.TabIndex = 7;
            this.Six.Text = "6";
            this.Six.UseVisualStyleBackColor = true;
            this.Six.Click += new System.EventHandler(this.Open1_Click);
            //
            // Seven
            //
            this.Seven.ForeColor = System.Drawing.Color.Red;
            this.Seven.Location = new System.Drawing.Point(6, 100);
            this.Seven.Name = "Seven";
            this.Seven.Size = new System.Drawing.Size(22, 23);
            this.Seven.TabIndex = 6;
            this.Seven.Text = "7";
            this.Seven.UseVisualStyleBackColor = true;
            this.Seven.Click += new System.EventHandler(this.Open1_Click);
            //
            // Eight
            //
            this.Eight.ForeColor = System.Drawing.Color.Red;
            this.Eight.Location = new System.Drawing.Point(44, 100);
            this.Eight.Name = "Eight";
            this.Eight.Size = new System.Drawing.Size(22, 23);
            this.Eight.TabIndex = 5;
            this.Eight.Text = "8";
            this.Eight.UseVisualStyleBackColor = true;
            this.Eight.Click += new System.EventHandler(this.Open1_Click);
            //
            // Nine
            //
            this.Nine.ForeColor = System.Drawing.Color.Red;
            this.Nine.Location = new System.Drawing.Point(77, 100);
            this.Nine.Name = "Nine";
            this.Nine.Size = new System.Drawing.Size(22, 23);
            this.Nine.TabIndex = 4;
            this.Nine.Text = "9";
            this.Nine.UseVisualStyleBackColor = true;
            this.Nine.Click += new System.EventHandler(this.Open1_Click);
            //
            // Zoer
            //
            this.Zoer.ForeColor = System.Drawing.Color.Red;
            this.Zoer.Location = new System.Drawing.Point(6, 139);
            this.Zoer.Name = "Zoer";
            this.Zoer.Size = new System.Drawing.Size(22, 23);
            this.Zoer.TabIndex = 3;
            this.Zoer.Text = "0";
            this.Zoer.UseVisualStyleBackColor = true;
            this.Zoer.Click += new System.EventHandler(this.Open1_Click);
            //
            // Two
            //
            this.Two.ForeColor = System.Drawing.Color.Red;
            this.Two.Location = new System.Drawing.Point(44, 20);
            this.Two.Name = "Two";
            this.Two.Size = new System.Drawing.Size(22, 23);
            this.Two.TabIndex = 2;
            this.Two.Text = "2";
            this.Two.UseVisualStyleBackColor = true;
            this.Two.Click += new System.EventHandler(this.Open1_Click);
            //
            // Point
            //
            this.Point.ForeColor = System.Drawing.Color.Red;
            this.Point.Location = new System.Drawing.Point(44, 139);
            this.Point.Name = "Point";
            this.Point.Size = new System.Drawing.Size(22, 23);
            this.Point.TabIndex = 1;
            this.Point.Text = ".";
            this.Point.UseVisualStyleBackColor = true;
            this.Point.Click += new System.EventHandler(this.btn_Oper);
            //
            // One
            //
            this.One.ForeColor = System.Drawing.Color.Red;
            this.One.Location = new System.Drawing.Point(6, 20);
            this.One.Name = "One";
            this.One.Size = new System.Drawing.Size(22, 23);
            this.One.TabIndex = 0;
            this.One.Text = "1";
            this.One.UseVisualStyleBackColor = true;
            this.One.Click += new System.EventHandler(this.Open1_Click);
            //
            // textShow
            //
            this.textShow.Location = new System.Drawing.Point(65, 22);
            this.textShow.Name = "textShow";
            this.textShow.Size = new System.Drawing.Size(154, 21);
            this.textShow.TabIndex = 1;
            this.textShow.Text = "0.";
            this.textShow.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            //
            // linkLabel1
            //
            this.linkLabel1.AutoSize = true;
            this.linkLabel1.Location = new System.Drawing.Point(33, 315);
            this.linkLabel1.Name = "linkLabel1";
            this.linkLabel1.Size = new System.Drawing.Size(101, 12);
            this.linkLabel1.TabIndex = 2;
            this.linkLabel1.TabStop = true;
            this.linkLabel1.Text = "欢迎进入我的空间";
            this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            //
            // pictureBox1
            //
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(1, 305);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(26, 22);
            this.pictureBox1.TabIndex = 3;
            this.pictureBox1.TabStop = false;
            //
            // pictureBox2
            //
            this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
            this.pictureBox2.Location = new System.Drawing.Point(1, 269);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(28, 30);
            this.pictureBox2.TabIndex = 4;
            this.pictureBox2.TabStop = false;
            this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Aqua;
            this.label1.Location = new System.Drawing.Point(36, 286);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(59, 12);
            this.label1.TabIndex = 5;
            this.label1.Text = "353926774";
            //
            // pictureBox3
            //
            this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
            this.pictureBox3.Location = new System.Drawing.Point(101, 279);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(21, 20);
            this.pictureBox3.TabIndex = 6;
            this.pictureBox3.TabStop = false;
            this.pictureBox3.Click += new System.EventHandler(this.pictureBox3_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.GhostWhite;
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.ClientSize = new System.Drawing.Size(223, 331);
            this.Controls.Add(this.pictureBox3);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.linkLabel1);
            this.Controls.Add(this.textShow);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "计算器";
            this.groupBox1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion


        #region
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button close;
        private System.Windows.Forms.Button Open;
        private System.Windows.Forms.Button Equt;
        private System.Windows.Forms.Button Three;
        private System.Windows.Forms.Button Four;
        private System.Windows.Forms.Button Five;
        private System.Windows.Forms.Button Six;
        private System.Windows.Forms.Button Seven;
        private System.Windows.Forms.Button Eight;
        private System.Windows.Forms.Button Nine;
        private System.Windows.Forms.Button Zoer;
        private System.Windows.Forms.Button Two;
        private System.Windows.Forms.Button Point;
        private System.Windows.Forms.Button One;
        private System.Windows.Forms.TextBox textShow;
        private System.Windows.Forms.Button Div;
        private System.Windows.Forms.Button Add;
        private System.Windows.Forms.Button Sub;
        private System.Windows.Forms.Button Mult;  
        #endregion

 


        #region 各变量和常数的声明
        public const int NULL = 0;// 定义操作码 
        public const int ADD = 1;     //表示加
        public const int SUB = 2;     //减
        public const int MULT = 3;     //乘
        public const int DIV = 4;    //除// 定义操作码   
        public const int SQR = 5;    //求平方
        public const int SQRT = 6;   //求平方根
        public const int NODOT=0 ;     // 定义是否点击了小数点,0 为没点
        public const int HASDOT = 1;
        private double res = 0;         // 记录结果数
        private double tmp = 0;         // 当前输入的操作数
        private int opt = NULL;         // 记录操作码
        private int dot = NODOT;    // 记录是否点击了小数点,0为没有点
        private int num = 0;        // 记录输入操作数的个数
        private int dotnum = 0;        // 记录小数点部分的个数       
        string strOper;
        public bool btndot;
        private System.Windows.Forms.LinkLabel linkLabel1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.PictureBox pictureBox3;
                 //获取操作符
        #endregion

 

       

    }
}

 

 

 

 

 

 

 

 

 

 

 

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

namespace Calc
{
    public partial class Form1 : Form

    {
        System.Windows.Forms.Button btnTmp = new System.Windows.Forms.Button();
        public Form1()
        {
            InitializeComponent();
        }

       private void Open1_Click(object sender, EventArgs e)
        {
              //System.Windows.Forms.Button btnTmp = new System.Windows.Forms.Button();
            double i;
           // btnTmp = NULL;
            btnTmp = sender as System.Windows.Forms.Button;
           if (btnTmp != null)
            {
                if (dot == NODOT)
                {
                    // 没有点击小数点
                    //try
                    //{
                   
                        i = double.Parse(btnTmp.Text.ToString()); //取用户自定义的控件关联数,并转换成double型
                        tmp = tmp *10 + i ;
                        textShow.Text = tmp.ToString();   //将其放入文本显示屏啊
                    //}
                    //catch (Exception s)
                    //{
                        //Show(s);
                   // }
                }
                else   // 点击了小数点
                {
                    dotnum++;    //记录小数点部分的个数
                    // 生成小数部分的新的数值
                    i = double.Parse(btnTmp.Text.ToString()) / System.Math.Pow(10, dotnum);
                    tmp = tmp + i;  //将小数点后的值加到当前操做数
                    textShow.Text = tmp.ToString();
                }
            }
        }

        private void Equt_Click(object sender, EventArgs e)
        {
            calc();
        }
        #region        //等于运算方法
        private void calc()
        {
            // 生成结果
            if (num == 0)    //是否有操作数,没有就返回0
            {
                res = 0;
                tmp = 0;
                textShow.Text = res.ToString();
                return;
            }
            switch (opt)  //找到对应的运算符进行计算
            {
                // 加法
                case ADD:
                    res = res + tmp;
                    break;
                // 减法
                case SUB:
                    res = res - tmp;
                    break;
                // 乘法
                case MULT:
                    res = res * tmp;
                    break;
                // 除法
                case DIV:
                    res = res / tmp;
                    break;
            }
                    textShow.Text = res.ToString();   //结果输出到文本显示屏
                    opt = NULL;  //运算符清空
                    tmp = 0;
                    dot = NODOT;
                //res = 0;
                //num = 0;
           
        }
        #endregion

 


        #region 获取操作符运事件
        //获取操作符运事件
        private void btn_Oper(object obj, EventArgs ea)
        {
            Button tmp1=(Button)obj;
            strOper=tmp1.Text;
            switch (strOper)
            {
                case "/":       //除法运算
              if(opt!=NULL&&opt!=DIV)
                    {
                        calc();
                    }
                    opt = DIV;
                    if (num != 0)  //判断操作数的个数,如果两个就做二元运算
                    {
                        if (tmp != 0)
                            res = res / tmp;
                    }
                    else    res = tmp;
                    num++;                     
                    tmp = 0;
                    textShow.Text = res.ToString();
                    dot = NODOT;
                    break;
                case "*":
                    // 乘法运算
                    if(opt!=NULL&&opt!=MULT)
                    {
                        calc();}
                    opt = MULT;
                   
                    if (num!= 0 )     //判断操作数的个数,如果两个就做二元运算   
                    {
                            if(tmp!=0)
                         res = res * tmp;
                    }
                    else
                        res = tmp;
                       num++;                                     
                    tmp = 0;
                    textShow.Text = res.ToString();
                    dot = NODOT;
                    break;
               
               
case "+":          //加法运算
                    if(opt!=NULL&&opt!=ADD)
                    {
                        calc();
                    }
                    opt = ADD;
                    if (num != 0)    //判断操作数的个数,如果两个就做二元运算
                        res = res + tmp;
                    else
                        res = tmp;
                    num++;                   
                    tmp = 0;
                    textShow.Text = res.ToString();
                    dot = NODOT;
                    break;
case "-":        //减法运算
                    if(opt!=NULL&&opt!=SUB)
                    {
                        calc();
                    }
                    /*if(opt==ADD)
                    {
                        res=res+tmp;
                        tmp=0;
                    }*/
                    opt = SUB;
                    if (num != 0)    //判断操作数的个数,如果两个就做二元运算
                    res = res - tmp;
                    else
                        res = tmp;
                    num++;                    
                    tmp = 0;
                    textShow.Text = res.ToString();
                    dot = NODOT;
                    break;
 
 
                case ".":
                            // 点击了小数点
                    if (dot==HASDOT)
                    {

                        dot = HASDOT;

                        return;
                       
                    }
                    else
                    {
                        dotnum = 0;
                    }                   
                    break;
        //        case "+/-":
                                // 点击了符号运算
                 //   if(tmp!=0)
                  //  {
                  //      tmp = -tmp;
                  // //     txtShow.Text = tmp.ToString();
                   // }
                      //  dot = HASDOT;
                   //else
                  //  {
                      //  res=-res;
                        //res=tmp;
                       // txtShow.Text = res.ToString();   
                  //  }
                   // dot = NODOT;
                   // break;   
                case "NO":     //清除运算
                    res = 0;         // 记录结果数
                    tmp = 0;         // 当前输入的操作数
                    opt = NULL;         // 记录操作码
                    dot = NODOT;    // 记录是否点击了小数点
                    num = 0;        // 记录输入操作数的个数
                    dotnum = 0;        // 记录小数点部分的个数
                    textShow.Text="";                   
                    break;
                case "OFF":         //清除运算
                    res = 0;         // 记录结果数
                    tmp = 0;         // 当前输入的操作数
                    opt = NULL;         // 记录操作码
                    dot = NODOT;    // 记录是否点击了小数点
                    num = 0;        // 记录输入操作数的个数
                    dotnum = 0;        // 记录小数点部分的个数
                    textShow.Text="0.";
                    break;
            }
        }
        #endregion

                private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("
http://353926774.qzone.qq.com");
        }

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

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("
http://baidu.com");
        }

    }
}