-4

im new to C# and i need help on this.

Im doing a basic calculator and i'm not able to do multiples operand calculations.

Example: 1+1+1 = 3.

If i try this, i get 2 as result cause its adding the last 2 values.

Here is the code.

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

}

Thanks in advance.

arielgpe
  • 19
  • 1
  • 1
  • 4
  • (A) this shows no attempt at supporting beyond *n op m* and (B) parsing and evaluating an equation is not an easy problem and (C) there are duplicates of this if you look hard enough. – Austin Salonen Sep 11 '13 at 21:33
  • Read here on how to ask questions: http://stackoverflow.com/questions/how-to-ask – user Sep 11 '13 at 21:34
  • possible duplicate of [operators as strings](http://stackoverflow.com/questions/174664/operators-as-strings) – Austin Salonen Sep 11 '13 at 21:40
  • This can be starting point. http://en.wikipedia.org/wiki/Reverse_Polish_notation – I4V Sep 11 '13 at 21:44
  • Writing a string calculator is actually a popular Kata. see http://codekatas.org/casts/7014098.aspx and http://osherove.com/tdd-kata-1/ for example – user Sep 11 '13 at 21:44

1 Answers1

1

You should be doing something like:

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);
    }
}

You are getting too many negative points as this is not technical question, its almost you are asking someone to do programming for you.

Think about States your calculator can be in: https://www.clear.rice.edu/comp212/06-spring/labs/13/

This should be fun part of the programming, trying to resolve something on your own :) Good Luck

user007
  • 1,122
  • 1
  • 10
  • 30