| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1680 人关注过本帖
标题:c#新手计算器求助!!!
只看楼主 加入收藏
伍嚎
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-11
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:19 
c#新手计算器求助!!!
这是我用窗体编的计算器代码,计算结果都不对,求解释,有附件(只编到计算的...)
ex4.rar (38.16 KB)

namespace ex4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            textBox1.Text = textBox1.Text + btn.Text;
            
        }
        string a ;
        string f;
        int num1;

        private void button14_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            a = textBox1.Text;
            num1 = Convert.ToInt32(a);
            textBox1.Text = "";
            f = btn.Text;
            


        }
        
        private void button18_Click(object sender, EventArgs e)
        {
            if (f == "+")
            {
                int c = num1 + Convert.ToInt32(a);
                textBox1.Text = Convert.ToString(c);
            }
            else if (f == "-")
            {
                int c = num1 - Convert.ToInt32(a);
                textBox1.Text = Convert.ToString(c);
            }
            else if (f == "*")
            {
                int c = num1 * Convert.ToInt32(a);
                textBox1.Text = Convert.ToString(c);
            }
            else if (f == "/")
            {
                int c = num1 / Convert.ToInt32(a);
                textBox1.Text = Convert.ToString(c);
            }

        }  
    }
}
搜索更多相关主题的帖子: 计算器 
2011-04-28 23:39
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
你在加两个相同的数,没有一点逻辑!

   唯实惟新 至诚致志
2011-04-29 07:28
adlyy
Rank: 2
等 级:论坛游民
帖 子:14
专家分:12
注 册:2011-3-25
收藏
得分:0 
的确乱的很!!
2011-04-29 13:33
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
好好学习后再做吧!你的基础不太好!

   唯实惟新 至诚致志
