Subtracting fractions
Time limit1sMemory limit1024 MB
Subtracts the fraction c/d from a/b read from a file, then prints the reduced result both as a plain fraction and as a formatted mixed number.
- Level
Easy2 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
Write a program that can subtract ordinary fractions.
Input
The first and second lines of the text file contain the fractions and , written with a slash. Here and are non-negative, and and are positive integers up to 1000. Find the difference as a reduced fraction or as a mixed number. Output the result both unformatted and formatted.
Output
The first line of the text file contains the integer part of the result (it may be 0). If the fractional part is not zero, output it on the second line as a reduced fraction in the form x/y. If the fractional part is zero, leave the second line of the file empty.
If the fractional part is not zero, also output the result in formatted form. The third line contains the numerator of the fractional part. The fourth line contains the integer part and a dividing line made of minus signs. The fifth line contains the denominator of the fractional part. The dividing line must be as long as the denominator. The numerator and denominator must end at the same position in the line as the dividing line. An integer part equal to zero is not printed in the formatted form.
If the result is negative, mark it with a minus sign in front of the integer part. If the result is negative and there is no integer part, print -0 as the integer part in unformatted form, and in formatted form print a minus sign in front of the fraction without the digit 0 (see the example). A non-negative result is printed without a sign.
Print all results without spaces, except for the spaces at the start of the third and fifth lines that are needed to align the numerator and denominator of the mixed number.