Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fraction Program Using A Data File
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
phuong




PostPosted: Thu Jun 02, 2005 11:21 am   Post subject: Fraction Program Using A Data File

check out my fractions program. it reads the fractions from a data file and performs operations on them. the program also puts the fractions into lowest terms and into mixed fraction form. tell me what you think. ^^
the data file is set up as follows:
operator numerator1 denominator1 numerator2 denominator2



COMP. SCI. - Assignment 10 (May. 29, 2005).t
 Description:
New Uploaded Fraction Program

Download
 Filename:  COMP. SCI. - Assignment 10 (May. 29, 2005).t
 Filesize:  4.07 KB
 Downloaded:  148 Time(s)


fractiondata.t
 Description:
Fraction Data File

Download
 Filename:  fractiondata.t
 Filesize:  96 Bytes
 Downloaded:  166 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Wed Jun 08, 2005 11:28 am   Post subject: (No subject)

After the data is 'sucessfully loaded', an error pops up. Take a look at it.

Quote:

Line 96
Division (or modulus) by zero in Integer expression.
MysticVegeta




PostPosted: Sun Jun 26, 2005 9:13 am   Post subject: (No subject)

How about my fraction program: Instructions are there in the source:
code:
/* Created by : Tan.
 All 3 in one module!
 1: Reduces the Fractions into Lowest terms
 2: Finds the LCM in 2 denomintors
 3: Adds mixed fractions and results the answer in lowest terms!
 */

module Maths  %% Module

    export LCM, fractionAdd, Reduce %% .LCM

    fcn Reduce (whole : int, numerator : int, denominator : int) : string    %%% REDUCTION PROCEDURE

        var num := (denominator * whole) + numerator %% NUMERATOR
        var den := denominator %% DENOMINATOR
        var numorden : int
        var ans : string %% ANSWER

        if num mod den = 0 then %% IF IT IS A LIKE FRACTION
            ans := intstr (num div den) + "/" + "1"
            %%put ans
        else % IF IT IS AN UNLIKE FRACTION

            if den < num then   %% IF THE DENOMINATOR IS GREATOR THEN WE TAKE THE GCF FROM DECREASING DENOMINATOR
                numorden := den
            elsif den > num then %% VICE VERSA
                numorden := num
            end if

            for decreasing s : numorden .. 1 %% GREATEST COMMON FACTOR
                if (num mod s = 0) and (den mod s = 0) then
                    numorden := s %% NUM OR DEN NOW BECOMES THE GCF
                    exit
                end if
            end for

            if den > num then  %% ANSWERS
                %% IF IT CANT BE REDUCED MORE THEN
                ans := intstr (num div numorden) + "/" + intstr (den div numorden)
            elsif den < num then
                %% IF IT CAN BE REDUCED, EXPRESS IT AS A MIXED FRACTION
                ans := intstr ((num div numorden) div (den div numorden)) + " " +
                    intstr ((num div numorden) - (((num div numorden) div (den div numorden)) * den div numorden)) + "/" + intstr (den div numorden)
            end if
        end if

        result ans %% PUTTING THE MAIN ANSWER WE GOT

    end Reduce %% ENDING THE REDUCE


    %%%%% Finds the LCM %%%%%
    fcn LCM (denominator1 : int, denominator2 : int) : int %% Main Function

        var choice, ans : int %choice detemines whether to start counting from numerator or denominator

        if denominator1 > denominator2 then
            choice := denominator1 %if numerator is bigger then choice = numerator
        elsif denominator1 < denominator2 then
            choice := denominator2 %elsif it = denominator
        else
            choice := denominator1 %% both are equal
        end if

        loop
            exit when (choice mod denominator1 = 0) and (choice mod denominator2 = 0)
            choice += 1
        end loop

        ans := choice

        result ans

    end LCM %% Lcm Ends

    %%%%%% Adds the Fractions and then reduces them to lowest terms%%%%%%
    fcn fractionAdd (whole1 : int, num1 : int, den1 : int, whole2 : int, num2 : int, den2 : int) : string
        var wholetotal := whole1 + whole2
        var lcm := LCM (den1, den2)
        var numerator := ((lcm div den1) * num1) + ((lcm div den2) * num2)
        result Reduce (wholetotal, numerator, lcm)
    end fractionAdd

end Maths %% End Module

put Maths.Reduce (0, 10, 25) %Whole num, Numerator, denominator
put skip, Maths.LCM (23, 99) % Denominator1, Denominator2
put skip, Maths.fractionAdd (2, 7, 5, 3, 2, 3) %Whole1, Numerator1, Denominator1, Whole2, Numerator2, Denominator2
Cervantes




PostPosted: Sun Jun 26, 2005 10:15 am   Post subject: (No subject)

You could save yourself some lines in your code, Mystic, by removing the ans variable from your LCM and Reduce functions. Instead of storing the answer into a variable and then resulting that, just result the answer.

Turing:

    fcn Reduce (whole : int, numerator : int, denominator : int) : string    %%% REDUCTION PROCEDURE

        var num := (denominator * whole) + numerator %% NUMERATOR
        var den := denominator %% DENOMINATOR
        var numorden : int

        if num mod den = 0 then %% IF IT IS A LIKE FRACTION
            result intstr (num div den) + "/" + "1"
        end if %% IF IT IS AN UNLIKE FRACTION

        if den < num then       %% IF THE DENOMINATOR IS GREATOR THEN WE TAKE THE GCF FROM DECREASING DENOMINATOR
            numorden := den
        elsif den > num then     %% VICE VERSA
            numorden := num
        end if

        for decreasing s : numorden .. 1     %% GREATEST COMMON FACTOR
            if (num mod s = 0) and (den mod s = 0) then
                numorden := s     %% NUM OR DEN NOW BECOMES THE GCF
                exit
            end if
        end for

        if den > num then      %% ANSWERS
            %% IF IT CANT BE REDUCED MORE THEN
            result := intstr (num div numorden) + "/" + intstr (den div numorden)
        elsif den < num then
            %% IF IT CAN BE REDUCED, EXPRESS IT AS A MIXED FRACTION
            result := intstr ((num div numorden) div (den div numorden)) + " " +
                intstr ((num div numorden) - (((num div numorden) div (den div numorden)) * den div numorden)) + "/" + intstr (den div numorden)
        end if

    end Reduce %% ENDING THE REDUCE


    %%%%% Finds the LCM %%%%%
    fcn LCM (denominator1 : int, denominator2 : int) : int %% Main Function

        var choice : int %choice detemines whether to start counting from numerator or denominator

        if denominator1 > denominator2 then
            choice := denominator1 %if numerator is bigger then choice = numerator
        elsif denominator1 < denominator2 then
            choice := denominator2 %elsif it = denominator
        else
            choice := denominator1 %% both are equal
        end if

        loop
            exit when (choice mod denominator1 = 0) and (choice mod denominator2 = 0)
            choice += 1
        end loop

        result choice

    end LCM %% Lcm Ends


Also, mixed fractions are ugly. Improper fractions are the way to go. Wink
AsianSensation




PostPosted: Sun Jun 26, 2005 10:28 am   Post subject: (No subject)

Here is Euclid's algorithm, it finds the GCD of 2 different numbers.

Turing:
function gcd (a, b : int) : int
    if b = 0 then
        result a
    else
        result gcd (b, a mod b)
    end if
end gcd


I think it's a bit better as the reducing function.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: