标题:c#新手计算器求助!!!
只看楼主
Snow_Pu
Rank: 2
等 级:论坛游民
帖 子:27
专家分:45
注 册:2011-5-11
得分:0 
回复 10楼 伍嚎
我刚也做了一个 你看看
Calculator20110513.rar (40.48 KB)
2011-05-14 17:24
Chinazhan530
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-5-14
得分:0 
仍需努力啊...
2011-05-14 21:44
chenweijun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-25
得分:0 
我也是新手,textBox1.Text = textBox1.Text + btn.Text;可以这样进行运算吗?
2011-05-16 12:40
Snow_Pu
Rank: 2
等 级:论坛游民
帖 子:27
专家分:45
注 册:2011-5-11
得分:0 
回复 13楼 chenweijun
可以的~而且用+=就可以
2011-05-16 15:51
緣來④伱
Rank: 2
来 自:河南商丘
等 级:论坛游民
帖 子:35
专家分:37
注 册:2011-3-9
得分:0 
我也正在写这个,第二次输入运算符出结果还在调试中
2011-05-16 21:26
moridiansha
Rank: 6Rank: 6
来 自:承德
等 级:侠之大者
威 望:4
帖 子:254
专家分:417
注 册:2009-10-21
得分:0 
我没看你的程序,不过你的代码挺乱的,最好把按钮的名字都改了。还有最后的连着一堆if的可以用catch  case
你可以到百度搜 《大话设计模式》 第一个就是讲的计算器

www.qysy.tk
2011-05-17 07:17
Nik2011
Rank: 2
等 级:论坛游民
帖 子:23
专家分:37
注 册:2011-4-28
得分:0 
回复 9楼 Snow_Pu
不能,我也在想
2011-05-22 16:14
xhmlwaf
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-6-4
得分:0 
回复 10楼 伍嚎
我的思路:
定义一个链表, 按照 链表第一个位置放第一个操作数 ,第二个位置放操作符,第三个位置放第二个操作数。

然后处理按钮的事件。

我只说 + 的 处理吧,当点到加号。
链表为空 ,不处理
一个元素 ,则把链表第二个位置 为+
两个元素,则把链表第二个位置 为+ (操作符覆盖)
三个元素,就是说两个操作数和操作符都有了,那么点加号就可以先将前面的算出来,并且显示。

