Class help please
Sep 7, 2016 at 7:15pm UTC
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
//
// main.cpp
// exercise 9.6 Rational Class
//
// Created by James Farrow on 02/09/2016.
// Copyright © 2016 James Farrow. All rights reserved.
//
#include <iostream>
#include "RationalNumber.hpp"
int main() {
RationalNumber object1; //create RationalNumber object
RationalNumber object2; //create RationalNumber object
RationalNumber object3; //create RationalNumber object for addition of first 2
RationalNumber object4; //create RationalNumber object for subtraction of first 2
RationalNumber object5; //create RationalNumber object for multiplication of first 2
RationalNumber object6; //create RationalNumber object for division of first 2
object1.setNumerator();
object1.setDenomenator();
object1.setGreatestCommonDivisor(object1.getNumerator(), object1.getDenomenator());
std::cout << std::endl;
object2.setNumerator();
object2.setDenomenator();
object2.setGreatestCommonDivisor(object2.getNumerator(), object2.getDenomenator());
std::cout << "\n\nThe rational number1 as entered: " << object1 << std::endl;// print the rational number as entered
object1.simplify();//simplify the rational number
std::cout << "In reduced form: " << object1 << std::endl;// print it in its reduced form
std::cout << "In decimal form: " << object1.getFloatingPointEquiv() << std::endl;// in decimal form
std::cout << "\n\nThe rational number2 as entered: " << object2 << std::endl;// print the rational number as entered
object2.simplify();//simplify the rational number
std::cout << "In reduced form: " << object2 << std::endl;// print it in its reduced form
std::cout << "In decimal form: " << object2.getFloatingPointEquiv() << std::endl;// in decimal form
//addition of rational numbers
object3 = object1 + object2;
object3.setGreatestCommonDivisor(object3.getNumerator(), object3.getDenomenator());
std::cout << "\nAddition of both rational numbers: " << object3 << std::endl;
object3.simplify();
std::cout << "Addition in reduced form: " << object3 << std::endl;
std::cout << "Addition in decimal form: " << object3.getFloatingPointEquiv() << std::endl;// in decimal form
//subtraction of rational numbers
object4 = object1 - object2;
if (object4.getNumerator() == 0 || object4.getDenomenator() == 0) {
std::cout << "\n\nSubraction of rational numbers: " << object4 << std::endl;
std::cout << "Subtraction in decimal form: " << object4.getFloatingPointEquiv() << std::endl;// in decimal form
}
else {
object4.setGreatestCommonDivisor(object4.getNumerator(), object4.getDenomenator());
std::cout << "\n\nSubraction of rational numbers: " << object4 << std::endl;
object4.simplify();
std::cout << "Subtraction in reduced form: " << object4 << std::endl;
std::cout << "Subtraction in decimal form: " << object4.getFloatingPointEquiv() << std::endl;// in decimal form
}
//multiplication of rational numbers
object5 = object1 * object2;
object5.setGreatestCommonDivisor(object5.getNumerator(), object5.getDenomenator());
std::cout << "\nMultiplication of both rational numbers: " << object5 << std::endl;
object3.simplify();
std::cout << "Multiplication in reduced form: " << object5 << std::endl;
std::cout << "Multiplication in decimal form: " << object5.getFloatingPointEquiv() << std::endl;// in decimal form
//division of rational numbers
object6 = object1 / object2;
object6.setGreatestCommonDivisor(object6.getNumerator(), object6.getDenomenator());
std::cout << "\nMultiplication of both rational numbers: " << object6 << std::endl;
object3.simplify();
std::cout << "Multiplication in reduced form: " << object6 << std::endl;
std::cout << "Multiplication in decimal form: " << object6.getFloatingPointEquiv() << std::endl;// in decimal form
return 0;
}
//
// RationalNumber.hpp
// exercise 9.6 Rational Class
//
// Created by James Farrow on 04/09/2016.
// Copyright © 2016 James Farrow. All rights reserved.
//
#ifndef RationalNumber_hpp
#define RationalNumber_hpp
#include <iostream>
class RationalNumber{
public :
RationalNumber();
void setNumerator();
void setDenomenator();
void setGreatestCommonDivisor( int numerator, int denomentor);
void simplify();
int getNumerator()const ;
int getDenomenator()const ;
double getFloatingPointEquiv();
int getGreatestCommonDivisor();
RationalNumber operator +(const RationalNumber &b )const ;
RationalNumber operator -(const RationalNumber &b )const ;
RationalNumber operator *(const RationalNumber &b )const ;
RationalNumber operator /(const RationalNumber &b )const ;
private :
int numerator;
int denomenator;
double floatingPointEquivalent = 0;
int gcd;
};
std::ostream &operator <<( std::ostream& out, RationalNumber const &rhs );
#endif /* RationalNumber_hpp */
Sep 7, 2016 at 7:18pm UTC
Sorry but I have had to put my code across 2 posts. This is what I have come up with so far.
The overloaded */=- seem to work ok.
I know its messy - any ideas on how to tidy it would be welcome!!
Thanks for your help so far...!
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
//
// RationalNumber.cpp
// exercise 9.6 Rational Class
//
// Created by James Farrow on 04/09/2016.
// Copyright © 2016 James Farrow. All rights reserved.
//
#include "RationalNumber.hpp"
#include <iostream>
RationalNumber::RationalNumber(){
//setNumerator();
//setDenomenator();
//setGreatestCommonDivisor(numerator, denomenator);
}
void RationalNumber::setNumerator(){
std::cout << "Input your rational number's none zero numerater: " ;
std::cin >> numerator;
while (! std::cin || numerator == 0) {
std::cin.clear();
std::cin.ignore();
std::cout << "\nInput your rational number's none zero numerater: " ;
std::cin >> numerator;
}
}
void RationalNumber::setDenomenator(){
std::cout << "Input your rational number's none zero denomenater: " ;
std::cin >> denomenator;
while (! std::cin || denomenator == 0) {
std::cin.clear();
std::cin.ignore();
std::cout << "\nInput your rational number's none zero numerater: " ;
std::cin >> denomenator;}
}
void RationalNumber::simplify(){
numerator /= gcd;
denomenator /= gcd;
}
int RationalNumber::getNumerator()const {
return numerator;
}
int RationalNumber::getDenomenator()const {
return denomenator;
}
int RationalNumber::getGreatestCommonDivisor(){
return gcd;
}
void RationalNumber::setGreatestCommonDivisor(int a, int b){
while ( b != 0 ){
int temp1, temp2;
temp1 = b;
temp2 = a % b;
a = temp1;
b = temp2;
gcd = a;
}
}
double RationalNumber::getFloatingPointEquiv(){
if (numerator == 0 && denomenator == 0) {
floatingPointEquivalent = 0;
return floatingPointEquivalent;
}
floatingPointEquivalent = static_cast <double >(numerator)/denomenator;
return floatingPointEquivalent;
}
std::ostream &operator <<( std::ostream& out, RationalNumber const &rhs )
{
out << rhs.getNumerator() << '/' << rhs.getDenomenator();
return out;
}
RationalNumber RationalNumber::operator +(const RationalNumber &b )const {
RationalNumber fraction;
int tempDenom = this ->denomenator * b.denomenator;
int firstTempNum = b.denomenator * this ->numerator;
int secondTempNum = this ->denomenator * b.numerator;
fraction.numerator = firstTempNum + secondTempNum;
fraction.denomenator = tempDenom;
return fraction;
}
RationalNumber RationalNumber::operator -(const RationalNumber &b )const {
RationalNumber fraction;
if (this ->floatingPointEquivalent == b.floatingPointEquivalent) {
fraction.numerator = 0;
fraction.denomenator = 0;
}
else {
int tempDenom = this ->denomenator * b.denomenator;
int firstTempNum = b.denomenator * this ->numerator;
int secondTempNum = this ->denomenator * b.numerator;
fraction.numerator = firstTempNum - secondTempNum;
fraction.denomenator = tempDenom;
}
return fraction;
}
RationalNumber RationalNumber::operator *(const RationalNumber &b )const {
RationalNumber fraction;
fraction.numerator = this ->numerator * b.numerator;
fraction.denomenator = this ->denomenator * b.denomenator;
return fraction;
}
RationalNumber RationalNumber::operator /(const RationalNumber &b )const {
RationalNumber fraction;
fraction.numerator = this ->denomenator * b.numerator;
fraction.denomenator = this ->numerator * b.denomenator;
return fraction;
}
Sep 10, 2016 at 7:11am UTC
Thanks to everyone who took the time to post and help.
I have managed to tidy the code and complete the exercise!!
Cheers
James
Topic archived. No new replies allowed.