计算器和C中的浮点模数函数
我必须为计算器的’%’(模数)函数写一个案例。 当我编译时,它告诉我我错误地使用了fmod
函数。 如果没有’%’,代码的其余部分就可以正常工作。
#include #include #include #include // Originally missing #define MAXOP 100 #define NUMBER '0' int getop(char []); void push(double); double pop(void); main() { int type; double op2; char s[MAXOP]; printf("\nEnter numbers below in the following format\nwith a space between each:\n"); printf("\tnum1 num2 operator\n"); printf("\tyou may use (+, -, /, *) for the operators\n"); while((type = getop(s)) != EOF) { switch(type) { case NUMBER: push(atof(s)); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); break; case '/': op2 = pop(); if(op2 != 0.0) push(pop() / op2); else printf("error: zero divisor\n"); break; case '%': op2=pop(); if(op2 != 0.0) push(fmod(pop(),op2)); else printf("error: zero divisor"); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }
我fmod
了fmod
函数,我仍然无法弄清楚为什么它不起作用。 有什么想法吗?
我添加了math.h标头,我现在收到的错误消息是:
/tmp/ccgiVhWI.o: In function `main': calc.c:(.text+0x14f): undefined reference to `fmod' collect2: ld returned 1 exit status
你缺少#include
所以编译器认为fmod()
是一个返回int
的函数,或者它抱怨它知道fmod()
但是你没有正确地声明它,或者就是这样。
您最好包含确切的编译器错误消息(除非文件名有很长的路径,在这种情况下截断文件名将是礼貌的)。
您缺少math.h
标头以提供fmod
的原型。