C# 基本计算器

本文关键字:计算器 | 更新日期: 2023-09-27 18:31:02

我是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();
        } 
    }
}

}

提前谢谢。

C# 基本计算器

你应该做这样的事情:

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/

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