The C programming language 4.1
2014-08-06 00:29
190 查看
4.2对ATOF函数进行扩充,使它可以处理形如123.45e-6的科学表示法,其中浮点数后面可能会跟e或E以及一个指数。
4.3对计算器程序进行扩充,加入取模%运算符,并考虑负数的情况。
4.4 在栈操作中添加命令,在不弹出元素的情况下打印栈顶元素;复制栈顶元素;交换栈顶两个元素的值。另增加一个命令用于清空栈。
4.5 给计算器程序增加sin/exp/pow等库函数的操作。
4.6 给计算器程序增加处理变量的命令。增加一个变量存放最近打印的值。
本来想装X,把几个题全部写出来。答案上也确实在4.6中给出了综合版,但是这个题目有一个很严重的问题在于不实用,或者说在我看来,没有有效的测试方式来证明写出来的代码是对的,在data structure& algorithm analysis中曾经提到过 逆波兰表示法。而且,几道题目的功能,例如最后一个,混乱,模糊,导致这道题繁琐复杂而又简单。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX 15 double atof_test(char[]); int main(void) { int i = 0; double result; char s[MAX]; while (strlen((gets_s(s)))) { result = atof_test(s); printf("%f\n",result ); } return 0; } double atof_test(char s[]) { int i=0,sign,flag=1,j; double result=0,power=1,exp=1; char c; while (isspace(s[i])) i++; if ((c=s[i] )== '+' || c == '-') { sign = (c == '+' ? 1 : -1); i++; } if (!(isdigit((c = s[i])))) { return 0; } while (isdigit((c=s[i]))) { result = result * 10 + (c - '0'); i++; } if (c=='.') { i++; while (isdigit((c = s[i]))) { result = result * 10 + (c - '0'); power *= 10.0; i++; } } if (c == 'e' || c == 'E') { i++; if ((c=s[i])=='-') { flag = 0; i++; } //默认e后面的倍数不会超过10 c = s[i] - '0'; if (flag) { while (c--) exp *= 10.0; } else { while (c--) exp *= 0.1; } } return exp*result / power*sign; }
4.3对计算器程序进行扩充,加入取模%运算符,并考虑负数的情况。
4.4 在栈操作中添加命令,在不弹出元素的情况下打印栈顶元素;复制栈顶元素;交换栈顶两个元素的值。另增加一个命令用于清空栈。
4.5 给计算器程序增加sin/exp/pow等库函数的操作。
4.6 给计算器程序增加处理变量的命令。增加一个变量存放最近打印的值。
本来想装X,把几个题全部写出来。答案上也确实在4.6中给出了综合版,但是这个题目有一个很严重的问题在于不实用,或者说在我看来,没有有效的测试方式来证明写出来的代码是对的,在data structure& algorithm analysis中曾经提到过 逆波兰表示法。而且,几道题目的功能,例如最后一个,混乱,模糊,导致这道题繁琐复杂而又简单。
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #define MAXOP 100 #define NUMBER '0' #define IDENTIFIER 1 #define MAXVARS 30 #define MAX_ID_LEN 32 struct varType { double val; char NAME[MAX_ID_LEN]; }var[MAXVARS]; struct varType last; int getop(char[]); void push(double); double pop(void); void printTop(void); void deplicate(void); void exchange(void); void clearstacks(struct varType []); void DealwithNAME(char[]); void DealwithVar(char[], struct varType[]); int pos = 0; int main(void) { int type; double op2; char s[MAXOP]; while ((type=getop(s))!=EOF) { switch (type) { case NUMBER: push(atof(s)); break; case IDENTIFIER: DealwithNAME(s); break; case '+': push(pop() + pop()); break; case '-': op2 = pop(); push(pop() - op2);// 如果是pop()-pop(),不能确定两个pop的次序。 break; case '*': push(pop() * pop()); break; case '/': op2 = pop(); if (op2==0.0) { printf("Error! Zero divisor!\n"); getchar(); exit(1); } push(pop() / op2); break; case '%': op2 = pop(); if (op2 == 0.0) { printf("Error! Zero divisor!\n"); getchar(); exit(1); } push(fmod(pop(), op2)); break; case '?': printTop(); break; case '#': deplicate(); break; case '~': exchange(); break; case '!': clearstacks(var); break; case '=': pop(); var[pos].val = pop(); last.val = var[pos].val; push(last.val); break; case '<': printf("The last variable used was: %s (value == %g)\n", last.NAME, last.val); break; default: break; } } return 0; } void DealwithNAME(char s[]) { double result, tmp; if (!strcmp(s, "sin")) { tmp = pop(); push(sin(tmp)); } else if (!strcmp(s, "exp")) { tmp = pop(); push(exp(tmp)); } else if (!strcmp(s, "pow")) { tmp = pop(); push(pow(pop(),tmp)); } else { DealwithVar(s, var); } } void DealwithVar(char s[], struct varType var[]) { int i = 0; while (var[i].NAME[0] != '\0' && i < MAXVARS - 1) { if (!strcmp(s, var[i].NAME)) { strcpy(last.NAME, s); last.val = var[i].val; push(var[i].val); pos = i; return; } i++; } /* variable name not found so add it */ strcpy(var[i].NAME, s); /* And save it to the last variable */ strcpy(last.NAME, s); push(var[i].val); pos = i; }; #define MAXVAL 100 int sp = 0;//默认sp在栈顶+1 double val[MAXVAL]; void push(double f) { if (sp<MAXVAL) { val[sp++] = f; } else { printf("ERROR! No more space can be provided.\n"); } } double pop(void) { if (sp>0) { return val[--sp]; } else { printf("ERROR! Stack empty.\n"); return 0.0; } } void printTop(void) { double c = pop(); push(c); printf("%g", c); } void deplicate(void) { double c = pop(); push(c); push(c); } void exchange(void) { double c = pop(),tmp; tmp = pop(); push(c); push(tmp); } void clearStacks(struct varType var[]) { int i; /* Clear the main stack by setting the pointer to the bottom. */ sp = 0; /* Clear the variables by setting the initial element of each name to the terminating character. */ for (i = 0; i < MAXVARS; ++i) { var[i].NAME[0] = '\0'; var[i].val = 0.0; } } void clearstacks(void); #include <ctype.h> int getch(void); void ungetch(int); int getop(char s[])//s是作为一个地址传送getop()函数的参数 { int i=0,c; while (isspace(c = getch())) ; if (isalpha(c)) { s[i++] = i; while (isalpha(c)) { s[i++] = c; c == getch(); } if (c!=EOF) { ungetch(c); } return IDENTIFIER; } if (!isdigit(c) && c != '.'&&c != '-') { return c; } if (isdigit(c)) { s[i++] = c; while (isdigit(c = getch())) s[i++] = c; } if (c=='.') { s[i++] = c;//不用在.前面加0, atof()函数可以处理.**类型的小数 while (isdigit(c = getch())) s[i++] = c; } s[i] = '\0'; if (c!=EOF) { ungetch(c); } return NUMBER; } #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch(void) { return (bufp > 0) ? buf[bufp--] : getchar(); } void ungetch(int c) { if (bufp<BUFSIZE) { buf[bufp++] = c; } else { printf("ERROR!No extra space can be used in ungetch()!\n"); } }
相关文章推荐
- The C Programming Language 2nd Edition Execise4-2 Answer
- The C Programming Language 练习题2-10
- The C Programming Language 练习题4-2
- [iOS翻译]《The Swift Programming Language》系列:Welcome to Swift-01
- The Swift Programming Language 英文原版官方文档下载
- The C# Programming Language(Third Edition) Part I
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- 《The Swift Programming Language》中文版(全本翻译已完结).html
- The Swift Programming Language--语言附注--类型
- 转:从《The C Programming Language》中学到的那些编程风格和设计思想
- The Swift Programming Language--语言指南--基本运算符
- C程序设计语言(The C Programming Language) 读书笔记
- The C# Programming Language Notes
- The Swift Programming Language (基础部分)
- Generics in the Java Programming Language Java
- 基本语言细节--《The C++ Programming Language 》--(2)类型和声明
- The C# Programming Language Notes II
- [精校版]The Swift Programming Language
- The Swift Programming Language - (1)- 基础部分
- 《The C Programming Language》(2nd Ed) Introduction 翻译