C程序设计(第二版 新版)第四章 习题
来源:互联网 发布:网络词强撩是什么意思 编辑:程序博客网 时间:2024/06/06 03:07
1.strindex(s, t) 返回字符串t在字符串s中首次出现的位置,没有返回-1
strrindex1(s, t) 和 strindex2(s, t)返回字符串t在字符串s中最右边的位置,没有返回-1 (4-1 以及 书本例子)
strrindex1(s, t) 和 strindex2(s, t)返回字符串t在字符串s中最右边的位置,没有返回-1 (4-1 以及 书本例子)
#include<stdio.h>#include<string.h>#define MAXLINE 100int getline(char line[], int max);int strindex(char source[], char searchfor[]);int strrindex1(char source[], char searchfor[]);int strrindex2(char source[], char searchfor[]);char searchfor[] = "yi";int main(){ char line[MAXLINE]; int found = 0; while(getline(line, MAXLINE) > 0) { printf("strindex = %d\n",strindex(line,searchfor)); printf("strrindex1 = %d\n",strrindex1(line,searchfor)); printf("strrindex2 = %d\n",strrindex2(line,searchfor)); } }int getline(char line[], int lim){ int c, i; i = 0; while( --lim > 0 && (c = getchar()) != EOF && c != '\n') { line[i++] = c; } if( c == '\n') line[i++] = c; line[i] = '\0'; return i;}int strindex(char source[], char searchfor[]){ int i, j, k; for(i = 0; source[i] != '\0'; i++) { for(j = i, k = 0; searchfor[k] != '\0' && source[j] == searchfor[k]; j++,k++) ; if( k > 0 && searchfor[k] == '\0') return i; } return -1;}int strrindex1(char source[], char searchfor[]){ int i, j, k, pos = -1; for(i = 0; source[i] != '\0'; ++i) { for(j = i, k = 0; searchfor[k] != '\0' && source[j] == searchfor[k]; ++j,++k) ; if( k > 0 && searchfor[k] == '\0') pos = i; } return pos;}int strrindex2(char source[], char searchfor[]){ int i, j, k; for(i = strlen(source) - strlen(searchfor); i >= 0; --i) { for(j = i, k = 0; searchfor[k] != '\0' && source[j] == searchfor[k]; ++j,++k) ; if( k > 0 && searchfor[k] == '\0') return i; } return -1;}
2. atof(s) 把字符串s转化成浮点数作为返回值(该函数可以处理一般的单精度和带e的浮点数如“12.34”,“-12.34e-2”)
#include<stdio.h>#include<ctype.h>double atof(char s[]){ int i, sign, exp; float val, power; i = 0; while(isspace(s[i])) i++; sign = (s[i] == '-' ? -1 : 1); if(s[i] == '-' || s[i] == '+') i++; val = 0; while(isdigit(s[i])) { val = val * 10 + (s[i] - '0'); i++; } if(s[i] == '.') i++; power = 1.0; while(isdigit(s[i])) { val = val * 10.0 + (s[i] - '0'); i++; power *= 10.0; } val = sign * val / power; if(s[i] == 'e' || s[i] == 'E') i++; sign = (s[i] == '-' ? -1 : 1); if(s[i] == '-' || s[i] == '+') i++; exp = 0; while(isdigit(s[i])) { exp = exp * 10 + (s[i] - '0'); i++; } if( sign == 1) { while( exp-- > 0 ) val = val * 10.0; } else { while( exp-- > 0) val = val / 10.0; } return val;} int main(){ char s[100]=" -12.34e-2"; scanf("%s%*c",s); printf("result = %f\n",atof(s)); getchar(); return 1;}
3. 输入逆波兰式的字符串,求出它的的值(ex > 1 2 - 4 5 + *)(课本p63例题)
#include<stdio.h>#include<stdlib.h>#define MAXOP 100#define NUMBER '0'void push(double s);double pop();int getop(char s[]);int main(){ int type; double op2; char s[MAXOP]; 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) printf("error: zero divisor\n"); else push(pop() / op2); break; case '\n': printf("%8.8f\n", pop()); break; default: printf("error: unknown command! \n"); break; } } getchar(); return 1;} #define MAXVAL 100int sp = 0;double val[MAXVAL];void push(double s){ if(sp >= MAXVAL) printf("error: stack is full\n"); else val[sp++] = s;}double pop(){ if(sp <= 0) { printf("error: stack is empty\n"); return 0; } else return val[--sp];}#include<ctype.h>int getch();void ungetch(int c);int getop(char s[]){ int i, c; while( (c= s[0]= getch()) == ' ' || c == '\t') ; s[1] = '\0'; if(!isdigit(s[0]) && c != '.') return c; i = 0; if(isdigit(c)) while(isdigit(c = s[++i] = getch())) ; if(c == '.') while(isdigit(c = s[++i] = getch())) ; s[i] = '\0'; if( c != EOF) ungetch(c); return NUMBER;}#define BUFSIZE 100int buf[BUFSIZE];int bufp = 0;int getch(){ return (bufp > 0)? buf[--bufp]: getchar();}void ungetch(int c){ if( bufp >= BUFSIZE) printf("error: bufstack is full!\n"); else buf[bufp++] = c;}
4.在课本的基础上加入取模预算,以及考虑负数的情况。
//其中main函数和getop函数的改变:int main(){ int type; double op2; char s[MAXOP]; 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) printf("error: zero divisor\n"); else push(pop() / op2); break; case '%': op2 = pop(); if(op2 == 0.0) printf("error: zero divisor\n"); else push(fmod(pop(),op2)); break; case '\n': printf("%8.8f\n", pop()); break; default: printf("error: unknown command! \n"); break; } } getchar(); return 1;}int getop(char s[]){ int i, c; while( (c= s[0]= getch()) == ' ' || c == '\t') ; s[1] = '\0'; if(!isdigit(s[0]) && c != '.' && c != '-') return c; i = 0; if( c == '-') { if(isdigit(c = getch()) || c == '.') s[++i] = c; else { if( c != EOF) ungetch(c); return '-'; } } if(isdigit(c)) while(isdigit(c = s[++i] = getch())) ; if(c == '.') while(isdigit(c = s[++i] = getch())) ; s[i] = '\0'; if( c != EOF) ungetch(c); return NUMBER;}
5. 增加一些变量,以及结果保存在v中(4-6 可在表达式过程中添加 2 A = 这些赋值语句以及保存上一个表达式的值在v中 如: v 1 +)只需要改动main
int main(){ int type, i, var = 0; double op2, v; char s[MAXOP]; double variable[26]; for( i = 0; i < 26; i++) variable[i] = 0.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) printf("error: zero divisor\n"); else push(pop() / op2); break; case '%': op2 = pop(); if(op2 == 0.0) printf("error: zero divisor\n"); else push(fmod(pop(),op2)); break; case '\n': v = pop(); printf("%8.8f\n", v); break; case '=': pop(); if( var >= 'A' && var <= 'Z') variable[var - 'A'] = pop(); else printf("error: no variable name! \n"); break; default: if(type >= 'A' && type <= 'Z') push(variable[type - 'A']); else if (type == 'v') push(v); else printf("error: unknown command! \n"); break; } var = type; } getchar(); return 1;}
6. 用getline(line,limit)输入一行输入来代替getch() 和 ungetch() 函数
#include<stdio.h>#include<stdlib.h>#define MAXOP 100#define NUMBER '0'int getop(char s[]);void push(double f);double pop();int main(){ int type; double op2; char s[MAXOP]; 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 '\n': printf("\t%.8g\n",pop()); break; default: printf("error: unknown command %s \n",s); break; } } return 0;}#define MAXVAL 100int sp = 0;double val[MAXVAL];void push(double f){ if(sp < MAXVAL) { val[sp++] = f; } else printf("error: stack full, can't push %g\n",f);}double pop(){ if(sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0; }}#include<ctype.h>#define MAXLINE 100int getline(char s[],int limit);char line[MAXLINE];int li = 0;int getop(char s[]){ int i, c; if(line[li] == '\0') { if( getline(line,MAXLINE) == 0) return EOF; else li = 0; } while((s[0] = c = line[li++]) == ' ' || c == '\t') ; s[1] = '\0'; if(!isdigit(s[0]) && c != '.') return c; i = 0; if(isdigit(c)) { while(isdigit(s[++i] = c = line[li++])) ; if(c == '.') while(isdigit(s[++i] = c = line[li++])) ; s[i] = '\0'; li --; return NUMBER; } }int getline(char line[], int limit){ int c; int i = 0; while( --limit > 0 && (c = getchar()) != EOF && c != '\n') line[i++] = c; if( c == '\n') line[i++] = '\n'; line[i] = '\0'; return i;}
- C程序设计(第二版 新版)第四章 习题
- C程序设计(第二版 新版)第二章 习题
- C程序设计(第二版 新版)第六章 习题
- C程序设计(第二版 新版)第七章 习题
- C程序设计(第二版 新版)第三章 习题
- C程序设计(第二版 新版)第八章 习题
- C程序设计(第二版 新版)第五章 习题
- C程序设计(第二版 新版)第一章 习题
- 《C语言程序设计(第二版新版)》第二章习题解答(部分)
- C程序设计(第二版)第六章习题
- C程序设计(第二版)第七章习题
- C程序设计(第二版) 第八章习题
- C程序设计(第二版) 第十章习题
- 《C语言程序设计(第二版新版)》第三章习题解答(部分)
- 谭浩强著《C程序设计(第二版)》习题13.5
- 谭浩强著《C程序设计(第二版)》习题13.6
- 谭浩强 《C程序设计》 第三版 第四章习题答案 (修正)
- IBM-PC汇编语言程序设计(第二版)课后习题答案(清华大学出版社)(沈美明,温冬蝉著)第四章
- atof
- MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中?
- TWAIN学习记录
- 隐藏控件的作用
- RAC环境下配置tnsname实现failover:TAF
- C程序设计(第二版 新版)第四章 习题
- 机器学习与人工智能学习资源导引
- Java笔记—String,StringBuffer ,StringBuilder 的区别
- ios push notification service _简单的服务器客服端解析以及代码。
- 在Windows CE音频驱动里面得到调用者(发声)的应用程序
- synergy——多台电脑共享一套键盘鼠标
- C++虚基类
- 学习网站
- 对于unity3D里的toolkit2D插件正确购买了,有时候会消失或者出现不能使用问题