简单的计算器与多个数字

时间:2013-09-11 04:37:15

标签: c# winforms

当我调试代码时,第一个数字正在正常执行该功能但当我点击按钮“=”时,结果出现在文本框中,当我用来点击另一个按钮时,数字仍在继续从结果而不是清除字段

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 CACalculator
{
    public partial class Form1 : Form
    {
        double num1, num2, result;
        string op;
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn9_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text + btn9.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn8.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn7.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn6.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn5.Text;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn4.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn3.Text;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn2.Text;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn1.Text;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            txtnum.Text = txtnum.Text+ btn0.Text;
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnadd.Text;
        }

        private void btnsub_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnsub.Text;
        }

        private void btnmul_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnmul.Text;
        }

        private void btndiv_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btndiv.Text;
        }

        private void btnquo_Click(object sender, EventArgs e)
        {
            num1 = Convert.ToDouble(txtnum.Text);
            txtnum.Clear();
            op = btnquo.Text;
        }

        private void btneql_Click(object sender, EventArgs e)
        {
            num2 = Convert.ToDouble(txtnum.Text);
            switch (op)
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "*":
                    result = num1 * num2;
                    break;
                case "/":
                    result = num1 / num2;
                    break;
                case "%":
                    result = num1 % num2;
                    break;
            }
            txtnum.Text = result.ToString();
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            txtnum.Clear();
        }
    }

}

1 个答案:

答案 0 :(得分:1)

你必须将Num1和num2 aswell作为文本框清除,如下所示:

    private void btnC_Click(object sender, EventArgs e)
    {
        txtnum.Clear();
        num1 = 0;    
        num2 = 0;   //clear all the numbers used in the calculator.
        result = 0; 
    }

否则你会清除视觉部分,而不是它背后的数字,你实际上是用

计算的

如果有帮助,请告诉我。