I ran across some errors in this cpp program I had to write for school, and I can't figure out what to do...my instructions are below; any help or advice is greatly appreciated!
Rational fractions are of the form a / b, in which a and b are integers and b ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations on fractions are defined by the following rules:
a/b + c/d = (ad + bc)/bd
a/b - c/d = (ad - bc)/bd
a/b * c/d = ac/bd
(a/b) / (c/d) = ad/bc, in which c/d ≠ 0
Fractions are compared as follows: a / b op c / d if ad op bc, in which op is any of the relational operations. For example, a / b < c / d if ad < bc.
Design a class fractionType that performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output.
Write the class fractionType, that performs operations on fractions. Among other things, test the following: Suppose x, y, and z are objects of type fractionType. If the input is 2/3, the statement:
cin >> x;
should store 2/3 in x. The statement:
cout << x + y << endl;
should output the value of x + y in fraction form. The statement:
z = x + y;
should store the sum of x and y in z in fraction form. Your answer need not be in the lowest terms.
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
|
//In my fractionType.h file
#pragma once
//#ifdef _fractionType_h
//#define _fractionType_H
#include <iostream>
using namespace std;
class fractionType
{
friend ostream& operator << (ostream&, const fractionType&);
friend istream& operator >> (istream&, fractionType&);
public:
const fractionType& operator=(const fractionType&);
fractionType();
fractionType(const fractionType&);
fractionType(const int&, const int&);
~fractionType();
bool operator==(const fractionType&) const;
bool operator!=(const fractionType&) const;
bool operator<=(const fractionType&) const;
bool operator< (const fractionType&) const;
bool operator>=(const fractionType&) const;
bool operator> (const fractionType&) const;
fractionType operator + (const fractionType&) const;
fractionType operator - (const fractionType&) const;
fractionType operator * (const fractionType&) const;
fractionType operator / (const fractionType&) const;
int a;
int b;
};
|
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
//In my farctionType.cpp file
#include <iostream>
#include "fractionType.h"
using namespace std;
fractionType::fractionType(const int& nu, const int& de)
{
a = nu;
if (de == 0)
{
cout << "ntInvalid denominator. "
<< "Default value considered for denominator. ";
b = 1;
}
else
b = de;
}
fractionType::fractionType()
{
a = 0;
b = 1;
}
fractionType::fractionType(const fractionType& rightFraction)
{
a = rightFraction.a;
b = rightFraction.b;
}
fractionType::~fractionType()
{
}
bool fractionType::operator==(const fractionType& rightFraction) const
{
return ((a == rightFraction.a) && (b == rightFraction.b));
}
bool fractionType::operator != (const fractionType& rightFraction) const
{
return ((a != rightFraction.a) || (b != rightFraction.b));
}
bool fractionType::operator<(const fractionType& rightFraction) const
{
return (a * rightFraction.b < b * rightFraction.a);
}
bool fractionType::operator<=(const fractionType& rightFraction) const
{
return (a * rightFraction.b <= b * rightFraction.a);
}
bool fractionType::operator>(const fractionType& rightFraction) const
{
return (a * rightFraction.b > b * rightFraction.a);
}
bool fractionType::operator>=(const fractionType& rightFraction) const
{
return (a * rightFraction.b >= b * rightFraction.a);
}
fractionType fractionType::operator + (const fractionType & rightFraction) const
{
fractionType temp;
temp.a = (a * rightFraction.b) + (b * rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType::operator - (const fractionType & rightFraction) const
{
fractionType temp;
temp.a = (a * rightFraction.b) - (b * rightFraction.a);
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType::operator*(const fractionType& rightFraction) const
{
fractionType temp;
temp.a = a * rightFraction.a;
temp.b = b * rightFraction.b;
return temp;
}
fractionType fractionType::operator/(const fractionType& rightFraction) const
{
fractionType temp;
if ((rightFraction.a == 0) || (rightFraction.b == 0))
{
temp.a = 0;
temp.b = 1;
cout << "ntInvalid to perform division. ";
}
else
{
temp.a = a * rightFraction.b;
temp.b = b * rightFraction.b;
}
return temp;
}
ostream& operator<<(ostream& osObject, const fractionType& myFraction)
{
osObject << myFraction.a << "/" << myFraction.b;
return osObject;
}
istream& operator>>(istream &isObject, fractionType& myFraction)
{
char ch;
isObject >> myFraction.a >> ch >> myFraction.b;
return isObject;
}
|
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
|
#include <iostream>
//#include "fractionType.h"
using namespace std;
int main ()
{
cout << "A program to check the functionality"
<< "\nof the class fractionType. \n";
fractionType myFraction1;
fractionType myFraction2;
cout << "\nEnter a fraction in the form of a/b: ";
cin >> myFraction1;
cout << "\nEnter another fraction in the form a/b: ";
cin >> myFraction2;
cout << "\n" << myFraction1 << " + " << myFraction2 <<" = " << myFraction1 + myFraction2;
cout << "\n" << myFraction1 << " - " << myFraction2 <<" = " << myFraction1 - myFraction2;
cout << "\n" << myFraction1 << " * " << myFraction2 <<" = " << myFraction1 * myFraction2;
cout << "\n" << myFraction1 << " / " << myFraction2 <<" = " << myFraction1 / myFraction2;
cout << "\n" << myFraction1 << " == " << myFraction2;
if (myFraction1 == myFraction2)
{
cout << " is true.";
}
else
cout << " is false.";
cout << "\n" << myFraction1 << " != " << myFraction2;
if (myFraction1 != myFraction2)
{
cout << " is true. ";
}
else
{
cout << " is false. ";
}
cout << "\n" << myFraction1 << " < " << myFraction2;
if (myFraction1 < myFraction2)
{
cout << " is true. ";
}
else
{
cout << " is false. ";
}
cout << "\n" << myFraction1 << " <= " << myFraction2;
if (myFraction1 <= myFraction2)
{
cout << " is true. ";
}
else
{
cout << " is false. ";
}
cout << "\n" << myFraction1 << " > " << myFraction2;
if (myFraction1 > myFraction2)
{
cout << " is true. ";
}
else
{
cout << " is false. ";
}
cout << "\n" << myFraction1 << " >= " << myFraction2;
if (myFraction1 != myFraction2)
{
cout << " is true. ";
}
else
{
cout << " is false. ";
}
//cout << "\n";
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
fractionType.h:1:9: warning: #pragmaonce in main file
#pragma once
^~~~
main.cpp: In function ‘int main()’:
main.cpp:20:3: error: ‘fractionType’was not declared in this scope
fractionType myFraction1;
^~~~~~~~~~~~
main.cpp:22:16: error: expected ‘;’ before ‘myFraction2’
fractionType myFraction2;
^~~~~~~~~~~
main.cpp:28:10: error: ‘myFraction1’was not declared in this scope
cin >> myFraction1;
^~~~~~~~~~~
main.cpp:34:10: error: ‘myFraction2’was not declared in this scope
cin >> myFraction2;
^~~~~~~~~~~
|