| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8246 人关注过本帖, 1 人收藏
标题:C#计算器连按等号实现连加功能?
取消只看楼主 加入收藏
mosiying
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-6
结帖率:50%
收藏(1)
已结贴  问题点数:20 回复次数:1 
C#计算器连按等号实现连加功能?
大家好,我想用C#语言写一个计算器的程序,实现如下功能:第一次输入2+1= 得到结果3,之后按等号就可以一直执行+1操作,显示结果4,5,6……
                                                      如果输入的是2+3= 得到结果5,之后按等号就可以一直执行+3操作,显示结果8,11,14……
以下是已经写好的没有连加功能的程序,该如何改动呢?
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        double sum = 0;//记录部分和
        bool blnClear = false; //表示如果再输入数字或小数点,先清除编辑框中显示的前一个加数
        string strOper = "+"; //记录输入的运算符
        public   Form1()
        {
            InitializeComponent();
        }
        private void button10_Click(object sender, EventArgs e) //0数字键
        {
            if (blnClear) //如果为真,输入下一个加数前,先清除textBox1显示内容
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1=(Button )sender ;
            if (textBox1.Text != "0") //如果前面已经输入非零数字,如:12
                textBox1.Text += b1.Text; //此时键入0,应为120
            else
                textBox1.Text = b1.Text; //如已输入的数字为0,显示的数字应为0
        }
        private void button1_Click(object sender, EventArgs e) //1数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button2_Click(object sender, EventArgs e) //2数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button3_Click(object sender, EventArgs e) //3数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button4_Click(object sender, EventArgs e) //4数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button5_Click(object sender, EventArgs e) //5数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button6_Click(object sender, EventArgs e) //6数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button7_Click(object sender, EventArgs e) //7数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button8_Click(object sender, EventArgs e) //8数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button9_Click(object sender, EventArgs e) //9数字键
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            Button b1 = (Button)sender;
            if (textBox1.Text != "0")
                textBox1.Text += b1.Text;
            else
                textBox1.Text = b1.Text;
        }
        private void button11_Click(object sender, EventArgs e) //小数点的代码
        {
            if (blnClear)
            {
                textBox1.Text = "0";
                blnClear = false;
            }
            int n = textBox1.Text.IndexOf(".");
            if (n == -1) //如果没有小数点,增加小数点,否则不增加
                textBox1.Text = textBox1.Text + ".";
        }
        private void button13_Click(object sender, EventArgs e) //加号的代码
        {
               double dbSencond = Convert.ToDouble(textBox1 .Text );
            if (!blnClear) //如果未输入第二个操作符,不运算,例如连续输入+-*/或=
                switch (strOper) //按上次记录的运算符号运算
                {
                    case "+":
                        sum += dbSencond;
                        break;
                    case "-":
                        sum -= dbSencond;
                        break;
                    case "*":
                        sum *= dbSencond;
                        break;
                    case "/":
                        sum /= dbSencond;
                        break;

                }
            if (sender == button13) //记录此次键入的运算符
                strOper = "+";
            if (sender == button12)
                strOper = "=";
            textBox1.Text = Convert.ToString(sum ); // 显示部分和
            blnClear = true; // 设置标记使键入另一个加数前,先清除显示的前个加数
        }
        private void button12_Click(object sender, EventArgs e) //等号的代码
        {
            
            double dbSencond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSencond ;
                        break;
                    case "-":
                        sum -= dbSencond;
                        break;
                    case "*":
                        sum *= dbSencond;
                        break;
                    case "/":
                        sum /= dbSencond;
                        break;
                }
            if (sender == button13)
                strOper = "+";
            if (sender == button12)
            {
                strOper = "=";
               
            }
            
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
           
        }
        private void button14_Click(object sender, EventArgs e) //减号的代码
        {
            double dbSencond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSencond;
                        break;
                    case "-":
                        sum -= dbSencond;
                        break;
                    case "*":
                        sum *= dbSencond;
                        break;
                    case "/":
                        sum /= dbSencond;
                        break;
                }
            if (sender == button14)
                strOper = "-";
            if (sender == button12)
                strOper = "=";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }
        private void button15_Click(object sender, EventArgs e) //乘号的代码
        {
            double dbSencond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSencond;
                        break;
                    case "-":
                        sum -= dbSencond;
                        break;
                    case "*":
                        sum *= dbSencond;
                        break;
                    case "/":
                        sum /= dbSencond;
                        break;
                }
            if (sender == button15)
                strOper = "*";
            if (sender == button12)
                strOper = "=";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }
        private void button16_Click(object sender, EventArgs e) //除号的代码
        {
            double dbSencond = Convert.ToDouble(textBox1.Text);
            if (!blnClear)
                switch (strOper)
                {
                    case "+":
                        sum += dbSencond;
                        break;
                    case "-":
                        sum -= dbSencond;
                        break;
                    case "*":
                        sum *= dbSencond;
                        break;
                    case "/":
                        sum /= dbSencond;
                        break;
                }
            if (sender == button16)
                strOper = "/";
            if (sender == button12)
                strOper = "=";
            textBox1.Text = Convert.ToString(sum);
            blnClear = true;
        }
        private void button17_Click(object sender, EventArgs e) //清除键的代码
        {
            textBox1.Text = "0";
            sum = 0;
            blnClear = false;
            strOper = "+";
        }  
  }
}

[ 本帖最后由 mosiying 于 2011-4-6 21:53 编辑 ]
搜索更多相关主题的帖子: 计算器 
2011-04-06 21:52
mosiying
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-6
收藏
得分:0 
回复 楼主 mosiying
等号的运算循环怎么写啊?还是不会啊~~那个number是不是之前算出来的sum啊?好晕啊~
2011-04-07 10:36
快速回复:C#计算器连按等号实现连加功能?
数据加载中...
 
   



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

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