【问题标题】:Trouble with using constructors to print specific numbers使用构造函数打印特定数字的问题
【发布时间】: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


    【解决方案1】:

    函数Rational::output()返回无效。
    因此,当您调用时编译器会显示错误:

    // TODO2: Use output to print both rationals
    cout << d.output() << " " << e.output() << endl;
    cout << e.output() << endl;
    

    我想我们应该把它改成:

    // TODO2: Use output to print both rationals
    d.output();
    e.output();
    

    【讨论】:

    • 另一种方法是将Rational::output 更改为它不会打印出来而是返回一个格式化的字符串。这是更通用的,因为调用者可以对字符串做任何他们想要的,打印它,把它塞进一个日志文件,把它喂给猫......
    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多