我之前写过一个程序,它将两个分数组合在一起并打印输出,我知道这很简单。我最初使用结构实现了程序,现在我正在尝试使用类重新实现它,不是那么简单我猜...问题是每当我打印输出时它返回为零(对于分母为双零,无论我输入什么数字,我都不知道为什么会这样做。
Sample output:
Enter First Numerator: 1
Enter First Denominator: 2
Enter Second Numerator: 1
Enter Second Denominator: 4
-------------------------
0/00 + 0/00 = 0/00
请查看我的代码并告诉我问题是什么?
#include <iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction()
{
numerator = 0;
denominator = 0;
}
public:
void set(int n, int d) {n = numerator; d = denominator;};
int getNumerator() {return numerator;};
int getDenominator() {return denominator;};
Fraction addTo(Fraction &frac2, Fraction& frac3);
int print() { cout << numerator << "/" << denominator; return 0;};
};
//Adds the value of the Fraction with the attached "addTo" method with the value of a second specified Fraction and assigns the result to a third specified Fraction.
Fraction Fraction::addTo(Fraction& frac2, Fraction& addedTo)
{
frac2.numerator = (numerator * frac2.denominator) + (denominator * frac2.numerator);
frac2.denominator = (denominator * frac2.denominator);
frac2.numerator = addedTo.numerator;
frac2.denominator = addedTo.denominator;
return addedTo;
}
int main()
{
Fraction frac1, frac2, frac3;
int n1, n2, d1, d2;
//Prompts the user for data and then assigns it to the correct variables.
cout << "Enter First Numerator: ";
cin >> n1;
cout << "Enter First Denominator: ";
cin >> d1;
cout << "Enter Second Numerator: ";
cin >> n2;
cout << "Enter Second Denominator: ";
cin >> d2;
frac1.set(n1, d1);
frac2.set(n2, d2);
frac1.addTo(frac2, frac3);
//Formats output.
cout<< "-------------------------\n"
<< frac1.print() << " + " << frac2.print()<< " = " << frac3.print() << endl;
cout << "\n";
return 0;
}
答案 0 :(得分:-1)
你在这里弄错了:
int print() { cout << numerator << "/" << denominator; /* why to hell this method returns 0?! */ return 0;};
就这样写:
void print() { cout << numerator << "/" << denominator; }; // this method doesn't need to return any value
你在这里犯了一个错误:
//Formats output.
cout<< "-------------------------\n"
<< frac1.print() << " + " << frac2.print()<< " = " << frac3.print() << endl;
cout << "\n";
您可以简单地写一下:
// Formats output
frac1.print();
std::cout << " + ";
frac2.print();
std::cout << " = ";
frac3.print();
是的,当然你可以尝试创建将fraction返回为字符串的方法,但是你必须将int转换为string然后使用stringstream,所以我的想法更容易。
<强> @EDIT 强>
你没有在这里设置分子和分母的值:
//Prompts the user for data and then assigns it to the correct variables.
cout << "Enter First Numerator: ";
cin >> n1;
cout << "Enter First Denominator: ";
cin >> d1;
cout << "Enter Second Numerator: ";
cin >> n2;
cout << "Enter Second Denominator: ";
cin >> d2;
你必须这样写:
//Prompts the user for data and then assigns it to the correct variables.
cout << "Enter First Numerator: ";
cin >> n1;
frac1.numerator = n1;
cout << "Enter First Denominator: ";
cin >> d1;
frac1.denominator = d1;
cout << "Enter Second Numerator: ";
cin >> n2;
frac2.numerator = n2;
cout << "Enter Second Denominator: ";
cin >> d2;
frac2.denominator = d2;
<强> @ EDIT2 强>
我刚刚发现了你犯的一个新错误。你必须写这样的add方法:
//Adds the value of the Fraction with the attached "addTo" method with the value of a second
specified Fraction and assigns the result to a third specified Fraction.
Fraction Fraction::addTo(Fraction& frac2, Fraction& addedTo)
{
addedTo.numerator = (numerator * frac2.denominator) + (denominator * frac2.numerator);
addedTo.denominator = (denominator * frac2.denominator);
// frac2.numerator = addedTo.numerator; // this line was a mistake because you tried to set result's numerator again
// frac2.denominator = addedTo.denominator; // the same as above
return addedTo;
}