Advertisement
Guest User

Lab 5

a guest
May 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. // Acimovic, David
  2. // Lab 5
  3. // Comp Sci 540
  4.  
  5. //main.cpp
  6. #include "Fraction.hpp"
  7. #include <iostream>
  8. #include <iomanip>
  9.  
  10. const int A = 1; // Global Constant
  11.  
  12. //Generates Array
  13. void generateArray(int den, Fraction** & twoDArray);
  14. //Prints Fraction Table
  15. void printArray(int den, Fraction** & twoDArray);
  16. //Clears Memory held by Array
  17. void freeMemory(int den, Fraction** & twoDArray);
  18. //Fills Array
  19. void initializePointerArray(int den, Fraction** & twoDArray);
  20. //user input
  21. int getDen();
  22.  
  23. int main()
  24. {
  25. int den = getDen(); //user input
  26. //Declare a pointer to two dimensional array of pointer
  27. Fraction**twoDArray = nullptr;
  28.  
  29.  
  30. generateArray(den, twoDArray); //Generates Array
  31. printArray(den, twoDArray); //Prints Fraction Table
  32. freeMemory(den, twoDArray); //Clears Memory held by Array
  33.  
  34. return EXIT_SUCCESS; //Ends Program Succesfully
  35. }
  36.  
  37. //Void Function that creates twoDArray
  38. void generateArray(int den, Fraction** & twoDArray)
  39. {
  40. int i = 0; // Declare Variables
  41. twoDArray = new (nothrow) Fraction*[((den - A)*(den - A))]; // Creates Array
  42.  
  43. //Checks if twoDArray is dynamically allocated
  44. if (twoDArray == nullptr)
  45. {
  46. cout << "Memory could not be dynamically allocated successfully. \n\n";
  47. exit(0);
  48. //Outputs Errors and exits with a failure if twoDArray isnt dynamically allocated
  49. }
  50.  
  51. initializePointerArray(den, twoDArray); //fills array
  52. do {
  53. for (int rowCounter = 1; rowCounter < den; rowCounter++) {
  54. for (int columnCounter = 1; columnCounter < den; columnCounter++) {
  55. twoDArray[i] = new (nothrow) Fraction((rowCounter*columnCounter), (den*den));
  56. //Checks if twoDArray is dynamically allocated
  57. if (twoDArray[i] == nullptr) {
  58. cout << "Memory could not be dynamically allocated successfully. \n\n";
  59. freeMemory(den, twoDArray);
  60. exit(0);
  61. //Outputs Errors and exits with a failure if twoDArray isnt dynamically allocated
  62. }
  63. i++;
  64. }
  65. }
  66. }
  67. while (i < ((den - A)*(den - A)));
  68. }
  69.  
  70. //Prints Fraction Table
  71. void printArray(int den, Fraction** & twoDArray)
  72. {
  73. for (int i = 0; i < den; i++) {
  74. if (i == 0)
  75. cout << " ";
  76. else
  77. cout << "\t" << i << "/" << den << " \t";
  78. }
  79. cout << endl;
  80.  
  81. int i = 0;
  82. do {
  83. for (int rowCounter = 1; rowCounter < den; rowCounter++) {
  84. cout << " " << rowCounter << "/" << den << " \t";
  85. for (int columnCounter = 1; columnCounter < den; columnCounter++) {
  86. cout << *twoDArray[i] << "\t\t";
  87. i++;
  88. }
  89. cout << endl;
  90. }
  91. }
  92. while (i < ((den - A)*(den - A)));
  93. cout << endl;
  94. }
  95.  
  96. void initializePointerArray(int den, Fraction** & twoDArray)
  97. {
  98. for (int i = 0; i < ((den - A)*(den - A)); i++)
  99. twoDArray[i] = nullptr;
  100. }
  101.  
  102. //Clears Memory held by Array
  103. void freeMemory(int den, Fraction** & twoDArray)
  104. {
  105. for (int i = 0; i < ((den - A)*(den - A)); i++)
  106. {
  107. if (twoDArray[i] != nullptr) {
  108. delete twoDArray[i];
  109. twoDArray[i] = nullptr;
  110. }
  111. }
  112. delete[] twoDArray;
  113. }
  114.  
  115. //user input
  116. int getDen(){
  117. int tmp;
  118. do{
  119. cout << "Enter the denominator" << endl;
  120. cin >> tmp;
  121. if (tmp < 1){
  122. cout << "Error: No negative numbers" << endl;
  123. }
  124. } while (tmp < 1);
  125. return tmp;
  126. }
  127. -----------------------------------------------------------------------------
  128. //Fraction.hpp
  129.  
  130. #ifndef Fraction_hpp
  131. #define Fraction_hpp
  132.  
  133. #endif /* Fraction_hpp */
  134. #include <stdio.h>
  135. #include <iostream>
  136.  
  137. using namespace std;
  138. class Fraction
  139. {
  140. public:
  141. Fraction(); // default constructor
  142. Fraction(int num, int den); // 2 arguement constructor
  143. Fraction(int whole, int num, int den); // 3 arguement constructors
  144. Fraction(const Fraction &fract); //copy constructor
  145.  
  146.  
  147. const Fraction operator + (const Fraction &frac2);
  148. //Addition operator for the Fraction class
  149. const Fraction operator * (const Fraction &frac2);
  150. //Mult. operator for the Fraction class
  151. const bool operator == (const Fraction &frac2);
  152. //Equality operator for the Fraction class
  153. const Fraction operator = (const Fraction &frac2);
  154. //Assignment operator for the Fraction class
  155. friend ostream& operator << (ostream& out, const Fraction &frac2);
  156. // << operator for the Fraction class
  157. friend istream& operator >> (istream& in, const Fraction &frac2);
  158. //>> operator for the Fraction class
  159.  
  160. ~Fraction(); // destructor
  161.  
  162. void setFraction(int num, int den); // mutator functions
  163. void setFraction(int whole, int num, int den);
  164.  
  165. int getNumerator()const; // accessor functions
  166. int getDenominator()const;
  167.  
  168. void printFraction()const; // print function
  169. private:
  170. int numerator; // private variables
  171. int denominator;
  172. };
  173. -----------------------------------------------------------------------------
  174. //Fraction.cpp
  175.  
  176. #include "Fraction.hpp"
  177. using namespace std;
  178.  
  179. Fraction::Fraction()
  180. {
  181. numerator = 0;
  182. denominator = 1;
  183.  
  184. }
  185.  
  186. // 2 arguement constructor
  187. Fraction::Fraction(int num, int den)
  188. {
  189. setFraction(0, num, den);
  190. }
  191.  
  192. // 3 arguement constructor
  193. Fraction::Fraction(int whole, int num, int den)
  194. {
  195. setFraction(whole, num, den);
  196. }
  197.  
  198. //copy constructor
  199. Fraction::Fraction(const Fraction &fract)
  200. {
  201. cout << "Copy constructor called.\n";
  202. numerator = fract.numerator;
  203. denominator = fract.denominator;
  204. }
  205.  
  206.  
  207. Fraction::~Fraction()
  208. {
  209. numerator = 0;
  210. denominator = 1;
  211. }
  212.  
  213. // mutator function sets the numerator and denominator
  214. void Fraction::setFraction(int num, int den)
  215. {
  216. setFraction(0, num, den);
  217. }
  218. // mutator function sets the numerator and denominator and whole number
  219. void Fraction::setFraction(int whole, int num, int den)
  220. {
  221. if( num >= 0 && den > 0 && whole >= 0)
  222. {
  223. numerator = num + (whole * den);
  224. denominator = den;
  225. }
  226. else
  227. {
  228. cout << "Error: Only Positive Values" << endl;
  229. numerator = 0;
  230. denominator = 1;
  231. }
  232. }
  233.  
  234. // accessor function returns the numerator
  235. int Fraction::getNumerator()const
  236. {
  237. return numerator;
  238. }
  239. // accessor function returns the denominator
  240. int Fraction::getDenominator()const
  241. {
  242. return denominator;
  243. }
  244.  
  245. // prints out the fraction.
  246. void Fraction::printFraction()const
  247. {
  248. cout << numerator / denominator << " " << numerator % denominator << "/" << denominator << endl;
  249. }
  250.  
  251. //returns a type Fraction with the addition of two fractions
  252. const Fraction Fraction:: operator + (const Fraction &frac2)
  253. {
  254. return Fraction(0, (frac2.numerator * denominator) + (numerator * frac2.denominator),frac2.denominator * denominator);
  255. }
  256. //returns a type Fraction with the addition of two fractions
  257. const Fraction Fraction:: operator * (const Fraction &frac2)
  258. {
  259. return Fraction(0, (frac2.numerator *numerator),(frac2.denominator * denominator));
  260. }
  261.  
  262. //checks if two fractions are equal to each other
  263. const bool Fraction::operator == (const Fraction &frac2)
  264. {
  265. return (frac2.numerator * denominator) == (numerator * frac2.denominator);
  266. }
  267.  
  268. const Fraction Fraction::operator = (const Fraction &frac2) {
  269. // do the copy
  270. numerator = frac2.numerator;
  271. denominator = frac2.denominator;
  272. // return the existing object so we can chain this operator
  273. return *this;
  274. }
  275.  
  276. //allows user input
  277. istream& operator >> (istream& in, Fraction &frac2){
  278. int num,den;
  279. in>>num>>den;
  280. frac2.setFraction( 0, num, den);
  281. return in;
  282. }
  283.  
  284. // outputs frac2
  285. ostream& operator << (ostream& out,const Fraction &frac2)
  286. {
  287. out <<frac2.numerator<<"/"<<frac2.denominator;
  288. return out;
  289. }
  290. -----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement