| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 858 人关注过本帖
标题:计算器算出结果后,再按数字会加在结果后面!
只看楼主 加入收藏
biggreywolf
Rank: 1
来 自:广东梅州
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-11-8
结帖率:75%
收藏
 问题点数:0 回复次数:0 
计算器算出结果后,再按数字会加在结果后面!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器
{
    public partial class Form1 : Form
    {
        double num1, num2, result;                    // 操作数及运算结果
        bool decimalFlag = false;                      // 判断输入的是否为小数
        string myOperator;                          // 操作类型
      
        public Form1()
        {
            InitializeComponent();
        }  

        private void Form1_Load(object sender, EventArgs e)
        {
            txtReadout.Text = "0.";
            // 指定文本框的对齐方式为右对齐
            txtReadout.TextAlign = HorizontalAlignment.Right;

        }
        public void NumClick(int myNumber)
        {
            if (decimalFlag)                            // 输入小数
            {
                txtReadout.Text = txtReadout.Text + myNumber;
            }
            else
            {
                if (txtReadout.Text == "0.")        // 输入整数
                {
                    // 输入的是整数的第一个数字
                    txtReadout.Text = myNumber + ".";
                }
                else
                {
                    // 输入的不是整数的第一个数字
                    txtReadout.Text = txtReadout.Text.Substring
                        (0, txtReadout.Text.Length - 1) + myNumber + ".";
                }
            }
        }   

        private void btnAmount_Click(object sender, EventArgs e)
        {
            num2 = double.Parse(txtReadout.Text);
            switch (myOperator)
            {
                case "add":
                    result = num1 + num2;
                    txtReadout.Text = result.ToString();
                 
                    break;
                case "Sub":
                    result = num1 - num2;
                    txtReadout.Text = result.ToString();
               
                    break;
                case "Mul":
                    result = num1 * num2;
                    txtReadout.Text = result.ToString();                    
                    break;
                case "Div":
                    if (num2 == 0)
                    {
                        txtReadout.Text = "除数不能为“0”!";
                    }
                    else
                    {
                        result = num1 / num2;
                        txtReadout.Text = result.ToString();                       
                    }                  
                    break;
            }
        }
        private void btnNum1_Click_1(object sender, EventArgs e)
        {
            NumClick(1);
        }
        private void btnNum0_Click_1(object sender, EventArgs e)
        {
            NumClick(0);
        }
        private void btnNum3_Click(object sender, EventArgs e)
        {
            NumClick(3);
        }
        private void btnNum4_Click(object sender, EventArgs e)
        {
            NumClick(4);
        }
        private void btnNum2_Click(object sender, EventArgs e)
        {
            NumClick(2);
        }
        private void btnNum5_Click(object sender, EventArgs e)
        {
            NumClick(5);
        }
        private void btnNum6_Click(object sender, EventArgs e)
        {
            NumClick(6);
        }
        private void btnNum7_Click(object sender, EventArgs e)
        {
            NumClick(7);
        }
        private void btnNum8_Click(object sender, EventArgs e)
        {
            NumClick(8);
        }
        private void btnNum9_Click(object sender, EventArgs e)
        {
            NumClick(9);
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            myOperator = "add";
            num1 = double.Parse(txtReadout.Text);
            txtReadout.Text = "0.";
            decimalFlag = false;
        }
        private void btnSub_Click(object sender, EventArgs e)
        {
            myOperator = "Sub";
            num1 = double.Parse(txtReadout.Text);
            txtReadout.Text = "0.";
            decimalFlag = false;
        }
        private void btnMul_Click(object sender, EventArgs e)
        {
            myOperator = "Mul";
            num1 = double.Parse(txtReadout.Text);
            txtReadout.Text = "0.";
            decimalFlag = false;
        }
        private void btnDiv_Click(object sender, EventArgs e)
        {
            myOperator = "Div";
            num1 = double.Parse(txtReadout.Text);
            txtReadout.Text = "0.";
            decimalFlag = false;
        }
        private void btnDot_Click(object sender, EventArgs e)
        {
            decimalFlag = true;  
        }
        private void button18_Click(object sender, EventArgs e)
        {
            num1 = 0;
            num2 = 0;
            txtReadout.Text = "0.";
            decimalFlag = false;
        }
          }
} 这是我的代码,结果是可以计算出来得。但是算出结果后,再按数字键的话,这个数字会继续接在上次结果后面。例如:10+23=33 再按45 会变成3345.。哪位朋友告诉我怎么改进?PS: 我想要设置“<——”退格键,代码是什么样的?

搜索更多相关主题的帖子: 计算器 class public double result 
2011-12-15 20:33
快速回复:计算器算出结果后,再按数字会加在结果后面!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037493 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved