【发布时间】:2019-10-30 02:34:33
【问题描述】:
代码的目标是使用类/构造函数以“4/5”形式获取输入,不除以另一个有理数,例如“3/4”,并输出分子和分母。
这些任务的代码执行得很好,但是我在 main.js 的底部为 todo1 和 todo2 任务编写代码时遇到了麻烦。 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;
}
【问题讨论】:
标签: c++ class constructor