Results 1 to 10 of 10

Thread: [Student] Looking for exception help in calculator.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    7

    [Student] Looking for exception help in calculator.

    Hi,

    One of our projects we had to do was make a simple calculator (surprise, surprise..)

    I have it more or less finished but still have 1 error for wich I can't find a solution.

    First off, my code here:

    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button13_Click(sender As Object, e As EventArgs) Handles btnClear.Click
            txtAntwoord.Clear()
            lbl1.Text = ""
            lbl2.Text = ""
            lbl3.Text = ""
    
        End Sub
    
        Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
            txtAntwoord.AppendText(9)
        End Sub
    
        Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
            txtAntwoord.AppendText(8)
        End Sub
    
        Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
            txtAntwoord.AppendText(7)
        End Sub
    
        Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
            txtAntwoord.AppendText(4)
        End Sub
    
        Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
            txtAntwoord.AppendText(5)
        End Sub
    
        Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
            txtAntwoord.AppendText(6)
        End Sub
    
        Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
            txtAntwoord.AppendText(3)
        End Sub
    
        Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
            txtAntwoord.AppendText(2)
        End Sub
    
        Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
            txtAntwoord.AppendText(1)
        End Sub
    
        Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
            txtAntwoord.AppendText(0)
        End Sub
    
        Private Sub btnPunt_Click(sender As Object, e As EventArgs) Handles btnPunt.Click
            txtAntwoord.AppendText(".")
        End Sub
    
        Private Sub btnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
            lbl1.Text = txtAntwoord.Text
            lbl2.Text = "+"
            txtAntwoord.Clear()
        End Sub
    
        Private Sub btnMin_Click(sender As Object, e As EventArgs) Handles btnMin.Click
            lbl1.Text = txtAntwoord.Text
            lbl2.Text = "-"
            txtAntwoord.Clear()
        End Sub
    
        Private Sub btnMaal_Click(sender As Object, e As EventArgs) Handles btnMaal.Click
            lbl1.Text = txtAntwoord.Text
            lbl2.Text = "*"
            txtAntwoord.Clear()
        End Sub
    
        Private Sub btnDeel_Click(sender As Object, e As EventArgs) Handles btnDeel.Click
            lbl1.Text = txtAntwoord.Text
            lbl2.Text = "/"
            txtAntwoord.Clear()
        End Sub
    
        Private Sub btnEQ_Click(sender As Object, e As EventArgs) Handles btnEQ.Click
            Try
    
                lbl3.Text = txtAntwoord.Text
            Dim sign As Char
            sign = lbl2.Text
            Dim n1 As Double
            n1 = Convert.ToDouble(lbl1.Text)
            Dim n2 As Double
            n2 = Convert.ToDouble(lbl3.Text)
            Dim n3 As Double
    
            Select Case (sign)
                Case "+"
                    n3 = n1 + n2
                Case "-"
                    n3 = n1 - n2
                Case "*"
                    n3 = n1 * n2
                Case "/"
                    n3 = n1 / n2
    
            End Select
            txtAntwoord.Text = Convert.ToString(n3)
    
            Catch ex As Exception
            MsgBox("Gelieve enkel cijfers in te geven")
    
            End Try
    
        End Sub
    End Class
    The calc itself looks something like this: http://imgur.com/GiSfJ2o

    Here's the problem: Whenever I do 2 operations after each other without using the enter or 'equals as' button, it forgets the first operation.
    By example, 3+3=6 but 3+3+3 also = 6 AND 3-3+3 is ALSO 6.

    I know the problem lies within the fact I only use 3 labels in wich I store my numbers/operations but I don't know how to fix this in an easy/elegant way.

    All tips are welcome, thank you in advance for your time and input!

    Dieter

  2. #2

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    7

    Re: [Student] Looking for exception help in calculator.

    A possible solution could be (I think) if the calculator also recognises the operations as a command to calculate the solution.

    After he does this, he could write the temporary solution in the now not needed label1 and continiue counting.

    Either this or just give an error saying you have to press equal as first..

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    7

    Re: [Student] Looking for exception help in calculator.

    Bump!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [Student] Looking for exception help in calculator.

    Well, yeah, that's what it will do, you wrote it that way. To get a second operator in there isn't all that simple. After all, what would you do with 3+3*3? If you were to add the first two, you'd end up with a result of 18, but the correct answer is 27. Essentially, as long as you just do a single operator, then all is well. As soon as you go to two operators, the problem becomes considerably more difficult.

    There is no solution that is both simple and correct. The issue for the example you gave is that you are discarding or overwriting things when you press any operator button. If you want a simple and incorrect solution (which will work for addition and subtraction, or any time you use the same operator for all operations), you would need to do essentially what you are doing in the equals buttons for any of the operator buttons. That isn't worth pursuing, though, because most answers would be wrong.

    Solving a more complex equation won't be easy at all, so do you really want to do it?
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    7

    Re: [Student] Looking for exception help in calculator.

    I understand, was afraid it might turn out this was.
    I'm thinking of just adding an error message that pops up when you try to make 2 or more operations after each other.
    What would be the easiest way to do this? Haven't got much experience with this.

    Thank you!

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [Student] Looking for exception help in calculator.

    Quote Originally Posted by Shaggy Hiker View Post
    Well, yeah, that's what it will do, you wrote it that way. To get a second operator in there isn't all that simple. After all, what would you do with 3+3*3? If you were to add the first two, you'd end up with a result of 18, but the correct answer is 27.
    I wondered about this... so I typed it into a calculator... it comes back 18... I know that over all the epxression should result in 27, but calculators rarely get the whole epxression so they calculate as they go... as soon as I hit the *, it evaluates the expression it has at hand - 3+3... and comes back with 6, then when I hit enter, it evaluate the expression it has on hand 6*3 .. and returns 18 ... So for a "true" calculator functionality, you would calculate the expression as soon as an operator (any operator) is selected... at some point that operator will be "=" signalling the end of the calculation.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [Student] Looking for exception help in calculator.

    That's a good point. There's a difference between "3 + 3 = ? * 3 = ??" versus "3 + 3 * 3 = ?". So, in that case, you'd have to decide what type of functionality you want. As TG shows, lots of calculators will get the answer for two operators, and if you add in a third, it will use the previous answer as the first input. So, that is a valid solution. I have a calculator that would let you put in 3 + 3 * 3, and will only calculate when you press the = sign. This gives a more correct solution, but it's much harder to write. On that calculator, if you write 3 + 3 then press equals, then press *, what you see is "Ans * " and you can then press a number, so it would look like "Ans * 3", where Ans is the answer to the previous part.

    If you want to follow the simpler calculator design, it would be valid, as TG shows, but what it would mean is that you'd have to enter a number, enter an operator, then enter a second number, all of which you have. At that point, you can EITHER press the = button, OR press any of the operator buttons. If you press an operator button when you already have an operator in there (you know this because you have something in lbl2), then you do all the steps you currently have in the = method, except that the results go into lbl1.Text rather than lbl3.Text.

    So, you get to decide whether that is acceptable. If it is, it isn't hard to do, because you already have virtually every line written. You just have to adjust the operator buttons to see whether there is already something in lbl2, and if there is, then do the calculation and put the results in lbl1, rather than just the new number, then put the new operator into lbl2 and proceed.
    My usual boring signature: Nothing

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,374

    Re: [Student] Looking for exception help in calculator.

    What this boils down to is how do you want the functionality of the calculator to work? Would you like for it to accept full expressions like Shaggy presented with 3 + 3 * 3 equaling 3 + (3 * 3) or would you like for it to accept one expression at a time.

    If you want to accept full expressions then I would suggest creating an expression evaluator, it makes everything simpler in the long run. The process is essentially you take the infix notation(3 + 3 * 3) and convert it to a prefix or postfix notation(I use reverse polish notation) where the equation would be transformed into something like: 3 3 * 3 +. From there it is much easier for a computer to solve the expression since the order of operations will be followed by reading left to right. I have an expression evaluator in the codebank as well as a tutorial in the Tutorials forum.

    If you want to accept only one expression at a time. Then if you are storing both values as variables then when the user hits an operator, set the first value variable equal to the new value and clear the second value. When the user hits the equal sign, clear both variables.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [Student] Looking for exception help in calculator.

    C'mon guys...
    make a simple calculator
    Simple... simple... simple...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,374

    Re: [Student] Looking for exception help in calculator.

    Quote Originally Posted by techgnome View Post
    C'mon guys...
    Simple... simple... simple...

    -tg
    If you understand the principles behind an expression evaluator then you could whip up a scannerless one in the matter of about an hour. But I do agree that he should likely go with the "one expression at a time" model to avoid that all together.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width