Closed Thread
Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: calc project

  1. #11
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: calc project

    Hello everyone,

    So far I finally got it with some help from a friend, here is the code below, i have made a class equations, for all 4 equations, and class number to store the numbers temperarily. the good news is i found online a example that sorta helped except they used a temp array, for me i just used 3 variables (two double and 1 string for equation) the only problem is sometimes it will add, subtract, multiply or divide and sometimes it wont, also my backspace and clear button doesnt work as thought as it would, can anyone check what am i doing wrong and if you can correct and post the answer? Thank you in advance i will continue my answer on this but so far nothing.

    main form
    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            numbers num = new numbers();
    
            public Form1()
            {
                InitializeComponent();
            }
            enum operation
            {
                add,
                subtract,
                divide,
                multiply
            }
    
            private bool hasDecimal = false;
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void btnclear_Click(object sender, EventArgs e)
            {
                num.numbering = 0;
                num.numbering2 = 0;
                firstNumberSet = false;
                txtbox1.Clear();
               
            }
    
            private void txtbox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private bool firstNumberSet = false;
            private void btn1_Click(object sender, EventArgs e)
            {
                
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 1;
                    }
                    else
                    {
                        num.numbering2 = 1;
                    }
                    txtbox1.Text += "1";
                }
    
                catch(Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn2_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 2;
                    }
                    else
                    {
                        num.numbering2 = 2;
                    }
                    txtbox1.Text += "2";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn3_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 3;
                    }
                    else
                    {
                        num.numbering2 = 3;
                    }
                    txtbox1.Text += "3";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn4_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 4;
                    }
                    else
                    {
                        num.numbering2 = 4;
                    }
                    txtbox1.Text += "4";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
    
            }
    
            private void btn5_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 5;
                    }
                    else
                    {
                        num.numbering2 = 5;
                    }
                    txtbox1.Text += "5";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn6_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 6;
                    }
                    else
                    {
                        num.numbering2 = 6;
                    }
                    txtbox1.Text += "6";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn7_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 7;
                    }
                    else
                    {
                        num.numbering2 = 7;
                    }
                    txtbox1.Text += "7";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn8_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 8;
                    }
                    else
                    {
                        num.numbering2 = 8;
                    }
                    txtbox1.Text += "8";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn9_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 9;
                    }
                    else
                    {
                        num.numbering2 = 9;
                    }
                    txtbox1.Text += "9";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            private void btn0_Click(object sender, EventArgs e)
            {
                try
                {
                    if (!firstNumberSet)
                    {
                        num.numbering = 0;
                    }
                    else
                    {
                        num.numbering2 = 0;
                    }
                    txtbox1.Text += "0";
                }
    
                catch (Exception)
                {
                    MessageBox.Show("WHAT DID YOU DO!!");
                }
            }
    
            operation currentOperation;
            private void btnplus_Click(object sender, EventArgs e)
            {
                txtbox1.Text += "+";
                currentOperation = operation.add;
                firstNumberSet = true;                    // this is where we decide that the first number is done and we should
                                                          //start on the second one
            }
    
            private void btnsubtract_Click(object sender, EventArgs e)
            {
                txtbox1.Text += "-";                        // this is where we decide that the first number is done and we should
                currentOperation = operation.subtract; //start on the second one
                firstNumberSet = true;
            }
    
            private void btnmultiply_Click(object sender, EventArgs e)
            {
                txtbox1.Text += "*";
                currentOperation = operation.multiply;  // this is where we decide that the first number is done and we should
                firstNumberSet = true;                      //start on the second one
            }
    
            private void btndivide_Click(object sender, EventArgs e)
            {
                txtbox1.Text += "/";
                currentOperation = operation.divide;   // this is where we decide that the first number is done and we should
                firstNumberSet = true;                     //start on the second one
            }
    
            private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                aboutfrm bout = new aboutfrm();
                bout.Show();
            }
    
            private void btnequal_Click(object sender, EventArgs e)
            {
                Equations equat = new Equations();
                     
                switch (currentOperation) 
                { 
                    case operation.add:   // get info from the buttons either divide, multiply, add, and subtract
                        
                        txtbox1.Text = equat.add(num.numbering, num.numbering2).ToString();
                        break;
                    case operation.subtract:
                        txtbox1.Text = equat.subtract(num.numbering, num.numbering2).ToString();
                        break;
                    case operation.multiply:
                        txtbox1.Text = equat.mulitply(num.numbering, num.numbering2).ToString();
                        break;
                    case operation.divide:
                        txtbox1.Text = equat.divide(num.numbering, num.numbering2).ToString();
                        break;
                }
                 
            }
    
            private void btnback_Click(object sender, EventArgs e)
            {
                //Declare locals needed
                string str;
                int loc;
                //Make sure the text length is > 1
                if (txtbox1.Text.Length > 0)
                {
                    //Get the next to last character
                    str = txtbox1.Text.Substring(txtbox1.Text.Length - 1);
                    //Check if its a decimal
                    if (str == ".")
                    {
                        //If it is toggle the hasDecimal flag
                        hasDecimal = false;
                    }
                    //Get the length of the string
                    loc = txtbox1.Text.Length;
                    //Remove the last character, incrementing by 1
                    txtbox1.Text = txtbox1.Text.Remove(loc - 1, 1);
                }
    
            }
    
            private void btndecimal_Click(object sender, EventArgs e)
            {
                if (firstNumberSet)
                {
                    //Check if it already has a decimal (if it does then do nothing)
                    if (!hasDecimal)
                    {
                        //Check to make sure the length is > than 1
                        //Dont want user to add decimal as first character
                        if (txtbox1.Text.Length != 0)
                        {
                            //Make sure 0 isnt the first number
                            if (txtbox1.Text != "0")
                            {
                                //It met all our requirements so add the zero
                                txtbox1.Text += btndecimal.Text;
                                //Toggle the flag to true (only 1 decimal per calculation)
                                hasDecimal = true;
                            }
                        }
                        else
                        {
                            //Since the length isnt > 1
                            //make the text 0.
                            txtbox1.Text = "0.";
                        }
                    }
                }
    
            }
        }
    }
    class equations
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        class Equations
        {
            public double add(double num1, double num2)
            {
                return (num1 + num2);
            }
            public double subtract(double num1, double num2)
            {
                return num1 - num2;
            }
            public double mulitply(double num1, double num2)
            {
                return num1 * num2;
            }
            public double divide(double num1, double num2)
            {
                return num1 / num2;
            }
            
        }
    }
    class numbers

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        class numbers
        {
            private double number;
            private double number2;
            
            public double numbering
            {
                get { return number; }
                set
                {
                    number = number * 1;   // this will move the other digits left one
                    number += value;        // this puts the new one in the ones column
                }
            }
    
            public double numbering2
            {
                get { return number2; }
                set
                {
                    number2 = number2 * 1;   // this will move the other digits left one
                    number2 += value;        // this puts the new one in the ones column
                }
            }
        }
    }
    Thanks again in advance

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #12
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: calc project

    why does it blank out the *** on cl***?

Closed Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need Help with Payment Calc
    By msummons76 in forum Java Help
    Replies: 7
    Last Post: 11-01-2011, 11:39 AM
  2. My Calc in Java GUI
    By VakhoQ in forum Java Help
    Replies: 5
    Last Post: 09-04-2011, 02:49 AM
  3. VB Calc.
    By Erobobo in forum Visual Basic Programming
    Replies: 2
    Last Post: 01-28-2010, 11:08 PM
  4. Stat or Calc?
    By meisle726 in forum General Programming
    Replies: 2
    Last Post: 03-31-2009, 03:13 PM
  5. Looking for PERL equivalent of calc
    By Panarchy in forum Perl
    Replies: 0
    Last Post: 11-23-2008, 04:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts