计算器

点击查看计算器代码

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 SK
{
public partial class Form1 : Form
{
double a = 0;
double b = 0;
bool c = false;
string d;

    public Form1()
    {
        InitializeComponent();
    }
    //按钮 数字8
    private void button2_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;
        }
        shuru.Text += "8";
    }
    //按钮减号
    private void button11_Click(object sender, EventArgs e)
    {
        //如果文本框输入值为空,则提示弹出提示框
        if (shuru.Text=="")
        {
            MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        //否则
        else
        {
            c = true; b = double.Parse(shuru.Text);
            d = "-";
        }
    }
    //按钮 数字7
    private void button1_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;
        }shuru.Text += "7";
    }
    //小数点
    private void xiaoshudian_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;
        }
        shuru.Text += ".";
    }
    //按钮 数字9
    private void button3_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;
        }
        shuru.Text += "9";
    }
    //按钮 数字4
    private void button6_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "4";
    }
    //按钮 数字5
    private void button5_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "5";
    }
    //按钮 数字6
    private void button4_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "6";
    }
    //按钮 数字1
    private void button9_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "1";
    }
    //按钮 数字2
    private void button8_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "2";
    }
    //按钮 数字3
    private void button7_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "3";
    }
    //按钮 数字0
    private void button16_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            shuru.Text = "";
            c = false;

        }
        shuru.Text += "0";
        //如果输入的符号为除号 则弹出提示框
        if (d=="/")
        {
            shuru.Clear();
            MessageBox.Show("除数不能为零,", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
    //按钮 加号
    private void button12_Click(object sender, EventArgs e)
    {
        //如果输入框输入的值为空 则弹出提示框
        if (shuru.Text=="")
        {
            MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            c = true;
            b = double.Parse(shuru.Text);
            d = "+";
        }
    }
    //按钮 乘号
    private void button10_Click(object sender, EventArgs e)
    {
        if (shuru.Text == "")
        {
            MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            c = true;
            b = double.Parse(shuru.Text);
            d = "*";
        }
    }
    //按钮 除号
    private void 除_Click(object sender, EventArgs e)
    {
        if (shuru.Text == "")
        {
            MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            c = true;
            b = double.Parse(shuru.Text);
            d = "/";
        }
    }
    //按钮 等于号
    private void isok_Click(object sender, EventArgs e)
    {
        //switch 允许一个变量等于多个值
        switch (d)
        {
            case "+": a = b + double.Parse(shuru.Text); break;
            case "-": a = b + double.Parse(shuru.Text); break;
            case "*": a = b + double.Parse(shuru.Text); break;
            case "/": a = b + double.Parse(shuru.Text); break;
        }
        shuru.Text = a + "";
        c = true;
    }
    //按钮 清除(C)
    private void button13_Click(object sender, EventArgs e)
    {
        shuru.Text = "";
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

}

posted @ 2021-12-29 11:17  高晓婷  阅读(67)  评论(0)    收藏  举报