我已经在C程序上完成了此函数计算器的工作,但是我遇到了一个问题,这的确使我发疯。我可以遍历代码和所有内容,但是在编译代码时,我不断得到隐式声明函数和冲突错误。如何解决我的代码以摆脱它们?我是C语言的新手,也是一般编程人员。谢谢:)
#include <stdio.h>
int main(){
int response;
do{
displayWelcomeMessage();
scanf("%d",&response);
selectOption(response);
if(response<-1||response>8){
printf("Please enter a number between 1 and 8\n");
}
}while(response!=-1);
printf("Thanks for using the program!");
return 0;
}
void selectOption(int response){
int adder(int x,int y);
int subtract(int x,int y);
int multiplicate(int x,int y);
double divisor(int x,int y);
int modeOperator(int x,int y);
int factorialOperator(int x);
double powerOperator(int x,int y);
int FibonacciOperator(int x);
int x,y;
switch(response){
case 1 :
printf("the result is: %d\n",adder(x,y));
break;
case 2 :
printf("the result is: %d\n",subtract(x,y));
break;
case 3 :
printf("the result is: %d\n",multiplicate(x,y));
break;
case 4 :
printf("the result is: %lf\n",divisor(x,y));
break;
case 5 :
printf("the result is: %d\n",modeOperator(x,y));
break;
case 6 :
printf("Please enter a variable to calculate the factorial:\n");
scanf("%d",&x);
printf("the result is: %d\n",factorialOperator(x));
break;
case 7 :
printf("Enter base:\n");
scanf("%d", &x);
printf("Enter exponent:\n");
scanf("%d", &y);
printf("the result is: %lf\n",powerOperator(x,y));
break;
case 8 :
printf("enter a value:\n");
scanf("%d",&x);
for(int i=1;i<=x;i++){
printf("%d\n",FibonacciOperator(i));
}
break;
}
}
void displayWelcomeMessage(){
printf("welcome to final programming homework!\n");
printf("please enter 1 for the addition operation\n");
printf("please enter 2 for the subtraction operation\n");
printf("please enter 3 for the multiplication operation\n");
printf("please enter 4 for the division operation\n");
printf("please enter 5 for the mode operation\n");
printf("please enter 6 for the factorial operation\n");
printf("please enter 7 for the power operation\n");
printf("please enter 8 for the Fibonacci operation\n");
printf("please enter -1 to exit\n");
}
int adder(int x,int y){
printf("please enter two variables to be added:\n");
scanf("%d %d",&x,&y);
return x+y;
}
int subtract(int x,int y){
printf("please enter two variables to be subtracted:\n");
scanf("%d %d",&x,&y);
return x-y;
}
int multiplicate(int x,int y){
printf("please add two variables to be multiplied:\n");
scanf("%d %d",&x,&y);
return x*y;
}
double divisor(int x,int y){
printf("please add two variables to be divided:\n");
scanf("%d %d",&x,&y);
return (double)x/(double)y;
}
int modeOperator(int x,int y){
printf("please add two variables to find the reminder:\n");
scanf("%d %d",&x,&y);
return x%y;
}
int factorialOperator(int x){
if (x==1)
return 1;
else
return x*factorialOperator(x-1);
}
double powerOperator(int x,int y){
if(y == 0)
return 1;
else if(y > 0)
return (double)x * powerOperator(x,y-1);
else
return 1 / powerOperator(x,-y);
}
int FibonacciOperator(int x){
if (x==1 || x==2)
return 1;
else
return FibonacciOperator(x-1)+FibonacciOperator(x-2);
}
您需要在定义之前包括所使用功能的头文件。因为它们来自诸如scanf之类的标准库,或者因为您稍后定义了它们。
您在定义或声明函数之前先调用它们。
C编译器仅运行一次,因此,如果在声明函数之前调用函数,则会收到警告。在这种情况下,编译器将假定函数返回int
。然后,当编译器找到实际定义时,如果它与隐式定义不匹配,则会出现错误。
您可以通过重新排列功能,以便在使用前对其进行定义,或者在文件顶部添加这些功能的声明来解决此问题。
所以代替:
int main(){
...
displayWelcomeMessage();
...
}
void displayWelcomeMessage(){
...
}
您想要:
void displayWelcomeMessage(){
...
}
int main(){
...
displayWelcomeMessage();
...
}
或:
void displayWelcomeMessage();
int main(){
...
displayWelcomeMessage();
...
}
void displayWelcomeMessage(){
...
}
对main
以外的所有功能都执行相同的操作。