这将创建一个分数并相应地分配负号。但是,当我要打印整数,分子和分母时,负号不会传递到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;
}

最佳答案

丢失的信号来自:

result.wholeNumber = (numerator/denominator)*-1;


这是在numerator * denominator < 0所在的块内,因此此行始终将wholeNumber设置为正。取出-1

另外,您在其他一些地方也有垃圾,因为您没有在应有的情况下未将wholeNumber初始化为0。由于使用fabs并存在四舍五入的问题,当您尝试存储较大的值时会出现此问题。使用整数求反而不是fabs

10-07 14:46