其他的按钮事件处理都是这个思路。
2011-06-07 12:33
xp白马飞马
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-8-7
得分: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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void kong()
        {
            if (t.Text == "0" || shu == "+" || shu == "-" || shu == "*" || shu == "/" || shu == "=" || shu == "NO")
            {
                t.Text = "";
            }
        }

        //1
        private void b1_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "1";
                if (shu == "1")
                {
                    t.Text = t.Text + 1;
                }
            }
        }
        //3
        private void b3_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "3";
                if (shu == "3")
                {
                    t.Text = t.Text + 3;
                }
            }
        }
        //6
        private void b6_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "6";
                if (shu == "6")
                {
                    t.Text = t.Text + 6;
                }
            }
        }
        //5
        private void b5_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "5";
                if (shu == "5")
                {
                    t.Text = t.Text + 5;
                }
            }
        }
        //4
        private void b4_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "4";
                if (shu == "4")
                {
                    t.Text = t.Text + 4;
                }
            }
        }
        //7
        private void b7_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "7";
                if (shu == "7")
                {
                    t.Text = t.Text + 7;
                }
            }
        }
        //8
        private void b8_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "8";
                if (shu == "8")
                {
                    t.Text = t.Text + 8;
                }
            }
        }
        //9
        private void b9_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "9";
                if (shu == "9")
                {
                    t.Text = t.Text + 9;
                }
            }
        }
        //0
        private void b0_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "0";
                if (shu == "0")
                {
                    t.Text = t.Text + 0;
                }
            }
        }

        decimal i = 0;
        string shu = "";
        string huhao = "";
        string myhuhao = "";
        //jia 加
         private void jia_Click(object sender, EventArgs e)
        {
            decimal a = 0;
            a = a + i;
            if (myhuhao == "+")
             {
                 if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                 {
                     myhuhao = "+";
                 }
                 else
                 {
                     return;
                 }
             }
            bool aa = decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                if (a == 0)
                {
                    t.Text = t.Text;
                    if (myhuhao == "*")
                    {
                        t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                    }
                }
                else
                {
                    if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                    {
                        if (myhuhao == "+")
                        {
                            t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "-")
                        {
                            t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "*")
                        {
                            t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "/")
                        {
                            if (t.Text == "0")
                            {
                                MessageBox.Show("除数不为0");
                            }
                            else
                            {
                                t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("只可以输入数字");
            }
            decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                shu = "+";
                huhao = "+";
                myhuhao = "+";
                dian = "off";
            }                                                                           
        }

        //jian 减
        private void jian_Click(object sender, EventArgs e)
          {
            decimal a = 0;
            a = a + i;
            if (myhuhao == "-")
            {
                if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                {
                    myhuhao = "-";
                }
                else
                {
                    return;
                }
            }
            bool aa = decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                if (a == 0)
                {
                    t.Text = t.Text;
                    if (myhuhao == "*")
                    {
                        t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                    }
                }
                else
                {
                    if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                    {
                        if (myhuhao == "+")
                        {
                            t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "-")
                        {
                            t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "*")
                        {
                            t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "/")
                        {
                            if (t.Text == "0")
                            {
                                MessageBox.Show("除数不为0");
                            }
                            else
                            {
                                t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("只可以输入数字");
            }
            decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                shu = "-";
                huhao = "-";
                myhuhao = "-";
                dian = "off";
            }
        }
        //cheng 乘
        private void cheng_Click(object sender, EventArgs e)
        {
            decimal a = 0;
            a = a + i;
            if (myhuhao == "*")
            {
                if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                {
                    myhuhao = "*";
                }
                else
                {
                    return;
                }
            }
            bool aa = decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                if (a == 0)
                {
                    t.Text = t.Text;
                    if (myhuhao == "*")
                    {
                        t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                    }
                }
                else
                {
                    if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                    {
                        if (myhuhao == "+")
                        {
                            t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "-")
                        {
                            t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "*")
                        {
                            t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "/")
                        {
                            if (t.Text == "0")
                            {
                                MessageBox.Show("除数不为0");
                            }
                            else
                            {
                                t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("只可以输入数字");
            }
            decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                shu = "*";
                huhao = "*";
                myhuhao = "*";
                dian = "off";
            }
        }
        //chu 除
        private void chu_Click(object sender, EventArgs e)
        {
            decimal a = 0;
            a = a + i;
            if (myhuhao == "/")
            {
                if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                {
                    myhuhao = "/";
                }
                else
                {
                    return;
                }
            }
            bool aa = decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                if (a == 0)
                {
                    t.Text = t.Text;
                    if (myhuhao == "*")
                    {
                        t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                    }
                }
                else
                {
                    if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
                    {
                        if (myhuhao == "+")
                        {
                            t.Text = (a + Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "-")
                        {
                            t.Text = (a - Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "*")
                        {
                            t.Text = (a * Convert.ToDecimal(t.Text)).ToString();
                        }
                        else if (myhuhao == "/")
                        {
                            if (t.Text == "0")
                            {
                                MessageBox.Show("除数不为0");
                            }
                            else
                            {
                                t.Text = (a / Convert.ToDecimal(t.Text)).ToString();
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("只可以输入数字");
            }
            decimal.TryParse(t.Text, out i);
            if (aa == true)
            {
                shu = "/";
                huhao = "/";
                myhuhao = "/";
                dian = "off";
            }
        }
        //deng 等
        private void deng_Click(object sender, EventArgs e)
        {
            if (shu == "1" || shu == "2" || shu == "3" || shu == "4" || shu == "5" || shu == "6" || shu == "7" || shu == "8" || shu == "9" || shu == "0")
            {
                myhuhao = "=";
            }
            else
            {
                return;
            }
            if (huhao == "+")
            {
                t.Text = (i + Convert.ToDecimal(t.Text)).ToString();
            }
            else if (huhao == "-")
            {
                t.Text = (i - Convert.ToDecimal(t.Text)).ToString();
            }
            else if (huhao == "*")
            {
                t.Text = (i * Convert.ToDecimal(t.Text)).ToString();
            }
            else if (huhao == "/")
            {
                if (t.Text == "0")
                {
                    MessageBox.Show("除数不为0");
                    dian = "NO";
                    myhuhao = "NO";
                    t.Text = "0";
                    i = 0;
                    huhao = "off";
                    return;
                }
                else
                {
                    t.Text = (i / Convert.ToDecimal(t.Text)).ToString();
                }
            }
            myhuhao = "=";
            shu = "NO";
            dian = "off";
        }

        //2
        private void b2_Click(object sender, EventArgs e)
        {
            dian = "NO";
            if (myhuhao == "=")
            {
                MessageBox.Show("请选 + - * / 在按数字");
            }
            else
            {
                kong();
                shu = "2";

                if (shu == "2")
                {
                    t.Text = t.Text + 2;
                }
            }
        }

        //C
        private void C_Click(object sender, EventArgs e)
        {
            dian = "NO";
            myhuhao = "NO";
            t.Text = "0";
            i = 0;
            huhao = "off";
        }

        //点

        string dian = "";

        private void bu1_Click(object sender, EventArgs e)
        {
            if (dian == "off")
            {
                MessageBox.Show("按了 + - * / 不可以按<点>请选按数字,再按点.如:0.1等...");
            }
            else
            {
                //t.Text = ".";
                if (t.Text == "")
                {
                    //t.Text = "请输入数字";
                    return;
                }
                if (t.Text.IndexOf('.') == -1)
                {
                    //t.text.indexof('.') 是查找字符串t中,字符"." 的位置  
                    // 如果t中没有包含 .那么会返回 -1

                    //找不到
                    t.Text = t.Text + ".";
                }
                else
                {
                    //找到. 已存在一个.
                    return;
                }
            }
        }
    }
}
2011-08-08 11:17
xp白马飞马
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2011-8-7
得分:0 
可以不接等号连加。减。乘。除的可以不接等号连加。减。乘。除的
2011-08-08 11:21



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




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

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