This is the output of the program ASSIGNMENT QUESTION: Reduce Fractions [5 point
ID: 3881581 • Letter: T
Question
This is the output of the program
ASSIGNMENT QUESTION:
Reduce Fractions [5 points]
Add a private "simplify()" function to your class and call it from the appropriate member functions. (If you write your code the way that 90% of students write it, there will be 6 places where you need to call it. But if you call it a different number of times and your class works, that's also fine.) The best way to do this is to make the function a void function with no parameters that reduces the calling object.
Recall that "simplifying" or "reducing" a Fraction is a separate task from converting it from an improper Fraction to a mixed number. Make sure you keep those two tasks separate in your mind.
For now (until you get down to the part of the assignment where you improve your insertion operator) your Fractions will still be printed as improper Fractions, not mixed numbers. In other words, 19/3 will still be 19/3, not 6+1/3. Make sure that your class will reduce ANY Fraction, not just the Fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In other words, you should ensure that all Fraction objects are reduced before the end of any member function. To put it yet another way: each member function must be able to assume that all Fraction objects are in simple form when it begins execution.
You must create your own algorithm for reducing Fractions. Don't look up an already existing algorithm for reducing Fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don't use Euclid's algorithm. Don't worry about being efficient. It's fine to have your function check every possible factor, even if it would be more efficient to just check prime numbers. Just create something of your own that works correctly on ANY Fraction.
Your simplify() function should also ensure that the denominator is never negative. If the denominator is negative, fix this by multiplying numerator and denominator by -1. Also, if the numerator is 0, the denominator should be set to 1.
Better Insertion Operator
Now modify your overloaded << operator so that improper Fractions are printed as mixed numbers. Whole numbers should print without a denominator (e.g. not 3/1 but just 3). Improper Fractions should be printed as a mixed number with a + sign between the two parts (2+1/2). Negative Fractions should be printed with a leading minus sign.
Note that your class should have only two data members. Fractions will be stored as improper Fractions. The << operator is responsible for printing the improper Fraction as a mixed number. Also, the '+' in mixed numbers does not mean add. It is simply a separator (to separate the integer part from the Fraction part of the number). So the Fraction "negative two and one-sixth" would be written as -2+1/6, even though -2 plus 1/6 is not what we mean.
Extraction Operator
You should be able to read any of the formats described above (mixed number, negative number, whole numbers, etc.). You may assume that there are no spaces or formatting errors in the Fractions that you read. This means, for example, that in a mixed number only the whole number (not the numerator or denominator) may be negative, and that in a fraction with no whole number part only the numerator (not the denominator) may be negative. Note: You may need to exceed 15 lines for this function. My solution is about 20 lines long.
Since your extraction operator should not consume anything after the end of the Fraction being read, you will probably want to use the .peek() function to look ahead in the input stream and see what the next character is after the first number is read. If it's not either a '/'' or a '+', then you are done reading and should read no further. I have something like this:
Hint: You don't need to detect or read the minus operator as a separate character. When you use the extraction operator to read an int, it will interpret a leading minus sign correctly. So, for example, you shouldn't have "if (in.peek() == '-')"
Hint: The peek() function does not consume the character from the input stream, so after you use it to detect what the next character is, the first thing you need to do is get past that character. One good way to do that would be to use in.ignore(), which ignores one single character in the input stream.
Three Files and Namespaces
Split the project up into three files: client file, implementation file, and header (specification) file. Also, place the class declaration and implementation in a namespace. Normally one would call a namespace something more likely to be unique, but for purposes of convenience we will all call our namespace "cs_Fraction". Namespaces are covered in lesson 16.15.
Add Documentation
See Style Convention 1, especially Style Convention 1D.
Every public member function and friend function, however simple, must have a precondition (if there is one) and a postcondition listed in the header file. Here is a two part explanation of pre- and post- conditions. You'll have to download these to view them: part 1, part 2.
The most complex of your function definitions will need additional comment in the implementation file. Most of the function definitions in the implementation file will not need a comment.
Hints about reading input files:
I suggest that you copy the text from the input file webpage(s) and paste it into a file that you have created using your IDE. The files you create when you type in your IDE are always text files (even if they don't end with .txt). If you're using Windows, you could also use Notepad, but there's no reason to open another application when you are already working in your IDE. I strongly suggest that you don't use TextEdit (Mac) or Word, because these do not store files as text files by default.
Where to save your input file so that your IDE can find it:
In Visual C++, right click on the name of the project in the solution explorer. It appears in bold there. Unless you chose a different name for the project, it will be ConsoleApplication1. From the drop-down menu, choose "show folder in windows explorer". The folder that opens up will be the correct place to save your file.
In Xcode, while on the project navigator tab (looks like a folder) you'll see a yellow folder called Products. click the expansion triangle next to it and your project executable should show up. right click it and choose show in finder. The folder it's in pops up and you can add your input file to that folder.
Here is a tutorial video created by a former student about creating and placing input files in the right spot for Xcode to find them. It's an alternate method. I would suggest watching this video only if the suggestion above doesn't seem to be working for you. In order for these instructions to work, you must have executed a program in your project at least once. Otherwise the folder named "debug" that the video refers to will not exist.
Extensions are part of the file name. If you want to count the words in a file named "myfile.txt", typing "myfile" or "myfile.cpp" won't work.
For Windows users, before you start this assignment, I suggest that you make sure that Windows is showing you the complete file name of your files, including the extensions. Windows hides this from you by default. In Windows 7 and 8 the procedure is as follows:
Choose "control panel" from the start menu
Choose "Folder Options" from the list of control panel items. In Windows 8 you may have to type "Folder Options" into the control panel search bar.
Choose the "view" tab from the Folder Options window
Find the checkbox that says "Hide extensions for known file types".
Uncheck that checkbox.
I've received two different suggestions from student about how to do this in Windows 10. One student says that the procedure is exactly the same as that outlined above, except that the "Folder Options" window is now the "File Explorer Options" window. The other student says this: go to file explorer and click "View" on the toolbar, then go to "Options" on the furthest right, click on the drop down arrow and choose "Change folder and search options". Click "View" on the top and uncheck "Hide extensions for known file types".
Explanation / Answer
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(ifstream& in);
int main()
{
BasicTest();
RelationTest();
BinaryMathTest();
MathAssignTest();
}
void BasicTest()
{
cout << " ----- Testing basic fraction creation & printing ";
cout << "(fractions should be in reduced form, and as mixed numbers.) ";
const fraction fr[] = {fraction(4, 8), fraction(-15,21),
fraction(10), fraction(12, -3),
fraction(), fraction(28, 6), fraction(0, 12)};
for (int i = 0; i < 7; i++){
cout << "fraction [" << i <<"] = " << fr[i] << endl;
}
// Comment the rest of this function out until you finish writing the extraction operator.
cout << " ----- Now reading fractions from file ";
ifstream in("fraction.txt");
assert(in);
while (!eof(in)) {
fraction f;
if (in.peek() == '#') {
in.ignore(128, ' '); //skip this line, it's a comment
} else {
in >> f;
cout << "Read fraction = " << f << endl;
}
}
}
bool eof(ifstream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
void RelationTest()
{
cout << " ----- Testing relational operators between fractions ";
const fraction fr[] = {fraction(3, 6), fraction(1,2), fraction(-15,30),
fraction(1,10), fraction(0,1), fraction(0,2)};
for (int i = 0; i < 5; i++) {
cout << "Comparing " << fr[i] << " to " << fr[i+1] << endl;
cout << " Is left < right? " << (fr[i] < fr[i+1]) << endl;
cout << " Is left <= right? " << (fr[i] <= fr[i+1]) << endl;
cout << " Is left > right? " << (fr[i] > fr[i+1]) << endl;
cout << " Is left >= right? " << (fr[i] >= fr[i+1]) << endl;
cout << " Does left == right? " << (fr[i] == fr[i+1]) << endl;
cout << " Does left != right ? " << (fr[i] != fr[i+1]) << endl;
}
cout << " ----- Testing relations between fractions and integers ";
fraction f(-3,6);
int num = 2;
cout << "Comparing " << f << " to " << num << endl;
cout << " Is left < right? " << (f < num) << endl;
cout << " Is left <= right? " << (f <= num) << endl;
cout << " Is left > right? " << (f > num) << endl;
cout << " Is left >= right? " << (f >= num) << endl;
cout << " Does left == right? " << (f == num) << endl;
cout << " Does left != right ? " << (f != num) << endl;
fraction g(1,4);
num = -3;
cout << "Comparing " << num << " to " << g << endl;
cout << " Is left < right? " << (num < g) << endl;
cout << " Is left <= right? " << (num <= g) << endl;
cout << " Is left > right? " << (num > g) << endl;
cout << " Is left >= right? " << (num >= g) << endl;
cout << " Does left == right? " << (num == g) << endl;
cout << " Does left != right ? " << (num != g) << endl;
}
void BinaryMathTest()
{
cout << " ----- Testing binary arithmetic between fractions ";
const fraction fr[] = {fraction(1, 6), fraction(1,3),
fraction(-2,3), fraction(5), fraction(-4,3)};
for (int i = 0; i < 4; i++) {
cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1] << endl;
cout << fr[i] << " - " << fr[i+1] << " = " << fr[i] - fr[i+1] << endl;
cout << fr[i] << " * " << fr[i+1] << " = " << fr[i] * fr[i+1] << endl;
cout << fr[i] << " / " << fr[i+1] << " = " << fr[i] / fr[i+1] << endl;
}
cout << " ----- Testing arithmetic between fractions and integers ";
fraction f(-1, 2);
int num = 4;
cout << f << " + " << num << " = " << f + num << endl;
cout << f << " - " << num << " = " << f - num << endl;
cout << f << " * " << num << " = " << f * num << endl;
cout << f << " / " << num << " = " << f / num << endl;
fraction g(-1, 2);
num = 3;
cout << num << " + " << g << " = " << num + g << endl;
cout << num << " - " << g << " = " << num - g << endl;
cout << num << " * " << g << " = " << num * g << endl;
cout << num << " / " << g << " = " << num / g << endl;
}
void MathAssignTest()
{
cout << " ----- Testing shorthand arithmetic assignment on fractions ";
fraction fr[] = {fraction(1, 6), fraction(4),
fraction(-1,2), fraction(5)};
for (int i = 0; i < 3; i++) {
cout << fr[i] << " += " << fr[i+1] << " = ";
cout << (fr[i] += fr[i+1]) << endl;
cout << fr[i] << " -= " << fr[i+1] << " = ";
cout << (fr[i] -= fr[i+1]) << endl;
cout << fr[i] << " *= " << fr[i+1] << " = ";
cout << (fr[i] *= fr[i+1]) << endl;
cout << fr[i] << " /= " << fr[i+1] << " = ";
cout << (fr[i] /= fr[i+1]) << endl;
}
cout << " ----- Testing shorthand arithmetic assignment using integers ";
fraction f(-1, 3);
int num = 3;
cout << f << " += " << num << " = ";
cout << (f += num) << endl;
cout << f << " -= " << num << " = ";
cout << (f -= num) << endl;
cout << f << " *= " << num << " = ";
cout << (f *= num) << endl;
cout << f << " /= " << num << " = ";
cout << (f /= num) << endl;
cout << " ----- Testing increment/decrement prefix and postfix ";
fraction g(-1, 3);
cout << "Now g = " << g << endl;
cout << "g++ = " << g++ << endl;
cout << "Now g = " << g << endl;
cout << "++g = " << ++g << endl;
cout << "Now g = " << g << endl;
cout << "g-- = " << g-- << endl;
cout << "Now g = " << g << endl;
cout << "--g = " << --g << endl;
cout << "Now g = " << g << endl;
}
----------------------------------------------------------------------------------------------
fraction.cpp
-----------------------------------------
#include <iostream>
#include "fraction.h"
#include <stdlib.h>
using namespace std;
long l_sign(long lNum)
{
long ret = 1L;
if (lNum < 0L)
ret = -1L;
return ret;
}
long l_min(long num1, long num2)
{
if (num1 < num2)
return num1;
return num2;
}
fraction::fraction(long nuParam, long deParam)
{
numerator = nuParam;
if (deParam != 0L)
denominator = deParam;
else
denominator = 1L;
if (deParam < 0L)
{
numerator = -numerator;
denominator = -denominator;
}
simplify();
}
void fraction::simplify()
{
long nu = abs(numerator);
long de = abs(denominator);
long loopCtrl = 2L;
while ( loopCtrl <= l_min(nu, de) )
{
if (nu % loopCtrl == 0L && de % loopCtrl == 0L)
{
nu /= loopCtrl;
de /= loopCtrl;
}
else
loopCtrl++;
}
if (numerator != 0L)
numerator = l_sign(numerator) * nu;
denominator = de;
}
bool operator==(const fraction & f1, const fraction & f2)
{
bool twoAreEqual = (f1.numerator * f2.denominator == f2.numerator * f1.denominator);
return twoAreEqual;
}
bool operator!=(const fraction & f1, const fraction & f2)
{
bool twoAreNE = (f1.numerator * f2.denominator != f2.numerator * f1.denominator);
return twoAreNE;
}
bool operator<(const fraction & f1, const fraction & f2)
{
bool isLT = (f1.numerator * f2.denominator < f2.numerator * f1.denominator);
return isLT;
}
bool operator<=(const fraction & f1, const fraction & f2)
{
bool isLE = (f1.numerator * f2.denominator <= f2.numerator * f1.denominator);
return isLE;
}
bool operator>(const fraction & f1, const fraction & f2)
{
bool isGT = (f1.numerator * f2.denominator > f2.numerator * f1.denominator);
return isGT;
}
bool operator>=(const fraction & f1, const fraction & f2)
{
bool isGE = (f1.numerator * f2.denominator >= f2.numerator * f1.denominator);
return isGE;
}
fraction& fraction::operator=(const fraction & f)
{
numerator = f.numerator;
denominator = f.denominator;
return *this;
}
fraction& fraction::operator+=(const fraction & f)
{
*this = *this + f;
return *this;
}
fraction& fraction::operator-=(const fraction & f)
{
*this = *this - f;
return *this;
}
fraction& fraction::operator*=(const fraction & f)
{
*this = *this * f;
return *this;
}
fraction& fraction::operator/=(const fraction & f)
{
*this = *this / f;
return *this;
}
fraction operator+(const fraction & f1, const fraction & f2)
{
long numerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
long denominator = f1.denominator * f2.denominator;
fraction temp(numerator, denominator);
return temp;
}
fraction operator-(const fraction & f1, const fraction & f2)
{
long numerator = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
long denominator = f1.denominator * f2.denominator;
fraction temp(numerator, denominator);
return temp;
}
fraction operator*(const fraction & f1, const fraction & f2)
{
long numerator = f1.numerator * f2.numerator;
long denominator = f1.denominator * f2.denominator;
fraction temp(numerator, denominator);
return temp;
}
fraction operator/(const fraction & f1, const fraction & f2)
{
long numerator = 0L;
long denominator = 1L;
if (f2.numerator != 0L) // execute the division if the denominator does not become zero
{
numerator = f1.numerator * f2.denominator;
denominator = f1.denominator * f2.numerator;
} else // if the denominator will be zero, return this fraction value instead
{
numerator = f1.numerator;
denominator = f1.denominator;
}
fraction temp(numerator, denominator);
return temp;
}
fraction& fraction::operator++()
{
numerator += denominator;
return *this;
}
fraction fraction::operator++(int unused)
{
fraction temp(*this);
numerator += denominator;
return temp;
}
fraction& fraction::operator--()
{
numerator -= denominator;
return *this;
}
fraction fraction::operator--(int unused)
{
fraction temp(*this);
numerator -= denominator;
return temp;
}
ostream& operator<<(ostream & out, const fraction & f)
{
if (f.denominator == 1L)
out << f.numerator;
else if (f.numerator == 0L)
out << 0;
else if (abs(f.numerator) < abs(f.denominator))
out << f.numerator << "/" << f.denominator;
else
{
long wholeNum = f.numerator / f.denominator;
long numeratorNum = abs(f.numerator) - abs(wholeNum) * f.denominator;
out << wholeNum << "+" << numeratorNum << "/" << f.denominator;
}
return out;
}
istream& operator>>(istream & in, fraction & f)
{
char c;
long sign = 1L;
long temp = 0L;
if (in.peek() == '-')
{
in >> c;
sign = -1L;
}
in >> temp;
if (in.peek() == ' ' || in.peek() == ' ' || in.peek() == ' ')
{
f.numerator = sign * temp;
f.denominator = 1L;
}
else if (in.peek() == '/')
{
in >> c;
f.numerator = sign * temp;
in >> temp;
f.denominator = temp;
}
else if (in.peek() == '+')
{
long wholeNum = temp;
in >> c;
in >> temp;
if (in.peek() == '/')
{
in >> c;
f.numerator = temp;
in >> temp;
f.denominator = temp;
f.numerator = sign * (wholeNum * f.denominator + f.numerator);
}
}
f.simplify();
return in;
}
----------------------------------------------------------------------------------------------
fraction.h
--------------------------------------------
#ifndef FRACTION_H_
#define FRACTION_H_
class fraction
{
private:
long numerator;
long denominator;
void simplify();
public:
fraction(long numerator = 0L, long denominator = 1L);
friend bool operator==(const fraction &, const fraction &);
friend bool operator!=(const fraction &, const fraction &);
friend bool operator<(const fraction &, const fraction &);
friend bool operator<=(const fraction &, const fraction &);
friend bool operator>(const fraction &, const fraction &);
friend bool operator>=(const fraction &, const fraction &);
fraction& operator=(const fraction &);
fraction& operator+=(const fraction &);
fraction& operator-=(const fraction &);
fraction& operator*=(const fraction &);
fraction& operator/=(const fraction &);
friend fraction operator+(const fraction &, const fraction &);
friend fraction operator-(const fraction &, const fraction &);
friend fraction operator*(const fraction &, const fraction &);
friend fraction operator/(const fraction &, const fraction &);
fraction& operator++();
fraction operator++(int);
fraction& operator--();
fraction operator--(int);
friend std::ostream& operator<<(std::ostream &out, const fraction & f);
friend std::istream& operator>>(std::istream &in, fraction & f);
};
#endif /* FRACTION_H_ */
------------------------------------------------------------------------------------------------------------------
Output
-----------------------------------------
----- Testing basic fraction creation & printing
(fractions should be in reduced form, and as mixed numbers.)
fraction [0] = 1/2
fraction [1] = -5/7
fraction [2] = 10
fraction [3] = -4
fraction [4] = 0
fraction [5] = 4+2/3
fraction [6] = 0
----- Now reading fractions from file
Read fraction = 1/3
Read fraction = 1/2
Read fraction = 3/4
Read fraction = -4/5
Read fraction = 6
Read fraction = 5
Read fraction = -8
Read fraction = 1+2/5
Read fraction = -16+2/3
Read fraction = 1+1/4
Read fraction = 2
Read fraction = -4+1/4
Read fraction = -10+5/6
----- Testing relational operators between fractions
Comparing 1/2 to 1/2
Is left < right? 0
Is left <= right? 1
Is left > right? 0
Is left >= right? 1
Does left == right? 1
Does left != right ? 0
Comparing 1/2 to -1/2
Is left < right? 0
Is left <= right? 0
Is left > right? 1
Is left >= right? 1
Does left == right? 0
Does left != right ? 1
Comparing -1/2 to 1/10
Is left < right? 1
Is left <= right? 1
Is left > right? 0
Is left >= right? 0
Does left == right? 0
Does left != right ? 1
Comparing 1/10 to 0
Is left < right? 0
Is left <= right? 0
Is left > right? 1
Is left >= right? 1
Does left == right? 0
Does left != right ? 1
Comparing 0 to 0
Is left < right? 0
Is left <= right? 1
Is left > right? 0
Is left >= right? 1
Does left == right? 1
Does left != right ? 0
----- Testing relations between fractions and integers
Comparing -1/2 to 2
Is left < right? 1
Is left <= right? 1
Is left > right? 0
Is left >= right? 0
Does left == right? 0
Does left != right ? 1
Comparing -3 to 1/4
Is left < right? 1
Is left <= right? 1
Is left > right? 0
Is left >= right? 0
Does left == right? 0
Does left != right ? 1
----- Testing binary arithmetic between fractions
1/6 + 1/3 = 1/2
1/6 - 1/3 = -1/6
1/6 * 1/3 = 1/18
1/6 / 1/3 = 1/2
1/3 + -2/3 = -1/3
1/3 - -2/3 = 1
1/3 * -2/3 = -2/9
1/3 / -2/3 = -1/2
-2/3 + 5 = 4+1/3
-2/3 - 5 = -5+2/3
-2/3 * 5 = -3+1/3
-2/3 / 5 = -2/15
5 + -1+1/3 = 3+2/3
5 - -1+1/3 = 6+1/3
5 * -1+1/3 = -6+2/3
5 / -1+1/3 = -3+3/4
----- Testing arithmetic between fractions and integers
-1/2 + 4 = 3+1/2
-1/2 - 4 = -4+1/2
-1/2 * 4 = -2
-1/2 / 4 = -1/8
3 + -1/2 = 2+1/2
3 - -1/2 = 3+1/2
3 * -1/2 = -1+1/2
3 / -1/2 = -6
----- Testing shorthand arithmetic assignment on fractions
1/6 += 4 = 4+1/6
4+1/6 -= 4 = 1/6
1/6 *= 4 = 2/3
2/3 /= 4 = 1/6
4 += -1/2 = 3+1/2
3+1/2 -= -1/2 = 4
4 *= -1/2 = -2
-2 /= -1/2 = 4
-1/2 += 5 = 4+1/2
4+1/2 -= 5 = -1/2
-1/2 *= 5 = -2+1/2
-2+1/2 /= 5 = -1/2
----- Testing shorthand arithmetic assignment using integers
-1/3 += 3 = 2+2/3
2+2/3 -= 3 = -1/3
-1/3 *= 3 = -1
-1 /= 3 = -1/3
----- Testing increment/decrement prefix and postfix
Now g = -1/3
g++ = -1/3
Now g = 2/3
++g = 1+2/3
Now g = 1+2/3
g-- = 1+2/3
Now g = 2/3
--g = -1/3
Now g = -1/3
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.