练习 4-5 给计算器程序增加访问 sin、exp 与 pow 等库函数的操作。有关这些库函数的详细信息,参见附录 B.4 节中的头文件math.h。
函数strcmp在比较结果相等的时候返回值是0不是1!!!一开始在调用这里的时候一直以为返回值会是1,怎么算也算不出来,想了好久。
主函数——main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "calc.h"#define MAXLEN 100
#define MAXOP 100int getop(char []);
void push(double);
double pop(void);
void math_op(char s[]);
void mod(void);int main()
{int type;double op1, op2;char s[MAXOP];while ((type = getop(s)) != EOF){switch (type) {case NUMBER:push(atof(s)); //printf("push number\n");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);elseprintf("error:zero divisior\n");break;case '%':mod();break;case MATHOP: //判断是否是math.h中函数math_op(s);break;case '\n':printf("result: %.8g\n", pop());break;default:printf("error: unknown command %s\n", s);break;}}return 0;}void mod(void)
{double op1, op2;op2 = pop();if ((op1 = pop()) <= 0.0){printf("error:mod num lower than zero\n");return;}else{printf("%g %g\n", op1, op2);printf("%.1lf %.1lf %g %g \n",floor(op1 + 0.5), floor(op2 + 0.5), op1, op2);if (floor(op1 + 0.5) != op1 || floor(op2 + 0.5) != op2){printf("error: not integer\n");return;}if (op2 < 0.0){while (op2 < 0.0)op2 += op1;push((int)op1 % (int)op2);return;}elsepush((int)op1 %(int)op2);return;}}void math_op(char s[])
{if (!strcmp(s, "sin")){push(sin(pop()));return;}else if (!strcmp(s, "cos")){push(cos(pop()));return;}else if (!strcmp(s, "tan")){push(tan(pop()));return;}else if (!strcmp(s, "exp")){push(exp(pop()));return;}else if (!strcmp(s, "pow")){push(pow(pop(),pop()));return;}else{printf("error: unknown command %s\n", s);return;}
}
为了让主函数看起来清爽一点,把模运算和调用math.h库中函数的运算写了个函数移到了外面。
从输入字符串中获取操作数或操作值——getop.c
#include <stdio.h>
#include <ctype.h>
#include "calc.h"int getch(void);
void ungetch(int);int getop(char s[])
{int i, c;while ((s[0] = c = getch()) == ' ' || c == '\t');s[1] = '\0';if (!isdigit(c) && !islower(c) && c != '.')return c;i = 0;if (islower(c)){while (islower(s[++i] = c = getch()));s[i] = '\0';if (c != EOF)ungetch(c);return MATHOP;}if (isdigit(c))while (isdigit(s[++i] = c = getch()));if (c == '.')while (isdigit(s[++i] = c = getch()));s[i] = '\0';if (c != EOF)ungetch(c);return NUMBER;
}
增加了islower的函数来判断是否可能输入了可识别的函数,如果函数不可识别也会返回值,但是在主函数中会被自动忽略。
getop函数调用的函数,可以大致理解为getchar()——getch.c
#include <stdio.h>
#define BUFSIZE 10
#include "calc.h"char buf[BUFSIZE];
int bufp = 0;int getch(void)
{return (bufp > 0) ? buf[--bufp] : getchar();
}void ungetch(int c)
{if (bufp >= BUFSIZE)printf("ungetch: too many characters\n");elsebuf[bufp++] = c;return;
}
stack.c
#include <stdio.h>#define MAXVAL 100int sp = 0;
double val[MAXVAL];void push(double f)
{if (sp < MAXVAL)val[sp++] = f;elseprintf("error: stack full, can't push %g\n", f);return;
}double pop(void)
{if (sp > 0)return val[--sp];else{printf("error:stack empty\n");return 0.0;}
}
头文件——calc.h
#define NUMBER '0'
#define MATHOP 'm'void push(double);
double pop(void);
int getop(char []);
int getch(void);
void ungetch(int);
由于主函数main()中的switch不接受字符串,所以将所有函数库中的函数统一以MATHOP表示,具体是什么函数交给math_op识别处理。
多文件编译在4-3博客中说了,不再赘述。