c#实现简易计算器(半成品)

思路:1.俩个文本框textbox

           2.设计n各button,包括各个数字和运算符

           3.设计textbook和button代码,实现相应功能

实现:1.单击按钮将其对应内容显示在文本框中
           2.单击符号(+、-、×、÷、%)时将第一次输入的数储存起来
           3.单击等号时将第二次输入的数存储起来并将第一次输入的数与第二次输入的数按照所单                     击的符号进行运算将结果显示在第二个文本框中
           4.单击C时将两个文本框中的内容清空kesh

仅可实现清空,输入等,最后计算有误(但是知识没弄懂,懒得做了,以后有用再回来看)

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 WinFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //声明2个变量
        string type;//符号类型
        double x;//第一个数
        bool c = false;
        private void button7_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '1';
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if(c == true);
            type = "-";
            textBox1.Text += "-";
            x = double.Parse(textBox1.Text);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox1.Text = "";//c删除全部
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '6';
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '2';
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '3';
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '4';
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '5';
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '7';
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '8';
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '9';
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += '0';

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
            this.Text = "计算器";
            textBox1.TabIndex = 0;//

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button15_Click(object sender, EventArgs e)
        {
            c = true;
            type = "/";
            textBox1.Text += "/";
            x = double.Parse(textBox1.Text);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            c = true;
            type = "*";
            textBox1.Text += "*";
            x = double.Parse(textBox1.Text);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text += ".";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if(c==true)
            type = "+";
            textBox1.Text += "+";
            x = double.Parse(textBox1 .Text);
        }
        //编写=
        private void button17_Click(object sender, EventArgs e)
        {
            if (type == "+") 
            {
                textBox1.Text = (x + textBox1.Text).ToString();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kzd.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值