There are several things wrong with the code. Things could also be organized a little better.
For starters, a Rational should only have two data members: numerator and denominator. Any other values that you need should be local variables inside the methods.
getNum() and getDem() should just return the values. They shouldn't prompt the user for them. The same goes for the other methods: they should not prompt for the values, they should just do the operation. You may need to write a separate function (outside the class) to prompt for a numerator and denominator and return a Rational:
1 2
|
int getNum() { return numerator; }
int getDem() { return denominator; }
| |
You shouldn't let the user set the numerator or denominator separately. I think there should just be a set() method that takes both:
1 2 3 4
|
void set(int n, int d) {
numerator=n;
denominator=d;
}
| |
Your constructor has some of the code to reduce. Why? Just set the values and reduce the rational:
Adding() (why not just call it add()?) should return a Rational. It should NOT print the value. If the code that calls this wants to print the value, then it can call printRational. The code that you have commented out is good except for setting the denominator:
1 2 3 4 5 6 7 8 9
|
Rational Rational::add(const Rational& a)
{
Rational r;
r.numerator = a.numerator * denominator + a.denominator * numerator;
r.denominator = a.denominator * denominator;
r.reduce();
return r;
}
| |
lessthan() isn't right. Compare rationals the same way you'd compare them by hand: put them into a form with a common denominator and then compare the numerators.
You should implement lessthan and equalto. Then implement the other comparisons as calls to these two methods.
Lookup a function to compute the GCD() of two numbers. There are more efficient ways than what you have. Also, I think reduce() should handle the case of a negative denominator. In other words, you should be able to pass any whacky old fraction to reduce() and it should clean it up so the denominator is positive and the numerator & denominator are in lowest form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void Rational::reduce()
{
// Make the denominator positive
if (denominator < 0) {
denominator = -denominator;
numerator = - numerator;
}
int v = gcd(numerator, denominator);
if (v) {
numerator /= gcd;
denominator /= gcd;
}
}
| |
Putting this altogether. Here is a program that creates one rational (1/2), prompts for a second one, adds them together and prints the result. I pasted everything into one file for my own convenience.
Try writing subtract() and multiply() using this as a starting point.
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
|
//project1driver.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#ifndef project1header_h
#define project1header_h
class Rational {
public:
Rational(int n = 0, int d = 1);
Rational add(const Rational& other);
Rational subtract(const Rational& s);
Rational multiply(const Rational& m);
Rational divid(const Rational& b);
Rational invert(const Rational& i);
Rational mixedfraction(const Rational& mf);
int lessthan(const Rational& lt);
int lessthanorequalto(const Rational& ltoet);
int greaterthan(const Rational& gt);
int greaterthanorequalto(const Rational& gtoet);
int equalto(const Rational& et);
int getNum() {return numerator; }
int getDem() {return denominator; }
void set(int n, int d) {
numerator=n;
denominator=d;
}
void printRational();
void printFloat();
private:
int numerator;
int denominator;
void reduce();
};
#endif
// #include "project1header.h"
using namespace std;
unsigned long gcd(unsigned long x, unsigned long y)
{
if (x < y) return gcd(y,x);
// x >= y
while (y) {
unsigned long tmp = y;
y = x%y;
x = tmp;
}
return x;
}
void Rational::printRational()
{
cout << numerator << " / " << denominator;
}
Rational::Rational(int n, int d) {
numerator = n;
denominator = d;
reduce();
}
Rational Rational::add(const Rational& a)
{
Rational r;
r.numerator = a.numerator * denominator + a.denominator * numerator;
r.denominator = a.denominator * denominator;
r.reduce();
return r;
}
void Rational::reduce()
{
// Make the denominator positive
if (denominator < 0) {
denominator = -denominator;
numerator = - numerator;
}
int v = gcd(abs(numerator), denominator);
if (v) {
numerator /= v;
denominator /= v;
}
}
Rational getRational()
{
int n, d;
cout << "Enter a numerator: ";
cin >> n;
cout << "Enter a denominator: ";
cin >> d;
return Rational(n,d);
}
int main()
{
Rational a(1,2);
Rational b = getRational();
Rational c = a.add(b);
b.printRational();
cout << " plus 1/2 is ";
c.printRational();
cout << '\n';
}
| |