.C& .H文件错误

时间:2015-11-30 05:24:46

标签: c xcode struct fractions

我得到18个屈服符号错误& Xcode中的2个停止符号错误 对于所有18个屈服误差,他们说"使用浮点绝对值函数' fabs'当参数是整数类型"。这是什么意思?
对于2个停止符号错误,它们用于" _main",引用自:链接器命令失败,退出代码为1(使用-v查看调用)。
我需要做些什么才能让我的脚本构建和运行?

H档:

struct fraction{
    int wholeNumber;
    int numerator;
    int denominator;
};

struct fraction fractionCreate(int numerator, int denominator);
struct fraction fractionAdd(struct fraction a, struct fraction b);
struct fraction fractionSubtract(struct fraction a, struct fraction b);
struct fraction fractionMultiply(struct fraction a, struct fraction b);
struct fraction fractionDivide(struct fraction a, struct fraction b);
void fractionPrint(struct fraction f);

C档案:

#include <math.h>
#include <stdio.h>
#include "fraction.h"

struct fraction fractionCreate(int numerator, int denominator)
{
    struct fraction result;
    result.wholeNumber = 0;
    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;
                }
            }
        }

        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;
                    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 if(f.numerator ==0 && f.denominator !=0)
    {
        printf("0\n");
    }
    else
    {
        // There IS a whole number
        if (f.wholeNumber !=0)
        {
            //ONLY whole number
            if (f.numerator == 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);
        }
    }

}




struct fraction fractionAdd(struct fraction a, struct fraction b)
{
    struct fraction result;

    //If whole number exists
    if(a.wholeNumber!=0)
    {
        if (a.numerator ==1 && a.denominator ==1)
        {
            a.numerator =0;
        }
    }
    else if (b.wholeNumber!=0)
    {
        if (b.numerator ==1 && b.denominator ==1)
        {
            b.numerator =0;
        }
    }
    //If the numerator of a is 1 and denom is 1, then make it 0
    //If the numerator of b is 1 and deom is 1, then make it 0

    //Open up the mixed fraction
    a.numerator = (a.wholeNumber*a.denominator) + a.numerator;
    b.numerator = (b.wholeNumber*b.denominator) + b.numerator;

    //If the denominators equal
    if (a.denominator == b.denominator)
    {
        result.numerator = a.numerator + b.numerator;
        result.denominator = b.denominator;
        result = fractionCreate(result.numerator, result.denominator);
        return result;
    }
    else if (a.denominator == 0 && b.denominator == 0)
    {
    }
    //If the denominator is not equal,
    else if (a.denominator != b.denominator)
    {
        int LCM;
        int temp =0;
        //only use the absolute values when finding the GCD
        int tempDenominatorA = fabs(a.denominator);
        int tempDenominatorB = fabs(b.denominator);

        while(tempDenominatorB!=0)
        {
            temp = tempDenominatorB;
            tempDenominatorB = tempDenominatorA%tempDenominatorB;
            tempDenominatorA = temp;
        }
        int GCD = tempDenominatorA;
        LCM = fabs(a.denominator*b.denominator)/GCD;

        //then multiply each denominator and numerator by a number that will make it the LCM
        int multiplyA, multiplyB;
        multiplyA = LCM/fabs(a.denominator);
        multiplyB = LCM/fabs(b.denominator);
        a.numerator = a.numerator*multiplyA;
        b.numerator = b.numerator*multiplyB;

        result.numerator = a.numerator + b.numerator;
        result.denominator = LCM;

        result = fractionCreate(result.numerator, result.denominator);

        return result;

    }

    return result;
}

struct fraction fractionSubtract(struct fraction a, struct fraction b)
{
    struct fraction result;

    //If whole number exists
    if(a.wholeNumber!=0)
    {
        if (a.numerator ==1 && a.denominator ==1)
        {
            a.numerator =0;
        }
    }
    else if (b.wholeNumber!=0)
    {
        if (b.numerator ==1 && b.denominator ==1)
        {
            b.numerator =0;
        }
    }
    //If the numerator of a is 1 and denom is 1, then make it 0
    //If the numerator of b is 1 and deom is 1, then make it 0

    //Open up the mixed fraction
    a.numerator = (a.wholeNumber*a.denominator) + a.numerator;
    b.numerator = (b.wholeNumber*b.denominator) + b.numerator;
    a.wholeNumber = 0;
    b.wholeNumber = 0;

