| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8246 人关注过本帖, 1 人收藏
标题:C#计算器连按等号实现连加功能?
只看楼主 加入收藏
mosiying
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-6
结帖率:50%
收藏(1)
已结贴  问题点数:20 回复次数:8 
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
lwsfyts
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:132
注 册:2011-4-3
收藏
得分:5 
增加三个变量: private bool isLoop;//控制=操作是否循环 private bool isOperator//是否连续运算 private double number//记录=前的数
=方法{
this.isOerator=false;
if(!isLoop)
{
//计算number...
}
isLoop=true;
//添加=号的运算循环}
同理修改下运算下的;

 

[ 本帖最后由 lwsfyts 于 2011-4-7 08:21 编辑 ]
2011-04-07 08:20
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:5 
要是我写的话,就用两个变量记忆两个操作数,按+输出结果。如果再按+的话,第一个操作数更新为前面两个操作数之和,接下来输入的值给第二个操作数。当然这其中要有一个标志判断是否是第一次按+号键了!

   唯实惟新 至诚致志
2011-04-07 08:34
mosiying
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-6
收藏
得分:0 
回复 楼主 mosiying
等号的运算循环怎么写啊?还是不会啊~~那个number是不是之前算出来的sum啊?好晕啊~
2011-04-07 10:36
lwsfyts
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:132
注 册:2011-4-3
收藏
得分:0 
{
this.isOperator = false;//把运算循环停止
if (!isLoop)//如果是第一次按了等号
{ //先读取=号的number  }   
isLoop = true;//
//根据记录的运算符求得结果(+=/-=/...)number;
//显示结果
}           
2011-04-07 15:31
one_june
Rank: 4
等 级:业余侠客
帖 子:139
专家分:212
注 册:2010-3-14
收藏
得分:5 
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cale
{
    public partial class Form1 : Form
    {
        int x = 0;
        int y = 0;
        string runType = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

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

        private void btn_1_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            this.textBox1.Text+=   b.Text;
           //Button c= sender as Button;
        }

        private void but_add_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            this.runType = b.Text;
            this.x = int.Parse(this.textBox1.Text);
            this.textBox1.Clear();
        }

        private void btn_out_Click(object sender, EventArgs e)
        {
            this.y = int.Parse(this.textBox1.Text);
            if (this.runType == "+")
            {
                this.textBox1.Text = (this.x + this.y).ToString();
            }
            else if (this.runType == "-")
            {
                this.textBox1.Text = (this.x - this.y).ToString();
            }
            else if (this.runType == "*")
            {
                this.textBox1.Text = (this.x * this.y).ToString();
            }
            else if (this.runType == "/")
            {
                this.textBox1.Text = (this.x / this.y).ToString();
            }
        }


        private void btn_clear_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
        }
    }
}


选中0-9,写一个Click事件多简单 !

[ 本帖最后由 one_june 于 2011-4-7 15:57 编辑 ]
2011-04-07 15:55
lwsfyts
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:132
注 册:2011-4-3
收藏
得分:0 
楼上这个代码还要进一步完善:1).小数2).连续运算2+3+5..
3)=循环3+5=8=13..4)按0再按0..判断...
2011-04-07 21:16
domon
Rank: 3Rank: 3
来 自:河南
等 级:论坛游侠
帖 子:92
专家分:191
注 册:2009-10-26
收藏
得分:5 
你是不是想实现 按一下1+1=2  然后连续按=号 连续算出结果 2 3 4 5 。。。 ?
写一个两个数相加的方法 double A(dobule x,dobule y)用来返回相加结果
给每一个按钮加标示,标示记录前两个计算符号按钮按钮,比如:如果按下1+1,假设+的标示为+,那么标示字段里为“+”
在=号按钮事件内判断标示,如果标示最后一个不为=,假设为“+”,把标示改为“+=”,调用A(1,1)
若标示最后一位为=  则不改变标示,直接A(sum,1),sum为上一次计算出的值

希望,是走出来的!!!
2011-04-08 12:46
ww714826314
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-7-21
收藏
得分:0 
回复 3 楼 qq1023569223
我在做一个计算器,请问连加的代码怎么写,比如1+2+3+4
2014-07-21 09:40
快速回复:C#计算器连按等号实现连加功能?
数据加载中...
 
   



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

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