这会创建一个分数并相应地分配负号。但是,当我想打印出整数,分子和分母时,负号不会传递到fractionPrint函数中。因此,仅打印出正值。
struct fraction fractionCreate(int numerator, int denominator)
{
struct fraction result;
if (denominator == 0)
{
result.numerator = 0;
result.denominator = 0;
}
else
{
if (numerator*denominator <0)
{
int tempNumerator = fabs(numerator);
int tempDenominator = fabs(denominator);
int temp = 0;
while(tempDenominator != 0)
{
temp = tempDenominator;
tempDenominator = tempNumerator%tempDenominator;
tempNumerator = temp;
}
int GCD = tempNumerator;
numerator = fabs(numerator);
denominator = fabs(denominator);
//Num is larger than denom
if(numerator>=denominator)
{
//whole number
if(numerator%denominator == 0)
{
result.wholeNumber = (numerator/denominator)*-1;
result.numerator = 1;
result.denominator = 1;
}
//mixed number
else
{
result.wholeNumber = ((numerator - (numerator%denominator)) /denominator)*-1;
numerator = (numerator%denominator);
if (numerator%GCD ==0 && denominator%GCD ==0)
{
result.numerator = numerator/GCD;
result.denominator= denominator/GCD;
}
else
{
result.numerator = numerator;
result.denominator = denominator;
}
}
}
//simple fraction
else
{
if (numerator%GCD ==0 && denominator%GCD ==0)
{
result.numerator = (numerator/GCD)*-1;
result.denominator= denominator/GCD;
}
else
{
result.numerator = numerator*-1;
result.denominator = denominator;
}
}
}
if (numerator*denominator > 0)
{
int tempNumerator = fabs(numerator);
int tempDenominator = fabs(denominator);
int temp = 0;
while(tempDenominator != 0)
{
temp = tempDenominator;
tempDenominator = tempNumerator%tempDenominator;
tempNumerator = temp;
}
int GCD = tempNumerator;
numerator = fabs(numerator);
denominator = fabs(denominator);
//Num is larger than denom
if(numerator>=denominator)
{
//whole number
if(numerator%denominator == 0)
{
result.wholeNumber = numerator/denominator;
result.numerator = 1;
result.denominator = 1;
}
//mixed number
else
{
result.wholeNumber = (numerator - (numerator%denominator)) /denominator;
numerator = (numerator%denominator);
if (numerator%GCD ==0 && denominator%GCD ==0)
{
result.numerator = numerator/GCD;
result.denominator= denominator/GCD;
}
else
{
result.numerator = numerator;
result.denominator = denominator;
}
}
}
//simple fraction
else
{
if (numerator%GCD ==0 && denominator%GCD ==0)
{
result.numerator = numerator/GCD;
result.denominator= denominator/GCD;
}
else
{
result.numerator = numerator;
result.denominator = denominator;
}
}
}
}
return result;
}
void fractionPrint(struct fraction f)
{
if (f.numerator == 0 && f.denominator == 0)
{
printf("Divide by zero!\n");
}
else
{
// There IS a whole number
if (f.wholeNumber !=0)
{
int tempNumerator;
if (f.numerator <0)
{
tempNumerator = f.numerator *-1;
}
//ONLY whole number
if (tempNumerator == 1 && f.denominator ==1 )
{
printf("%d\n", f.wholeNumber);
}
//Mixed number
else
{
printf("%d %d/%d\n", f.wholeNumber, f.numerator, f.denominator);
}
}
//fraction only
else
{
printf("%d/%d\n", f.numerator, f.denominator);
}
}
}
int main (void)
{
struct fraction a, b, c, d, r, bad;
a = fractionCreate (-5, -6);
fractionPrint(a);
b = fractionCreate (-10, 2);
fractionPrint(b);
c = fractionCreate (56, -160);
fractionPrint(c);
d = fractionCreate (-35, 150);
fractionPrint(d);
bad = fractionCreate (8, 0);
fractionPrint(bad);
return 0;
}
答案 0 :(得分:1)
丢失的标志来自:
result.wholeNumber = (numerator/denominator)*-1;
这是在numerator * denominator < 0
的块内,因此该行始终将wholeNumber
设置为正数。取出-1
。
此外,您在其他一些地方也有垃圾,因为您没有将wholeNumber
初始化为0
。由于使用了fabs
和舍入,当您尝试存储更大的值时,会出现潜在的问题;使用整数否定而不是fabs
。