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;                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值