Having an Issue With My Bool Function For Fractions
Can someone help explain to me why my bool functions aren't working?
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <iostream>
using namespace std;
class fraction
{
private:
int numerator;
int denominator;
public:
fraction (); //default constractor....check
fraction (int,int); //default constractor with parameters ....check
fraction AddedTo (fraction value) const;
fraction MultipliedBy (fraction value) const; //binary observer type of operation
fraction Subtract (fraction value) const;
fraction DividedBy (fraction value) const;
fraction isGreaterThan ();
fraction isEqualTo ();
fraction print() const;
};
#include <iostream>
using namespace std;
int main()
{
fraction f1(9,8); //calling a parameterized class constructor
fraction f2(2,3); //calling a parameterized class constructor
fraction result; //calling a default class constructor
fraction f3; //calling a default class constructor
cout << "The result starts off at ";
result.print(); //calling an observer function
cout << endl;
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2); //a class binary operation - function
result.print();
cout << endl;
f3 = result; //assignment
if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function
f2.print();
cout <<" is greater than ";
f3.print();
cout<<endl;
} else {
f2.print();
cout <<" is less than ";
f3.print();
cout<<endl;
}
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2); //a class binary operation - function
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2); //a class binary operation - function
result.print();
cout << endl;
if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}
const fraction f4(12, 8);
const fraction f5(202, 303);
result = f4.DividedBy(f5); //a class binary operation - function
cout << "The quotient of ";
f4.print();
cout << " and ";
f5.print();
cout << " is ";
result.print();
cout << endl;
return 0;
}
fraction::fraction()
{
numerator = 0;
denominator = 1;
}
//**********************************
fraction::fraction(int newNumerator, int newDenominator)
{
numerator=newNumerator;
denominator=newDenominator;
}
//***************************
fraction fraction::AddedTo (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)+(value.numerator*denominator);
result.denominator = denominator * value.denominator;
return result;
}
//********************************
fraction fraction::Subtract (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)-(value.numerator*denominator);
result.denominator = denominator * value.denominator;
return result;
}
//**************************************
fraction fraction::MultipliedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.numerator;
result.denominator = denominator * value.denominator;
return result;
}
//************************************
fraction fraction::DividedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.denominator;
result.denominator = denominator * value.numerator;
return result;
}
//**********************************
bool fraction::isGreaterThan (fraction value)
{
int fraction1;
int fraction2;
fraction1=(numerator/denominator);
fraction2=(value.numerator/value.denominator);
if(fraction1>fraction2)
return true;
else
return false;
}
//*************************************
bool fraction::isEqualTo(fraction value)
{
if((numerator/denominator)==(value.numerator/value.denominator))
return true;
else
return false;
}
//**************************************
fraction fraction::print() const
{
int num=numerator;
int den=denominator;
if(num>den)
{
for(int counter=2;counter<den;counter++)
{
while(num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
else if(den>num)
{
for(int counter=2;counter<num;counter++)
{
while(num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
cout<<num<<"/"<<den;
}
|
«aren't working» is not a descriptive error message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class fraction
{
private:
int numerator;
int denominator;
public:
fraction (); //default constractor....check
fraction (int,int); //default constractor with parameters ....check
fraction AddedTo (fraction value) const;
fraction MultipliedBy (fraction value) const; //binary observer type of operation
fraction Subtract (fraction value) const;
fraction DividedBy (fraction value) const;
fraction isGreaterThan ();
fraction isEqualTo ();
fraction print() const;
};
|
I don't see any
bool
*Sigh* Can't believe I missed that...need...sleep. Sorry about the non-descriptive error message. Works fine now!
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <iostream>
using namespace std;
class fraction
{
private:
int numerator;
int denominator;
public:
fraction (); //default constractor....check
fraction (int,int); //default constractor with parameters ....check
fraction AddedTo (fraction value) const;
fraction MultipliedBy (fraction value) const; //binary observer type of operation
fraction Subtract (fraction value) const;
fraction DividedBy (fraction value) const;
bool isGreaterThan (fraction value);
bool isEqualTo (fraction value);
fraction print() const;
};
#include <iostream>
using namespace std;
int main()
{
fraction f1(9,8); //calling a parameterized class constructor
fraction f2(2,3); //calling a parameterized class constructor
fraction result; //calling a default class constructor
fraction f3; //calling a default class constructor
cout << "The result starts off at ";
result.print(); //calling an observer function
cout << endl;
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2); //a class binary operation - function
result.print();
cout << endl;
f3 = result; //assignment
if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function
f2.print();
cout <<" is greater than ";
f3.print();
cout<<endl;
} else {
f2.print();
cout <<" is less than ";
f3.print();
cout<<endl;
}
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2); //a class binary operation - function
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2); //a class binary operation - function
result.print();
cout << endl;
if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}
const fraction f4(12, 8);
const fraction f5(202, 303);
result = f4.DividedBy(f5); //a class binary operation - function
cout << "The quotient of ";
f4.print();
cout << " and ";
f5.print();
cout << " is ";
result.print();
cout << endl;
return 0;
}
fraction::fraction()
{
numerator = 0;
denominator = 1;
}
//**********************************
fraction::fraction(int newNumerator, int newDenominator)
{
numerator=newNumerator;
denominator=newDenominator;
}
//***************************
fraction fraction::AddedTo (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)+(value.numerator*denominator);
result.denominator = denominator * value.denominator;
return result;
}
//********************************
fraction fraction::Subtract (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)-(value.numerator*denominator);
result.denominator = denominator * value.denominator;
return result;
}
//**************************************
fraction fraction::MultipliedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.numerator;
result.denominator = denominator * value.denominator;
return result;
}
//************************************
fraction fraction::DividedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.denominator;
result.denominator = denominator * value.numerator;
return result;
}
//**********************************
bool fraction::isGreaterThan (fraction value)
{
int fraction1;
int fraction2;
fraction1=(numerator/denominator);
fraction2=(value.numerator/value.denominator);
if(fraction1>fraction2)
return true;
else
return false;
}
//*************************************
bool fraction::isEqualTo(fraction value)
{
if((numerator/denominator)==(value.numerator/value.denominator))
return true;
else
return false;
}
//**************************************
fraction fraction::print() const
{
int num=numerator;
int den=denominator;
if(num>den)
{
for(int counter=2;counter<den;counter++)
{
while(num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
else if(den>num)
{
for(int counter=2;counter<num;counter++)
{
while(num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
cout<<num<<"/"<<den;
}
|
Topic archived. No new replies allowed.