The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

I have a Fraction Calculator I pieced together in Visual basic 2005 express. My kids use to check homework. It works just fine But my oldest child has been getting some problems that have negative numbers example: -1/2 X 1/4 = -1/8 The rules for multipying and division are simple...If both fractions (numbers) have the same sign the anwser is positive But with adding or subtracting it is a diffrerent story. Is there a built in math function or a way to assign a number to be negative? if so will it follow the negative / position rukles of math. On a calculator you would use the +/- button. Or do I have to create my own code? any help would be appreicated. I have my code I am using now if you need to see it.....Thanks

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1. you would need to get the denominators and multiply them

denominators are 8 and 2 ==> 8 * 2 = 16

2. then cross multiply

so 2 * 2 = 4 (where the first 2 is from the numerator in 2/8 and the second 2 is from the denominator in 1/2)

also 1 * 8 = 8

3. Now both fractions have the same denominator so you can add and subtract easily

4/16 + 8/16 ==> (4+8)/16 = 12/16

Let's say you have 3/8 - 1/4

1. 8*4 = 32

2. 3*4 = 12; 1*8 = 8

3. 12/32 - 8/32 ==> (12-8)/32 = 4/32

The only thing missing is making the result into a proper fraction which I'm not sure how to achieve

i.e 4/32 = 1/8 or 12/16 = 3/4

Usually you would try to find the HCF (Highest Common Factor of both numbers),

in the case of 4 and 32 - the HCF is 4 ==> (4/4) / (32/4) = 1/8

in the case of 12 and 16 - the HCF is 4 ==> (12/4) / (16/4) = 3/4

Hope this makes some sense

~BC

Public Function GetHCF(ByVal A As Integer, ByVal B As Integer) As Integer

' Make sure B > A, if not switch it around

If A = B Then

Return A

ElseIf A > B Then

Dim temp As Integer = B

B = A

A = temp

End If

Dim C As Integer

Do

C = B Mod A

If C = 0 Then

Return A

Else

B = A

A = C

End If

Loop

End Function

HTH

~BC

Public Class CFraction

Private _iNum As Integer

Private _iDenum As Integer

Private _iSign As Integer = 1

Public Sub New(ByVal iNum As Integer, ByVal iDenum As Integer)

_iNum = iNum

_iDenum = iDenum

End Sub

Public Sub ToggleSign()

_iSign *= -1

End Sub

Public ReadOnly Property Sign() As Integer

Get

Return _iSign

End Get

End Property

Public ReadOnly Property Denumerator() As Integer

Get

Return _iDenum

End Get

End Property

Public ReadOnly Property Numerator() As Integer

Get

Return _iNum

End Get

End Property

Public Shared Function Add(ByVal frac1 As CFraction, ByVal frac2 As CFraction) As CFraction

If frac1 Is Nothing OrElse frac2 Is Nothing Then Return Nothing

Dim iMultiply As Integer = frac1.Denumerator * frac2.Denumerator

Dim iNumerator As Integer = (frac1.Sign * frac1.Numerator * iMultiply / frac1.Denumerator) + (frac2.Sign * frac2.Numerator * iMultiply / frac2.Denumerator)

Return New CFraction(iNumerator, iMultiply)

End Function

Public Shared Function Subtract(ByVal frac1 As CFraction, ByVal frac2 As CFraction) As CFraction

frac2.ToggleSign()

Return Add(frac1, frac2)

End Function

End Class

try to use it like this:

Dim frac1 As New CFraction(1, 4)

Dim frac2 As New CFraction(1, 3)

Dim frac3 As CFraction = CFraction.Add(frac1, frac2)

Dim frac4 As CFraction = CFraction.Subtract(frac1, frac2)

You might add the HCF function of bcHoor to normalize your fraction result

Good luck!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial~BC

Public Function Add(ByVal frac As CFraction) As CFraction

Return CFraction.Add(Me, frac)

End Function

Public Function Subtract(ByVal frac As CFraction) As CFraction

Return CFraction.Add(Me, frac)

End Function

That will allow you to do :

Dim frac1 As New CFraction(1, 4)

Dim frac2 As New CFraction(1, 3)

Dim frac3 As CFraction = frac1.Add(frac2)

or to put it on a single line:

Dim fracResult As CFraction = New CFraction(1, 4).Add(New CFraction(1, 3))

Public Function Subtract(ByVal frac As CFraction) As CFraction

Return CFraction.Subtract(Me, frac)

End Function

Like this ofcourse :D

Visual Basic.NET

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

Dim i As Integer = 1

i *= -1