练习4-3 Given the basic framework ,it's straightfoward to extend the calculator .Add the modulus(%)operator and provisions for negative numvers.
#include<stdio.h>
#include<math.h> /*for fmod()*/
#include<stdlib.h> /*for atof() */
#define MAXOP 100 /* max size of operand or operator*/
#define NUMBER '0' /*signal that a number was found*/
int getop(char []);
void push(double);
double pop(void);
/*reverse Polish calculator*/
main()
{
int type;
double op2;
char s[MAXOP];
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)
push(fmod(pop(),op2));
else
printf("error:zero divisor\n");
break;
case '\n':
printf("\t%.8g\n",pop());
break;
default:
printf("errro:unknown command%s\n",s);
break;
}
}
return 0;
}
编译时会出现一个错误,即为:undefined reference to ‘ fmod’
查阅前辈的解决办法是添加 #include <math.h>,但是添加之后仍然是同样的错误。但又有人在gcc编译通过命令行使得gcc链接到math library。也就是“ - lim +所涉及的数据库。”
问题(一)是:我没有在gcc下面直接编译,而使用的是anjuta IDE。所以没有办法使用上述命令链接math.h。那么就没有办法解决此问题了。
问题(二):是否是anjuta的编译没有完全和C语言的函数库链接好呢?