请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错,如:1+2=3,不...

  [复制链接]
查看4965 | 回复4 | 2016-7-22 11:55:13 | 显示全部楼层 |阅读模式
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 Calculator
{
    public partial class Form1 : Form
    {
        public enum CalcuType
        {
            None,
            /// <summary>
            /// 加法
            /// </summary>
            Addition,
            /// <summary>
            /// 减法
            /// </summary>
            Substraction,
            /// <summary>
            /// 乘法
            /// </summary>
            Multiplication,
            /// <summary>
            /// 除法
            /// </summary>
            Division,
            /// <summary>
            /// 乘方
            /// </summary>
            Involution,
            /// <summary>
            /// 开方
            /// </summary>
            Square,
        }

        private double? _ValueF = null;
        private double? _ValueL = null;
        private CalcuType _CalculateType = CalcuType.None;
        private bool _IsNew = false;
        private double? _StoredValue = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btnVal0.Click += new EventHandler(btnVal_Click);
            btnVal1.Click += new EventHandler(btnVal_Click);
            btnVal2.Click += new EventHandler(btnVal_Click);
            btnVal3.Click += new EventHandler(btnVal_Click);
            btnVal4.Click += new EventHandler(btnVal_Click);
            btnVal5.Click += new EventHandler(btnVal_Click);
            btnVal6.Click += new EventHandler(btnVal_Click);
            btnVal7.Click += new EventHandler(btnVal_Click);
            btnVal8.Click += new EventHandler(btnVal_Click);
            btnVal9.Click += new EventHandler(btnVal_Click);
        }

        private void btnVal_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string numberStr = this.txtValue.Text;
            if (this._IsNew)
            {
                numberStr = btn.Text;
                this._ValueL = double.Parse(numberStr);
            }
            else
            {
                if (new string[] { "0", "0.", "-0", "-0." }.Contains(numberStr))
                {
                    numberStr = "";
                }
                numberStr += btn.Text;
                this._ValueF = double.Parse(numberStr);
            }

            this.txtValue.Text = numberStr;
            this._IsNew = false;
        }
        private void btnPI_Click(object sender, EventArgs e)
        {
            if (this._IsNew)
            {
                this._ValueL = Math.PI;
            }
            else
            {
                this._ValueF = Math.PI;
            }
            this.txtValue.Text = Math.PI.ToString();
            this._IsNew = true;
        }







        private void btnResult_Click(object sender, EventArgs e)
        {
            switch (_CalculateType)
            {
                case CalcuType.Addition:
                    this.txtValue.Text = (_ValueF + _ValueL).ToString();
                    break;
                case CalcuType.Substraction:
                    this.txtValue.Text = (_ValueF - _ValueL).ToString();
                    break;
                case CalcuType.Multiplication:
                    this.txtValue.Text = (_ValueF * _ValueL).ToString();
                    break;
                case CalcuType.Division:
                    if (_ValueL != 0)
                    {

                        this.txtValue.Text = (_ValueF / _ValueL).ToString();

                    }

                    else
                    {
                        MessageBox.Show("除数不能为0");

                    }
                    break;
                case CalcuType.Involution:
                    this.txtValue.Text = Math.Pow((double)_ValueF, (double)_ValueL).ToString();
                    break;
                case CalcuType.Square:
                    this.txtValue.Text = Math.Pow((double)_ValueF, 1 / (double)_ValueL).ToString();
                    break;



            }
            this._ValueF = double.Parse(this.txtValue.Text);
        }

        private void btnClears_Click(object sender, EventArgs e)
        {
            this._ValueL = null;
            this._ValueF = null;
            this._CalculateType = CalcuType.None;
            this.txtValue.Text = "0.";
        }

        private void btnAddition_Click(object sender, EventArgs e)
        {
            this.btnResult_Click(sender, e);
            this._CalculateType = CalcuType.Addition;
            this._IsNew = true;
        }
        private void btnSubstraction_Click(object sender, EventArgs e)
        {
            this.btnResult_Click(sender, e);
            this._CalculateType = CalcuType.Substraction;
            this._IsNew = true;
        }

        private void btnMultiplication_Click(object sender, EventArgs e)
        {
            this.btnResult_Click(sender, e);
            this._CalculateType = CalcuType.Multiplication;
            this._IsNew = true;
        }

        private void btnDivision_Click(object sender, EventArgs e)
        {
            this.btnResult_Click(sender, e);
            this._CalculateType = CalcuType.Division;
            this._IsNew = true;
        }

        private void btnSquare_Click(object sender, EventArgs e)
        {

            this.btnResult_Click(sender, e);
            this._CalculateType = CalcuType.Square;
            this._IsNew = true;

        }

        private void btnInvolution_Click(object sender, EventArgs e)
        {
            this.btnResult_Click(sender, e);
            this._CalculateType = CalcuType.Involution;
            this._IsNew = true;

        }

        private void btnBackspace_Click(object sender, EventArgs e)
        {
            if (this.txtValue.Text.Length == 1)
            {
                this.txtValue.Text = "0";
            }
            else
            {
                this.txtValue.Text = txtValue.Text.Substring(0, txtValue.Text.Length - 1);

            }

        }

        private void btnMC_Click(object sender, EventArgs e)
        {
            this._StoredValue = null;
            this.lblM.Text = "";
        }

        private void btnMR_Click(object sender, EventArgs e)
        {
            if (this._IsNew)
            {
                this._ValueL = this._StoredValue;
            }
            else
            {
                this._ValueF = this._StoredValue;
            }
            this.txtValue.Text = this._StoredValue.ToString();
            this._IsNew = true;
        }

        private void btnMS_Click(object sender, EventArgs e)
        {
            try
            {
                this._StoredValue = double.Parse(this.txtValue.Text);
                this.lblM.Text = "M";
            }
            catch (Exception)
            {
            }
        }

        private void btnMPlus_Click(object sender, EventArgs e)
        {
             try
             {
                 if (this._StoredValue == null)
                 {
                     this._StoredValue = double.Parse(this.txtValue.Text);
                 }
                 else
                 {
                  this._StoredValue += double.Parse(this.txtValue.Text);
                }
             }
            catch(Exception)
            {
        }
        }

        private void btnDot_Click(object sender, EventArgs e)
        {
            if(txtValue.Text=="")
               txtValue.Text="0.";
            else
               txtValue.Text=txtValue.Text+".";
        }
    }
}


月魂雪 | 2016-7-22 11:57:46 | 显示全部楼层
c#开发的计算器,第一次运算得出结果后,在等号之后继续运算会出错,如:1+2=3,不清除,再点乘号就变成5了,再点3,点等号就等于15了,本来正确结果应该是9,谁帮忙看下怎么改。详细点感激不尽啊
ibcadmin | 2016-7-24 19:26:24 | 显示全部楼层
我来了...周一给解决  没上班 休息时间..
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
剑弑 | 2016-7-25 08:31:04 | 显示全部楼层
      在点乘之前你没有把CalcuType先更改成 Multiplication,所以你代码的运行顺序是1+2+2*3;还有你+、-*、/的运算不应该这样写,你这样写每一次都会冲突。你的想法是把输入框里面的数字先存到Value,但你没有考虑到进行一次运算后要进行对Value的清空重新赋值。



(下次能直接截图?这样真的行不好了解你的代码
      
ibcadmin | 2016-7-25 09:41:41 | 显示全部楼层
看完了  你的_ValueL 没有清空
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则