C#编写计算器:C# 基本计算器(simple calculator in c# visual studio 2019)

IM 新的 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 cl Form1 : Form
  {
    double operador1 = 0;
    double operador2 = 0;
    char operando;
    double  resultado = 0;
    double extra;
    public Form1()
    {
        InitializeComponent();
    }
    private void on1_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "1";
    }
    private void on2_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "2";
    }
    private void on3_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "3";
    }
    private void on4_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "4";
    }
    private void on5_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "5";
    }
    private void on6_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "6";
    }
    private void on7_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "7";
    }
    private void on8_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "8";
    }
    private void on9_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "9";
    }
    private void on0_Click(object sender, EventArgs e)
    {
        lblpantalla.Text += "0";
    }
    private void on_clear_Click(object sender, EventArgs e)
    {
        lblpantalla.Text = string.Empty;
        operador1 = 0;
        operador2 = 0;
    }
    private void on_plus_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '+';
        lblpantalla.Text = string.Empty;
    }
    private void on_minus_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '-';
        lblpantalla.Text = string.Empty; 
    }
    private void on_multy_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '*';
        lblpantalla.Text = string.Empty; 
    }
    private void on_divide_Click(object sender, EventArgs e)
    {
        operador1 = Convert.ToDouble(lblpantalla.Text);
        operando = '/';
        lblpantalla.Text = string.Empty; 
    }
    private void on_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 on_dot_Click(object sender, EventArgs e)
    {
        if (lblpantalla.Text.Contains("."))
        {
            return;
        }
            lblpantalla.Text += ".";
    }
    private void on_reciproco_Click(object sender, EventArgs e)
    {
        double opera1;
        if (double.TryP(lblpantalla.Text, out opera1))
        {
            lblpantalla.Text = (-opera1).ToString();
        } 
    }
    private void on_recipro_Click(object sender, EventArgs e)
    {
        double opera1;
        if (double.TryP(lblpantalla.Text, out opera1))
        {
            lblpantalla.Text = (1/opera1).ToString();
        } 
    }
}

}

提前谢谢。

1

你应该做这样的事情:

on_plus_Click{
    //code from your method that need to be refactored
    on_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/

这应该是编程的乐趣的一部分,试图解决你自己的东西:) 祝你好运

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(179)
Ek mtc:获取TPM的公共EK:前导/尾随位的含义
上一篇
C 4 h 9 oh:如何将VP9webm h264MP4和h265MP4嵌入到单个视频标签中
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(26条)