Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Fraction Program Using A Data File
Author Message
phuong

Posted: 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:

Filename:  COMP. SCI. - Assignment 10 (May. 29, 2005).t
Filesize:  4.07 KB

fractiondata.t
Description:
 Fraction Data File

Filename:  fractiondata.t
Filesize:  96 Bytes

jamonathin

Posted: 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

Posted: 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

Posted: 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.
AsianSensation

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: