我正在查找谐波数,只要输入我用来调试程序的行(即最后的 cout 行),该函数就会循环直到找到正确的谐波数。如果我删除 cout 的任何部分,程序就会再次开始失败。我完全不知道 cout 如何影响该计划。没有它,一切都存储零。
struct fraction
{
long nume, denom;
};
void add (fraction x, fraction y, fraction& result)
{
long Numerator, Denominator, GCD, Higher, Lower;
Numerator = (x.nume * y.denom + x.denom * y.nume);
Denominator = (x.denom * y.denom);
if (Numerator > Denominator)
{
Lower = Denominator;
Higher = Numerator;
}
else if (Numerator < Denominator)
{
Higher = Denominator;
Lower = Numerator;
}
while (Higher % Lower > 0)
{
GCD = Higher % Lower;
Higher = Lower;
Lower = GCD;
}
result.nume = Numerator/GCD;
result.denom = Denominator/GCD;
cout << "d " << result.nume << " " << GCD << " " << Denominator/GCD << " " << result.denom << endl;
}