我创建了一个C ++程序,让用户输入两个分数,然后减少分数,添加分数,减去它们,乘以它们,然后将它们分开,然后显示结果。
当我输入负数时,我遇到了减少分数的问题。该程序完美无缺地使用正数,但是当我输入负数时,它不会简化。相反,黑屏告诉我这个:
Enter a value for the numerator: -3
Enter a value for the denominator: 2
Enter a value for the numerator: 5
Enter a value for the denominator: 2
The fractions reduced are -3/2 and 5/2
When added it gives 1
When subtracted it gives -16/4` **Does not reduce**
When multiplied it gives -15/4
When divided it gives -6/10 ` **Does not reduce**
They are not equal
The built in fixed 2 argument constructor gives the fraction 3/5
The default constructor gives the fraction 1/2
Would you like to go again? (Y/N):
这是功能:
//Function that simplifies fraction.
rational rational::reduce()
{
//Set the greatest common factor to the denominator * numerator
int GCF = denom * numer;
//Create a loop that decreases the GCF by 1 each time.
for (GCF; GCF > 1; GCF--)
{
//If the numerator and the denominator both have a remainder of zero when dividing by the GCF
if ((numer % GCF == 0) && (denom % GCF == 0))
{
numer /= GCF;
denom /= GCF;
}
}
rational answer;
//Store the numerator and denominator into a rational variable
answer.numer = numer;
answer.denom = denom;
//Return the answer
return answer;
}
对于我做错的任何帮助都表示赞赏,我是初学者,只是我们。