1、C程序设计语言 第二版 课后答案第四章Exercise 4-1Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.#include #define MAXLINE 1000int getline(char line, int max);int find_the_situaion(char t,char s);int count(char v);void main() char targetMAXL
2、INE; char lineMAXLINE; while (getline(line, MAXLINE) 0) printf(%d, find_the_situaion(line, target); int getline(char s, int lim) int c, i; i = 0; while (-lim 0 & (c = getchar() != EOF & c != n) si+ = c; if (c = n) si+ = c; si = 0; return i;int find_the_situaion(char t, char s) int i, j, k; int b = c
3、ount(s); int c = count(t); for (i = 0; si != 0; i+) for (j = i, k = 0; tk != 0 & sj = tk; j+,k+) ; if(k=b+1) return c - i - b; return -1;int count(char v) int a = 0; if (va!=0) a+; return a;Exercise 4-2Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may
4、be followed by e or E and an optionally signed exponent.Exercise 4-3Given the basic framework, its straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.#include #include pch.h#include #define MAXOP 100#define NUMBER 0 #define TRUE 1;int Getop(c
5、har);int getch(void);double pop(void);void push(double f);void ungetch(int c);int main(void) int type; double op2; char sMAXOP; int flag = TRUE; while (type = Getop(s) != EOF) switch (type) case %: op2 = pop(); if (op2) push(fmod(pop(), op2); else printf(nError: Division by zero!); break; return EXI
6、T_SUCCESS;int Getop(char s)#define PERIOD . int i = 0; int c; int next; while (s0 = c = getch() = | c = t) ; s1 = 0; if (!isdigit(c) & c != PERIOD & c != -) return c; if (c = -) next = getch(); if (!isdigit(next) & next != PERIOD) return c; c = next; else c = getch(); while (isdigit(s+i = c) c = get
7、ch(); if (c = PERIOD) while (isdigit(s+i = c = getch() ; si = 0; if (c != EOF) ungetch(c); return NUMBER;#define MAXVAL 100int sp = 0; double valMAXVAL;double pop(void) if (sp 0)return val-sp; else printf(error: stack emptyn); return 0.0; void push(double f) if (sp 0) ? buf-bufp : getchar(); void un
8、getch(int c) if (bufp = BUFSIZE) printf(ungetch: too many charactersn); else bufbufp+ = c; Exercise 4-4Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack. #include pch.h#include#include#include#inclu
9、de#define MAXOP 100#define NUMBER 0#define TRUE 1#define FALSE 0int Getop(char s);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();int main(void) int type; double op2; char sMAXOP; int flag = TRUE; while (type = Getop(s) != EOF) sw
10、itch (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) push(pop() / op2); else printf(n出现错误!); break; case %: op2 = pop(); if (op2) push(fmod(pop(), op2); else pr
11、intf(n出现错误!); break; case ?: showTop(); break; case #: duplicate(); break; case : swapItems(); break; case !: clearStack(); case n: printf(nt%.8gn, pop(); break; default: printf(nError: 不清楚的指令 %s.n, s); break; return EXIT_SUCCESS;#define MAXVAL 100int sp = 0; double valMAXVAL; void push(double f) if
12、 (sp 0) return val-sp; else printf(n出现错误n); return 0.0; void showTop(void) if (sp 0) printf(Top of stack contains: %8gn, valsp - 1); else printf(The stack is empty!n);void duplicate(void) double temp = pop(); push(temp); push(temp);void swapItems(void) double item1 = pop(); double item2 = pop(); pus
13、h(item1); push(item2);void clearStack(void) sp = 0;int getch(void);void unGetch(int);int Getop(char s) int i = 0; int c; int next; while (s0 = c = getch() = | c = t) ; s1 = 0; if (!isdigit(c) & c != . & c != -) return c; if (c = -) next = getch(); if (!isdigit(next) & next != .) return c; c = next;
14、else c = getch(); while (isdigit(s+i = c) c = getch(); if (c = .) while (isdigit(s+i = c = getch() ; si = 0; if (c != EOF) unGetch(c); return NUMBER;#define BUFSIZE 100char bufBUFSIZE;int bufp = 0;int getch(void) return (bufp 0) ? buf-bufp : getchar();void unGetch(int c) if (bufp = BUFSIZE) printf(n
15、出现错误n); else bufbufp+ = c;Exercise 4-5Add access to library functions like sin , exp , and pow . See in Appendix B, Section 4.#include#include#include#include#include #define MAXOP 100#define NUMBER 0#define IDENTIFIER 1#define TRUE 1#define FALSE 0int Getop(char s);void push(double val);double pop(
16、void);void showTop(void);void duplicate(void);void swapItems(void);void clearStack();void dealWithName(char s);int main(void) int type; double op2; char sMAXOP; int flag = TRUE; while (type = Getop(s) != EOF) switch (type) case NUMBER: push(atof(s); break; case IDENTIFIER: dealWithName(s); break; ca
17、se +: push(pop() + pop(); break; case *: push(pop() * pop(); break; case -: op2 = pop(); push(pop() - op2); break; case /: op2 = pop(); if (op2) push(pop() / op2); else printf(nError: division by zero!); break; case %: op2 = pop(); if (op2) push(fmod(pop(), op2); else printf(nError: division by zero
18、!); break; case ?: showTop(); break; case #: duplicate(); break; case : swapItems(); break; case !: clearStack(); case n: printf(nt%.8gn, pop(); break; default: printf(nError: unknown command %s.n, s); break; return EXIT_SUCCESS;#define MAXVAL 100int sp = 0; double valMAXVAL; void push(double f) if
19、(sp 0) return val-sp; else printf(nError: stack emptyn); return 0.0; void showTop(void) if (sp 0) printf(Top of stack contains: %8gn, valsp - 1); else printf(The stack is empty!n);void duplicate(void) double temp = pop(); push(temp); push(temp);void swapItems(void) double item1 = pop(); double item2
20、 = pop(); push(item1); push(item2);void clearStack(void) sp = 0;void dealWithName(char s) double op2; if (0 = strcmp(s, sin) push(sin(pop(); else if (0 = strcmp(s, cos) push(cos(pop(); else if (0 = strcmp(s, exp) push(exp(pop(); else if (!strcmp(s, pow) op2 = pop(); push(pow(pop(), op2); else printf
21、(%s is not a supported function.n, s);int getch(void);void unGetch(int);int Getop(char s) int i = 0; int c; int next; while (s0 = c = getch() = | c = t) ; s1 = 0; if (isalpha(c) i = 0; while (isalpha(si+ = c) c = getch(); si - 1 = 0; if (c != EOF) unGetch(c); return IDENTIFIER; if (!isdigit(c) & c !
22、= . & c != -) return c; if (c = -) next = getch(); if (!isdigit(next) & next != .) return c; c = next; else c = getch(); while (isdigit(s+i = c) c = getch(); if (c = .) while (isdigit(s+i = c = getch() ; si = 0; if (c != EOF) unGetch(c); return NUMBER;#define BUFSIZE 100char bufBUFSIZE;int bufp = 0;
23、int getch(void) return (bufp 0) ? buf-bufp : getchar();void unGetch(int c) if (bufp = BUFSIZE) printf(nUnGetch: too many charactersn); else bufbufp+ = c;Exercise 4-6Add commands for handling variables. (Its easy to provide twenty-six variables with single-letter names.) Add a variable for the most r
24、ecently printed value. #include #include #include #include #include #define MAXOP 100#define NUMBER 0/* 4-6 these are new for this exercise*/#define IDENTIFIER 1#define ENDSTRING 2/* 4-6 end of new stuff */#define TRUE 1#define FALSE 0#define MAX_ID_LEN 32#define MAXVARS 30struct varType char nameMAX_ID_LEN; double val;int Getop(char s);void push(double val);double pop(void);void showTop(void);void duplicate(void);void swapItems(void);void clearStacks(struct varType var);void dealWithName(char s, struct varType var);void dealWithVar(char s, str
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1