标题:关于简易计算器的一个小问题
取消只看楼主
shengshm1989
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-11-15
结帖率:16.67%
 问题点数:0 回复次数:2 
关于简易计算器的一个小问题
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace caculator
{
    public partial class Form1 : Form
    {
        bool DataFlag = false;
        bool OperFlag = false;
        double dataNum1 = 0;
        double dataNum2 = 0;
        int Oper_Num = 0;
        public Form1()
        {
            InitializeComponent();
            txt_show.KeyPress += new KeyPressEventHandler(txt_show_KeyPress);
          
        }
        //检验数字键
        void txt_show_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < 47 || e.KeyChar > 58)
                e.Handled = true;
            else
                e.Handled = false;

        }

       

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //backspace按键工作
        private void btn_backsapce_Click(object sender, EventArgs e)
        {
            try
            {
                this.txt_show.Text = txt_show.Text.Substring(0, txt_show.Text.Length - 1);
                dataNum1 = 0;
                dataNum2 = 0;
            }
            catch (SystemException)
            { 
            }
        }
        //C按键工作
        private void btn_C_Click(object sender, EventArgs e)
        {
            txt_show.Text = txt_show.Text.Substring(0, 0);
            dataNum1 = 0;
            dataNum2 = 0;
        }
        //数字按键
        private void btn_0_Click(object sender, EventArgs e)
        {
            DataNum_Click(0);
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            DataNum_Click(1);
        }

        private void btn_2_Click(object sender, EventArgs e)
        {
            DataNum_Click(2);
        }

        private void btn_3_Click(object sender, EventArgs e)
        {
            DataNum_Click(3);
        }

        private void btn_4_Click(object sender, EventArgs e)
        {
            DataNum_Click(4);
        }

        private void btn_5_Click(object sender, EventArgs e)
        {
            DataNum_Click(5);
        }

        private void btn_6_Click(object sender, EventArgs e)
        {
            DataNum_Click(6);
        }

        private void btn_7_Click(object sender, EventArgs e)
        {
            DataNum_Click(7);
        }

        private void btn_8_Click(object sender, EventArgs e)
        {
            DataNum_Click(8);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            DataNum_Click(9);
        }
        //组合数字
        private void DataNum_Click(int temp)
        {
            DataFlag = true;
            if (DataFlag)
            {
                if (!OperFlag)
                {
                    dataNum1 = double.Parse(txt_show.Text + temp.ToString());
                    txt_show.Text = dataNum1.ToString();
                }
                else
                {
                    dataNum2 = double.Parse(txt_show.Text+temp.ToString());
                    txt_show.Text = dataNum2.ToString();
                }
            }
        }
        //操作符处理
        private void btn_add_Click(object sender, EventArgs e)
        {   
            Oper_Num = 1;
            DataFlag = false;
            OperFlag = true;

        }

        private void btn_substract_Click(object sender, EventArgs e)
        {
            Oper_Num = 2;
            DataFlag = false;
            OperFlag = true;
        }

        private void btn_multiply_Click(object sender, EventArgs e)
        {
            Oper_Num = 3;
            DataFlag = false;
            OperFlag = true;
        }

        private void btn_divid_Click(object sender, EventArgs e)
        {
            Oper_Num = 4;
            DataFlag = false;
            OperFlag = true;
        }
        //等号求结果
        private void btn_equal_Click(object sender, EventArgs e)
        {
            if (OperFlag)
            {
                calculate(dataNum1, dataNum2, Oper_Num);
                txt_show.Text = dataNum1.ToString();
                dataNum2 = 0;
                OperFlag = false;
            }
            else
            {
                txt_show.Text = dataNum1.ToString(); 
            }
        }
        private void calculate(double data1, double data2, int oper)
        {
            int oper_num = oper;
            switch(oper_num)
            {   case 1:dataNum1=data1+data2;break;
                case 2:dataNum1=data1-data2;break;
                case 3:dataNum1=data1*data2;break;
                case 4:dataNum1=data1/data2;break;
             }

        }

        private void btn_dot_Click(object sender, EventArgs e)
        {
            char[] ch = txt_show.Text.ToCharArray();
            for (int i = 0; i < ch.Length; i++)//这里数组的长度改成字符串的长度运行的时候就会报错
            {
                if (ch[i] == '.')
                {
                    txt_show.Text = "#####";
                    this.Close();
                }
              txt_show.Text = txt_show.Text+ btn_dot.Text;//如果是输入1个数显示正确,但是输入两个以上就会输出两个点
            }
        }
    }
}

代码不完整,只有一小部分功能,现在有奇怪的是,我是输入小数点的时候,会出现两个小数点。希望大家指点下。
搜索更多相关主题的帖子: 计算器 
2014-07-09 16:08
shengshm1989
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-11-15
得分:0 
txt_show.Text = txt_show.Text+ btn_dot.Text这个不是将两个字符串连接起来的么,怎么不对了啊?
2014-07-10 17:36
shengshm1989
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-11-15
得分:0 
恩恩,我知道,可是我是点了一次小数点,文本中会出现两个小数点,如果是输入多个数字,会出现更多的小数点。不懂。
2014-07-11 13:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-433874-1-1.html




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

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