1. A valid fraction must have a denominator that is positive and non-zero. The numerator can be any integer value.

2. A proper fraction is valid and the absolute value of the numerator is smaller than the absolute value of the denominator.

3. An improper fraction is valid and is not a proper fraction.

4. An improper fraction can be converted to a whole or mixed number. For example, -3/2 == -1 1/2 and 48/6 == 8

Design a C++ program that will

* Display your name, section #, and the assignment # at the start of your output.

* Use Linux redirection to read a set of input values from a data file. The file will consist of fractions. Each fraction will consist of 3 components: an integer (representing the numerator), a forward slash ‘/’, an integer (representing the denominator). There may be 0 or more blanks between the 3 components. Samples: 24/36 10 / 15 -3/ 17 25 /-5

* For each fraction

o Determine if the fraction is invalid because the denominator is zero. If so, it cannot be adjusted (turned into a valid fraction).

o If the fraction is invalid because the denominator is negative, it can be adjusted so that it will be valid. Move the negative sign from the denominator to the numerator (perform the arithmetic necessary to make the denominator positive and change the sign of the numerator appropriately). Note: if the numerator is zero, do not make the numerator negative (i.e. 0/-4 should become 0/4 not -0/4).

o Count the number of fractions that are invalid and cannot be adjusted, the number of proper fractions (including adjustment) and the number of improper fractions (including adjustment).

* Required output for each fraction (all on one line – see sample output below)

o Display the original input values with no blank spaces between them.

o If the fraction was invalid and could not be adjusted, display a message indicating that the denominator is zero.

o If the fraction is valid (after any necessary adjustment)

+ If the fraction is proper, display the fraction in a valid form (see fact #1 above). If the fraction can be simplified (numerator and denominator have a common divisor), reduce it to lowest terms and display reduced fraction.

# if input is -12 / -16, the output should be -12/-16 == 12/16 == 3/4

+ If the fraction is improper, display the valid form (see fact #1 above). If the fraction can be simplified (numerator and denominator have a common divisor), reduce it to lowest terms and display reduced fraction followed by the mixed or whole number equivalent.

# if input is 24/ -10, the output should be 24/-10 == -24/10 == -12/5 == -2 2/5

o Display the counts of invalid, proper, and improper fractions read from the input file with appropriate labels.

Formatting Requirements

* Separate the original input fraction and the message/reformatted fraction/simplified fraction/mixed number equivalent with ” == “. See sample output below.

* If the numerator of a fraction is 0, do not attempt to simplify it.

* When displaying a whole number equivalent, do not display a fractional component with a zero numerator.

o 24/6 == 4 not 4 0/6

* When displaying a mixed number equivalent, there should be a blank space between the whole number and fractional parts.

* When displaying a negative mixed number equivalent, the negative sign should precede the whole number part.

Input File Assumptions

* Each fraction will consist of a valid integer, followed by a forward slash ‘/’, followed by another valid integer.

* Each value in a fraction will be separated by zero or more blanks.

* The program should continue processing input until the end of file is reached.

NOTE: The header file cstdlib contains a library function called abs(n) that expects one int type parameter, n, and returns the absolute value of n.

Also, please note that the fraction, if negative, must have the negative sign in front for the end result. (see 144/ -256 should end result as this)

144/ -256 == -144/256 == -9/16

Here is a sample of how the program should run:

[Bob@bobby]$ more data4six2

15/ 0

-43 / 10

144 / -256

0/ 23

300 /15

[Bob@bobby]$ g++ hw06v2.cpp

[Bob@bobby]$ ./a.out

Bobby Reyes Assignment #6 v2 Section #100_

15/0 == is invalid because denominator is zero

-43/10 == -43/10 == -4 3/10

144/-256 == -144/256 == -9/16

0/23 == 0/23

300/15 == 300/15 == 20/1 == 20

Invalid fractions: 1

Proper fractions: 2

Improper fractions: 2

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *