-4

我是 C# 新手,我需要这方面的帮助。

我正在做一个基本的计算器,但我无法进行多重操作数计算。

示例:1+1+1 = 3。

如果我尝试这个,我得到 2 作为结果,因为它添加了最后 2 个值。

这是代码。

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 calculadora
{
 public partial class Form1 : Form
  {

    double operador1 = 0;
    double operador2 = 0;
    char operando;
    double  resultado = 0;

    double extra;

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "1";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "2";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "3";
    }

    private void button4_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "4";
    }

    private void button5_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "5";
    }

    private void button6_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "6";
    }

    private void button7_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "7";
    }

    private void button8_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "8";
    }

    private void button9_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "9";
    }

    private void button0_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "0";
    }

    private void button_clear_Click(object sender, EventArgs e)
    {
        lblpantalla.Text = string.Empty;
        operador1 = 0;
        operador2 = 0;
    }

    private void button_plus_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);

        operando = '+';
        lblpantalla.Text = string.Empty;

    }

    private void button_minus_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '-';
        lblpantalla.Text = string.Empty; 
    }

    private void button_multy_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '*';
        lblpantalla.Text = string.Empty; 
    }

    private void button_divide_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '/';
        lblpantalla.Text = string.Empty; 
    }

    private void button_equal_Click(object sender, EventArgs e)
    {
        try
        {

            operador2 = Convert.ToDouble(lblpantalla.Text);

            double ug;


            switch (operando)
            {
                case '+': //suma
                    resultado = (operador1 + operador2);
                    break;


                case '-': //resta
                    resultado = (operador1 - operador2);
                    break;

                case '*': //multiply
                    resultado = (operador1 * operador2);
                    break;

                case '/': //division
                    if (operador2 != 0)
                    {
                       resultado = (operador1 / operador2);
                    }
                    else
                    {
                        lblpantalla.Text = "Can't divide by 0";
                    }
                    break;
            }
            lblpantalla.Text = resultado.ToString();
        }

        catch (Exception ex)
        {
            MessageBox.Show("Unexpected error occured. Details: " +
                ex.Message);
        } 
    }


    private void button_dot_Click(object sender, EventArgs e)
    {
        if (lblpantalla.Text.Contains("."))
        {
            return;
        }

            lblpantalla.Text += ".";


    }

    private void button_reciproco_Click(object sender, EventArgs e)
    {
        double opera1;
        if (double.TryParse(lblpantalla.Text, out opera1))
        {
            lblpantalla.Text = (-opera1).ToString();
        } 

    }

    private void button_recipro_Click(object sender, EventArgs e)
    {
        double opera1;
        if (double.TryParse(lblpantalla.Text, out opera1))
        {
            lblpantalla.Text = (1/opera1).ToString();
        } 
    }
}

}

提前致谢。

4

1 回答 1

1

您应该执行以下操作:

button_plus_Click{

    //code from your method that need to be refactored
    button_equal_Click(){
        if(there is operando 1, and lblpantalla and operando)
        switch (operando)
            {
                case '+': //suma
                    operador1 = (operador1 + lblpantalla);
    }
}

由于这不是技术问题,因此您得到了太多负面评价,几乎您是在要求某人为您编程。

想想你的计算器可以处于的状态: https ://www.clear.rice.edu/comp212/06-spring/labs/13/

这应该是编程的有趣部分,尝试自己解决一些问题:) 祝你好运

于 2013-09-11T21:52:26.187 回答