    //If the denominators equal
    if (a.denominator == b.denominator)
    {
        result.numerator = a.numerator - b.numerator;
        result.denominator = b.denominator;
        result = fractionCreate(result.numerator, result.denominator);
        return result;
    }
    else if (a.denominator == 0 && b.denominator == 0)
    {
    }
    //If the denominator is not equal,
    else if (a.denominator != b.denominator)
    {
        int LCM;
        int temp =0;
        //only use the absolute values when finding the GCD
        int tempDenominatorA = fabs(a.denominator);
        int tempDenominatorB = fabs(b.denominator);

        while(tempDenominatorB!=0)
        {
            temp = tempDenominatorB;
            tempDenominatorB = tempDenominatorA%tempDenominatorB;
            tempDenominatorA = temp;
        }
        int GCD = tempDenominatorA;
        LCM = fabs(a.denominator*b.denominator)/GCD;

        //then multiply each denominator and numerator by a number that will make it the LCM
        int multiplyA, multiplyB;
        multiplyA = LCM/fabs(a.denominator);
        multiplyB = LCM/fabs(b.denominator);
        a.numerator = a.numerator*multiplyA;
        b.numerator = b.numerator*multiplyB;

        result.numerator = a.numerator - b.numerator;
        result.denominator = LCM;

        result = fractionCreate(result.numerator, result.denominator);

        return result;

    }

    return result;
}

struct fraction fractionMultiply(struct fraction a, struct fraction b)
{
    struct fraction result;
    //If whole number exists
    if(a.wholeNumber!=0)
    {
        if (a.numerator ==1 && a.denominator ==1)
        {
            a.numerator =0;
        }
    }
    else if (b.wholeNumber!=0)
    {
        if (b.numerator ==1 && b.denominator ==1)
        {
            b.numerator =0;
        }
    }
    //If the numerator of a is 1 and denom is 1, then make it 0
    //If the numerator of b is 1 and deom is 1, then make it 0

    //Open up the mixed fraction
    a.numerator = (a.wholeNumber*a.denominator) + a.numerator;
    b.numerator = (b.wholeNumber*b.denominator) + b.numerator;

    a.wholeNumber = 0;
    b.wholeNumber = 0;

    result.numerator = a.numerator*b.numerator;
    result.denominator = a.denominator*b.denominator;

    result = fractionCreate(result.numerator, result.denominator);

    return result;
}

struct fraction fractionDivide(struct fraction a, struct fraction b)
{
    struct fraction result;
    //If whole number exists
    if(a.wholeNumber!=0)
    {
        if (a.numerator ==1 && a.denominator ==1)
        {
            a.numerator =0;
        }
    }
    else if (b.wholeNumber!=0)
    {
        if (b.numerator ==1 && b.denominator ==1)
        {
            b.numerator =0;
        }
    }
    //If the numerator of a is 1 and denom is 1, then make it 0
    //If the numerator of b is 1 and deom is 1, then make it 0

    //Open up the mixed fraction
    a.numerator = (a.wholeNumber*a.denominator) + a.numerator;
    b.numerator = (b.wholeNumber*b.denominator) + b.numerator;

    a.wholeNumber = 0;
    b.wholeNumber = 0;

    result.numerator = a.numerator*b.denominator;
    result.denominator = a.denominator*b.numerator;

    result = fractionCreate(result.numerator, result.denominator);

    return result;
}

2 个答案:

答案 0 :(得分:2)

好的,fabs问题是你将整数传递给一个接收并生成浮点数的函数。例如,传递int numerator,然后从numerator = fabs(numerator);处的浮点返回值进行设置。看起来相同的模式足以发生18次错误。

也许你想要abs()和fabs()函数? Abs()接受并返回整数。或者你可以从浮子上来回投掷(丑陋)。

第二个问题是没有main()函数。这就是代码将开始运行的地方,并且链接器声称它已丢失。

答案 1 :(得分:1)

函数fabs()需要参数类型double,并且需要#include<math.h>

  

双晶圆厂(双x);

和函数abs()需要参数类型int,并且需要#include<stdlib.h>

  

int abs(int n);

  • fabs

    /* fabs example */
    #include <stdio.h>      /* printf */
    #include <math.h>       /* fabs */
    
    int main ()
    {
      printf ("The absolute value of 3.1416 is %f\n", fabs (3.1416) );
      printf ("The absolute value of -10.6 is %f\n", fabs (-10.6) );
      return 0;
    }
    
  • abs

    /* abs example */
    #include <stdio.h>      /* printf */
    #include <stdlib.h>     /* abs */
    
    int main ()
    {
       int n,m;
       n=abs(23);
       m=abs(-11);
       printf ("n=%d\n",n);
       printf ("m=%d\n",m);
       return 0;
     }