C#入门基础之计算器

实验目的及要求:
(1)掌握用 C#语言调试程序的基本方法;
(2)掌握用Visual Studio C#创建一个窗体应用程序,完成界面设计;
(3)依据实际逻辑关系,实现计算器的功能;
实验内容:
通过模拟小米手机自带计算器,完成计算器界面设计以及相关功能的实现。
实验环境:电脑+Visual Studio 2012
实验原理:
(1)通过Visual Studio 2012创建一个窗体应用程序;
(2)使用button和textbox控件设计计算器界面;
(3)依据实际逻辑关系,实现计算器功能
实验流程:
1.创建一个窗体应用程序,并命名为MyComputer;
2.使用button和textbox控件绘制计算器界面;
3.依据实际逻辑关系,编写代码,实现相关功能;
实验步骤:
1.界面设计
这里写图片描述
2.详细代码

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


namespace Mycomputer2._0
{
    public partial class frmcs : Form
    {
        private double num1 = 0;    //第一操作数
        private double num2;        //第二操作数
        private string sign;        //运算符号
        private double num3;        //运算结果
        private bool check = true;  //检查是否为第一操作数
        private bool check1 = true;
        private double ans;         //记录上次运算结果
        private string error = "Error";
        public frmcs()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)      //全部清除
        {
            textBox1.Text = "";     //文本框的值置空
            textBox2.Text = "";
            num1 = 0;
            num2 = 0;
            num3 = 0;
            ans = 0;
        }

        private void button2_Click(object sender, EventArgs e)      //撤销操作数
        {
            textBox1.Text = "";     //文本框的值置空
            if (check == true)
            {
                num1 = 0;
            } 
            else
            {
                num2 = 0;
            }
        }

        private void button3_Click(object sender, EventArgs e)      //除法运算
        {
            sign = "/";
            textBox2.Text = num1.ToString();
            textBox1.Text = "/";
            check = false;
        }

        private void button4_Click(object sender, EventArgs e)      //乘法运算
        {
            sign = "*";
            textBox2.Text = num1.ToString();
            textBox1.Text = "*";
            check = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";     
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)      //减法运算
        {
            sign = "-";
            textBox2.Text = num1.ToString();
            textBox1.Text = "-";
            check = false;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)         //加法运算
        {
            sign = "+";
            textBox2.Text = num1.ToString();
            textBox1.Text = "+";
            check = false;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (check1 == true)
            {
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";
                check1 = true;
                if (check == true)
                {
                    Button b = (Button)sender;                  //将该按钮的属性值赋给按钮b
                    textBox1.Text += b.Text;                    //追加文本框的值(PS:用于解决位数在两位或三位以上得值)
                    num1 = double.Parse(textBox1.Text);         //将文本框的值强制为double类型,并赋值给num1
                }
                else
                {
                    textBox2.Text = textBox1.Text;
                    textBox1.Text = "";
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void button16_Click(object sender, EventArgs e)         //“=”处理
        {
            switch (sign)
            {
                case "+":
                    num3 = num1 + num2;
                    break;
                case "-":
                    num3 = num1 - num2;
                    break;
                case "*":
                    num3 = num1 * num2;
                    break;
                case "/":
                    if (num2 == 0)
                    {
                        textBox1.Text = error;
                    }
                    else
                    {
                        num3 = num1 / num2;
                    }
                    break;
                case "%":
                    if (num2 == 0)
                    {
                        textBox1.Text = error;
                    }
                    else
                    {
                        num3 = num1 % num2;
                    }
                    break;
            }
            num1 = num3;
            textBox1.Text = num3.ToString();
            check = true;
            check1 = false;
        }

        private void button17_Click(object sender, EventArgs e)         //求余运算
        {
            sign = "%";
            textBox2.Text = num1.ToString();
            textBox1.Text = "%";
            check = false;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (check == true)
            {
                Button b = (Button)sender;
                textBox1.Text += b.Text;
                num1 = double.Parse(textBox1.Text);
            }
            else
            {
                textBox2.Text = textBox1.Text;
                textBox1.Text = "";    
                Button b = (Button)sender;
                textBox1.Text += b.Text;
                num2 = double.Parse(textBox1.Text);
            }
        }

        private void button19_Click(object sender, EventArgs e)     //小数点处理
        {
            if (textBox1.Text != "")                                //判断文本框不为空
            {
                if (check == true)
                {
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num1 = double.Parse(textBox1.Text);

                }
                else if (check == false)
                {
                    Button b = (Button)sender;
                    textBox1.Text += b.Text;
                    num2 = double.Parse(textBox1.Text);
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

实验结果:
本实验代码参考了大佬们的代码,其逻辑功能的实现相对较为容易,在模仿其代码后,所设计出来的计算器功能相对较简单,却没有完全模仿小米手机自带计算器,故在其基础上改编升级为MyComputer2.0,尽可能的模仿了小米手机自带计算器,当仍存在诸如除数为0,操作异常(PS:本想尝试用try-catch,当由于才接触C#,未能实现);当操作数,直接先输入小数点,在输入数时操作异常等问题。还请大家帮我看看。给点改进意见,谢谢

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值