C++ C++ Client program Data file Correct output Reduce Fractions Add a private \
ID: 638450 • Letter: C
Question
C++
C++
Client program
Data file
Correct output
Reduce Fractions
Add a private "simplify()" function to your class and call it from the appropriate member functions. (There will probably be 5 places where you need to call it.) The best way to do this is to make the function a void function with no parameters that reduces the calling object.
For now 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.
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. 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:
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 "cs2b_fraction".
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. There is a section on preconditions and postconditions in your textbook. In the fifth edition it is on page 121.
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.
Name your files fraction.h and fraction.cpp.
Last week's assignment is here:
http://www.chegg.com/homework-help/questions-and-answers/c-question-operator-overloading-client-program-include-include-using-namespace-std-void-ba-q6453387
Explanation / Answer
Source-code : https://dl.dropboxusercontent.com/u/20998734/Fraction.zip
Online Testing URL : http://goo.gl/NwqLIo
fraction.h:
#include <iostream>
#include <string>
using namespace std;
namespace cs2b_fraction{
class fraction{
private:
int num;
int den;
void simplify(){
if (this->den<0) { // if denominator is negative multiply both numerator and deno by -1.
this->num=-this->num;
this->den=-this->den;
}
int max=abs(this->num)>abs(this->den)?abs(this->num):abs(this->den); // find the max by applying abs function incase either part of fractions are negative.
bool divided=false;
for(int i=2;i<=max;i++){ // divide the num and deno from factor 2 till max out of two. If divided set the bool var to true.
if(((this->num%i)==0) && ((this->den%i)==0)){
this->num=this->num/i;
this->den=this->den/i;
divided=true;
}
}
if (divided) simplify(); // call the simplfy method again incase the number are divided
}
public:
//Construction of a fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and
//denominator, just one is assumed to be a whole number, and zero arguments creates a zero fraction.
//Use default parameters so that you only need a single function to implement all three of these constructors.
fraction(int num=0, int den=1){
this->den = (den!=0)?den:1;
this->num = num;
this->simplify();
}
//Printing a fraction to a stream with an overloaded << operator.
friend ostream& operator<<(ostream& out, const fraction& obj){
int r=abs(obj.num%obj.den); // calculate the absolute value of remainder between numerator and denominator
int no;
if(obj.den==1 || obj.num == 0) { // Print numerator incase numerator is 0 or denominator is 1
out << obj.num;
}
else if (abs(obj.den)>abs(obj.num)){ // Incase deno > num and num>0 then
out<<obj.num<<"/"<<obj.den; // Print the fraction as it is.
}
else if (obj.num<0){ // Incase of numerator is less than 0 i.e. -ve
// else if num is -ve then set num = remainder of deno and nume
r=obj.den-r; // subtract absolute value of remainder from deno and set it as numerator;
no=-((abs(obj.num)+r)/obj.den); // set the first part by adding the new numerator + -nve numeartor and divide it by deno.
out << no <<"+"<<r<<"/"<<obj.den;
}
else { // Incase num > deno and num>0 then
int no=((obj.num-r)/obj.den); // first part is remainder + existing numerator / divided by deno.
out << no <<"+"<<r<<"/"<<obj.den;
}
return out;
}
//Read the fraction data from input stream
friend istream& operator>>(istream& in, fraction& r)
{
int num, den,n=0;
// Read the top
in >> num;
// If there is a '+', read the next number
char c;
in >> c;
if (c == '+'){
in >> n;
den=num;
num=n;
n=den;
in >> c;
}
if (c == '/')
in >> den;
else
{
in.putback(c);
den = 1;
}
num=(den*n)+num;
r = fraction(num, den);
return in;
}
//All six of the relational operators (<, <=, >, >=, ==, !=) should be supported.
//They should be able to compare fractions to other fractions as well as fractions to integers.
//Either fractions or integers can appear on either side of the binary comparison operator.
//You should only use one function for each operator.
friend bool operator<(const fraction& obj,const fraction& obj2){
return (obj.num*obj2.den<obj.den*obj2.num);
}
friend bool operator<=(const fraction& obj,const fraction& obj2){
return (obj.num*obj2.den<=obj.den*obj2.num);
}
friend bool operator>(const fraction& obj,const fraction& obj2){
return (obj.num*obj2.den>obj.den*obj2.num);
}
friend bool operator>=(const fraction& obj,const fraction& obj2){
return (obj.num*obj2.den>=obj.den*obj2.num);
}
friend bool operator==(const fraction& obj,const fraction& obj2){
return (obj.num*obj2.den==obj.den*obj2.num);
}
friend bool operator!=(const fraction& obj,const fraction& obj2){
return (obj.num*obj2.den!=obj.den*obj2.num);
}
//The four basic arithmetic operations (+, -, *, /) should be supported.
//Again, they should allow fractions to be combined with other fractions, as well as with integers.
//Either fractions or integers can appear on either side of the binary operator.
//Only use one function for each operator.
friend fraction operator+(const fraction& obj,const fraction& obj2){
return fraction(obj.num*obj2.den+obj.den*obj2.num, obj.den*obj2.den);
}
friend fraction operator-(const fraction& obj,const fraction& obj2){
return fraction(obj.num*obj2.den-obj.den*obj2.num, obj.den*obj2.den);
}
friend fraction operator*(const fraction& obj,const fraction& obj2){
return fraction(obj.num*obj2.num, obj.den*obj2.den);
}
friend fraction operator/(const fraction& obj,const fraction& obj2){
return fraction(obj.num*obj2.den,obj.den*obj2.num);
}
//The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented.
//fractions can appear on the left-hand side, and fractions or integers on the right-hand side.
fraction& operator+=(const fraction& obj){
*this = *this+obj;
simplify();
return *this;
}
fraction& operator-=(const fraction& obj){
*this = *this-obj;
return *this;
}
fraction& operator*=(const fraction& obj){
*this = *this*obj;
return *this;
}
fraction& operator/=(const fraction& obj){
*this = *this/obj;
return *this;
}
//The increment and decrement (++, --) operators should be supported in both prefix and postfix form for fractions.
//To increment or decrement a fraction means to add or subtract (respectively) one (1).
fraction& operator++(){
*this +=1;
return *this;
}
fraction& operator--(){
*this -=1;
return *this;
}
fraction operator++(int k){
fraction local(*this);
++(*this);
return local;
}
fraction operator--(int k){
fraction local(*this);
--(*this);
return local;
}
};
}
main.cpp:
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;
using namespace cs2b_fraction;
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(ifstream& in);
string boolString(bool convertMe);
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;
}
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;
}
string boolString(bool convertMe) {
if (convertMe) {
return "true";
} else {
return "false";
}
}
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? " << boolString(fr[i] < fr[i+1]) << endl;
cout << " Is left <= right? " << boolString(fr[i] <= fr[i+1]) << endl;
cout << " Is left > right? " << boolString(fr[i] > fr[i+1]) << endl;
cout << " Is left >= right? " << boolString(fr[i] >= fr[i+1]) << endl;
cout << " Does left == right? " << boolString(fr[i] == fr[i+1]) << endl;
cout << " Does left != right ? " << boolString(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? " << boolString(f < num) << endl;
cout << " Is left <= right? " << boolString(f <= num) << endl;
cout << " Is left > right? " << boolString(f > num) << endl;
cout << " Is left >= right? " << boolString(f >= num) << endl;
cout << " Does left == right? " << boolString(f == num) << endl;
cout << " Does left != right ? " << boolString(f != num) << endl;
fraction g(1,4);
num = -3;
cout << "Comparing " << num << " to " << g << endl;
cout << " Is left < right? " << boolString(num < g) << endl;
cout << " Is left <= right? " << boolString(num <= g) << endl;
cout << " Is left > right? " << boolString(num > g) << endl;
cout << " Is left >= right? " << boolString(num >= g) << endl;
cout << " Does left == right? " << boolString(num == g) << endl;
cout << " Does left != right ? " << boolString(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.txt:
# This file shows the patterns your fraction class needs to be able to
# read. A fraction may be just a single integer, two integers separated by
# a slash, or a mixed number which consists of an integer, followed by a +
# and then two integers with a slash. A minus sign may appear in the
# very first character to indicate the whole fraction is negative.
# No white space is allowed in between the component parts of a fraction.
#
1/3
3/6
3072/4096
-4/5
12/2
5
-8
21/15
-50/3
1+1/4
1+5/5
-4+3/12
-10+10/12
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 = -17+1/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? false
Is left <= right? true
Is left > right? false
Is left >= right? true
Does left == right? true
Does left != right ? false
Comparing 1/2 to -1/2
Is left < right? false
Is left <= right? false
Is left > right? true
Is left >= right? true
Does left == right? false
Does left != right ? true
Comparing -1/2 to 1/10
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing 1/10 to 0
Is left < right? false
Is left <= right? false
Is left > right? true
Is left >= right? true
Does left == right? false
Does left != right ? true
Comparing 0 to 0
Is left < right? false
Is left <= right? true
Is left > right? false
Is left >= right? true
Does left == right? true
Does left != right ? false
----- Testing relations between fractions and integers
Comparing -1/2 to 2
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing -3 to 1/4
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
----- 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 = -6+1/3
-2/3 * 5 = -4+2/3
-2/3 / 5 = -2/15
5 + -2+2/3 = 3+2/3
5 - -2+2/3 = 6+1/3
5 * -2+2/3 = -7+1/3
5 / -2+2/3 = -4+1/4
----- Testing arithmetic between fractions and integers
-1/2 + 4 = 3+1/2
-1/2 - 4 = -5+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 = -2+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 = -3+1/2
-3+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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.