Целью кода является использование классов / конструкторов для получения ввода в форме «4/5» без деления и другого рационального числа, такого как «3/4», а также для вывода числителя и знаменателя.
Код работает хорошо для этих задач, но у меня возникают проблемы при написании кода для задач todo1 и todo2 внизу main. Todo1 Я пробовал такие вещи, как рациональный r (3,4), рациональный r (4,5), однако вывод просто печатается как огромная ошибка.
Любая помощь приветствуется!
// Add appropriate headers
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
using namespace std;
/* KEEP THIS COMMENT
* class Rational
* represents a Rational number. Remember rational means ratio-nal
* which means there is a numerator and denominator having
* integer values. Using good ADT techniques, we have made member
* variable private (also known as instance variables) and made member
* functions public.
*/
class Rational
{
private:
int numerator;
int denominator;
public:
// ToDo: Default Constructor
Rational();
// ToDo: Constructor that takes int numerator
Rational(int num);
// ToDo: Constructor that takes int numerator and int denominator
Rational(int num, int den);
// ToDo: Member function to read a rational in the form: n/d
void input();
// ToDo: Member function to write a rational as n/d
void output();
// ToDo: declare an accessor function to get the numerator
int getNumerator();
// ToDo: declare an accessor function to get the denominator
int getDenominator();
// ToDo: delcare a function called Sum that takes two rational objects
// sets the current object to the sum of the given objects using the
// formula: a/b + c/d = ( a*d + b*c)/(b*d)
void sum(Rational a, Rational b);
// test if two rational numbers are equal.
bool isEqual(const Rational& op);
};
int main()
{
// ToDo: declare three rational objects using the default constructor
Rational a, b, c;
char answer='Y';
// Main loop to read in rationals and compute the sum
do {
cout << "\nEnter op1 (in the format of p/q): ";
a.input();
//Debug line
a.output();
// ToDo: use your input member function to read the first rational
cout << "\nEnter op2 (in the format of p/q): ";
// ToDo: use your input member function to read the second rational
b.input();
//Debug line
b.output();
// ToDo: use the third rational to call Sum with first and second as parameters
c.sum(a, b);
cout << "\nThe sum of op1 and op2 is: ";
c.output();
// ToDo: ouptput the third rational
cout << endl;
cout << "\nTry again (Y/N)?";
cin >> answer;
cin.ignore(256,'\n');
} while (answer == 'y' || answer == 'Y');
// ToDo: test getters
cout << "\nC's numerator is: " << c.getNumerator() << endl;
cout << "\nC's denominator is: " << c.getDenominator() << endl;
// TODO1: Use two constructors to declare two numbers 3/4 and 4/5
// Rational d(3,4);
// Rational e(4,5);
// TODO2: Use output to print both rationals
//cout << d.output() << " " << e.output() << endl;
//cout << e.output() << endl;
return 0;
}
// ToDO: Implement your class member functions below.
Rational::Rational()
{
numerator = 0;
denominator = 1;
}
Rational::Rational(int num)
{
numerator = num;
denominator = 1;
}
Rational::Rational(int num, int den)
{
if(den == 0)
den = 1;
numerator = num;
denominator = den;
}
void Rational::sum(Rational a, Rational b)
{
int num = (a.numerator*b.denominator + a.denominator*b.numerator);
int den = (a.denominator*b.denominator);
numerator = num;
denominator = den;
}
void Rational::input()
{
string in;
//cout << "Enter a rational number in the form of x/y : ";
getline(cin, in);
//cin >> in;
// find the index position of /
int indx = in.find("/");
// seperator out the numerator
numerator = atoi(in.substr(0, indx).c_str());
// seperate out the denominator
denominator = atoi(in.substr(indx+1, in.length()).c_str());
Rational(numerator, denominator);
//numerator = r.numerator;
//denominator = r.denominator;
//cout <<num << " " <<den << endl; // Debug Line
//numerator = num;
//denominator = den;
//cout <<numerator << " " <<denominator << endl; // Debug Line
}
void Rational::output()
{
cout << numerator << "/" << denominator;
}
// Two getter functions
int Rational::getNumerator()
{
return numerator;
}
int Rational::getDenominator()
{
return denominator;
}