Thread: Operation on fraction

  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
    603
    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,336
    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
    28,413
    Both master5001's and slingerland3g's suggestions are entirely within the realm of C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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, 09: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, 08:56 PM