Operation on fraction

This is a discussion on Operation on fraction within the C Programming forums, part of the General Programming Boards category; Can anyone help me to write a program with the problem as shown below: Write a complete C program to ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    Operation on fraction

    Can anyone help me to write a program with the problem as shown below:

    Write a complete C program to perform computations with common fractions and get results that are common fractions in reduced form. Your program shoud be able to perform addition, subtraction, multiplication or division on the given two fractions of a/b where a and b are integers representing numerator and denominator respectively.
    Sample input and output:
    Enter the first fraction as two integers seperated by a slash> 3/4
    Enter the second fraction as two integers seperated by a slash> 5/8
    Enter an arithmatic operator (+,-,*,/)> +
    3/4 + 5/8 = 11/8
    Would you like to continue<Y/N> Y
    Enter the first fraction as two integers seperated by a slash> 3/4
    Enter the second fraction as two integers seperated by a slash> 5/8
    Enter an arithmatic operator (+,-,*,/)> *
    3/4 * 5/8 = 15/32
    Would you like to continue?<Y/N> N
    Bye. Bye...have a nice day!

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I would start by programming something and then asking for help when you get stuck.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I wrote an algebra solver that handled this sort of stuff. If you are just doing straight fractions you could start off with something like:

    Example:
    Code:
    struct fraction_t
    {
        signed int numerator;
        unsigned int denominator;
    };
    From there on out, it should be fairly simple logic for you to program on your own, right? On my algebra solver, however, I stored the numerator and denominator in a custome type that was designed to be arbitrary precision. You'd probably get some bonus points from your teacher for writing something like that.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    I would review the use to strtok(). To split your inputed string on '\'. Then do the appropriate math on your numerator and denominator.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    sorry I think the solution you all give is in C++ not C program. Can you give me in C language? Thanks

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by boonjie View Post
    sorry I think the solution you all give is in C++ not C program. Can you give me in C language? Thanks
    Your thought is incorrect. All the above are C.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,446
    Both master5001's and slingerland3g's suggestions are entirely within the realm of C.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I don't understand what you need help with. Can you write a Hello World program? Do you know what a variable is? Do you know how to get input from the user? Do you understand how the math works?

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am always more of a C programmer than C++ programmer, for whatever that is worth, boonjie. C is to my speaking English as C++ is to my speaking Spanish (granted I am more confident and fluent in C++ than Spanish, but I digress).

    I don't know of a cleaner way of doing it than using a structure to keep track of your numerators and denominators. You could also use an array of integers and consider one index to be the numerator and the other to be the denominator.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Quote Originally Posted by master5001 View Post
    I am always more of a C programmer than C++ programmer, for whatever that is worth, boonjie. C is to my speaking English as C++ is to my speaking Spanish (granted I am more confident and fluent in C++ than Spanish, but I digress).

    I don't know of a cleaner way of doing it than using a structure to keep track of your numerators and denominators. You could also use an array of integers and consider one index to be the numerator and the other to be the denominator.
    But how to give the answer in reduce form?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by boonjie View Post
    But how to give the answer in reduce form?
    You need to find the greatest common divisor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Euclidean algorithm...

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I will reiterate what you have been already told several times, you produce some code, you will get some code in return. I will give psuedo code to people who sound like they need help and do know how to program, but just don't know where to start. And finally, I will write source code for those who are actually trying.

    Try my friend. Do something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 07:15 PM
  2. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 11:38 AM
  3. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 08:20 PM
  4. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 07:48 AM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 07:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21