Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Acimovic, David
- // Lab 5
- // Comp Sci 540
- //main.cpp
- #include "Fraction.hpp"
- #include <iostream>
- #include <iomanip>
- const int A = 1; // Global Constant
- //Generates Array
- void generateArray(int den, Fraction** & twoDArray);
- //Prints Fraction Table
- void printArray(int den, Fraction** & twoDArray);
- //Clears Memory held by Array
- void freeMemory(int den, Fraction** & twoDArray);
- //Fills Array
- void initializePointerArray(int den, Fraction** & twoDArray);
- //user input
- int getDen();
- int main()
- {
- int den = getDen(); //user input
- //Declare a pointer to two dimensional array of pointer
- Fraction**twoDArray = nullptr;
- generateArray(den, twoDArray); //Generates Array
- printArray(den, twoDArray); //Prints Fraction Table
- freeMemory(den, twoDArray); //Clears Memory held by Array
- return EXIT_SUCCESS; //Ends Program Succesfully
- }
- //Void Function that creates twoDArray
- void generateArray(int den, Fraction** & twoDArray)
- {
- int i = 0; // Declare Variables
- twoDArray = new (nothrow) Fraction*[((den - A)*(den - A))]; // Creates Array
- //Checks if twoDArray is dynamically allocated
- if (twoDArray == nullptr)
- {
- cout << "Memory could not be dynamically allocated successfully. \n\n";
- exit(0);
- //Outputs Errors and exits with a failure if twoDArray isnt dynamically allocated
- }
- initializePointerArray(den, twoDArray); //fills array
- do {
- for (int rowCounter = 1; rowCounter < den; rowCounter++) {
- for (int columnCounter = 1; columnCounter < den; columnCounter++) {
- twoDArray[i] = new (nothrow) Fraction((rowCounter*columnCounter), (den*den));
- //Checks if twoDArray is dynamically allocated
- if (twoDArray[i] == nullptr) {
- cout << "Memory could not be dynamically allocated successfully. \n\n";
- freeMemory(den, twoDArray);
- exit(0);
- //Outputs Errors and exits with a failure if twoDArray isnt dynamically allocated
- }
- i++;
- }
- }
- }
- while (i < ((den - A)*(den - A)));
- }
- //Prints Fraction Table
- void printArray(int den, Fraction** & twoDArray)
- {
- for (int i = 0; i < den; i++) {
- if (i == 0)
- cout << " ";
- else
- cout << "\t" << i << "/" << den << " \t";
- }
- cout << endl;
- int i = 0;
- do {
- for (int rowCounter = 1; rowCounter < den; rowCounter++) {
- cout << " " << rowCounter << "/" << den << " \t";
- for (int columnCounter = 1; columnCounter < den; columnCounter++) {
- cout << *twoDArray[i] << "\t\t";
- i++;
- }
- cout << endl;
- }
- }
- while (i < ((den - A)*(den - A)));
- cout << endl;
- }
- void initializePointerArray(int den, Fraction** & twoDArray)
- {
- for (int i = 0; i < ((den - A)*(den - A)); i++)
- twoDArray[i] = nullptr;
- }
- //Clears Memory held by Array
- void freeMemory(int den, Fraction** & twoDArray)
- {
- for (int i = 0; i < ((den - A)*(den - A)); i++)
- {
- if (twoDArray[i] != nullptr) {
- delete twoDArray[i];
- twoDArray[i] = nullptr;
- }
- }
- delete[] twoDArray;
- }
- //user input
- int getDen(){
- int tmp;
- do{
- cout << "Enter the denominator" << endl;
- cin >> tmp;
- if (tmp < 1){
- cout << "Error: No negative numbers" << endl;
- }
- } while (tmp < 1);
- return tmp;
- }
- -----------------------------------------------------------------------------
- //Fraction.hpp
- #ifndef Fraction_hpp
- #define Fraction_hpp
- #endif /* Fraction_hpp */
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- class Fraction
- {
- public:
- Fraction(); // default constructor
- Fraction(int num, int den); // 2 arguement constructor
- Fraction(int whole, int num, int den); // 3 arguement constructors
- Fraction(const Fraction &fract); //copy constructor
- const Fraction operator + (const Fraction &frac2);
- //Addition operator for the Fraction class
- const Fraction operator * (const Fraction &frac2);
- //Mult. operator for the Fraction class
- const bool operator == (const Fraction &frac2);
- //Equality operator for the Fraction class
- const Fraction operator = (const Fraction &frac2);
- //Assignment operator for the Fraction class
- friend ostream& operator << (ostream& out, const Fraction &frac2);
- // << operator for the Fraction class
- friend istream& operator >> (istream& in, const Fraction &frac2);
- //>> operator for the Fraction class
- ~Fraction(); // destructor
- void setFraction(int num, int den); // mutator functions
- void setFraction(int whole, int num, int den);
- int getNumerator()const; // accessor functions
- int getDenominator()const;
- void printFraction()const; // print function
- private:
- int numerator; // private variables
- int denominator;
- };
- -----------------------------------------------------------------------------
- //Fraction.cpp
- #include "Fraction.hpp"
- using namespace std;
- Fraction::Fraction()
- {
- numerator = 0;
- denominator = 1;
- }
- // 2 arguement constructor
- Fraction::Fraction(int num, int den)
- {
- setFraction(0, num, den);
- }
- // 3 arguement constructor
- Fraction::Fraction(int whole, int num, int den)
- {
- setFraction(whole, num, den);
- }
- //copy constructor
- Fraction::Fraction(const Fraction &fract)
- {
- cout << "Copy constructor called.\n";
- numerator = fract.numerator;
- denominator = fract.denominator;
- }
- Fraction::~Fraction()
- {
- numerator = 0;
- denominator = 1;
- }
- // mutator function sets the numerator and denominator
- void Fraction::setFraction(int num, int den)
- {
- setFraction(0, num, den);
- }
- // mutator function sets the numerator and denominator and whole number
- void Fraction::setFraction(int whole, int num, int den)
- {
- if( num >= 0 && den > 0 && whole >= 0)
- {
- numerator = num + (whole * den);
- denominator = den;
- }
- else
- {
- cout << "Error: Only Positive Values" << endl;
- numerator = 0;
- denominator = 1;
- }
- }
- // accessor function returns the numerator
- int Fraction::getNumerator()const
- {
- return numerator;
- }
- // accessor function returns the denominator
- int Fraction::getDenominator()const
- {
- return denominator;
- }
- // prints out the fraction.
- void Fraction::printFraction()const
- {
- cout << numerator / denominator << " " << numerator % denominator << "/" << denominator << endl;
- }
- //returns a type Fraction with the addition of two fractions
- const Fraction Fraction:: operator + (const Fraction &frac2)
- {
- return Fraction(0, (frac2.numerator * denominator) + (numerator * frac2.denominator),frac2.denominator * denominator);
- }
- //returns a type Fraction with the addition of two fractions
- const Fraction Fraction:: operator * (const Fraction &frac2)
- {
- return Fraction(0, (frac2.numerator *numerator),(frac2.denominator * denominator));
- }
- //checks if two fractions are equal to each other
- const bool Fraction::operator == (const Fraction &frac2)
- {
- return (frac2.numerator * denominator) == (numerator * frac2.denominator);
- }
- const Fraction Fraction::operator = (const Fraction &frac2) {
- // do the copy
- numerator = frac2.numerator;
- denominator = frac2.denominator;
- // return the existing object so we can chain this operator
- return *this;
- }
- //allows user input
- istream& operator >> (istream& in, Fraction &frac2){
- int num,den;
- in>>num>>den;
- frac2.setFraction( 0, num, den);
- return in;
- }
- // outputs frac2
- ostream& operator << (ostream& out,const Fraction &frac2)
- {
- out <<frac2.numerator<<"/"<<frac2.denominator;
- return out;
- }
- -----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement