• 393.50 KB
  • 2022-04-22 11:51:32 发布

《C程序设计语言》读书笔记及课后答案.doc

  • 61页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'通过一周多的学习,大体看完了《C程序设计语言》这本书,这是我的笔记。(一)读这本书的目标是什么?(1)、读完后必须深入了解C的语法以及内涵,并且达到熟练应用。(2)、通过练习习惯一种编程风格,深入理解指针,数组,结构体以及内存分配。(3)、通过练习锻炼逻辑思维能力(4)、学完后编程要上一个层次,自己能够编写出有用的C代码。(二)这本书哪个部分是我要重点看的?(1)、指针、数组、结构体(2)、内存分配(3)、输入输出及接口问题(三)读这本书我有什么收获?3、1对于本书的感受这是一本经典的C语言书籍,与其他语法书不同的是所有语法都是用例题来讲的,通过例子理解并练习语法,另外这本书的习题比较难做,不是简单的考语法,而是一些有意义的习题。通过做练习发现还是有所收获的。本书中与Linux联系密切,用C重写了许多简化版的Shell命令如:grep,ls,cat,sort,tail,cp等等。3、2收获:因为本来就有C语言的基础知识,所以对于语法问题基本没有多学新的知识,但是仍然对以下几点有了新的了解:1)、位字段,不管是通过直接设置位字段还是用以前的位计算,都有新的了解与认识。2)、指针。建立了“指针为大”的思想,对于应用指针进行类型转换以及运算都有了新的认识。比如:inta=5,char*p,p=(char)&a,*p=‘c’;建立这样的过程对于更加复杂的指针转换就理解了。3)、结构体。通过第六章对于二叉树以及哈希表的应用深入了解结构体,并且通过调试程序观察递归过程,同时对递归有了一定的理解。4)、内存分配malloc函数,通过第八章的学习对malloc函数的内部进行分析,理解了动态内存管理机制。3.3学习的程度以及那些地方存在不足:对于C的基本语法已经差不多了,但是否达到了当初的目标即在编程方面上一个层次,这我并不敢确定,因为到现在为止这本书上的习题有些我都没有搞定,又看了一下以前的有关C的东西是觉得已经好了许多了,最起码对于很麻烦的程序不再感到畏惧了。另外觉得自己对于某些问题还是没有理解透彻,比如说输入输出时的缓冲机制,比如说指针与结构体的灵活运用,比如说如何能够运用模块化的思想把一个大的问题逐步细分,通过一个一个的小模块(函数)逐步解决,这些有的是与逻辑思维有关,有些是与某方面的专业知识有关,有些是单纯与C有关,比如说有关缓冲与内存的知识要了解操作系统,另外编译原理也要知道,这些我以前都没有学过,以后会找机会补一补。当然随着进一步的学习与应用,会逐渐的熟练或理解某一些知识点。另外因为时间的原因,对于许多练习没有真正的从各个方面去思考,只是做出来就完了,也没有返回去再仔细考虑每一道习题的意义以及对于我的收获。所以还有待以后有时间在对某些知识点进行学习。习题是本书的重点,也用了不少时间,其中有些是通过对章节内容的学习后自己做出来的(S),有些是参考别人的,参考的原因主要有:(A)对题目不理解,不明白具体要做到什么程度。 (B)自己写了但是不知道对不对参考后发现自己的程序有问题或是不如别人的好又修改的。(C)虽知道题意但自己没有思路或是思路不清晰。在每一道习题的前面都用(S)(A)(B)(C)来表明是属于那一种。另外因为时间问题程序大都没有注释,只是在开始的时候稍加说明并插入了日期。这里整理一下这几天做的练习,这些代码除了少数几个标注的外都是在Linux上调试成功的,当然虽然调试成功了但是有些程序并不完美,许多情况没有考虑,简化了代码但都能达到习题的要求。另外,有几个没有整理出来,原因如下:(1)代码段太小(2)到现在为止没有能够解决。属于后种情况的我在每一章最后都加以说明。第八章:Test8-1.c(S)(cat程序)用read,write,open,close系统调用代替标准库中功能等价的函数,重写第七章的cat程序,并比较相对执行速度。1./*thecatfunction2.WedAug1118:34:43CST2010*/3.#include"stdio.h"4.#include"stdlib.h"5.#include"fcntl.h"6.voidmain(intargc,char*argv[])7.{8.intfd;9.voidfilecopy(int,int);10.char*prog=argv[0];11.if(argc==1)12.filecopy(0,1);13.else14.while(--argc>0)15.{16.if((fd=open(*++argv,O_RDONLY,"r"))==-1)17.{18.fprintf(stderr,"%scan"topen%sn",prog,*argv);19.exit(1);20.}21.else22.{23.filecopy(fd,1);24.close(fd);25.}26.}27.exit(0);28.}29.voidfilecopy(intfd1,intfd2)30.{31.intn;32.staticcharbuf[BUFSIZ];33.while((n=read(fd1,buf,BUFSIZ))>0)34.if(write(1,buf,n)!=n)35.fprintf(stderr,"writeerror");36.}Test8-3.c(?)//本程序未调试通过设计编写fflush、_flush、fclose函数1./*thefopenfunction2.WedAug1119:23:51CST2010*/ 1.//#include"stdio.h"2.#include"fcntl.h"3.#include"mystdio2.h"4.#include"sys/file.h"5.#include"unistd.h"6.#include"stdlib.h"7.#definePERMS06668.FILE_iob[OPEN_MAX]={9.{0,(char*)0,(char*)0,_READ,0},10.{0,(char*)0,(char*)0,_WRITE,1},11.{0,(char*)0,(char*)0,_WRITE|_UNBUF,2}12.};13.FILE*fopen(char*name,char*mode)14.{15.FILE*fp;16.intfd;17.if(*mode!="r"||*mode!="w"||*mode!="a")18.returnNULL;19.for(fp=_iob;fp<_iob+OPEN_MAX;fp++)20.{21.if((fp->flag&(_READ|_WRITE))==0)22.break;23.}24.if(fp>=_iob+OPEN_MAX)25.returnNULL;26.if(*mode=="w")27.fd=creat(name,PERMS);28.elseif(*mode=="a")29.{30.if((fd=open(name,O_WRONLY,0))==-1)31.fd=creat(name,PERMS);32.lseek(fd,0L,2);33.}34.else35.fd=open(name,O_RDONLY,0);36.if(fd==-1)37.returnNULL;38.fp->fd=fd;39.fp->ptr=NULL;40.fp->base=NULL;41.fp->cnt=0;42.fp->flag=(*mode=="w")?_WRITE:_READ;43.returnfp;44.}45.int_fillbuf(FILE*fp)46.{47.intbufsize;48.if((fp->flag&(_READ|_EOF|_ERR))!=_READ)49.returnEOF;50.bufsize=(fp->flag&_UNBUF)?1:BUFSIZ;51.if(fp->base==NULL)52.{53.if((fp->base=(char*)malloc(bufsize))==NULL)54.returnEOF;55.}56.fp->ptr=fp->base;57.fp->cnt=read(fp->fd,fp->base,bufsize);58.if(--fp->cnt<0)59.{60.if(fp->cnt=-1)61.fp->flag|=_EOF;62.else63.fp->flag|=_ERR;64.fp->cnt=0;65.returnEOF;66.}67.return(unsignedchar)*fp->ptr++;68.}69.int_flushbuf(intx,FILE*fp)70.{71.unsignednc;72.intbufsize;73.if(fp<_iob||fp>_iob+OPEN_MAX)74.returnEOF; 1.if((fp->flag&(_WRITE|_EOF|_ERR))!=_WRITE)2.returnEOF;3.bufsize=(fp->flag&_UNBUF)?1:BUFSIZ;4.if(fp->base==NULL)5.{6.if((fp->base=(char*)malloc(BUFSIZ))==NULL)7.fp->flag|=_ERR;8.returnEOF;9.}10.else11.{12.nc=fp->ptr-fp->base;13.if(write(fp->fd,fp->base,nc)!=nc)14.fp->flag|=_ERR;15.returnEOF;16.}17.fp->ptr=fp->base;18.*fp->ptr++=(char)x;19.fp->cnt=bufsize-1;20.returnx;21.}22.intfflush(FILE*fp)23.{24.intrc=0;25.if(fp<_iob||fp>_iob+OPEN_MAX)26.returnEOF;27.if(fp->flag&_WRITE)28.rc=_flushbuf(0,fp);29.//fp->ptr=fp->base;30.fp->cnt=(fp->flag&_UNBUF)?1:BUFSIZ;31.returnrc;32.}33.intfclose(FILE*fp)34.{35.intrc;36.if((rc=fflush(fp))!=EOF)37.{38.free((void*)fp->base);39.fp->cnt=0;40.fp->ptr=NULL;41.fp->base=NULL;42.fp->flag&=~(_READ|_WRITE);43.}44.returnrc;45.}46.main()47.{48.unsignedcharc;49.FILE*fp;50.fp=fopen("mystdio2.h","r");51.getc(fp);52.putc(c,fp);53.}54./mystdio2.h55.#defineNULL056.#defineEOF(-1)57.#defineOPEN_MAX2058.#defineBUFSIZ102459.typedefstruct_iobuf{60.intcnt;61.char*ptr;62.char*base;63.intflag;64.intfd;65.}FILE;66.externFILE_ioba[OPEN_MAX];67.#definestdin(&_iob[0])68.#definestdout(&_iob[1])69.#definestderr(&_iob[2])70.enum_flags{71._READ=01,72._WRITE=02,73._UNBUF=04,74._EOF=010,75._ERR=02076.}; 1.int_fillbuf(FILE*);2.int_flushbuf(int,FILE*);3.//FILE*fopen(char*,char*);4.intfclose(FILE*);5.intfflush(FILE*);6.#definefeof(p)(((p)->flag&_EOF)!=0)7.#definegetc(p)(--(p)->cnt>=08.?(unsignedchar)*(p)->ptr++:_fillbuf(p))??宏定义但这不是个函数,如何应用,用一个变量接受他的结果呢?9.#defineputc(x,p)(--(p)->cnt>=010.?*(p)->ptr++=(x):_flushbuf((x),p))11.#definegetchar()getc(stdin)12.#defineputchar(x)putc((x),stdout)这段代码是原书上的内容,我添加了练习中的_flushfflush以及fclose函数,程序的从思路上走没什么问题,但是因为重写了stdio.h所以编译老是出错,而且对于我标出的哪一点我还是有些疑问的。Test8-4.c(?)//有疑问的程序(fseek程序)设计编写fseek(FILE*fp,longoffset,intorigin)函数1.#include"stdio.h"2.#include"stdlib.h"3.intfseek(FILE*fp,longoffset,intorigin)4.{5.unsignednc;6.longrc=0;7.if(fp->flag&_READ)8.{9.if(origin==1)10.offset-=fp->cnt;??为什么要这样做呢?11.rc=lseek(fp->fd,offset,origin);12.fp->cnt=0;13.}14.elseif(fp->flag&_WRITE)15.{16.if((nc=fp->ptr-fp->base)>0)17.if(write(fp->fd,fp->base,nc)!=nc)18.rc=-1;19.if(rc!=-1)20.rc=lseek(fp->fd,offset,origin);21.}22.return(rc==-1)?-1:0;23.}这是本题的一个答案,但是我并不怎么明白,Fseek的实现是调用了系统函数lseek但是lseek除了在第一个参数不同外难道offset这个参数也是不同的吗?书上并没有深入讲解lseek函数,所以对于这个问题我还是不太明白,我在网上了解了一些信息,并做了个跟踪fseek的程序,但是结果还是没有弄明白,假设fseek中所谓的移动的那个指针是系统管理的,但是那个FILE结构中的用于缓冲的指针让我对他的运算移动不了解,以下是跟踪程序:1.#include"stdio.h"2.#include"stdlib.h"3.#defineBUF_LEN34.Printbuf(char*buf,intlen)5.{6.inti;7.for(i=0;i_IO_read_ptr,ftell(fp));7.for(i=0;i<2;++i)8.{9.fgetc(fp);10.Printbuf(mybuf,BUF_LEN);11.printf("curp=%ptpos=%ldn",fp->_IO_read_ptr,ftell(fp));12.}13.fseek(fp,5L,1);14.Printbuf(mybuf,BUF_LEN);15.printf("curp=%ptpos=%ldn",fp->_IO_read_ptr,ftell(fp));16.fgetc(fp);17.Printbuf(mybuf,BUF_LEN);18.printf("curp=%ptpos=%ldn",fp->_IO_read_ptr,ftell(fp));19./*fgetc(fp);20.Printbuf(mybuf,BUF_LEN);21.printf("curp=%ptpos=%ldn",fp->_IO_read_ptr,ftell(fp));22.fgetc(fp);23.Printbuf(mybuf,BUF_LEN);24.printf("curp=%ptpos=%ldn",fp->_IO_read_ptr,ftell(fp));25.fgetc(fp);26.Printbuf(mybuf,BUF_LEN);27.printf("curp=%ptpos=%ldn",fp->_IO_read_ptr,ftell(fp));*/28.fclose(fp);29.//getchar();30.}这是当fseek(fp,2L,1)的时候:mybuf=0xbff05c15curp=0xbff05c15pos=0ABCcurp=0xbff05c16pos=1ABCcurp=0xbff05c17pos=2ABCcurp=0xbff05c15pos=4EFGcurp=0xbff05c16pos=5curp就是FILE结构体中的缓冲区指针,当执行fseek的时候他是怎样移动的?这是当fseek(fp,1L,1)的时候mybuf=0xbfcefe15curp=0xbfcefe15pos=0ABCcurp=0xbfcefe16pos=1ABCcurp=0xbfcefe17pos=2BCDcurp=0xbfcefe17pos=3BCDcurp=0xbfcefe18pos=4 这是当fseek(fp,5L,1)的时候mybuf=0xbfee6ce5curp=0xbfee6ce5pos=0ABCcurp=0xbfee6ce6pos=1ABCcurp=0xbfee6ce7pos=2FGHcurp=0xbfee6ce7pos=7FGHcurp=0xbfee6ce8pos=8这是fseek(fp,-3L,1)并且当for(i=0;i<4;++i)的时候mybuf=0xbfb199d5curp=0xbfb199d5pos=0ABCcurp=0xbfb199d6pos=1ABCcurp=0xbfb199d7pos=2ABCcurp=0xbfb199d8pos=3DEFcurp=0xbfb199d6pos=4DEFcurp=0xbfb199d5pos=3DEFcurp=0xbfb199d6pos=4Test8-5.c(?)(ls程序)1./*2.WedAug1106:24:34CST2010*/3.#include4.#include5.#include6.//#include"syscalls.h"7.#include8.#include9.#include"sys/dir.h"10.#include"stdlib.h"11.//#defineNAME_MAX1412.#defineMAX_PATH102413.#defineDIRSIZ1414.typedefstruct{15.longino;16.charname[NAME_MAX+1];17.}Dirent;18.typedefstruct{19.intfd;20.Direntd;21.}MYDIR;??不用MYDIR结构不行吗?用到fd的时候直接设个变量代替不行吗?22.structmydirect{23.ino_td_ino;24.chard_name[DIRSIZ];25.};26.MYDIR*myopendir(char*dirname);27.Dirent*myreaddir(MYDIR*dfd);28.voidmyclosedir(MYDIR*dfd);29.//intstat(char*,structstat*); 1.voiddirwalk(char*,void(*fcn)(char*));2.voidfsize(char*);3.voidmain(intargc,char*argv[])4.{5.if(argc==1)6.fsize(".");7.else8.while(--argc>0)9.fsize(*++argv);10.}11.voidfsize(char*name)12.{13.structstatstbuf;14.if(stat(name,&stbuf)==-1)15.{16.fprintf(stderr,"fsize:can"taccess%sn",name);17.return;18.}19.if((stbuf.st_mode&S_IFMT)==S_IFDIR)20.dirwalk(name,fsize);21.printf("%5u%6o%8ld%sn",(unsigned)stbuf.st_ino,stbuf.st_mode,stbuf.st_size,name);22.}23.voiddirwalk(char*dir,void(*fcn)(char*))24.{25.charname[MAX_PATH];26.Dirent*dp;27.MYDIR*dfd;28.if((dfd=myopendir(dir))==NULL)29.{30.fprintf(stderr,"dirwalk:can"topen%sn",dir);31.return;32.}33.while((dp=myreaddir(dfd))!=NULL)34.{35.if(strcmp(dp->name,".")==0||strcmp(dp->name,"..")==0)36.continue;37.if(strlen(dir)+strlen(dp->name)+2>sizeof(name))38.fprintf(stderr,"dirwalk:name%s%stoolongn",dir,dp->name);39.else40.{41.sprintf(name,"%s/%s",dir,dp->name);42.(*fcn)(name);43.}44.}45.myclosedir(dfd);46.}47.intfstat(intfd,structstat*);48.MYDIR*myopendir(char*dirname)49.{50.intfd;51.structstatstbuf;52.MYDIR*dp;53.if((fd=open(dirname,O_RDONLY,0))==-154.||fstat(fd,&stbuf)==-155.||(stbuf.st_mode&S_IFMT)!=S_IFDIR56.||(dp=(MYDIR*)malloc(sizeof(MYDIR)))==NULL)57.returnNULL;58.dp->fd=fd;59.returndp;60.}61.voidmyclosedir(MYDIR*dp)62.{63.if(dp)64.{65.close(dp->fd);66.free((void*)dp);67.} 1.}2.Dirent*myreaddir(MYDIR*dp)3.{4.structmydirectdirbuf;??为什么要借用这个结构?5.staticDirentd;??直接用Dirent不行吗?6.while(read(dp->fd,(char*)&dirbuf,sizeof(dirbuf))==sizeof(dirbuf))??出错,直接返回了NULL,7.{//读会有什么问题?8.if(dirbuf.d_ino==0)9.continue;10.d.ino=dirbuf.d_ino;11.strncpy(d.name,dirbuf.d_name,DIRSIZ);12.d.name[DIRSIZ]="";13.return&d;14.}15.returnNULL;16.}本题的代码仍然是原书上的代码,是一个Linux中经典命令ls的简化版本,习题只是让修改一点,但是这段代码一直在机器上实现不了功能,我跟踪了一下过程发现总是在我标出的地方直接返回NULL了,但是程序在逻辑上我觉得没什么错误呀,还有为什么要设计MYDIR结构呢,我怎么觉得只要一个Dirent结构就够了呢?具体用到文件描述符时定义一个int型的不就行了吗?要让opendir,readdir等用MYDIR结构操作我怎么感到多此一举呢?还有在readdir函数中借用了系统中的structdirect结构这又是为什么呢?直接用一个Dirent结构难道不行吗?这也就不用来回在转换再赋值了不是吗?Test8-6(S)Test8-7(C)Test8-8(S)(malloc、calloc、free程序)1./*themallocfunction:2.WedAug1103:29:19CST2010*/3.#include"stdio.h"4.#include"string.h"5.#defineNALLOC10246.#defineMAXNBYTES102407.typedeflongAlign;8.unionheader9.{10.struct11.{12.unionheader*next;13.unsignedsize;14.}s;15.Alignx;16.};17.typedefunionheaderHeader;18.staticunsignedmaxalloc;19.staticHeaderbase;20.staticHeader*freeptr=NULL;21.void*malloc(unsignednbytes)22.{23.Header*ptr;24.Header*prevptr;25.Header*morecore(unsigned);26.unsignednunits;27.if(nbytes>MAXNBYTES)28.{29.fprintf(stderr,"can"tallocmorethan%ubytesn",MAXNBYTES);30.returnNULL;31.}32.nunits=(nbytes+sizeof(Header)-1)/(sizeof(Header))+1;33.printf("thenunitsis%dn",nunits);34.if((prevptr=freeptr)==NULL)35.{36.base.s.next=prevptr=freeptr=&base; 1.base.s.size=0;2.}3.for(ptr=prevptr->s.next;;prevptr=ptr,ptr=ptr->s.next)4.{5.if(ptr->s.size>=nunits)6.{7.if(ptr->s.size==nunits)8.prevptr->s.next=ptr->s.next;9.else10.{11.ptr->s.size-=nunits;12.ptr=ptr+ptr->s.size;13.ptr->s.size=nunits;14.}15.freeptr=prevptr;16.return(void*)(ptr+1);17.}18.if(ptr==freeptr)19.{20.if((ptr=morecore(nunits))==NULL)21.returnNULL;22.}23.}24.}25.Header*morecore(unsignednu)26.{27.char*cp,*sbrk(int);28.Header*hp;29.voidfree(void*);30.if(nus.size=nu;37.maxalloc=(hp->s.size>maxalloc)?nu:maxalloc;38.free((void*)(hp+1));39.returnfreeptr;40.}41.voidfree(void*ap)42.{43.Header*bp;44.Header*p;45.bp=(Header*)ap-1;46.if(bp->s.size==0||bp->s.size>maxalloc)47.{48.fprintf(stderr,"free,can"tfree%uunitsn",bp->s.size);49.return;50.}51.for(p=freeptr;bp>p&&bps.next;p=p->s.next)52.{53.if(p>p->s.next&&(bp>p||bps.next))54.break;55.}56.if(bp+bp->s.size==p->s.next)57.{58.bp->s.size+=p->s.next->s.size;59.bp->s.next=p->s.next->s.next;60.}61.else62.bp->s.next=p->s.next;63.//p->s.next=bp;64.if(p+p->s.size==bp)65.{66.p->s.size+=bp->s.size;67.p->s.next=bp->s.next;68.}69.else70.p->s.next=bp;71.freeptr=p;72.}73.unsignedbfree(char*p,unsignedn)74.{ 1.Header*hp;2.hp=(Header*)p;3.if(ns.size=(n+sizeof(Header)-1)/sizeof(Header);6.free((void*)(hp+1));7.returnhp->s.size;8.}9.void*calloc(unsignedn,unsignedsize)10.{11.unsignednb;12.char*p,*q;13.inti;14.nb=n*size;15.if((p=q=(char*)malloc(nb))!=NULL)16.for(i=0;i0)20.returnstack[--sp];21.else22.{23.printf("error:stackempty");24.return0.0;25.}26.}27.intgetop(char*s)28.{29.charc;30.intrc;31.floatf;32.while((rc=scanf("%c",&c))!=EOF)33.if((s[0]=c)!=""&&c!="t")34.break;35.s[1]="";36.if(rc==EOF)37.returnEOF;38.elseif(!isdigit(c)&&c!=".")39.returnc;40.ungetc(c,stdin);41.scanf("%f",&f);42.sprintf(s,"%f",f);43.returnNUMBER;44.}45.#defineMAXOP10046.voidmain()47.{48.inttype,i;49.doubleop;50.chars[MAXOP];51.//doublevariable[26];52.//for(i=0;i<26;i++)53.//variable[i]=0.0;54.while((type=getop(s))!=EOF)55.{56.switch(type)57.{58.caseNUMBER:59.push(atof(s));60.break;61./*case1:62.hnumber(s);63.break;*/64.case"+":65.push(pop()+pop());66.break;67.case"-":68.op=pop();69.push(pop()-op);70.break;71.case"*":72.push(pop()*pop());73.break; 1.case"/":2.op=pop();3.if(op!=0)4.push(pop()/op);5.else6.printf("error:theoperateriswrongn");7.break;8.case"%":9.op=pop();10.if(op!=0.0)11.push((int)pop()%(int)op);12.else13.printf("error:wrongoperatern");14.break;15.case"n":16.printf("t%.8gn",pop());17.break;18.default:19.//if(type>="A"&&type<="Z")20.//push(variable[type-"A"]);21.//else22.printf("error:Unkowncommandn");23.break;24.}25.//var=type;26.}27.}Test7-6.c(S)1./*2.TueAug1001:09:30CST2010*/3.#include"stdio.h"4.#include"stdlib.h"5.#include"string.h"6.#defineMAXLEN10007.voidmain(intargc,char*argv[])8.{9.FILE*fp1,*fp2;10.charline1[MAXLEN],line2[MAXLEN];11.if((fp1=fopen(argv[1],"r"))==NULL)12.{13.fprintf(stderr,"%scan"topenn",argv[1]);14.exit(1);15.}16.if((fp2=fopen(argv[2],"r"))==NULL)17.{18.fprintf(stderr,"%scan"topenn",argv[2]);19.exit(1);20.}21.while(fgets(line1,MAXLEN,fp1)!=NULL)22.if(fgets(line2,MAXLEN,fp2)!=NULL)23.{24.if(strcmp(line1,line2)!=0) 1.{2.printf("%sn%sn",line1,line2);3.break;4.}5.}6.fclose(fp1);7.fclose(fp2);8.}Test7-7.c(B)(grep程序)1./*2.TueAug1002:35:29CST2010*/3.#include"stdio.h"4.#include"string.h"5.#include"stdlib.h"6.#defineMAXLEN1007.#defineMAXLINE10008.voidfmatch(FILE*fp,char*line,char*pattern,intnumber,intexcept);9.voidmain(intargc,char*argv[])10.{11.//charline[MAXLINE];12.//longlineno=0;13.intc,except=0,number=0;14.charpattern[MAXLEN];15.FILE*fp;16.while(--argc>0&&(*++argv)[0]=="-")17.while(c=*++argv[0])18.switch(c)19.{20.case"n":21.number=1;22.break;23.case"x":24.except=1;25.break;26.default:27.printf("find:illegaloption!");28.argc=0;29.break;30.}31.if(argc>=1)32.strcpy(pattern,*argv);33.else34.printf("usage:find-x-npatternfilenamen");35.if(argc==1)36.fmatch(stdin,"",pattern,number,except);37.else38.while(--argc>0)39.if((fp=fopen(*++argv,"r"))==NULL)40.{41.fprintf(stderr,"%scan"topenn",*argv);42.exit(1);43.}44.else45.{46.fmatch(fp,*argv,pattern,number,except);47.fclose(fp);48.}49.exit(0);50.}51.voidfmatch(FILE*fp,char*name,char*pattern,intnumber,intexcept)52.{53.charline[MAXLINE];54.longlineno=0;55.while(fgets(line,MAXLINE,fp)!=NULL) 1.{lineno++;2.if((strstr(line,pattern)!=NULL)!=except)3.{4.if(*name)5.printf("%s:",name);6.if(number)7.printf("t%ld:",lineno);8.printf("%sn",line);9.}10.}11.}Test7-8.c(C)(分页打印程序)1./*2.TueAug1003:35:19CST2010*/3.#include"stdio.h"4.#include"stdlib.h"5.#defineMAXHEAD56.#defineMAXTAIL37.#defineMAXPAGE668.#defineMAXLINE1009.voidfprint(FILE*fp,char*name);10.inthead(char*name,intpageno);11.voidmain(intargc,char*argv[])12.{13.FILE*fp;14.if(argc==1)15.fprint(stdin,"");16.else17.{18.while(--argc>0)19.{20.if((fp=fopen(*++argv,"r"))==NULL)21.{22.fprintf(stderr,"%scan"topenn",*argv);23.exit(1);24.}25.else26.{27.fprint(fp,*argv);28.fclose(fp);29.}30.}31.}32.exit(0);33.}34.voidfprint(FILE*fp,char*name)35.{36.intlineno,pageno=1;37.charlines[MAXLINE];38.lineno=head(name,pageno);39.while(fgets(lines,MAXLINE,fp)!=NULL)40.{41.if(lineno==1)42.{43.fprintf(stdout,"f");44.lineno=head(name,++pageno);45.}46.fputs(lines,stdout);47.lineno++;48.if(lineno>MAXPAGE-MAXTAIL)49.lineno=1;50.}51.printf("f");52.}53.inthead(char*name,intpageno)54.{55.intnl=3;56.printf("nn");57.printf("%st%d",name,pageno);58.while(nl++word=mystrdup(w);//这儿一开始我觉得为什么不直接用w付给p->word呢,而要用一个复制函数呢?后来有些明白,因为p->word没有分配空间,而且每一次调用p->word应该指向不同的位置,不知这样理解对不对48.p->lines=lalloc();49.p->lines->lnum=nlines;p->lines->next=NULL;50.p->left=p->right=NULL;51.}52.elseif((conp=strcmp(p->word,w))==0)53.addin(p,nlines);54.elseif(conp>0)55.p->left=addtree(p->left,w,nlines);56.else57.p->right=addtree(p->right,w,nlines);58.returnp;59.}60.voidaddin(structtnode*p,intnlines)61.{62./*structtnode*temp;63.temp->lines=p->lines; 1.while(temp->lines->next!=NULL&&temp->lines->lnum!=nlines)2.temp->lines=temp->lines->next;3.if(temp->lines->lnum!=nlines)4.{5.temp->lines->next=lalloc();6.temp->lines->next->lnum=nlines;7.temp->lines->next->next=NULL;8.}*///本段为刚开始的时候自己写的,只是没有设置structnlist变量直接用了structtnode变量来操作,就是麻烦一点,感觉逻辑上是对的,但是为什么运行时这个地方使得结果不正确了呢?9.structnlist*temp;10.temp=p->lines;11.while(temp->next!=NULL&&temp->lnum!=nlines)12.temp=temp->next;13.if(temp->lnum!=nlines)14.{15.temp->next=lalloc();16.temp->next->lnum=nlines;17.temp->next->next=NULL;18.}19.}20.voidtreeprint(structtnode*p)21.{22.structnlist*temp;23.if(p!=NULL)24.{25.treeprint(p->left);26.printf("%st",p->word);27.for(temp=p->lines;temp!=NULL;temp=temp->next)28.printf("%dt",temp->lnum);29.printf("n");30.treeprint(p->right);31.}32.}33.structtnode*talloc()34.{35.return(structtnode*)malloc(sizeof(structtnode));36.}37.structnlist*lalloc()38.{39.return(structnlist*)malloc(sizeof(structnlist));40.}41.intnoiseword(char*w)42.{43.staticchar*word[]={44."a",45."an",46."and",47."are",48."in",49."is",50."of",51."or",52."that",53."the",54."this",55."to"56.};57.intcond,mid;58.intlow=0;59.inthigh=sizeof(word)/sizeof(word[0])-1;60.while(low<=high)61.{62.mid=(low+high)/2;63.if((cond=strcmp(w,word[mid]))>0)64.low=mid+1;65.elseif(cond<0)66.high=mid-1;67.else68.returnmid;69.}70.return-1;71.} 1.char*mystrdup(char*s)2.{3.char*p;4.p=(char*)malloc(strlen(s)+1);5.if(p!=NULL)6.strcpy(p,s);7.returnp;8.}9.intmygetword(char*word,intlim)10.{11.intc,getch();12.voidungetch(int);13.char*w=word;14.while(isspace(c=getch())&&c!="n")15.;16.if(c!=EOF)17.*w++=c;18.if(!isalpha(c))19.{20.*w="";21.returnc;22.}23.for(;--lim>0;w++)24.if(!isalnum(*w=getch()))25.{26.ungetch(*w);27.break;28.}29.*w="";30.returnword[0];31.}32.intbufsp=0;33.charbuf[BUFSIZE];34.intgetch()35.{36.returnbufsp>0?buf[--bufsp]:getchar();37.}38.voidungetch(intc)39.{40.if(bufsp>=BUFSIZE)41.printf("error:Bufisoverfown");42.else43.buf[bufsp++]=c;44.}Test6-1(C)Test6-4(S)1./*2.SunAug823:18:28CST2010*/3.#include"ctype.h"4.#include"string.h"5.#include"stdio.h"6.#include"stdlib.h"7.#defineMAXWORD5008.#defineMAXLEN1009.#defineBUFSIZE50010.structtnode{11.char*word;12.intcount;13.structtnode*left;14.structtnode*right;15.};16.structtnodewnode[MAXWORD];17.structtnode*addtree(structtnode*,char*);18.intmygetword(char*,int);19.voidtreestor(structtnode*);20.voidmyqsort(structtnodewnode[],intleft,intright);21.structtnode*talloc(void);22.char*mystrdup(char*);23.intbufsp=0;24.charbuf[BUFSIZE];25.voidmain()26.{ 1.structtnode*root;2.charword[MAXLEN];3.inti,len=0;4.root=NULL;5.while(mygetword(word,MAXLEN)!=EOF)6.{7.if(isalpha(word[0]))8.root=addtree(root,word);9.len++;10.}11.treestor(root);12.myqsort(wnode,0,len-1);13.for(i=0;iword=mystrdup(w);23.p->count=1;24.p->left=p->right=NULL;25.}26.elseif((cond=strcmp(p->word,w))==0)27.p->count++;28.elseif(cond>0)29.p->left=addtree(p->left,w);30.else31.p->right=addtree(p->right,w);32.returnp;33.}34.intk=0;35.voidtreestor(structtnode*p)36.{37.if(p!=NULL)38.{39.treestor(p->left);40.wnode[k].word=p->word;41.wnode[k].count=p->count;42.k++;43.treestor(p->right);44.}45.}46.structtnode*talloc()47.{48.return(structtnode*)malloc(sizeof(structtnode));49.}50.char*mystrdup(char*s)51.{52.char*p;53.p=(char*)malloc(strlen(s)+1);54.if(p!=NULL)55.strcpy(p,s);56.returnp;57.}58.intmygetword(char*word,intlim)59.{60.intc,getch();61.voidungetch(int);62.char*w=word;63.while(isspace(c=getch()))64.;65.if(c!=EOF)66.*w++=c;67.if(!isalpha(c))68.{69.*w="";70.returnc;71.}72.for(;--lim>0;w++)73.if(!isalnum(*w=getch()))74.{75.ungetch(*w);76.break; 1.}2.*w="";3.returnword[0];4.}5./*intmygetword(char*word,intlim)6.{7.intc,getch();8.voidungetch(int);9.char*w=word;10.while(isspace(c=getch()))11.;12.if(c!=EOF)13.*w++=c;14.if(!isalpha(c))15.{16.switch(c)17.{18.case""":19.for(;--lim>0&&(*w=getch())!=""";w++)20.{21.if(*w=="n")22.{23.printf("error,missingmarkn");24.break;25.}26.}27.break;28.case"/":29.*w=getch();30.if(*w=="/")31.{32.for(;--lim>0&&(*w=getch())!="n";w++)33.;34.}35.elseif(*w="*")36.{intd;37.while((d=getch())!=EOF)38.if(d=="*")39.if((d=getch())=="/")40.break;41.else42.ungetchar(d);43.c=d;44.}45.else46.ungetch(*w);47.break;48.case"#":49.for(;--lim>0;w++)50.if(!isalnum(*w=getch())&&*w!="-")51.{52.ungetch();53.break;54.}55.break;56.default:57.break;58.*w="";59.returnc;60.}61.}62.for(;--lim>0;w++)63.if(!lisalnum(*w=getch()))64.{65.if(*w!="-")66.{67.ungetch(*w);68.break;69.}70.else71.continue;72.}73.*w="";74.returnword[0];75.}76.*/77.intgetch()78.{79.returnbufsp>0?buf[--bufsp]:getchar();80.} 1.voidungetch(intc)2.{3.if(bufsp>=BUFSIZE)4.printf("error:Bufisoverfown");5.else6.buf[bufsp++]=c;7.}8.voidmyqsort(structtnodev[],intleft,intright)9.{10.inti,last;11.voidswap(structtnodev[],int,int);12.if(left>=right)13.return;14.swap(v,left,(left+right)/2);15.last=left;16.for(i=left+1;i<=right;i++)17.{18.if(v[i].count>v[left].count)19.swap(v,++last,i);20.}21.swap(v,left,last);22.myqsort(v,left,last-1);23.myqsort(v,last+1,right);24.}25.voidswap(structtnodev[],inti,intj)26.{27.structtnodetemp;28.temp=v[i];29.v[i]=v[j];30.v[j]=temp;31.}本题是我用快速排序编写的统计单词数并按降序打印的程序,与许多版本比较起来使用快速排序更快,编程思路也比较清晰。Test6-5(C)1.*hashlinksforthedefine2.SunAug821:02:29CST2010*/3.#include"stdio.h"4.#include"string.h"5.#include"stdlib.h"6.#defineHASHSIZE1017.structnlist{8.structnlist*next;9.char*name;10.char*defn;11.};12.staticstructnlist*hashtab[HASHSIZE];13.char*mystrdup(char*s)14.{15.char*p;16.p=(char*)malloc(strlen(s)+1);17.if(p!=NULL)18.strcpy(p,s);19.returnp;20.}21.unsignedhash(char*s)22.{23.unsignedhashval=0;24.for(;*s!="";s++)25.hashval=31*hashval+*s;26.returnhashval%HASHSIZE;27.}28.structnlist*lookup(char*s)29.{30.structnlist*np; 1.for(np=hashtab[hash(s)];np!=NULL;np=np->next)2.if(!strcmp(s,np->name))3.returnnp;4.returnNULL;5.}6.structnlist*install(char*name,char*defn)7.{8.structnlist*np;9.unsignedhashval;10.if((np=lookup(name))==NULL)11.{12.np=(structnlist*)malloc(sizeof(structnlist));13.if(np==NULL||(np->name=mystrdup(name))==NULL)14.returnNULL;15.hashval=hash(name);16.np->next=hashtab[hashval];17.hashtab[hashval]=np;18.}19.else20.free((void*)np->defn);21.if((np->defn=mystrdup(defn))==NULL)22.returnNULL;23.returnnp;24.}25.char*getdefn(char*name)26.{27.structnlist*np;28.if((np=lookup(name))==NULL)29.returnNULL;30.else31.returnnp->defn;32.}33.voidundef(char*s)34.{35.structnlist*prev,*np;36.intindex;37.prev=NULL;38.index=hash(s);39.for(np=hashtab[index];np!=NULL;np=np->next)40.{41.if(!strcmp(s,np->name))42.break;43.prev=np;44.}45.if(np)46.{47.if(!prev)48.hashtab[index]=np->next;49.else50.prev->next=np->next;51.free((void*)np->name);52.free((void*)np->defn);53.free((void*)np);54.}55.}56.intmain(intargc,char*argv[])57.{58.char*p;59.install("INT_MAX","32767");60.install("INT_MIN","-32768");61.install("LONG_MAX","2147483647");62.install("LONG_MIN","-2147483647");63.undef("INT_MIN");64.if((p=getdefn(argv[1]))==NULL)65.printf("%sisnotfoundn",argv[1]);66.else67.printf("%sisdefined%sn",argv[1],p);68.return;69.}本题借用习题6-5与原书的表查找结构编写的从链表中查找已定义的宏,完成了表的建立插入以及查找的功能,并利用undef删除某些已定义的宏。 注:本章有未解决的两个题目,一个是6-2,编写程序用以读入c语言程序,并按字母顺序分组打印变量名,要求每一组内各个变量名的前6个字符相同,其余字符不同,字符串与注释中的单词不予考虑。本题我开始时没有思路,查找了一些解答后发现其实他们也是用树做的,但是却只考虑了很简单的情况,并没有真正实现真的分组,某一组的一些单词打印时会出现在另一组里面,但这样程序已经很复杂了,所以就没有深入思考找到更好的解决方法,以后有时间可能会再回过头来做一下。另一个是6-6:编写适合c语言使用的#define处理器的简单版本。一些答案也是在运行后没有出现想要的结果,就先搁置了。第五章Test5-1(S)(getint函数)1./*2.FriAug605:49:27CST2010*/3.#include"stdio.h"4.#defineBUFSIZE1005.intgetch();6.voidungetch();7.intgetint(int*pn)8.{9.intc,d,sign;10.while(isspace(c=getch()))11.;12.if(!isdigit(c)&&c!=EOF&&c!="+"&&c!="-")13.{14.ungetch(c);15.return0;16.}17.sign=(c=="-")?-1:1;18.if(c=="+"||c=="-")19.{20.d=c;21.c=getch();22.if(!isdigit(c))23.{24.if(c!=EOF)25.ungetch(c);26.ungetch(d);27.returnd;28.}29.}30.for(*pn=0.0;isdigit(c);(c=getch()))31.*pn=*pn*10+(c-"0");32./*if(*pn>0)33.*pn*=sign;34.else35.{36.ungetch(c);37.ungetch(d);38.return(d);39.}*/40.if(c!=EOF)41.ungetch(c);42.returnc;43.}44.intbufsp=0;45.charbuf[BUFSIZE];46.intgetch()47.{48.returnbufsp>0?buf[--bufsp]:getchar();49.}50.voidungetch(intc)51.{52.if(bufsp>=BUFSIZE)53.printf("error:Bufisoverfown");54.else55.buf[bufsp++]=c;56.}57.voidmain()58.{59.inti,array[100];60.for(i=0;i<100&&getint(&array[i])!=EOF;i++) 1.;2.for(;i>=0;i--)3.printf("%dn",array[i]);4.}Test5-2(B)getfloat函数1./*2.FriAug606:29:30CST2010*/3.#include"stdio.h"4.#defineBUFSIZE1005.intgetch();6.voidungetch();7.intgetfloat(float*pn)8.{9.intc,sign;10.floatpower;11.while(isspace(c=getch()))12.;13.if(!isdigit(c)&&c!=EOF&&c!="+"&&c!="-"&&c!=".")14.{15.ungetch(c);16.return0;17.}18.sign=(c=="-")?-1:1;19.if(c=="+"||c=="-")20.{21.//d=c;22.c=getch();23.}24.for(*pn=0.0;isdigit(c);(c=getch()))25.*pn=*pn*10+(c-"0");26.if(c==".")27.c=getch();28.for(power=1.0;isdigit(c);c=getch())29.{30.*pn=*pn*10+(c-"0");31.power*=10;32.}33.*pn=*pn*sign/power;34.if(c!=EOF)35.ungetch(c);36.returnc;37.}Test5-3(S)1./*thepointerversionofstrcat2.SatAug701:13:11CST2010*/3.#include"stdio.h"4.voidmystrcat(char*s,char*t)5.{6.for(;*s!="";s++)7.;8.while(*t!="")9.*s++=*t++;10.*s="";11.}12.voidmystrcpy(char*s,char*t)13.{14.while((*s++=*t++)!="")15.;16.} 1.voidmain()2.{3.chars[]="hello,";4.//chars[100];5.chart[]="world!";6.mystrcat(s,t);7.//mystrcpy(s,t);8.printf("%sn",s);9.}Test5-4(S)1./*strend(char*s,char*t)2.SatAug702:16:24CST2010*/3.#include"stdio.h"4.intstrend(char*s,char*t)5.{6.char*pa=t;7.while(*s!="")8.s++;9.while(*t!="")10.t++;11.s--;12.t--;13.while(*t--==*s--)14.;15.t++;16.if(++t==pa)17.return1;18.else19.return0;20.}21.voidmain()22.{23.chars[]="hello,thisisatest";24.chart[]="atest";25.intn=strend(s,t);26.if(n==1)27.printf("thematchisfoundn");28.else29.printf("notmatch!n");30.}Test5-5(S)1./*2.SatAug702:47:15CST2010*/3.#include"stdio.h"4.voidmystrcpy(char*s,char*t,intn)5.{6.inti=0;7.while(i0&&(*s==*t))23.{ 1.t++;2.s++;3.}4.if(*s==""||n<=0)5.return0;6.else7.return*s-*t;8.}9.voidmain()10.{11.chars[]="hello,word!";12.chart[]="Thisisatest!";13.charstr[100];14.mystrcpy(str,t,10);15.printf("%sn",str);16.mystrncat(s,t,10);17.printf("%sn",s);18.intn=mystrncmp(s,t,10);19.printf("%dn",n);Test5-7(B)1./*readline()2.SatAug703:53:50CST2010*/3.#include"stdio.h"4.#include"string.h"5.#defineMAXLEN10006.#defineMAXLINE50007.#defineMAXSTOR50008.intgetline1(char*,int);9.charlines[MAXLINE][MAXLEN];10.char*lineptr[MAXLINE];11.intreadline1(charlines[][MAXLEN],intmaxlines)//第一种实现方式,利用二维数组12.{13.intlen,nlines;14.charline[MAXLEN];15.nlines=0;16.while((len=getline1(lines[nlines],MAXLEN))>0)17.{18.if(nlines>maxlines)19.return-1;20.else21.lines[nlines++][len-1]="";22.}23.returnnlines;24.}25.intreadline2(char*linep[],char*linestor,intmaxline)//第二种方式,利用以为数组但要每次26.{//计算要存放位置27.intlen,nlines;28.charline[MAXLEN];29.char*p=linestor;30.char*linestop=linestor+MAXSTOR;31.nlines=0;32.while((len=getline1(line,MAXLEN))>0)33.if(nlines>=maxline||p+len>linestop)34.return-1;35.else36.{37.line[len-1]="";38.strcpy(p,line);39.linep[nlines++]=p;40.p=p+len;41.}42.returnnlines;43.}44.intgetline1(char*s,intlim)45.{46.inti,c;47.for(i=0;i12)13.return-1;14.//if(day<1||day>daytab[leap][month])15.//return-1;16.for(i=1;i366)||(leap==0&&yearday>365))28.return-1;29.//for(i=1;yearday>daytab[leap][i];i++)30.//yearday-=daytab[leap][i];31.for(i=1;yearday>*(*(daytab+leap)+i);i++)32.yearday-=*(*(daytab+leap)+i);33.*pmonth=i;34.*pday=yearday;35.}36.voidmain()37.{38.intyear,month,day,yearday,n;39.year=1987;40.month=4;41.day=5;42.yearday=88;43.n=day_of_year(year,month,day);44.printf("%dn",n);45.month_day(year,yearday,&month,&day);46.printf("%dyue%drin",month,day);47.}Test5-10(B)1./*thecalculatewhichevaluatesareversepolishexpressionfromthecommandline.SatAug718:02:53CST2010*/ 1.#include"stdio.h"2.#include"stdlib.h"3.#defineMAXVAL1004.intsp=0;5.doublestack[MAXVAL];6.voidpush(doublef)7.{8.if(sp0)16.returnstack[--sp];17.else18.{19.printf("error:stackempty");20.return0.0;21.}22.}23.intmain(intargc,char*argv[])24.{25.doubleop;26.intc,j=1;27.while(--argc>0)28.{29.switch(**++argv)30.{31.case"+":32.push(pop()+pop());33.break;34.case"-":35.op=pop();36.push(pop()-op);37.break;38.case"*":39.push(pop()*pop());40.break;41.case"/":42.op=pop();43.if(op==0.0)44.printf("thewrongoperatern");45.else46.push(pop()/op);47.break;48.//case"n":49.//printf("theresultis%fn",pop());50.//break;51.default:52.if(isdigit(**argv))53.{54.op=atof(*argv);55.push(op);56.}57.else58.{59.argc=1;60.printf("theunkowncommandn");61.}62.break;63.}64.//j++;65.}66.printf("theresultis%fn",pop());67.return0;68.}Test5-11(C)Test1-21(A)Test1-20(A)1.//entab程序2.#include"stdio.h"3.#defineMAXLINE1004.#defineTABINC85.#defineYES16.#defineNO07.voidentab(char*tab);8.voidsettab(intargc,char*argv[],char*tab);9.inttabpos(intpos,char*tab); 1.voidmain(intargc,char*argv[])2.{3.chartab[MAXLINE+1];4.settab(argc,argv,tab);5.entab(tab);6.}7.voidsettab(intargc,char*argv[],char*tab)8.{9.inti,pos;10.if(argc<=1)11.for(i=1;i0)20.{21.pos=atof(*++argv);22.if(pos>0&&pos<=MAXLINE)23.tab[pos]=YES;24.}25.}26.voidentab(char*tab)27.{28.intc,pos;29.intnb=0;30.intnt=0;31.for(pos=1;(c=getchar())!=EOF;pos++)32.{33.if(c=="")34.{35.if(tabpos(pos,tab)==NO)36.nb++;37.else38.{39.nb=0;40.nt++;41.}42.}43.else44.{45.for(;nt>0;nt--)46.putchar("t");47.if(c=="t")48.nb=0;49.else50.for(;nb>0;nb--)51.putchar("");52.putchar(c);53.if(c=="n")54.pos=0;55.elseif(c=="t")56.while(tabpos(pos,tab)!=YES)57.pos++;58.}59.}60.}61.inttabpos(intpos,char*tab)62.{63.if(pos>MAXLINE)64.returnYES;65.else66.returntab[pos];67.}Detab程序:68./*69.SatAug721:26:19CST2010*/70.#include"stdio.h"71.#defineMAXLINE10072.#defineTABINC873.#defineYES174.#defineNO075.voiddetab(char*tab);76.voidentab(char*tab);77.voidsettab(intargc,char*argv[],char*tab); 1.inttabpos(intpos,char*tab);2.voidmain(intargc,char*argv[])3.{4.chartab[MAXLINE+1];5.settab(argc,argv,tab);6.//entab(tab);7.detab(tab);8.}9.voidsettab(intargc,char*argv[],char*tab)10.{11.inti,pos;12.if(argc<=1)13.for(i=1;i0)22.{23.pos=atof(*++argv);24.if(pos>0&&pos<=MAXLINE)25.tab[pos]=YES;26.}27.}28.voiddetab(char*tab)29.{30.intc,pos=1;31.while((c=getchar())!=EOF)32.{33.if(c=="t")34.do35.{36.putchar("");37.}while(tabpos(pos++,tab)!=YES);38.elseif(c=="n")39.{40.pos=1;41.putchar(c);42.}43.else44.{45.putchar(c);46.pos++;47.}48.}49.}50.inttabpos(intpos,char*tab)51.{52.if(pos>MAXLINE)53.returnYES;54.else55.returntab[pos];56.}Test5-13(S)(tail–n程序)1./*2.SatAug722:29:46CST2010*/3.#include"stdio.h"4.#include"stdlib.h"5.#include"string.h"6.#defineMAXLINE50007.#defineMAXLEN10008.intgetline1(char*,int);9.intreadlines(char*lineptr[],intmaxlines);10.intmain(intargc,char*argv[])11.{12.intnlines;13.intn,m;14.char*lineptr[MAXLINE];15.//nlines=readlines(lineptr,MAXLINE);16.if(argc<=1)17.n=5; 1.elseif(argc==2&&(*++argv)[0]=="-")2.n=atoi(*argv+1);3.else4.{5.printf("usage:test5-13-nn");6.return-1;7.}8.nlines=readlines(lineptr,MAXLINE);9.if(n0)24.if(nlines>=maxline||(p=malloc(len))==NULL)25.return-1;26.else27.{28.line[len-1]="";29.strcpy(p,line);30.lineptr[nlines++]=p;31.}32.returnnlines;33.}34.intgetline1(char*s,intlim)35.{36.inti,c;37.for(i=0;i0&&(*++argv)[0]=="-")9.while(c=*++argv[0])10.switch(c)11.{12.case"d":13.dir=1;14.break;15.case"f":16.flag=1;17.break;18.case"n":19.numeric=1;20.break;21.case"r":22.reverse=1;23.break;24.default:25.printf("usage:sort-n-rn");26.printf("usage:sort-nrn");27.argc=0;28.return-1;29.}30.if((nlines=readlines(lineptr,MAXLINES))>=0)31.{32.if(numeric)33.myqsort1((void**)lineptr,0,nlines-1,reverse,34.(int(*)(void*,void*))numcmp);35./*if(flag)36.myqsort2((void**)lineptr,0,nlines-1,(int(*)(void*,void*))charcmp);37.else38.myqsort2((void**)lineptr,0,nlines-1,(int(*)(void*,void*))strcmp);*/39.else40.myqsort2((void**)lineptr,0,nlines-1,(int(*)(void*,void*))charcmp2);41.writelines(lineptr,nlines);42.return0;43.}44.else45.{46.printf("inputtoobigtosortn");47.return1;48.}49.}50.voidmyqsort1(void*v[],intleft,intright,intreverse,int(*cmp)(void*,void*))51.{52.inti,last;53.voidswap(void*v[],int,int);54.if(left>=right)55.return;56.swap(v,left,(left+right)/2);57.last=left;58.for(i=left+1;i<=right;i++)59.{60.if(reverse)61.{62.if((*cmp)(v[i],v[left])>0)63.swap(v,++last,i);64.}65.else66.{67.if((*cmp)(v[i],v[left])<0)68.swap(v,++last,i);69.}70.}71.swap(v,left,last);72.myqsort1(v,left,last-1,reverse,cmp);73.myqsort1(v,last+1,right,reverse,cmp);74.} 1.voidmyqsort2(void*v[],intleft,intright,int(*cmp)(void*,void*))2.{3.inti,last;4.voidswap(void*v[],int,int);5.if(left>=right)6.return;7.swap(v,left,(left+right)/2);8.last=left;9.for(i=left+1;i<=right;i++)10.{11.if((*cmp)(v[i],v[left])<0)12.swap(v,++last,i);13.}14.swap(v,left,last);15.myqsort2(v,left,last-1,cmp);16.myqsort2(v,last+1,right,cmp);17.}18.intnumcmp(constchar*s1,constchar*s2)19.{20.doublev1,v2;21.v1=atof(s1);22.v2=atof(s2);23.if(v1v2)26.return1;27.else28.return0;29.}30.intcharcmp(constchar*s,constchar*t)31.{32.for(;tolower(*s)==tolower(*t);s++,t++)33.;34.if(*s=="")35.return0;36.else37.returntolower(*s)-tolower(*t);38.}39.voidcharcmp2(constchar*s,constchar*t)40.{41.if(dir)42.{43.while(!isalnum(*s)&&*s!=""&&*s!="")44.s++;45.while(!isalnum(*t)&&*s!=""&&*s!="")46.t++;47.}48.if(flag)49.charcmp(s,t);50.else51.strcmp(s,t);52.}53.voidswap(void*v[],inti,intj)54.{55.void*temp;56.temp=v[i];57.v[i]=v[j];58.v[j]=temp;59.}60.intreadlines(char*lineptr[],intmaxline)61.{62.intlen,nlines;63.charline[MAXLEN];64.char*p;65.nlines=0;66.while((len=getline1(line,MAXLEN))>0)67.if(nlines>=maxline||(p=malloc(len))==NULL)68.return-1;69.else70.{71.line[len-1]=""; 1.strcpy(p,line);2.lineptr[nlines++]=p;3.}4.returnnlines;5.}6.intgetline1(char*s,intlim)7.{8.inti,c;9.for(i=0;i2.#include3.#include4.#defineMAXTOKEN1005.enum{NAME,PARENS,BRACKETS};6.voiddcl(void);7.voiddirdcl(void);8.intgettoken(void);9.inttokentype;/*typeoflasttoken*/10.chartoken[MAXTOKEN];/*lasttokenstring*/11.charname[MAXTOKEN];/*identifiername*/12.chardatatype[MAXTOKEN];/*datatype=char,int,etc.*/13.charout[1000];/*outputstring*/14.intmain()/*convertdeclarationtowords*/15.{16.while(gettoken()!=EOF){/*1sttokenonline*/17.strcpy(datatype,token);/*isthedatatype*/18.out[0]="";19.dcl();/*parserestofline*/20.if(tokentype!="n")21.printf("syntaxerrorn");22.printf("%s:%s%sn",name,out,datatype);23.}24.return0;25.}26.intgettoken(void)/*returnsnexttoken*/27.{28.intc,getch(void);/*getch:geta(possiblypushedback)character*/29.voidungetch(int);/*ungetch:pushcharacterbackoninput*/30./*reproducedatbottomofpage*/31.char*p=token;32.while((c=getch())==""||c=="t")/*skipwhitespace*/ 1.;2.if(c=="("){3.if((c=getch())==")"){4.strcpy(token,"()");5.returntokentype=PARENS;6.}7.else{8.ungetch(c);9.returntokentype="(";10.}11.}12.elseif(c=="["){13.for(*p++=c;(*p++=getch())!="]";);14.*p="";15.returntokentype=BRACKETS;16.}17.elseif(isalpha(c)){18.for(*p++=c;isalnum(c=getch());)*p++=c;19.*p="";20.ungetch(c);21.returntokentype=NAME;22.}23.else24.returntokentype=c;25.}26.voiddcl(void)/*parseadeclarator*/27.{28.intns;/*numberofstars*/29.for(ns=0;gettoken()=="*";)ns++;/*count*"s*/30.dirdcl();31.while(ns-->0)strcat(out,"pointerto");32.}33.voiddirdcl(void)/*parseadirectdeclarator*/34.{35.inttype;36.if(tokentype=="("){/*(dcl)*/37.dcl();38.if(tokentype!=")")printf("error:missing)n");39.}40.elseif(tokentype==NAME)/*variablename*/41.strcpy(name,token);42.else43.printf("error:expectednameor(dcl)n");44.while((type=gettoken())==PARENS||type==BRACKETS)45.if(type==PARENS)46.strcat(out,"functionreturning");47.else{48.strcat(out,"array");49.strcat(out,token);50.strcat(out,"of");51.}52.}53.#defineBUFSIZE10054.charbuf[BUFSIZE];/*bufferforungetch*/55.intbufp=0;/*nextfreepositioninbuf*/56.intgetch(void)/*geta(possiblypushedback)character*/57.{58.return(bufp>0)?buf[--bufp]:getchar();59.}60.voidungetch(intc)/*pretendtopushcharacterbackoninput*/ 1.{2.if(bufp>=BUFSIZE)printf("ungetch:toomanycharactersn");3.elsebuf[bufp++]=c;4.}这段代码运行后对于char(*(*x())[])()能出现结果,但是对于像int(*daytab)[13]这种就不行了,在逻辑上这段代码对于这种形式也是走不通的,但是网上只有一个英文论坛讨论过这个问题,但是回帖的却说运行通过了,不知道是我错了还是怎么回事?因为这段代码虽然短小但是dcl与dirdcl相互递归调用,所以很麻烦的,由于时间的原因没有去思考解决像int(*daytab)[13]这样的错误,因为我也不确定这段源代码是不是真的有问题。第四章Test4-1(B)1./*strindex(s,t),whitchreturnsthepositionoftherightmostoccurrenceoftins,or-1ifthereisnone.ThuAug517:09:47CST2010*/2.#include"stdio.h"3.intstrindex(char*s,char*t)4.{5.inti,j,k;6.intm=-1;7.for(i=0;s[i]!="";i++)8.{9.for(j=i,k=0;t[k]!=""&&s[j]==t[k];j++,k++)10.;11.if(t[k]=="")12.m=i;13.}14.returnm;15.}16.voidmain()17.{18.chars[]="thisisatest,thetestforstrindex.";19.chart[]="test";20.intn=strindex(s,t);21.printf("%dn",n);22.}Test4-2(S)1./*extandatof(char*s)tohandlethe123.456e-62.ThuAug517:24:14CST2010*/3.#include"stdio.h"4.doubleatof(char*s)5.{6.doubleval1,power;7.inti,j,val2,sign1,sign2;8.intm=1;9.for(i=0;isspace(s[i]);i++)10.;11.sign1=(s[i]=="-")?-1:1;12.if(s[i]=="-"||s[i]=="+")13.i++;14.for(val1=0.0;isdigit(s[i]);i++)15.val1=val1*10.0+(s[i]-"0");16.if(s[i]==".")17.i++;18.for(power=1.0;isdigit(s[i]);i++)19.{20.val1=val1*10.0+(s[i]-"0");21.power=power*10;22.}23.if(s[i]=="e"||s[i]=="E")24.{25.i++; 1.sign2=(s[i]=="-")?-1:1;2.if(s[i]=="-"||s[i]=="+")3.i++;4.for(val2=0;isdigit(s[i]);i++)5.val2=val2*10.0+(s[i]-"0");6.if(sign2==-1)7.{8.for(j=0;j20.//#defineNUMBER"0"21.#defineMAXOP10022.voidmain()23.{24.inttype,var=0,i;25.doubleop;26.chars[MAXOP];27.doublevariable[26];28.for(i=0;i<26;i++)29.variable[i]=0.0;30.while((type=getop(s))!=EOF)31.{32.switch(type)33.{34.case0:35.push(atof(s));36.break;37./*case1:38.hnumber(s);39.break;*/40.case"+":41.push(pop()+pop());42.break; 1.case"-":2.op=pop();3.push(pop()-op);4.break;5.case"*":6.push(pop()*pop());7.break;8.case"/":9.op=pop();10.if(op!=0)11.push(pop()/op);12.else13.printf("error:theoperateriswrongn");14.break;15.case"%":16.op=pop();17.if(op!=0.0)18.push((int)pop()%(int)op);19.else20.printf("error:wrongoperatern");21.break;22.case"=":23.pop();24.if(var>="A"&&var<="Z")25.variable[var-"A"]=pop();26.else27.printf("error:unkownoperatern");28.break;29.case"?":30.showTop();31.break;32.case"d":33.dumplicate();34.break;35.case"s":36.swap();37.break;38.case"c":39.clear();40.break;41.case"n":42.printf("t%.8gn",pop());43.break;44.default:45.if(type>="A"&&type<="Z")46.push(variable[type-"A"]);47.else48.printf("error:Unkowncommandn");49.break;50.}51.//var=type;52.}53.}54.//Getop.c55./*intgetop(char*s)56.ThuAug519:10:30CST2010*/57.#include"calc.h"58.#include"ctype.h"59.intgetop(char*s)60.{61.inti,c;62.while((s[0]=c=getch())==""||c=="t")63.;64.s[1]="";65.i=0;66.if(!isdigit(s[i])&&s[i]!=".")67.returnc;68.if(isdigit(s[i]))69.while(isdigit(s[++i]=c=getch()))70.;71.if(c==".")72.while(isdigit(s[++i]=c=getch()))73.;74.s[i]="";75.if(c!=EOF)76.ungetch(c);77.return0;78.}79.//getch.c80./*getch()81.ThuAug519:03:27CST2010*/82.#include"calc.h"83.#include"string.h"84.#defineBUFSIZE100 1.intbufsp=0;2.charbuf[BUFSIZE];3.intgetch()4.{5.returnbufsp>0?buf[--bufsp]:getchar();6.}7.voidungetch(intc)8.{9.if(bufsp>=BUFSIZE)10.printf("error:Bufisoverfown");11.else12.buf[bufsp++]=c;13.}14.voidungets(char*s)15.{16.intlen;17.len=strlen(s);18.while(len>0)19.ungetch(s[--len]);20.}21.//stack.c22./*thestackfunction:push(),pop().etc23.ThuAug518:56:15CST2010*/24.#include"stdio.h"25.#include"calc.h"26.#defineMAXVAL10027.intsp=0;28.doublestack[MAXVAL];29.voidpush(doublef)30.{31.if(sp0)39.returnstack[--sp];40.else41.{42.printf("error:stackempty");43.return0.0;44.}45.}46.voidshowTop()47.{48.doublec;49.if(sp>0)50.c=pop();51.printf("%fn",c);52.push(c);53.}54.voiddumplicate()55.{56.doublec;57.c=pop();58.push(c);59.push(c);60.}61.voidswap()62.{63.doublec1,c2;64.c1=pop();65.c2=pop();66.push(c2);67.push(c1);68.}69.voidclear()70.{71.sp=0;72.}73.//hnumber74./*sinexpandpow75.ThuAug522:52:59CST2010*/76.#include"calc.h"77.#include"string.h"78.#include"math.h" 1.voidhnumber(char*s)2.{3.doubleop;4.if(strcmp(s,"sin")==0)5.push(sin(pop()));6.elseif(strcmp(s,"cos")==0)7.push(cos(pop()));8.elseif(strcmp(s,"exp")==0)9.push(exp(pop()));10.elseif(strcmp(s,"pow")==0)11.{12.op=pop();13.push(pow(pop(),op));14.}15.else16.printf("error,Unkowncommandn");17.}18.//Test4-819./*20.FriAug600:25:01CST2010*/21.#include"stdio.h"22.charbuf=0;23.intgetch()24.{25.returnbuf==0?getchar():buf;26.}27.voidungetch(intc)28.{29.if(buf!=0)30.printf("overflown");31.else32.buf=c;33.}34.//Test4-10(getline版本的getop)35./*thegetlineversionofgetop()36.FriAug601:08:43CST2010*/37.#include"stdio.h"38.#defineMAXLENGTH10039.intindex1=0;40.intgetline1(char*s,intlim)41.{42.inti,j=0;43.intc;44.for(i=0;i0?x:-x5./*voidatoi(intn,char*s)//方法一6.{7.statici;8.if(n/10)9.atoi(n/10,s);10.else11.{12.i=0;13.if(n<0)14.s[i++]="-";15.}16.s[i++]=abs(n)%10+"0";17.s[i]="";18.}*/19.//chars[100];20.char*atoi(intn)//方法二21.{22.intm=n;23.staticchars[100];24.statici;25.if(n<0)26.{27.s[i++]="-";28.n=-n;29.}30.if(n/10)31.atoi(n/10);32.s[i++]=n%10+"0";33.s[i]=""; 1.if(-m==n)2.returns;3.}4.voidmain()5.{6.intn=-123456;7.chars[100];8.char*p=s;9.//atoi(n,s);10.p=atoi(n);11.printf("%sn",p);12.}Test4-13(S)(Reverse程序)1./*therecursiveversionoffunctionreverse(char*s)2.FriAug603:59:42CST2010*/3.#include"string.h"4.#include"stdio.h"5.reverser(char*s,inti,intj)6.{7.inttemp;8.if(i=s1[i];k--)39.s2[j++]=k;40.}41.}42.} 1.s2[j]="";2.}3.voidmain()4.{5.char*s[55555]={"a-z-","z-a-","-1-6-",6."a-ee-z","5-5"};7.charresult[1000];8.inti=0;9.while(s[i])10.{11.expand(s[i],result);12.printf("Unexpanded:%sn",s[i]);13.printf("expanded:%sn",result);14.++i;15.}16.}Test3-5(S)1./*itob(n,s,b)2.WedAug421:31:35CST2010*/3.#include"stdio.h"4.#include"string.h"5.voidreverse(char*s)6.{7.inti,j;8.inttemp;9.for(i=0,j=strlen(s)-1;i="a"&&c<="f")7.returnc-"a"+10;8.elseif(c>="A"&&c<="F")9.returnc-"A"+10;10.else11.return0;12.}13.inthtoi(char*s)14.{15.inti=0;16.intn;17.intanswer=0; 1.intvalid=1;2.if(s[i]=="0")3.{4.i++;5.if(s[i]=="x"||s[i]=="X")6.i++;7.else8.valid=0;9.}10.else11.valid=0;12.for(;s[i]!=""&&valid==1;i++)13.{14.if(s[i]>="0"&&s[i]<="9")15.{16.n=s[i]-"0";17.}18.elseif(s[i]>="a"&&s[i]<="f"||s[i]>="A"&&s[i]<="F")19.{20.n=hexalpha_to_int(s[i]);21.if(0==n)22.valid=0;23.}24.else25.valid=0;26.if(valid==1)27.answer=answer*16+n;28.}29.if(valid==0)30.return0;31.elsereturnanswer;32.}33.voidmain()34.{35.inti;36.char*s[]=37.{38."0X1",39."0xAF",40."0x0C0BE",41."0xab",42."123456"43.};44.unsignedintresult;45.intnum=sizeof(s)/sizeof(s[1]);46.for(i=0;i>n))<<(p-n+1));7.}Test2-7(S)1./*2.WedAug404:36:25CST2010*/3.#include"stdio.h"4.unsignedinvert(unsignedx,intp,intn)5.{6.returnx^((~(~0<0;n--)7.{8.if((x&1)==1)9.x=(x>>1)|~(~0>>1);10.else11.x=x>>1;12.}13.returnx;14.}*/15.unsignedintrightrot(unsignedintx,intn)//实现方法二16.{17.ints=sizeof(x);18.intp;19.if(p>p)|(x<<(s-p));5.}6./*unsignedintrightrot(unsignedintx,intn)//实现方法三7.{8.ints=sizeof(x);9.intp;10.if(p0)15.{16.unsignedbit=x<<(s-1);17.x=x>>1;18.x=x|bit;19.p--;20.}21.}*/22.voidmain()23.{24.unsignedintval;25.unsignedintpos;26.for(val=0;val<700;val+=49)27.for(pos=0;pos<8;pos++)28.printf("%u,%d:t%un",val,pos,rightrot(val,pos));29.}Test2-9(B)1./*2.WedAug406:54:52CST2010*/3.#include"stdio.h"4.intbitcount(unsignedintx)5.{6.intb;7.for(b=0;x!=0;x&=(x-1))8.b++;9.returnb;10.}11.voidmain()12.{13.unsignedintval=7;14.intm;15.m=bitcount(val);16.printf("%dn",m);17.}Test2-10(S)1./*2.WedAug406:46:09CST2010*/3.#include"stdio.h" 1.intlower(intc)2.{3.return(c>="A"&&c<="Z")?c+"a"-"A":c;4.}5.voidmain()6.{7.char*s="AshdglDHSLAKHGdslkhgsdJDSLAdjslkaf;SDHKLHFklsdahasd;hl";8.char*p=s;9.intresult;10.for(;*p!="";p++)11.{12.result=lower(*p);13.printf("[%c]invertto[%c]n",*p,result);14.}15.}第一章Test1-4(B)1./*PrintFahrenheit-CelsiusTable2.forfathr=0,20,........300;3.SatJul3117:48:11CST2010*/4.#include"stdio.h"5.voidmain()6.{7.floatfathr=0,celsius;8.floatstep,upper;9.step=20;//stepsize10.upper=300;//upperlimit11.printf("TheChangTablen");12.while(fathr<=upper)13.{14.celsius=(5.0/9.0)*(fathr-32);15.printf("%3.0f%6.1fn",fathr,celsius);16.fathr+=step;17.}18.}Test1-5(S)1./*TheDefinevisionoftest1-52.SatJul3118:16:13CST2010*/3.#include"stdio.h"4.#defineLOWER05.#defineSTEP206.#defineUPPER3007.voidmain() 1.{2.intfahr;3.printf("TheReverseChangTabn");4.for(fahr=UPPER;fahr>=LOWER;fahr-=STEP)5.{6.printf("%3d%6.1fn",fahr,(5.0/9.0)*(fahr-32));7.}8.}Test1-6.cTest1-7(B)1./*TheDefinevisionoftest1-52.SatJul3119:10:13CST2010*/3.#include"stdio.h"4.voidmain()5.{6.while((c=getchar())!=EOF)7.putchar(c);8.printf("%d",c);9.}Test1-8(S)1./*Countthenumberofntandthespace2.SatJul3119:19:35CST2010*/3.#include"stdio.h"4.voidmain()5.{6.intc;//Character7.doublecn=0;//Thenumberofn8.doublect=0;//Thenumberoft9.doublecs=0;//Thenumberof""10.while((c=getchar())!=EOF)11.{12.putchar(c);13.if("n"==c)14.cn++;15.if("t"==c)16.ct++;17.if(""==c)18.cs++;19.}20.printf("thenumberof\nis%.0f,thenumberof\tis%.0f,thenumberof""is%.0fn",cn,ct,cs);21.}Test1-9(S)1./*outputtheintputandomitthemore""2.SatJul3119:43:27CST2010*/3.#include"stdio.h" 1.voidmain()2.{3.intc;//Character4.intcold;//Theoldcharacter5.cold="a";//6.while((c=getchar())!=EOF)7.{8.if(""==c&&""==cold)9.;10.else11.{12.putchar(c);13.}14.cold=c;15.}16.}Test1-10(S)1./*2.SunAug104:31:33CST2010*/3.#include4.voidmain()5.{6.intc;//Character7.while((c=getchar())!=EOF)8.{9.if("11"==c)10.printf("\t");11.elseif("10"==c)12.printf("XXXXX");13.elseif("\"==c)14.printf("\");15.else16.putchar(c);17.}18.}Test1-12(B)1./*2.SunAug119:11:32CST2010*/3.#include"stdio.h"4.voidmain()5.{6.intc;//Character7.intflag=1;//identifier8.while((c=getchar())!=EOF)9.{10.if(""==c||"t"==c||"n"==c)11.{12.if(0==flag)13.{14.printf("n");15.flag=1; 1.}2.}3.else4.{5.putchar(c);6.flag=0;7.}8.}9.}Test1-13(A)1./*水平方向2.SunAug122:07:56CST2010*/3.#include"stdio.h"4.#defineMAXLENGTH105.#defineOVERFLOW106.voidmain()7.{8.inti,j;9.intc;//Character10.intflag=0;//identifier11.intnc=0;//Thenumberofcharacter12.intwl[MAXLENGTH+1];//containsthenumberofwordswhichhasdifferentlength13.for(i=1;i=10)24.nc=OVERFLOW;25.wl[nc]++;26.nc=0;27.flag=0;28.}29.}30.for(i=1;i=10t");38.for(j=1;j="a"&&c<="z"||c>="A"&&c<="Z")60.{61.if(flag==0)62.{nw++;63.flag=1;64.} 1.nc++;2.}3.else4.{5.if(flag==1)6.{7.if(nc>=10)8.nc=MAXLENGTH;9.wl[nc]++;10.nc=0;11.flag=0;12.}13.else14.continue;15.}16.}17.for(i=0;i<=MAXLENGTH;i++)18.{19.if(wl[i]>maxvalue)20.maxvalue=wl[i];21.}22.printf("nThenumberofwordsare%dn",nw);23.for(i=maxvalue;i>0;i--)24.{25.printf("%4d|",i);26.for(j=1;j<=MAXLENGTH;j++)27.{28.if(wl[j]>=i)29.printf("*");30.else31.printf("");32.}33.printf("n");34.}35.printf("+");36.for(i=0;i%dn",MAXLENGTH);42.//printf("123456789>=10n");43.}Test1-14(B)1./*2.ThuAug1223:23:02CST2010*/3.#include"stdio.h"4.#include"ctype.h"5.#defineMAXHIST156.#defineMAXCHAR1287.voidmain()8.{9.chars[MAXCHAR];10.inti,j,c;11.intlen;12.intmaxval;13.for(i=0;imaxval)24.maxval=s[i];25.}26.for(i=0;i0)2.{3.if((len=s[i]*MAXHIST/maxval)<0)4.len=1;5.}6.else7.len=0;8.for(j=0;j0)8.{9.printf("%dt%sn",len,line);10.if(len>max)11.{12.max=len;13.copy(line,longest);14.}15.}16.if(max>0)17.printf("%sn",longest);18.}Test1-17(S)1./*2.MonAug222:04:11CST2010*/3.#include"stdio.h"4.intgetline1(chars[])5.{6.intc;//Character7.inti;8.for(i=0;(c=getchar())!=EOF&&c!="n";i++)9.s[i]=c;10.if(c=="n")11.s[i++]=c;12.s[i]="";13.returni;14.}15.voidmain()16.{17.intlen;18.charline[1000];19.while((len=getline1(line))>0)20.if(len>80)21.printf("%sn",line);22.}Test1-18(S)1./*2.ThuAug1223:59:30CST2010*/3.#include"stdio.h"4.#defineMAXLEN10005.intgetline1(chars[])6.{7.intc;//Character8.inti;9.for(i=0;(c=getchar())!=EOF&&c!="n";i++)10.s[i]=c;11.if(c=="n")12.s[i++]=c;13.s[i]="";14.returni;15.}16.voidmain()17.{18.chars[MAXLEN];19.intlen;20.while((len=getline1(s))>0)21.{22.len=len-2;23.while(len>=0&&(s[len]==""||s[len]=="t"))24.len--;25.if(len>=0)26.s[len+1]="n"; 1.s[len+2]="";2.printf("%s",s);3.}4.}Test1-19(B)1.#include2.#defineMAX_LINE10243.voiddiscardnewline(chars[])4.{5.inti;6.for(i=0;s[i]!="";i++)7.{8.if(s[i]=="n")9.s[i]="";10.}11.}12.intreverse(chars[])13.{14.charch;15.inti,j;16.for(j=0;s[j]!="";j++)17.{18.}19.--j;20.for(i=0;i0)47.{48.discardnewline(line);49.reverse(line);50.printf("%sn",line);51.}52.return0;53.}Test1-22(B)1./*2.TueAug305:46:35CST2010*/3.#include"stdio.h" 1.#defineMAXLENGTH10002.#defineFOLDLENGTH603.intgetline1(char*str)4.{5.inti;6.intc;7.for(i=0;i0)24.{25.if(len>FOLDLENGTH)26.{27.i=0;28.local=0;29.while(i0)41.printf("unmatchbracen");42.if(brack>0)43.printf("unmatchbrackn");44.if(paren<0)45.printf("unmatchparenn");46.}47.voidsearch(intc)48.{49.if(c=="{")50.brace++;51.elseif(c=="}")52.brace--;53.elseif(c=="[")54.brack++;55.elseif(c=="]")56.brack--;57.elseif(c=="(")58.paren++;59.elseif(c==")")60.paren--;61.} 1.voidin_comment()2.{3.intc,d;4.c=getchar();5.d=getchar();6.while(c!="*"||d!="/")7.{8.c=d;9.d=getchar();10.}11.}12.voidin_quote(intc)13.{14.intd;15.while((d=getchar())!=c)16.if(d=="\")17.getchar();18.}'