The C Programming Language 练习题4-5
来源:互联网 发布:鲁滨逊漂流记java 编辑:程序博客网 时间:2024/05/17 11:34
题目
给计算器程序增加访问sin、exp 与pow 等库函数的操作。有关这些库函数的详细信息,参见附录B.4节中的头文件
#include <stdio.h>#include <stdlib.h> /* for atof() */#include <math.h>#include <string.h>#define MAXOP 100#define NUMBER '0'#define MAXVAL 100 /* maximum depth of val stack */#define NAME 'a'int getop(char[]);void push(double);double pop(void);void mathfnc(char s[]);int main(){ int type, i; double op2; char s[MAXOP], matchcmd[MAXOP]; i = 0; 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 '%': push((int)pop() % (int)pop()); break; case '\n': printf("\t%.8g\n", pop()); break; case NAME: mathfnc(s); break; default: printf("error: unknown command %s\n", matchcmd); break; } } return 0;}void mathfnc(char matchcmd[]){ double op4, op5; if(!strcmp(matchcmd, "sin")) { op4 = sin(pop()); push(op4); } else if (!strcmp(matchcmd, "exp")) { op4 = exp(pop()); push(op4); } else if(!strcmp(matchcmd, "pow")) { op4 = pop(); op4 = pow(pop(), op4); push(op4); } else printf("error: command unknown.");}int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//*push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}#include <ctype.h>int getch(void);void ungetch(int);/* getop: get next character or numeric operand */int getop(char s[]){ int i, c, d; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-' && !islower(c)) return c; /*not a number */ if ((c == '-' || c == '+') && !isdigit(d = getch())) { ungetch(d); return c; } i = 0; if(islower(c)) { while(islower(s[++i] = c = getch())) ; s[i] = '\0'; if(c != EOF) ungetch(c); return NAME; } if (isdigit(c))/* a number without '+' and '-' */ { while(isdigit(s[++i] = c = getch())) ; } else /* a number with '+' and '-' */ { s[++i] = d; while (isdigit(s[++i] = c = getch())) ; } if (c == '.') /*collect fraction part */ while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER;}#define BUFSIZE 1000char buf[BUFSIZE]; /*buffer for ungetch */int bufp = 0; /*next free position in buf */int getch(void) /*get a (possibly pushed-back character) */{ return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c) /*push character back on input */{ if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c;}
阅读全文
0 0
- The C Programming Language 练习题4-5
- The C Programming Language 练习题2-5
- The C Programming Language 练习题3-5
- The C Programming Language 练习题2-4
- The C Programming Language 练习题3-4
- The C Programming Language 练习题4-1
- The C Programming Language 练习题4-2
- The C Programming Language 练习题4-3
- The C Programming Language 练习题4-6
- 《The C Programming Language II》练习题代码
- The C Programming Language 练习题1-21
- The C Programming Language 练习题1-22
- The C Programming Language 练习题1-23
- The C Programming Language 练习题1-24
- The C Programming Language 练习题2-1
- The C Programming Language 练习题2-2
- The C Programming Language 练习题2-3
- The C Programming Language 练习题2-6
- 141. Linked List Cycle
- 尝试用es6封装方法
- Spring MVC——基础(简介,使用,地址映射)
- 【Leetcode-Medium-46】Permutations
- Two Sum
- The C Programming Language 练习题4-5
- 合并两个链表,两个表的元素交叉进行,合并之后合并后的链表变成空表
- Numpy混淆点理解
- PullToRefresh
- php实现redis消息队列将数据保存到mysql
- 1039. 到底买不买(20)
- 【OpenCV】error C4996: 'fopen': This function or variable may be unsafe
- 安卓无线调试 react-native
- Java并发编程(自定义同步组件)