ImageVerifierCode 换一换
格式:DOCX , 页数:34 ,大小:22.72KB ,
资源ID:4936259      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4936259.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C程序设计语言 第二版 课后答案第四章.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C程序设计语言 第二版 课后答案第四章.docx

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