简单函数、数制转化、归零、小数点功能计算器代码之一_裘马轻狂超的空间

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wdjisuanqi
{
    public partial class Form1 : Form
    {
        protected double iNum1;
        protected char cOperator;
        protected bool bNumBegins;
        protected bool bDot;
        protected int curBase;

            public Form1()
            {
                InitializeComponent();
                InitMembers();
            }
            private void InitMembers()
            {
                iNum1 = 0;
                curBase = 10;
                cOperator = '=';
                bNumBegins = true;
                bDot = false;
            }
            private void Numbers_Click(long i)
            {
                if (textBox1.Text == "Error")
                {
                    textBox1.Text = "0";
                }
                try
                {
                    if (bNumBegins)
                    {
                        textBox1.Text = i.ToString();
                        bNumBegins = false;
                    }
                    else
                    {
                        textBox1.Text += i.ToString();
                    }
                    double iCurrent = double.Parse(textBox1.Text);
                }
                catch
                {//捕捉异常
                    textBox1.Text = "Error";
                    InitMembers();
                    return;
                }
            }
            private void Dot_Click()
            {
                //不允许在一个数中输入两个或两个以上的小数点
                if (bDot)
                {
                    textBox1.Text = "Error";
                    InitMembers();
                    return;
                }
                else if (bNumBegins)
                {
                    textBox1.Text = "0.";
                    bNumBegins = false;
                    bDot = true;
                }
                else
                    textBox1.Text += ".";
                bDot = true;
            }
            private void Operators_Click(char op)
            {
                double iCurrent;
                try
                {
                    iCurrent = double.Parse(textBox1.Text);
                }
                catch
                {
                    textBox1.Text = "Error";
                    InitMembers();
                    return;
                }
                //计算结果
                double result;
                try
                {
                    switch (cOperator)
                    {
                        case '+':
                            checked { result = iNum1 + iCurrent; }
                            break;
                        case '-':
                            checked { result = iNum1 - iCurrent; }
                            break;
                        case '*':
                            checked { result = iNum1 * iCurrent; }
                            break;
                        case '/':
                            checked { result = iNum1 / iCurrent; }
                            break;
                        default:
                            result = iCurrent;
                            break;
                    }
                }
                catch
                {
                    //如果在计算中发生溢出,则显示"Error"
                    textBox1.Text = "Error";
                    InitMembers();
                    return;
                }
                //输出结果,并把当前结果保存起来
                textBox1.Text = result.ToString();
                iNum1 = result;
                //准备接受下一个数
                bNumBegins = true;
                bDot = false;
                //保存符号
                cOperator = op;
            }
            private void Fun_Click(char op)
            {
                double iCurrent;
                try
                {
                    iCurrent = double.Parse(textBox1.Text);
                    iCurrent = Math.PI * iCurrent / 180.0;//把弧度转化为度数
                }
                catch
                {
                    textBox1.Text = "Error";
                    InitMembers();
                    return;
                }
                //计算结果
                double result;
                try
                {
                    switch (op)
                    {
                        case 's':
                            checked { result = Math.Sin(iCurrent); }
                            break;
                        case 'c':
                            checked { result = Math.Cos(iCurrent); }
                            break;
                        default:
                            result = Math.Tan(iCurrent);
                            break;
                    }
                }
                catch
                {
                    //如果在计算中发生溢出,则显示"Error"
                    textBox1.Text = "Error";
                    InitMembers();
                    return;
                }
                //输出结果,并把当前结果保存起来
                textBox1.Text = result.ToString();
                iNum1 = result;
                //准备接受下一个数
                bNumBegins = true;
                bDot = false;
            }
            private void btnClear_Click(object sender, EventArgs e)
            {
                textBox1.Text = "0";
                InitMembers();
                return;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Numbers_Click(1);
            }

            private void button2_Click(object sender, EventArgs e)
            {
                Numbers_Click(2);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                Numbers_Click(3);
            }

            private void button4_Click(object sender, EventArgs e)
            {
                Numbers_Click(4);
            }

            private void button5_Click(object sender, EventArgs e)
            {
                Numbers_Click(5);
            }

            private void button6_Click(object sender, EventArgs e)
            {
                Numbers_Click(6);
            }

            private void button7_Click(object sender, EventArgs e)
            {
                Numbers_Click(7);
            }

            private void button8_Click(object sender, EventArgs e)
            {
                Numbers_Click(8);
            }

            private void button9_Click(object sender, EventArgs e)
            {
                Numbers_Click(9);
            }

            private void button0_Click(object sender, EventArgs e)
            {
                Numbers_Click(0);
            }

            private void btnAdd_Click(object sender, EventArgs e)
            {
                Operators_Click('+');
            }

            private void btnSubstract_Click(object sender, EventArgs e)
            {
                Operators_Click('-');
            }

            private void btnMultiply_Click(object sender, EventArgs e)
            {
                Operators_Click('*');
            }

            private void btnDivide_Click(object sender, EventArgs e)
            {
                Operators_Click('/');
            }

            private void btnDot_Click(object sender, EventArgs e)
            {
                Dot_Click();
            }

            private void btnEquals_Click(object sender, EventArgs e)
            {
                Operators_Click('=');
            }
           



郑重声明:资讯 【简单函数、数制转化、归零、小数点功能计算器代码之一_裘马轻狂超的空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——