gpt4 book ai didi

c++ - 使用构造函数打印特定数字时遇到问题

转载 作者:行者123 更新时间:2023-11-28 04:06:34 25 4
gpt4 key购买 nike

代码的目标是使用类/构造函数以不除以“4/5”形式的输入和另一个有理数(例如“3/4”)以及输出分子和分母。

代码对这些任务执行得很好,但我在为 main 底部的 todo1 和 todo2 任务编写代码时遇到了问题。 Todo1 我已经尝试过 rational r(3,4) rational 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;
}

最佳答案

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

// 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();

关于c++ - 使用构造函数打印特定数字时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58617975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com