| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1430 人关注过本帖
标题:C#的加减乘除的问题
取消只看楼主 加入收藏
武魂杰少
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-5-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
C#的加减乘除的问题
、为什么四则运算没有反应呢
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace calc
{
    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            InitializeComponent();
        }

        bool blnClear = true;
        double dblSec=0;
        String Op1, Op2;
        //预先输入操作数
        //小数点存在吗?
        int NumOps;
        //操作数个数
        String LastInput;
        //指示上一次按键事件的类型
        int OpFlag;
        //指示未完成的操作
        String TempReadout;

        private void btn_Click(object sender, EventArgs e)
        {
            if (blnClear)
                txtRest.Text = "";
            Button b3 = (Button)sender;
            txtRest.Text += b3.Text;
            if (txtRest.Text == ".")
                txtRest.Text = "0.";
            dblSec = Convert.ToDouble(txtRest.Text);
            blnClear = false;
        }

        private void MainForm1_Load(object sender, EventArgs e)
        {
            if (txtRest.Text == "")
            {
                txtRest.Text = "0.";
            }
            
            
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            txtRest.Text = "0.";

        }

        private void Key_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        public void OperatorCal(int MyOperator)
        {
            //取得操作代号
            int Index = MyOperator;
            //取得文本框文本
            TempReadout = txtRest.Text;
            //设置文本框最后一次输入类型
            if (LastInput == "NUMS")
            {
                NumOps = NumOps + 1;
                //将操作数加一
            }
            String Myop;
            Myop = NumOps.ToString();
            switch (NumOps)
            {
                case 0:
                    if (MyOperator == 3 && LastInput != "NEG")
                    {
                        txtRest.Text = "-" + txtRest.Text;
                        LastInput = "NEG";
                    }
                    //如果有一个操作数
                    break;
                case 1:
                    Op1 = txtRest.Text;
                    if (MyOperator == 3 && LastInput != "NUMS" && OpFlag != 4)
                    {
                        txtRest.Text = "-";
                        LastInput = "NEG";
                    }
                    //如果有两个操作数
                    break;
                case 2:
                    //取得第二个操作数
                    Op2 = TempReadout;
                    //根据操作标记进行不同的操作
                    switch (OpFlag)
                    {
                        //如果是加法操作
                        case 1:
                            Op1 = (Double.Parse(Op1) + Double.Parse(Op2)).ToString();
                            //将两个操作数相加然后复制给操作数1
                            //如果是减法
                            break;
                        case 3:
                            Op1 = (Double.Parse(Op1) - Double.Parse(Op2)).ToString();
                            //将两个操作数相减然后复制给操作数1
                            //如果是乘法
                            break;
                        case 2:
                            Op1 = (Double.Parse(Op1) * Double.Parse(Op2)).ToString();
                            //将两个操作数相乘然后复制给操作数1
                            //如果是除法
                            break;
                        case 0:
                            //首先判断出书是否为0
                            if (Double.Parse(Op2) == 0)
                            {
                                //如果为0则显示除数不能为0的提示框
                                MessageBox.Show("除数不能为0", "计算器");
                            }
                            else
                            {
                                Op1 = (Double.Parse(Op1) / Double.Parse(Op2)).ToString();
                                //将两个操作数相除然后复制给操作数1
                            }
                            break;
                        //如果是等号操作
                        case 4:
                            //取得当前值
                            Op1 = Op2;
                            break;
                    }
                    txtRest.Text = Op1;
                    //则显示计算结果
                    NumOps = 1;
                    //将操作数个数设置为一个
                    break;
            }
            if (LastInput != "NEG")
            {
                //如果最后一次输入是负数
                LastInput = "OPS";
                //设置输入类型
                OpFlag = MyOperator;
                //设置输入操作类型
            }
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            OperatorCal(0);//除法
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            OperatorCal(2);//乘法
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            OperatorCal(1);//加法
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            OperatorCal(3);//减法

        }

        private void btnEqu_Click(object sender, EventArgs e)
        {
            OperatorCal(4);//等号
        }
    }
}

[ 本帖最后由 武魂杰少 于 2015-6-7 10:12 编辑 ]
搜索更多相关主题的帖子: public double 
2015-06-06 10:51
快速回复:C#的加减乘除的问题
数据加载中...
 
   



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

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