Jump to content

Calculator logic

- - - - -

  • Please log in to reply
6 replies to this topic

#1
monkeyhead

monkeyhead

    Newbie

  • Members
  • Pip
  • 4 posts
Hi there,

Basically I'm having problems with a calculator I'm working on. Basically I'm using the windows calculator as a base to mimic its behaviours and I'm having problems if the user enters longer expressions such as 1-4-7 = -10

Heres what I have so far:-

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 Freshcalculator

{

    public partial class Form1 : Form

    {


        double TempValue1;

        double TempValue2;


        string OperatorFlag;

        string PrevOperatorFlag;


        public Form1()

        {

            InitializeComponent();

        }








        private void btnOne_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "1";

        }


        private void btnTwo_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "2";

        }


        private void btnThree_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "3";

        }


        private void btnFour_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "4";

        }


        private void btnFive_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "5";

        }


        private void btnSix_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "6";

        }


        private void btnSeven_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "7";

        }


        private void btnEight_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "8";

        }


        private void btnNine_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "9";

        }


        private void btnZero_Click(object sender, EventArgs e)

        {

            txtResult.Text = txtResult.Text + "0";

        }


        private void btnPlus_Click(object sender, EventArgs e)

        {


            TempValue1 = double.Parse(txtResult.Text);

            txtResult.Text = string.Empty;


            OperatorFlag = "+";

        }



        private void btnEquals_Click(object sender, EventArgs e)

        {




            switch (OperatorFlag)

            {

                //addition

                case "+":

                    TempValue2 = Double.Parse(txtResult.Text);

                    TempValue1 = TempValue1 + TempValue2;

                    PrevOperatorFlag = "+";

                    OperatorFlag = "";

                    break;


                case "-":

                    TempValue2 = Double.Parse(txtResult.Text);

                    TempValue1 = TempValue1 - TempValue2;

                    PrevOperatorFlag = "-";

                    OperatorFlag = "";

                    break;

                    




                case "":

                    if (PrevOperatorFlag == "+")

                    {

                        TempValue1 += TempValue2;

                    }

                    else if (PrevOperatorFlag == "-")

                    {

                        TempValue1 -= TempValue2;

                    }

                    break;

            }



            txtResult.Text = TempValue1.ToString();

        }


       


        private void btnMinus_Click(object sender, EventArgs e)

        {

            TempValue1 = double.Parse(txtResult.Text);

            txtResult.Text = string.Empty;


            OperatorFlag = "-";

        }


#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Your problem is you can't hold the third number to +/- with your previous numbers. For example you can do TempValue1+TempValue2 but there isn't a place to hold a third number. You can fix this best by completing the calculation on one variable when a operator button is clicked. Something like this(changes in red):
private void btnPlus_Click(object sender, EventArgs e)         
{              
      TempValue1 = double.Parse(txtResult.Text);             
[COLOR=#ff0000]    if(TempValue1 != 0)[/COLOR][COLOR=#006400]//If the # in the box isn't 0[/COLOR][COLOR=#ff0000]
    {[/COLOR]
          [COLOR=#ff0000]TempValue2 += TempValue1;[/COLOR][COLOR=#006400]//TempValue2 is now our total[/COLOR]
      txtResult.Text = string.Empty;              
      [COLOR=#ff0000]}[/COLOR]
}

 private void btnMinus_Click(object sender, EventArgs e)         
{             [COLOR=#ff0000] [/COLOR]
     TempValue1 = double.Parse(txtResult.Text);
[COLOR=#ff0000]     if(TempValue1 != 0)
     {[/COLOR]
                [COLOR=#ff0000]TempValue2 -= TempValue1;[/COLOR]
                 txtResult.Text = string.Empty; 
    [COLOR=#ff0000]            }[/COLOR]
         } 
 private void btnEquals_Click(object sender, EventArgs e)         
{
[COLOR=#ff0000]txtResult.Text = TempValue2.ToString(); [/COLOR]
}

I think something like that would work. I don't have an IDE to test the code so let me know if you have any problems. :)
~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
monkeyhead

monkeyhead

    Newbie

  • Members
  • Pip
  • 4 posts
That worked to some extent but still gave strange results.

#4
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Post what happened I can try to help. In a hour our so I'll be on a computer with C# to test code. :)
~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#5
monkeyhead

monkeyhead

    Newbie

  • Members
  • Pip
  • 4 posts
It sort of doubles the value sometimes.

The main issue is that if you do for example 1+2+3 =
It shows 3 (the 1+2) and not 6.

#6
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Ok I after a little testing I realized what was wrong. This should work.
private void btnPlus_Click(object sender, EventArgs e) 
{        
  TempValue1 = double.Parse(txtResult.Text);
  [COLOR=#ff0000]TempValue2 += TempValue1;[/COLOR]
  txtResult.Text = string.Empty;
[COLOR=#ff0000]  OperatorFlag = '+';[/COLOR][COLOR=#006400]//We'll need this later[/COLOR][COLOR=#FF0000][/COLOR]
}  

private void btnMinus_Click(object sender, EventArgs e) 
{                      
  TempValue1 = double.Parse(txtResult.Text);
  TempValue2 -= TempValue1;
  txtResult.Text = string.Empty;
[COLOR=#ff0000]  OperatorFlag = '-';[/COLOR]
}  

private void btnEquals_Click(object sender, EventArgs e)            
{  
    [COLOR=#ff0000] if(OperatorFlag == '+')[/COLOR][COLOR=#006400]//The Problem was we only added when an operator was clicked.  So you where in turn just adding 1+2 not 1+2+3.[/COLOR][COLOR=#FF0000]
     {                           [/COLOR][COLOR=#006400] //So we just see what the last operator was and use to get our answer.[/COLOR][COLOR=#FF0000][/COLOR][COLOR=#FF0000]
        TempValue1 = double.Parse(txtResult.Text);
        TempValue2 += TempValue1;              
     }[/COLOR]
    [COLOR=#ff0000] else if(OperatorFlag == '-')
     {
        TempValue1 = double.Parse(txtResult.Text);
        TempValue2 -= TempValue1;    
     }[/COLOR]
     txtResult.Text = TempValue2.ToString();
     TempValue2 = 0;
}

Hope that's better. :)
~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#7
monkeyhead

monkeyhead

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks for that the plus works fine, I think where I was going wrong was not resetting the variable value back to 0. One other thought how could I get the equals to repeat the last calculation if it was pressed multiple times? I would have thought using a flag as the previous symbol to know what the previous calculation was? The other thing ive noticed is the minus doesnt fully work if for example you do -9-9-9 = etc

Many thanks for your advice,




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users