Adding, subtracting, dividing and multipying Negative numbers

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
ucla12Asked:
Who is Participating?
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.

PockyMasterCommented:
uhm, what about

 Dim i As Integer = 1
        i *= -1

0
ucla12Author Commented:
was my question that dumb?   sorry about that ,  does this work for addiing and subtracting???
0
bchoorCommented:
for adding and subtracting fractions - say 2/8 + 1/2
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


0
Cloud Class® Course: CompTIA Cloud+

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

bchoorCommented:
I found this to calculate HCF - it was in C++, I changed it to VB.NET

    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
0
PockyMasterCommented:
I created this class for you:


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!
0

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
bchoorCommented:
Nice job Pocky...I came across this one as well http://www.vb-helper.com/howto_fraction_class.html

~BC
0
PockyMasterCommented:
Actually after I posted I thought I forgot 2 methods:

  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))
0
PockyMasterCommented:
Uhm...

 Public Function Subtract(ByVal frac As CFraction) As CFraction

        Return CFraction.Subtract(Me, frac)
    End Function

Like this ofcourse :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.