C-隐含的声明,我所有的函数调用
问题描述:
这整天在吃我。我对C相对来说比较陌生,我无法得到这个工作,我不知道为什么。我有3个文件..C-隐含的声明,我所有的函数调用
我也想为间距道歉,因为这使用堆栈溢出是我第一次......但不管怎么说,这里是代码...
assignment_1.h --- -------------------------------------------------- ------
#ifndef ASSIGNMENT_1_H
#define ASSIGNMENT_1_H
#define NULL 0
int CalculateFactorial(int input);
int CalculateFibonacci(int input);
void ReverseArray(int size, int array[]);
void ShuffleArray(int size, int array[]);
#endif
Main.c ---------------------------------- -------------------------------------
#include "assignment_1.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int fact = CalculateFactorial(15);
int fib = CalculateFibonacci(15);
printf("Factorial: %d\n", fact);
printf("Fib: %d\n", fib);
int array[] = {1,2,3,4,5,6};
int size = 6;
ReverseArray(size, array);
ShuffleArray(size, array);
return 0;
}/*end main*/
assignment_1.c --- ------------------ ------------------------------------------
#include "assignment_1.h"
int CalculateFactorial(int input){
if(input<=0) return 0;
int factorial = input;
int multiplier = input;
while(multiplier > 1){
factorial *= multiplier;
--multiplier;
}/*end while*/
return factorial;
}/*end calcfact*/
int CalculateFibonacci(int input){
if(input<=0) return 0;
else if(input == 1) return 1;
return CalculateFibonnaci(input-1) + CalculateFibonacci(input-2);
}/*end calcfib*/
void ReverseArray(int size, int array[]){
int last = size-1;
int first = 0;
int temp;
while (last-first > 1){ /*stops the loop if size is even*/
temp = array[last];
array[last] = array[first];
array[first] = temp;
++first;
--last;
if(last-first == 2) break; /*stops loop if size is odd*/
}/*end while*/
int i;
for (i = 0; i< size;++i){
printf("%d, ",array[i]);
}
printf("\n");
}/*end reverse*/
void ShuffleArray(int size, int array[]){
srand((unsigned int)time(NULL));
int i;
for (i = 0; i < size; ++i){
int idx = rand()%size; /*random unsigned int between 0 and the
max index of the array*/
int temp = array[i];
array[i] = array[idx];
array[idx] = temp;
}/*end for loop*/
for (i = 0; i< size;++i){
printf("%d, ",array[i]);
}
printf("\n");
}/*end shuffle*/
I不断收到一吨的错误都说警告:函数“”]
我非常感谢所有帮助隐含的声明...
答
既然你单独编译assignment_1.c到main.c中(这是合理的如果你打算使用的接头一旦他们编到两个连接在一起),你需要包括在该文件中,这些头,太;它们不会自动从一个翻译单元转移到另一个翻译单元。
插入到这些assignment_1.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
我发现了一个额外的错误,是你在assignment_1.h指定的NULL
定义; 你应该从未定义NULL
自己,因为它是一个标准的符号。这就像写你自己的printf
和scanf
。
NULL
在<stddef.h>
报头内所定义。当你想要使用NULL
时,也要包括这一点。
不能看到,但是请注意1)你不应该需要定义'NULL' 2)'函数srand()'应该叫一次,在节目的一开始,3)32位'int'将不会保持'15!'的价值。 –
你需要'#include'内,用于在'assignment_1.c'中使用的函数的标头'assignment_1.c'本身(或另一头,它包括)。如果两个文件都使用这些函数,可以将它们包含在'.c'文件中。 – Dmitri
@Dmitiri, 我不太清楚,如果我明白你的意思..在assignment1.c和main.ci的开头有'#include'assignment_1.h'' – spencerktm30