我的标题文件:
#ifndef __FRACTION_H__
#define __FRACTION_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);
#endif
我在.c文件中遇到的2个错误是:
Undefined symbols for architecture x86_64: "_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我的.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;
}
答案 0 :(得分:1)
你.c文件缺少main()
gcc或任何其他编译器首先在程序中查找main()(。c文件)
在我看来,你错过了.c文件中的main()
尝试在.c文件中添加main()并按照您想要的顺序调用main函数内的其他函数