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
|
nclude <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
class Rational {
private:
double n, d; // initialize numerator and denominator
public:
Rational(double n = 0, double d = 1) : n(n), d(d) {}; //constructor to establish the fraction format
void add(Rational b) { //member function for addition
n = (n * b.d) + (b.n * d);
d = (d * b.d);
}
void sub(Rational a, Rational b) { //member function for subtraction
n = (a.n * b.d) - (b.n * a.d);
d = (a.d * b.d);
}
void mul(Rational a, Rational b) { //member function for multiplication
n = (a.n * b.n);
d = (a.d * b.d);
}
void div(Rational a, Rational b) { //member function for division
n = (a.n * b.d);
d = (a.d * b.n);
}
bool less(Rational a, Rational b) {
if ((a.n / a.d) < (b.n / b.d)) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
void neg(Rational a) { //member function for division
n = -a.n;
d = (a.d);
}
string display() {
string na;
cout << n << '/' << d;
return na;
}
};
int main() {
int input;
int n1, n2, d1, d2;
cout << "Welcome to the Rational Number Operation Module (make a selection by entering the corresponding number): \n";
cout << "1 - add\n";
cout << "2 - sub\n";
cout << "3 - mul\n";
cout << "4 - div\n";
cout << "5 - less than\n";
cout << "6 - neg\n";
cout << "7 - input (fetches numbers from a file)\n";
cout << "8 - output (writes numbers to a file)\n";
cin >> input;
if (input == 1) {
cout << "Please enter your first fraction (n1 d1): ";
cin >> n1 >> d1;
cout << "Please enter your second fraction (n2 d2): ";
cin >> n2 >> d2;
Rational a(n1, d1);
Rational b(n2, d2);
Rational result = a.add(b); //my error message is on this line, underlining a.
cout << a.display();
cout << '+';
cout << b.display();
cout << '=';
cout << result.display();
}
if (input == 2) {
cout << "Please enter your first fraction (n1 d1): ";
cin >> n1 >> d1;
cout << "Please enter your second fraction (n2 d2): ";
cin >> n2 >> d2;
Rational a(n1, d1);
Rational b(n2, d2);
Rational c; c.sub(a, b);
cout << a.display();
cout << '-';
cout << b.display();
cout << '=';
cout << c.display();
}
if (input == 3) {
cout << "Please enter your first fraction (n1 d1): ";
cin >> n1 >> d1;
cout << "Please enter your second fraction (n2 d2): ";
cin >> n2 >> d2;
Rational a(n1, d1);
Rational b(n2, d2);
Rational c; c.mul(a, b);
cout << a.display();
cout << '*';
cout << b.display();
cout << '=';
cout << c.display();
}
if (input == 4) {
cout << "Please enter your first fraction (n1 d1): ";
cin >> n1 >> d1;
cout << "Please enter your second fraction (n2 d2): ";
cin >> n2 >> d2;
Rational a(n1, d1);
Rational b(n2, d2);
Rational c; c.div(a, b);
cout << a.display();
cout << '/';
cout << b.display();
cout << '=';
cout << c.display();
}
if (input == 5) {
cout << "Please enter your first fraction (n1 d1): ";
cin >> n1 >> d1;
cout << "Please enter your second fraction (n2 d2): ";
cin >> n2 >> d2;
Rational a(n1, d1);
Rational b(n2, d2);
cout << a.display();
cout << '<';
cout << b.display();
cout << '=';
Rational c; c.less(a, b);
//cout << a.less(a, b);
}
if (input == 6) {
cout << "Please enter your first and only fraction (n1 d1): ";
cin >> n1 >> d1;
Rational a(n1, d1);
Rational c; c.neg(a);
cout << a.display();
cout << '=';
cout << c.display();
}
}
|