Hi guys; posted this before but apparently I completely fucked up my assignment.
Scored 18 out of a possible 120...
I've been trying to account for the problems but I just can't do it..
Here's the simple prompt:
Design a C++ program to convert fractions into one of the following standard formats (whichever is appropriate):
-numerator/denominator (4/7)
-whole_number numerator/denominator (3 10/12)
-whole_number (-83) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main ()
{
int num, denom; // Declaring num as numerator and denom as denominator
char ch1; // Declaring the character needed for slash
cout << " Please enter a fraction (numerator / denominator) " << endl;
cin >> num >> ch1 >> denom;
if (num % denom == 0) // If the fraction can be solved then solve it.
{
cout << num << ch1 << denom << " " << "=" << " ";
cout << num/denom << endl;
}
else if (num < denom) // If the numerator is less than the denominator then simply display it.
{
cout << num << ch1 << denom << " " << "=" << " ";
cout << num << "/" << denom << endl;
}
else if (num > denom) // If the numerator is greater than the denominator then solve it and reformat it in standard f\
ormat.
{
cout << num << ch1 << denom << " " << "=" << " ";
cout << num/denom << " " << num%denom << "/" << denom << endl;
else if (denom == 0) // If the denominator is equal to zero then display a error message.
{
cout << num << ch1 << denom << " " << "=" << " ";
cout << (num/denom);
cout << "You can't divide by 0!" << endl;
}
else if (num == 0) // If the numerator is zero then display 0.
{
cout << num << ch1 << denom << " " << "=" << " ";
cout << "0" << endl;
}
else if ( num > 0 && denom > 0) // If both the numerator and denominator are less than zero then find the absolute va\
lue and display the fraction.
{
num = abs(num);
denom = abs(denom);
cout << num << ch1 << denom << " " << "=" << " ";
cout << abs(num/denom) << endl;
}
return 0;
}
|
I need to do the following:
When displaying a fraction in this manner,
If the fraction is negative, the negative sign should precede the numerator, not the denominator. For example, 2 / -3 should be displayed as -2/3.
If the fraction is positive, both the numerator and denominator should be positive. For example, -4 / -5 should be displayed as 4/5.
If the denominator is 0, the fraction is not valid.
If the fraction is improper, the fraction should be converted to a whole or mixed number and, if negative, the negative sign should precede the whole number. For example, -23 / 4 should be displayed as -5 3/4. 16/8 should be 2. |
This is what I need to fix:
-Account for zero in the denominator
-Account for zero in the numerator
-Find the absolute value if necessary.
Right now I get a floating point error if I try to account for zero in either the numerator or denominator.
Also if I have a fraction of the following: 13/5 then my program will correctly display 2 3/5.
But if I have a fraction of the following: -13/5 then my program doesn't display it properly.