| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2528 人关注过本帖
标题:C#计算器窗体程序
只看楼主 加入收藏
春暖花开mi
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-3-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
C#计算器窗体程序
我用C#做了一个计算器窗体程序,做好窗体后对应写了如下代码,但实现不了计算,请各位大师帮忙看看,谢谢!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        //(1)定义窗体的公共变量
        string str, opp, opp1;
        double num1, num2, result;
        //(2)编写“数字键”的单击事件代码
        private void number(object sender, EventArgs e)
        {
            Button b = (Button)(sender);
            str = b.Text;
            if (textBox1.Text == "0")
            {
                textBox1.Text = str;
            }
            else
                textBox1.Text = textBox1.Text + str;
        }
        //(3)编写“+ 、-、*、/”操作符键的单击事件代码

        private void operational(object sender, EventArgs e)
        {
            Button b = (Button)(sender);
            if (b.Text == "+")
            {
                num1 = double.Parse(textBox1.Text);
                textBox1.Text = "";
                opp = "+";
                opp1 = "";
            }
            else if (b.Text == "-")
            {
                num1 = double.Parse(textBox1.Text);
                textBox1.Text = "";
                opp = "-";
                opp1 = "";
            }
            else if (b.Text == "*")
            {
                num1 = double.Parse(textBox1.Text);
                textBox1.Text = "";
                opp = "*";
                opp1 = "";
            }
            else if (b.Text == "/")
            {
                num1 = double.Parse(textBox1.Text);
                textBox1.Text = "";
                opp = "/";
                opp1 = "";
            }
            else if (b.Text == "=")
            {
                if (opp1 != "=")
                {
                    num2 = double.Parse(textBox1.Text);
                }
                if (opp == "+")
                {
                    num1 = num1 + num2;
                    textBox1.Text = "" + num1.ToString();
                }
                else if (opp == "-")
                {
                    num1 = num1 - num2;
                    textBox1.Text = "" + num1.ToString();
                }
                else if (opp == "*")
                {
                    num1 = num1 * num2;
                    textBox1.Text = "" + num1.ToString();
                }
                else if (opp == "/")
                {
                    if (num2 == 0)
                    {
                        textBox1.Text = "除数不能为零";
                    }
                    else
                    {
                        num1 = num1 / num2;
                        textBox1.Text = "" + num1.ToString();
                    }
                }
                opp1 = "=";
            }
        }
        //(4)编写操作符键“退格<--、CE、C、sqrt、%、1/x、+/-、.”等按钮的单击事件代码
        private void operation(object sender, EventArgs e)
        {
            Button b = (Button)(sender);
            if (b.Text == "+/-")
            {
                num1 = double.Parse(textBox1.Text);
                result = num1 * (-1);
                textBox1.Text = result.ToString();
            }
            else if (b.Text == ".")
            {
                str = textBox1.Text;
                int index = str.IndexOf(".");
                if (index == -1)
                {
                    textBox1.Text = str + ".";
                }
            }
            else if (b.Text == "退格<--")
            {
                if (textBox1.Text != "")
                {
                    str = textBox1.Text;
                    str = str.Substring(0, str.Length - 1);
                    textBox1.Text = str;
                }
            }
            else if (b.Text == "CE")
            {
                textBox1.Text = "0";
            }
            else if (b.Text == "C")
            {
                result = num1 = num2 = 0;
                str = null;
                opp = null;
                textBox1.Text = "0";
            }
            else if (b.Text == "sqrt")
            {
                num1 = double.Parse(textBox1.Text);
                result = Math.Sqrt(num1);
                textBox1.Text = result.ToString();
            }
            else if (b.Text == "1/x")
            {
                num1 = double.Parse(textBox1.Text);
                result = 1 / num1;
                textBox1.Text = result.ToString();
            }
            else if (b.Text == "%")
            {
                num1 = double.Parse(textBox1.Text);
                result = num1 / 100;
                textBox1.Text = result.ToString();
            }
            opp1 = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            
        }
    }
}
搜索更多相关主题的帖子: 计算器 public 
2017-03-13 21:32
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:10 
是因为窗体按钮没有绑定事件吗,或是你贴出来的代码只是核心代码,并没有把写的所有代码贴出来
2017-03-14 21:33
q8021125
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2017-3-20
收藏
得分:10 
楼上正解
2017-03-20 10:44
快速回复:C#计算器窗体程序
数据加载中...
 
   



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

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