My problem consists of attempting to reduce a mixed fraction and not reducing the positive mixed fraction to match the output of my assignment *Will provide that below*. I am also having trouble reading a Fraction from a stream using an istream extraction (>>) overloaded operator and displaying the file contents which I will also provide sample output below as well. Any help would be appreciated I am really stuck.
---SAMPLE OUTPUT FOR BOTH THE REDUCING AND ISTREAM READABILITY-----
***********************************************************************
* Basic Test: Testing member constructor, simplify() and nonmember *
* friend ostream << operator for basic Fraction object 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
***********************************************************************
* Basic Test: Testing simplify() and nonmember friend istream >> and *
* ostream << operators for reading and display of Fraction objects *
* from data file *
***********************************************************************
Enter file name with fraction data to read: frac.txt
frac.txt not found!
Make sure the fraction data file to read is in the project folder.
Enter file name with fraction data to read: frac_data.txt
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
MY OUTPUT FOR THE FIRST PART:
* friend ostream << operator for basic Fraction object 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+0/-1
Fraction [4] = 0
Fraction [5] = 4+2/3
Fraction [6] = 0
Project#2 -Fraction class (ADT) testing now concluded.
Check output for correct results from the Fraction class (ADT) implementation.
Process returned 0 (0x0) execution time : 0.094 s
Press any key to continue.
Code for my header file
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
|
/*USED FOR CLASS WITH FUNCTIONS ETC */
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
using namespace std;
namespace cs10b_fraction
{
class Fraction
{
public:
Fraction(int n = 0, int d = 1)
{
num = n;
denom = d;
assert(denom != 0);
Simplify();
}
friend ostream& operator << (std::ostream &out, const Fraction printMe);
friend std::istream& operator >> (std::istream &in, Fraction readMe);
private:
int num;
int denom;
void Simplify();
};
}
#endif
|
Code for my implementation file
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/* USED FOR FUNCTIONS */
#include <iostream>
#include <cmath>
#include <cctype>
#include <cassert>
#include "fraction.h"
using namespace std;
namespace cs10b_fraction
{
void Fraction::Simplify()
//Pre:
//This function will reduce the two fractions
//after it has been transformed and output them
//correctly
//Post:
//The result of the two fractions after they are transformed
//are reduced
{
//For loop that goes through and simplifies fraction
for(int i = abs(num * denom); i > 1; i--)
{
if((num % i) == 0 && (denom % i) == 0)
{
num = num / i;
denom = denom / i;
}
}
}
ostream& operator << (std::ostream &out, const Fraction printMe)
{
if (printMe.denom == 1)
{
out << printMe.num << endl;
}
else if (printMe.num == 0)
{
out << printMe.num << endl;
}
else if(printMe.num < printMe.denom && printMe.num != 0)
out << printMe.num << "/" << printMe.denom << endl;
else if(abs(printMe.num) > abs(printMe.denom))
out << printMe.num / printMe.denom << "+" << (printMe.num % printMe.denom) << "/" << printMe.denom << endl;
/* else if (abs(printMe.num) > abs(printMe.denom))
out << printMe.num / printMe.denom << endl;
else if (abs(printMe.num) > abs(printMe.denom))
out << printMe.num / printMe.denom << "+" << (printMe.num % printMe.denom) << "/" << printMe.denom << endl; */
return out;
}
istream& operator >> (std::istream &in, Fraction readMe)
{
}
|
Code for my client file
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
using namespace cs10b_fraction;
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(/*inout*/ifstream& in);
string boolString(/*in*/bool convertMe);
int main()
{
cout << "Project#2: Fraction class (ADT) client program to test CLASS INVARIANT\n";
cout << "There are two data members used to represent the denominator and numerator\n";
cout << "for each instance of a Fraction class object class.\n";
cout << "The client code provides an interface to test correct operations on fraction \n";
cout << "objects that are negative, improper (larger numerator than denominator), \n";
cout << "or whole numbers (denominator of 1).\n";
cout << "Here is a list test type and order in which the tests will be conducted\n";
cout << "1. BasicTest\n";
cout << "2. RelationTest\n";
cout << "3. BinaryMathTest\n";
cout << "4. MathAssignTest\n\n";
BasicTest();
/*RelationTest();
BinaryMathTest();
MathAssignTest();*/
cout << "\nProject#2 -Fraction class (ADT) testing now concluded.\n";
cout << "Check output for correct results from the Fraction class (ADT) implementation.\n";
return 0;
}
// Function provides the interface to test the following Fraction class member implementation algorithms
// Class constructor, a fraction reduction algorithm in simplify, and two nonmember friend ostream << and istream >> operator functions
void BasicTest()
{
cout << "***********************************************************************\n";
cout << "* Basic Test: Testing member constructor, simplify() and nonmember *\n";
cout << "* friend ostream << operator for basic Fraction object creation & *\n";
cout << "* printing(Fractions should be in reduced form, and as mixed numbers.)*\n";
cout << "***********************************************************************\n";
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 << "\n***********************************************************************\n";
cout << "* Basic Test: Testing simplify() and nonmember friend istream >> and *\n";
cout << "* ostream << operators for reading and display of Fraction objects *\n";
cout << "* from data file *\n";
cout << "***********************************************************************\n";
string fileName;
cout << "Enter file name with fraction data to read: ";
cin >> fileName;
ifstream in(fileName);
while(!in)
{
cin.ignore(200, '\n');
cout << fileName << " not found!" <<endl;
cout << "Make sure the fraction data file to read is in the project folder." << endl;
cout << "Enter file name with fraction data to read: ";
cin >> fileName;
in.open(fileName);
}
while (!eof(in)) {
Fraction f;
if(in.peek() == '/' || in.peek() == '*' || isalpha(in.peek()) ){
in.ignore(200, '\n'); //skip this line, it's a comment
} else {
in >> f;
cout << "Read Fraction = " << f << endl;
}
}
}
|