2011-04-29 19:21
伍嚎
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-11
收藏
得分:0 
回复 4楼 qq1023569223
木有办法啊,刚学个皮毛老师就叫做计算器了......
2011-04-30 17:43
Nik2011
Rank: 2
等 级:论坛游民
帖 子:23
专家分:37
注 册:2011-4-28
收藏
得分:20 
我这里有一个例子,不过写的比较繁琐,仅供参考
程序代码:
 public partial class Form1 : Form
    {
        public double dblFirst = 0;
        public double dblSecond = 0;
        public bool blnFirstOpen = true;
        public bool blnClear = false;
        public string strOper;
        public Form1()
        {
            InitializeComponent();
        }

        private void button0_Click_1(object sender, EventArgs e)
        {
            if (sender == button0) append_num(0);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (sender == button4) append_num(1);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (sender == button8) append_num(2);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (sender == button12) append_num(3);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (sender == button3) append_num(4);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (sender == button7) append_num(5);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (sender == button11) append_num(6);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (sender == button2) append_num(7);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (sender == button6) append_num(8);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (sender == button10) append_num(9);
        }
           
        private void append_num(int i)
            {
                if(blnClear)
                {
                    if(blnFirstOpen==true)
                        dblFirst=Convert.ToDouble(textBox1.Text);
                    else
                        dblSecond=Convert.ToDouble(textBox1.Text);
                    textBox1.Text="";
                    blnClear=false;
                }
                if(((i==0)&&(textBox1.Text!=""))||(i!=0))
                    textBox1.Text=textBox1.Text+i.ToString();
            }

           
        
            private void btn_equ_Click_1(object sender, EventArgs e)//等号的处理
            {
                if (textBox1.Text != "")
                    if (blnFirstOpen == true)
                        dblFirst = Convert.ToDouble(textBox1.Text);
                    else
                        dblSecond = Convert.ToDouble(textBox1.Text);
                switch (strOper)
                {
                    case "+":
                        dblFirst += dblSecond;
                        break;
                    case "-":
                        dblFirst -= dblSecond;
                        break;
                    case "*":
                        dblFirst *= dblSecond;
                        break;
                    case "/":
                        dblFirst /= dblSecond;
                        break;
                }

                strOper = "=";
                blnFirstOpen = false;
                textBox1.Text = Convert.ToString(dblFirst);
                blnClear = true;
            }
            private void btn_clear_Click(object sender, EventArgs e)//清空文本框
            {
                dblFirst = 0;
                dblSecond = 0;
                blnFirstOpen = true;
                blnClear = false;
                textBox1.Text = "";
                textBox1.Focus();
            }

            private void btn_dot_Click_1(object sender, EventArgs e)//小数点的处理办法
            {
                if (textBox1.Text == "")
                    textBox1.Text = "0.";
                else if(textBox1.Text=="0..")
                    MessageBox.Show("小数不能函数两个小数点!");
                else
                    textBox1.Text = textBox1.Text + ".";
                blnClear = false;
            }

            private void btn_plus_Click_1(object sender, EventArgs e)//加法的实现函数
            {
                if (textBox1.Text != "")
                    if (blnFirstOpen == true)
                        dblFirst = Convert.ToDouble(textBox1.Text);
                    else
                        dblSecond = Convert.ToDouble(textBox1.Text);
               
                if (sender == btn_plus)
                    strOper = "+";
                else if (sender == btn_minus)
                    strOper = "-";
                if (sender == btn_mul)
                    strOper = "*";
                if (sender == btn_div)
                    strOper = "/";
                blnFirstOpen = false;
                textBox1.Text = Convert.ToString(dblFirst);
                blnClear = true;
            }

            private void btn_minus_Click(object sender, EventArgs e)//减法的实现函数
            {
                if (textBox1.Text != "")
                    if (blnFirstOpen == true)
                        dblFirst = Convert.ToDouble(textBox1.Text);
                    else
                        dblSecond = Convert.ToDouble(textBox1.Text);
               
                if (sender == btn_plus)
                    strOper = "+";
                else if (sender == btn_minus)
                    strOper = "-";
                if (sender == btn_mul)
                    strOper = "*";
                if (sender == btn_div)
                    strOper = "/";
                blnFirstOpen = false;
                textBox1.Text = Convert.ToString(dblFirst);
                blnClear = true;
            }

            private void btn_mul_Click(object sender, EventArgs e)//乘法的实现函数
            {
                if (textBox1.Text != "")
                    if (blnFirstOpen == true)
                        dblFirst = Convert.ToDouble(textBox1.Text);
                    else
                        dblSecond = Convert.ToDouble(textBox1.Text);
               
                if (sender == btn_plus)
                    strOper = "+";
                else if (sender == btn_minus)
                    strOper = "-";
                if (sender == btn_mul)
                    strOper = "*";
                if (sender == btn_div)
                    strOper = "/";
                blnFirstOpen = false;
                textBox1.Text = Convert.ToString(dblFirst);
                blnClear = true;
            }

            private void btn_div_Click(object sender, EventArgs e)//除法的实现
            {
                if (textBox1.Text != "")
                    if (blnFirstOpen == true)
                        dblFirst = Convert.ToDouble(textBox1.Text);
                    else
                        dblSecond = Convert.ToDouble(textBox1.Text);
               
                if (sender == btn_plus)
                    strOper = "+";
                else if (sender == btn_minus)
                    strOper = "-";
                if (sender == btn_mul)
                    strOper = "*";
                if (sender == btn_div)
                    strOper = "/";
                blnFirstOpen = false;
                textBox1.Text = Convert.ToString(dblFirst);
                blnClear = true;
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }        

           

          
          
           
        }
2011-05-01 00:19
永希
Rank: 2
等 级:论坛游民
帖 子:12
专家分:15
注 册:2011-4-12
收藏
得分:0 
求大神带
2011-05-01 21:33
永希
Rank: 2
等 级:论坛游民
帖 子:12
专家分:15
注 册:2011-4-12
收藏
得分:0 
求大神带
2011-05-01 21:33
Snow_Pu
Rank: 2
等 级:论坛游民
帖 子:27
专家分:45
注 册:2011-5-11
收藏
得分:0 
回复 6楼 Nik2011
想问你的能实现不按=,按运算符也能出结果,比如说6+1,不按等号,再按+号,能显示出7来吗?
2011-05-12 15:23
伍嚎
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-4-11
收藏
得分:0 
回复 9楼 Snow_Pu
不行,也在为这个发愁呢.....
2011-05-13 21:43
快速回复:c#新手计算器求助!!!
数据加载中...
 
   



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

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