C程序设计(第二版 新版)第一章 习题
来源:互联网 发布:视频变脸软件叫什么 编辑:程序博客网 时间:2024/05/29 13:58
-2.空格串替换为最少量的制表符和空格,保持单词之间的间隔不变。
#include<stdio.h>#define BASIC 8int main(){freopen("Example.in","r",stdin);int nb,nt,c;int pos;for(pos = 1,nb = nt = 0; (c = getchar()) != EOF; ++pos){if(c == ' '){if(pos % BASIC)++nb;else{nb = 0;++nt;}}else{while(nt){putchar('\t');--nt;}if(c == '\t')nb = 0;while(nb){putchar(' ');--nb;}if(c == '\n')pos = 0;else if(c == '\t')pos = pos -1 + (BASIC - (pos - 1) % BASIC);putchar(c);}}return 0;}
-1.删除每个输入末尾的空格及制表位,并删除完全是空格的行。
#include<stdio.h>#define MAXLINE 1000int getline(char line[], int maxline);void copy(char to[], char from[]);FILE *fp;int main(){fp = fopen("Example.in","r");if(!fp)return 1;int len;char line[MAXLINE];while((len = getline(line,MAXLINE))> 0)if(len >= MAXLINE)printf("%s",line);else if(remove(line,len) > 0)printf("%s",line);fclose(fp);return 0;}int getline(char line[],int lim){int i;int c;for(i = 0; i < lim -1 && (c = fgetc(fp))!= EOF && c != '\n'; ++i)line[i] = c;if(c == '\n')line[i++] = c;line[i] = '\0';return i;}int remove(char line[],int len){int i = len - 2;while(i >= 0 && (line[i] == ' ' || line[i] == '\t'))--i;if(i >= 0){line[++i] = '\n';line[++i] = '\0';}return i;}
0.单词计数(例1.5.4)
#include<stdio.h>#define OUT 1 /* 在单词内 */#define IN 0 /* 在单词外 */int main(){int c,nl,nw,nc,state;state = OUT;nl = nw = nc = 0; // nl:行数 nw:单词数 nc:字符数 while((c = getchar()) != EOF){++nc;if(c == '\n')++nl;if(c == ' ' || c == '\t' || c == '\n')state = OUT;else if(state == OUT){state = IN;++nw;}}printf("%d %d %d\n",nl,nw,nc);return 0;}
1. 打印输入单词长度的水平和数值直方图
#include<stdio.h>#define IN 1#define OUT 0#define MAXHIST 21 /* max length of histogram*/ int main(){ int i,j,nc,maxvalue,state,len; int c; int wl[MAXHIST];/* wl[i] 的值表示长度为i的单词的数量*/ for(i =1; i < MAXHIST; i++) wl[i] = 0; state = OUT; nc = 0; while( (c = getchar()) != EOF) { if( c == ' ' || c == '\n' || c == '\t') { state = OUT; if( nc > 0) wl[nc] ++; nc = 0; } else if( state == OUT) { state = IN; nc ++; } else nc ++; }/*输出水平直方图*/for(i = 1; i < MAXHIST; i++){ if(wl[i] > 0) len = wl[i]; else len = 0; printf("%2d - %2d :", i,len ); while( len-- > 0) putchar('*'); putchar('\n');}putchar('\n');/* 找出单词数量出现最大值*/maxvalue = 0;for( i = 1; i < MAXHIST; i++)if( maxvalue < wl[i])maxvalue = wl[i];/*水平输出直方图*/for( i = maxvalue; i > 0; i--){ for(j = 1; j < MAXHIST; j++ ) if( wl[j] >= i) printf(" * "); else printf(" "); putchar('\n');}for(i = 1; i < MAXHIST; i++)printf("%2d ",i);putchar('\n');for(i = 1; i < MAXHIST; i++) printf("%2d ",wl[i]);putchar('\n');getchar();return 1;}
2.把较长的输入行"拆"成短一些的两行或多行,折行的位置在第n列之前的最后一个非空格之后(注意空格和制表符)(c程序设计语言 1-22题 )
#include<stdio.h>#define TABINC 8#define MAXCOL 10char line[MAXCOL];void printl(int pos){ int i; for(i=0; i < pos; i++) { putchar(line[i]); } if(pos > 0) putchar('\n');}int exptab(int pos){ int i; while(pos < MAXCOL && pos % TABINC != 0) { line[pos]=' '; pos ++; } if( pos >= MAXCOL) { printl(MAXCOL); return 0; } else return pos;}int findblnk(int pos){ int i = pos; while( i >= 0 && line[i] != ' ') { i --; } if( i < 0) return MAXCOL; else return i+1;}int newpos(int pos){ if( pos <= 0 || pos >= MAXCOL) return 0; else { int i; for(i=0; pos < MAXCOL; i++,pos++) line[i] = line[pos]; return i; }}int main(){ int c, pos; pos = 0; while((c = getchar()) != EOF) { line[pos] = c; if( c == '\t') { pos = exptab(pos); } else if( c == '\n') { printl(pos); pos = 0; } else if( ++pos >= MAXCOL) { pos = findblnk(pos - 1); printl(pos); pos = newpos(pos); } } getchar(); return 1;}
3.删除c语言中的所有注释,正确处理引号中的字符串和字符常量。(1-23)
#include<stdio.h>void rcomment(int c);void in_comment(void);void out_comment(void);void echo_quote(int c);int main(){ int c; while((c = getchar()) != EOF) { // printf("recomment-in\n"); rcomment(c); // printf("recomment-out\n"); } getchar(); return 1;}void rcomment(int c){ int d; if( c == '/') { d = getchar(); if(d == '*') { in_comment(); } else if( d == '/') { out_comment(); } else { putchar(c); putchar(d); } } else if( c == '\'' || c == '\"') { echo_quote(c); } else putchar(c);}void in_comment(void){ int c ,d; c = getchar(); d = getchar(); while(c != '*' || d != '/') { c = d; d = getchar(); }}void out_comment(void){ int c; while( (c = getchar()) != '\n'); putchar(c);}void echo_quote(int c){ int d; while( (d = getchar()) != c) { putchar(d); if( d == '\\') putchar(getchar()); } putchar(d);}
4. 查找c语言中的基本语法错误,如圆括号、方括号、花括号不配对等。要正确处理引号(包括单引号和双引号)、转义字符序列与注释(1-24)
#include<stdio.h>int brace,brack,paren;void in_comment();void out_comment();void in_quote();void search(int);int main(){freopen("test.in","r",stdin);int c;while((c = getchar()) != EOF){if(c == '/'){int d = getchar();if(d == '*')in_comment();else if(d == '/')out_comment();elsesearch(d);}else if(c == '\'' || c == '\"')in_quote(c);elsesearch(c);if(brace < 0){puts("Unbalanced braces");brace = 0;}if(brack < 0){puts("Unbalanced bracks");brack = 0;}if(paren < 0){puts("Unbalanced parens");paren = 0;}}if(brace > 0)puts("Unbalanced braces");if(brack > 0)puts("Unbalanced bracks");if(paren > 0)puts("Unbalanced parens");return 0;}void in_comment(){int c = getchar();int d = getchar();while(c != '*' || d != '/'){c = d;d = getchar();}}void out_comment(){int c;while((c = getchar()) != '/n' && c != EOF);}void in_quote(int c){int d;while((d = getchar()) != c){if(d == '\\')getchar();}}void search(int c){switch(c){case '{':++brace;break;case '}':--brace;break;case '[':++brack;break;case ']':--brack;break;case '(':++paren;break;case ')':--paren;break;}}
- C程序设计(第二版 新版)第一章 习题
- C程序设计(第二版 新版)第二章 习题
- C程序设计(第二版 新版)第六章 习题
- C程序设计(第二版 新版)第七章 习题
- C程序设计(第二版 新版)第三章 习题
- C程序设计(第二版 新版)第四章 习题
- C程序设计(第二版 新版)第八章 习题
- C程序设计(第二版 新版)第五章 习题
- 《C语言程序设计(第二版新版)》第一章习题解答(部分)
- C程序设计语言(第二版)习题:第一章
- 《C语言程序设计(第二版新版)》第二章习题解答(部分)
- C程序设计(第二版)第六章习题
- C程序设计(第二版)第七章习题
- C程序设计(第二版) 第八章习题
- C程序设计(第二版) 第十章习题
- 谭浩强著《C程序设计(第二版)》习题13.5
- 谭浩强著《C程序设计(第二版)》习题13.6
- 《C语言程序设计(第二版新版)》第三章习题解答(部分)
- SQlHelper强大用处
- EL表达式应用
- cocos2d-x 贝塞尔曲线的简单运用(CCBezierTo,CCBezierBy)
- vi编辑器没有彩色显示解决
- 个人总结大型高并发高负载网站的系统架构
- C程序设计(第二版 新版)第一章 习题
- win7 shutdown命令设置自动关机、休眠
- Linux ALSA声卡驱动之一:ALSA架构简介
- C#中virtual方法和abstract方法的区别
- LUA 通过例子了解三种循环
- 基于axis2的webservice和android简单的本地数据交互(下
- Xenomai Linux Exercises
- linux上jdk环境变量配置
- 获取管道返回值