# 2 Simple (I Think) Calculator Questions

Page 1 of 1

## 5 Replies - 1084 Views - Last Post: 03 November 2012 - 05:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298466&amp;s=85e27d8e8b05c3a79c2ce5c46322059c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 CodyMcCoderson

Reputation: 0
• Posts: 30
• Joined: 08-October 12

# 2 Simple (I Think) Calculator Questions

Posted 03 November 2012 - 08:25 AM

Ok, I'll post my code near the bottom, but I'll give my questions ahead of time.

1. How do I make my +/- button function? Im thinking it needs to be an "IF" statement, but can I just say
if plusminus = "+" then
plusminus = "-"
else if plusminus = "-"
then plusminus = "+"
end if

2. My Sin, Cos and Tan at the bottom seem a bit drawn out to me, is there anyway of simplifying the code?

Thanks guys!

Public Class Form1
Dim FirstNum As Double
Dim SecondNum As Double
Dim AnsNum As Double
Dim Method1 As String

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
display.Text = display.Text & "1"
End Sub

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
display.Text = display.Text & "2"
End Sub

Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
display.Text = display.Text & "3"
End Sub

Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
display.Text = display.Text & "4"
End Sub

Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
display.Text = display.Text & "5"
End Sub

Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
display.Text = display.Text & "6"
End Sub

Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
display.Text = display.Text & "7"
End Sub

Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
display.Text = display.Text & "8"
End Sub

Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
display.Text = display.Text & "9"
End Sub

Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
display.Text = display.Text & "0"
End Sub

Private Sub btnPeriod_Click(sender As Object, e As EventArgs) Handles btnPeriod.Click
display.Text = display.Text & "."
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
display.Clear()
End Sub

FirstNum = Val(display.Text)
display.Text = ""
Method1 = "+"
End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
FirstNum = Val(display.Text)
display.Text = ""
Method1 = "-"
End Sub

Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
FirstNum = Val(display.Text)
display.Text = ""
Method1 = "*"
End Sub

Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
FirstNum = Val(display.Text)
display.Text = ""
Method1 = "/"
End Sub
Private Sub btnMod_Click(sender As Object, e As EventArgs) Handles btnMod.Click
FirstNum = Val(display.Text)
display.Text = ""
Method1 = "mod"
End Sub

Private Sub btnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
SecondNum = Val(display.Text)
If Method1 = "+" Then
AnsNum = FirstNum + SecondNum
End If
If Method1 = "-" Then
AnsNum = FirstNum - SecondNum
End If
If Method1 = "*" Then
AnsNum = FirstNum * SecondNum
End If
If Method1 = "/" Then
AnsNum = FirstNum / SecondNum
End If
If Method1 = "mod" Then
AnsNum = FirstNum Mod SecondNum
End If
display.Text = AnsNum
End Sub

Private Sub btnSin_Click(sender As Object, e As EventArgs) Handles btnSin.Click
display.Text = Math.Sin((Math.PI) / 180 * (display.Text))
End Sub

Private Sub btnCos_Click(sender As Object, e As EventArgs) Handles btnCos.Click
display.Text = Math.Cos((Math.PI) / 180 * (display.Text))
End Sub

Private Sub btnTan_Click(sender As Object, e As EventArgs) Handles btnTan.Click
display.Text = Math.Tan((Math.PI) / 180 * (display.Text))
End Sub
End Class

Is This A Good Question/Topic? 0

## Replies To: 2 Simple (I Think) Calculator Questions

### #2 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: 2 Simple (I Think) Calculator Questions

Posted 03 November 2012 - 10:10 AM

For the +/- you can have it like you wrote there (although you don't need to check for other state in else, if there are only 2 states possible). You could also have it as Boolean, and prevent possible other states confusion. Perhaps even better solution is to create PlusMinus class with Enum of Positive and Negative state. Something like:

Public Class PlusMinus
Public Sub New(state As PlusMinusState)
_state = state
End Sub

Private _state As PlusMinusState
Public Property State() As PlusMinusState
Get
Return _state
End Get
Set(value As PlusMinusState)
_state = value
End Set
End Property
End Class

Public Enum PlusMinusState
Positive
Negative
End Enum

You might think of it as overkill, but it is strongly typed, and VB.NET is OOP language.

As far as radians to degrees conversion... You could write yourself extension methods for converting numeric types from one to other, using the same formula ( Math.PI/180 ) and vice versa, OR extension methods for SinD CosD.... One way or another, you'd need to have extension for each numerical type, you want to use it. If you'll write extension for Decimal only, then it will be available only to Decimal type objects.

BTW I wrote a tutorial on how to handle that kind of situations, but is still nowhere to be found, and it's been 3 weeks + since I sent it to be reviewed. Perhaps one of mods would take a look at it... Authors and contributors can see it here

### #3 CodyMcCoderson

Reputation: 0
• Posts: 30
• Joined: 08-October 12

## Re: 2 Simple (I Think) Calculator Questions

Posted 03 November 2012 - 10:38 AM

Thanks!

One more quick question.

When I enter a number, and then add it to another number. its fine, but if i wanna add another number the result doesnt work.

ie.

2 + 3 = 5 <-THIS IS RIGHT, works fine.
9 + 3 + 6 = 9 <- This shows up wrong.

Basically how can I store the first 9, so that I can add it all together?

### #4 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: 2 Simple (I Think) Calculator Questions

Posted 03 November 2012 - 12:50 PM

You have just 2 variables for storing entered numbers. But that's not your only problem. I suggest you start it all over again, and spend some time planing and designing your solution first, and only then start coding. Make a solution, that has nothing to do with the forms, but will work with every user interface. Display and Keyboard are just interfaces, they are not to be used for calculations.

Edit:
I just remembered 2 wonderful tutorials, that will help you a lot:
1.) Part one
2.) Part 2

This post has been edited by lucky3: 03 November 2012 - 12:55 PM

### #5 CodyMcCoderson

Reputation: 0
• Posts: 30
• Joined: 08-October 12

## Re: 2 Simple (I Think) Calculator Questions

Posted 03 November 2012 - 02:07 PM

I didnt really want to start over.

But thanks I'll definitely check out those tutorials for some guidance.

### #6 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

## Re: 2 Simple (I Think) Calculator Questions

Posted 03 November 2012 - 05:56 PM

CodyMcCoderson, on 03 November 2012 - 03:07 PM, said:

I didnt really want to start over.

But thanks I'll definitely check out those tutorials for some guidance.

There are times when it is best to start over.

One of those times is when you are facing some very tangled code, requiring a lot of fiddling to get it working. Yes, you can do the fiddling, but you usually end up with some very tangled code that now works, but is not of much use later, for other projects.

Another time is when it is actually quicker to rewrite than to fix. The only problem with this is that it's usually difficult to figure out how long it will take for each path, mainly because you have invested a fair amount (or perhaps a lot) of time on the first attempt, and it seems like fixing that would be quicker. What you have trouble seeing is that once you have written the first attempt, a lot of that will go into the rewrite.

Just try to look at your program and decide what parts of it are best put into functions or subroutines, and write those first, testing each function or sub as you complete them. Once that's done, it's easy enough to make the GUI to allow input and to display output.