返回顶部

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

[复制链接]
admin1Lv.9 显示全部楼层 发表于 2022-10-8 13:26:17 |阅读模式 打印 上一主题 下一主题
《C程序设计语言》读书笔记及课后答案.doc (21.53 KB, 下载次数: 0, 售价: 1 威望)
通过一周多的学习,大体看完了《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)、指针。建立了“指针为大”的思想,对于应用指针进行类型转换以及运算
都有了新的认识。比如:int a=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)到现在为止没有能够解决。属于后种情况的我在每一章最后都加以说明。

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


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

用read,write,open,close 系统调用代替标准库中功能等价的函数,重写第七章的cat 程序,并比较相对执行速度。
1./* the cat function
2.Wed Aug 11 18:34:43 CST 2010*/
3.#include"stdio.h"
4.#include"stdlib.h"
5.#include"fcntl.h"
6.void main(int argc ,char * argv[])
7.{
8.int fd;
9.void filecopy(int , int);
10.char *prog = argv[0];
11.if(argc == 1)
12.filecopy(0,1);
13.else
14.while(--argc >0)
15.{
16.if((fd=open(*++argv,O_RDONLY,"r")) ==
-1)
17.{
18.fprintf(stderr,"%s can't
open %s\n",prog,*argv); 19.exit(1);
20.}
21.else
22.{
23.filecopy(fd,1);
24.close(fd);
25.}
26.}
27.exit(0);
28.}
29.void filecopy(int fd1,int fd2)
30.{
31.int n;
32.static char buf[BUFSIZ];
33.while((n=read(fd1,buf,BUFSIZ)) >0)
34.if(write(1,buf,n) != n)
35.fprintf(stderr,"write error");
36.

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

}
设计编写fflush、_flush、fclose 函数
1./* the fopen function
2.Wed Aug 11 19:23:51 CST 2010*/
3.//#include "stdio.h"
4.#include"fcntl.h"
5.#include"mystdio2.h"
6.#include"sys/file.h"
7.#include"unistd.h"
8.#include"stdlib.h"
9.#define PERMS 0666
10.FILE _iob[OPEN_MAX]={
11.{0,(char*)0,(char*)0,_READ,0},
12.{0,(char*)0,(char*)0,_WRITE,1},
13.{0,(char*)0,(char*)0,_WRITE | _UNBUF,2}
14.};
15.FILE *fopen(char *name,char *mode)
16.{
17.FILE *fp;
18.int fd;
19.if(*mode != 'r'|| *mode != 'w'|| *mode !=
'a')
20.return NULL;
21.for(fp = _iob;fpflag &(_READ | _WRITE)) == 0)
24.break;
25.}
26.if(fp>=_iob + OPEN_MAX)
27.return NULL;
28.if(*mode == 'w')
29.fd = creat(name,PERMS);
30.else if(*mode == 'a')
31.{
32.if((fd = open(name,O_WRONLY,0)) == -1)
33.fd = creat(name,PERMS);
34.lseek(fd,0L,2);
35.}
36.else
37.fd = open(name,O_RDONLY,0);
38.if(fd == -1)
39.return NULL; 40.fp->fd = fd;
41.fp->ptr = NULL;
42.fp->base =NULL;
43.fp->cnt = 0;
44.fp->flag = (*mode == 'w')?_WRITE:_READ;
45.return fp;
46.}
47.int _fillbuf(FILE * fp)
48.{
49.int bufsize;
50.if((fp->flag &(_READ|_EOF|_ERR)) !=
_READ)
51.return EOF;
52.bufsize = (fp->flag & _UNBUF)?1:BUFSIZ;
53.if(fp->base == NULL)
54.{
55.if((fp->base = (char *) malloc(bufsize))
== NULL)
56.return EOF;
57.}
58.fp->ptr = fp->base;
59.fp->cnt = read(fp->fd,fp->base,bufsize);
60.if(--fp->cnt < 0)
61.{
62.if(fp->cnt = -1)
63.fp->flag |= _EOF;
64.else
65.fp->flag |= _ERR;
66.fp->cnt = 0;
67.return EOF;
68.}
69.return (unsigned char) *fp->ptr++;
70.}
71.int _flushbuf(int x,FILE *fp)
72.{
73.unsigned nc;
74.int bufsize;
75.if(fp  _iob+OPEN_MAX)
76.return EOF;
77.if((fp->flag & (_WRITE | _EOF| _ERR)) !=
_WRITE)
78.return EOF;
79.bufsize = (fp->flag & _UNBUF)?1:BUFSIZ;
80.if(fp->base == NULL)
81.{
82.if((fp->base = (char *) malloc( BUFSIZ))
== NULL)
83.fp->flag |= _ERR;
84.return EOF;
85.}
86.else
87.{
88.nc = fp->ptr - fp->base;
89.if(write(fp->fd,fp->base,nc) != nc)
90.fp->flag |= _ERR;
91.return EOF;
92.}
93.fp->ptr = fp->base;
94.*fp->ptr++ = (char)x;
95.fp->cnt = bufsize -1;
96.return x;
97.}
98.int fflush(FILE *fp)
99.{
100.int rc= 0;
101.if(fp < _iob || fp > _iob+OPEN_MAX) 102.return EOF;
103.if(fp->flag & _WRITE )
104.rc = _flushbuf(0,fp);
105.// fp->ptr = fp->base;
106.fp->cnt = (fp->flag & _UNBUF)? 1:BUFSIZ; 107.return rc;
108.}
109.int fclose(FILE *fp)
110.{
111.int rc;
112.if((rc = fflush(fp)) != EOF)
113.{
114.free((void*)fp->base);
115.fp->cnt = 0; 116.fp->ptr =NULL;
117.fp->base =NULL;
118.fp->flag &= ~(_READ | _WRITE); 119.}
120.return rc;
121.}
122.main()
123.{
124.unsigned char c;
125.FILE *fp;
126.fp = fopen("mystdio2.h","r"); 127.getc(fp);
128.putc(c,fp);
129.}
130./mystdio2.h
131.#define NULL 0
132.#define EOF (-1)
133.#define OPEN_MAX 20
134.#define BUFSIZ 1024
135.typedef struct _iobuf{
136.int cnt;
137.char *ptr;
138.char *base;
139.int flag;
140.int fd;
141.}FILE;
142.extern FILE _ioba[OPEN_MAX]; 143.#define stdin (&_iob[0])
144.#define stdout (&_iob[1]) 145.#define stderr (&_iob[2])
146.enum _flags{
147._READ =01,
148._WRITE = 02,
149._UNBUF = 04,
150._EOF = 010,
151._ERR = 020
152.};
153.int _fillbuf(FILE *);
154.int _flushbuf(int ,FILE *);
155.//FILE * fopen(char*,char*);
156.int fclose(FILE*);
157.int fflush(FILE *);
158.#define feof(p) (((p)->flag & _EOF) != 0) 159.#define getc(p) (--(p)->cnt >= 0\ 160.?(unsigned char)
*(p)->ptr++:_fillbuf(p))?? 宏定义但这不
是个函数,如何应用,用一个变量接受他的结
果呢?
161.#define putc(x,p) (--(p)->cnt>=0\ 162.?*(p)->ptr++ = (x):_flushbuf((x),p))
163.#define getchar() getc(stdin)
164.#define putchar(x) putc((x),stdout)
这段代码是原书上的内容,我添加了练习中的_flush fflush以及fclose函数,程序的从思路上走没什么问题,但是因为重写了stdio.h所以编译老是出错,而且对于我标出的哪一点我还是有些疑问的。

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

设计编写fseek(FILE *fp,long offset,int origin)函数
1.#include"stdio.h"
2.#include"stdlib.h"
3.int fseek(FILE *fp,long offset,int
origin)
4.{
5.unsigned nc;
6.long rc = 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.else if(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.#define BUF_LEN 3
4.Printbuf(char *buf,int len)
5.{
6.int i;
7.for(i=0;i_IO_read_ptr,ftell(fp));
24.for(i=0;i_IO_read_ptr,ftell(fp));
29.}
30.fseek(fp,5L,1);
31.Printbuf(mybuf,BUF_LEN);
32.printf("curp = %p\tpos
=%ld\n",fp->_IO_read_ptr,ftell(fp)); 33.fgetc(fp);
34.Printbuf(mybuf,BUF_LEN);
35.printf("curp = %p\t pos
=%ld\n",fp->_IO_read_ptr,ftell(fp));
36./* fgetc(fp);
37.Printbuf(mybuf,BUF_LEN);
38.printf("curp = %p\t pos
=%ld\n",fp->_IO_read_ptr,ftell(fp));
39.fgetc(fp);
40.Printbuf(mybuf,BUF_LEN);
41.printf("curp = %p\t pos
=%ld\n",fp->_IO_read_ptr,ftell(fp));
42.fgetc(fp);
43.Printbuf(mybuf,BUF_LEN);
44.printf("curp = %p\t pos
=%ld\n",fp->_IO_read_ptr,ftell(fp));*/
45.fclose(fp);
46.//getchar();
47.}
这是当fseek(fp,2L,1)的时候:
mybuf = 0xbff05c15
curp = 0xbff05c15 pos = 0
ABC
curp = 0xbff05c16 pos = 1
ABC
curp = 0xbff05c17 pos = 2
ABC
curp = 0xbff05c15 pos =4
EFG
curp = 0xbff05c16 pos =5
curp就是FILE结构体中的缓冲区指针,当执行fseek的时候他是怎样移动的?这是当fseek(fp,1L,1)的时候
mybuf = 0xbfcefe15
curp = 0xbfcefe15 pos = 0
ABC
curp = 0xbfcefe16 pos = 1
ABC
curp = 0xbfcefe17 pos = 2
BCD
curp = 0xbfcefe17 pos =3
BCD
curp = 0xbfcefe18 pos =4
这是当fseek(fp,5L,1)的时候
mybuf = 0xbfee6ce5
curp = 0xbfee6ce5 pos = 0
ABC
curp = 0xbfee6ce6 pos = 1
ABC
curp = 0xbfee6ce7 pos = 2
FGH
curp = 0xbfee6ce7 pos =7
FGH
curp = 0xbfee6ce8 pos =8
这是fseek(fp,-3L,1)并且当for(i=0;i0)
38.fsize(*++argv);
39.}
40.void fsize(char *name)
41.{
42.struct stat stbuf;
43.if(stat(name,&stbuf) == -1)
44.{
45.fprintf(stderr,"fsize:can't
access %s\n",name);
46.return;
47.}
48.if((stbuf.st_mode & S_IFMT) == S_IFDIR)
49.dirwalk(name,fsize);
50.printf("%5u%6o%8ld %s\n",(unsigned)stbu
f.st_ino,stbuf.st_mode,stbuf.st_size,na
me);
51.}
52.void dirwalk(char *dir,void
(*fcn)(char*))
53.{
54.char name[MAX_PATH];
55.Dirent *dp;
56.MYDIR *dfd;
57.if((dfd =myopendir(dir)) == NULL)
58.{
59.fprintf(stderr,"dirwalk: can't
open %s\n",dir);
60.return ;
61.}
62.while((dp = myreaddir(dfd)) != NULL)
63.{
64.if(strcmp(dp->name,".") == 0 ||
strcmp(dp->name,"..")==0 )
65.continue;
66.if(strlen(dir)+strlen(dp->name)+2 >size
of(name))
67.fprintf(stderr,"dirwalk:name %s %s too
long\n",dir,dp->name);
68.else
69.{
70.sprintf(name,"%s/%s",dir,dp->name);
71.(*fcn)(name);
72.}
73.}
74.myclosedir(dfd);
75.}
76.int fstat(int fd,struct stat *);
77.MYDIR *myopendir(char *dirname)
78.{
79.int fd;
80.struct stat stbuf;
81.MYDIR *dp;
82.if((fd = open(dirname,O_RDONLY,0)) == -1
83.|| fstat(fd,&stbuf) == -1
84.|| (stbuf.st_mode & S_IFMT) != S_IFDIR
85.|| (dp = (MYDIR *)malloc(sizeof(MYDIR)))
== NULL)
86.return NULL;
87.dp->fd = fd;
88.return dp;
89.}
90.void myclosedir(MYDIR *dp)
91.{
92.if(dp)
93.{
94.close(dp->fd);
95.free((void*)dp);
96.}
97.}
98.Dirent *myreaddir(MYDIR *dp)
99.{
100.struct mydirect dirbuf;??为什么要借用这个结构?
101.static Dirent d;??直接用Dirent不行吗?
102.while(read(dp->fd,(char*)
&dirbuf,sizeof(dirbuf)) ==
sizeof(dirbuf))??出错,直接返回了NULL,103.{
//读会有什么问题?104.if(dirbuf.d_ino ==0)
105.continue;
106.d.ino = dirbuf.d_ino;
107.strncpy(d.name,dirbuf.d_name,DIRSIZ); 108.d.name[DIRSIZ] = '\0';
109.return &d;
110.}
111.return NULL;
112.}
本题的代码仍然是原书上的代码,是一个Linux中经典命令ls的简化版本,习题只是让修改一点,但是这段代码一直在机器上实现不了功能,我跟踪了一下过程发现总是在我标出的地方直接返回NULL了,但是程序在逻辑上我觉得没什么错误呀,还有为什么要设计MYDIR结构呢,我怎么觉得只要一个Dirent结构就够了呢?具体用到文件描述符时定义一个int型的不就行了吗?要让opendir,readdir等用MYDIR 结构操作我怎么感到多此一举呢?还有在readdir函数中借用了系统中的struct direct结构这又是为什么呢?直接用一个Dirent结构难道不行吗?这也就不用来回在转换再赋值了不是吗?

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


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

1./* the malloc function:
2.Wed Aug 11 03:29:19 CST 2010*/
3.#include"stdio.h"
4.#include"string.h"
5.#define NALLOC 1024
6.#define MAXNBYTES 10240
7.typedef long Align;
8.union header
9.{
10.struct
11.{
12.union header *next;
13.unsigned size;
14.}s;
15.Align x;
16.};
17.typedef union header Header;
18.static unsigned maxalloc;
19.static Header base; 20.static Header *freeptr=NULL;
21.void * malloc(unsigned nbytes)
22.{
23.Header *ptr;
24.Header *prevptr;
25.Header *morecore(unsigned );
26.unsigned nunits;
27.if(nbytes>MAXNBYTES)
28.{
29.fprintf(stderr,"can't alloc more than %u
bytes\n",MAXNBYTES);
30.return NULL;
31.}
32.nunits = (nbytes +
sizeof(Header)-1)/(sizeof(Header)) +1;
33.printf("the nunits is %d\n",nunits);
34.if((prevptr=freeptr) == NULL)
35.{
36.base.s.next=prevptr=freeptr=&base;
37.base.s.size = 0;
38.}
39.for(ptr=prevptr->s.next;;prevptr=ptr ,p
tr=ptr->s.next)
40.{
41.if(ptr->s.size >=nunits)
42.{
43.if(ptr->s.size==nunits)
44.prevptr->s.next = ptr->s.next;
45.else
46.{
47.ptr->s.size-=nunits;
48.ptr=ptr+ptr->s.size;
49.ptr->s.size = nunits;
50.}
51.freeptr=prevptr;
52.return (void *) (ptr+1);
53.}
54.if(ptr==freeptr)
55.{
56.if((ptr = morecore(nunits))== NULL)
57.return NULL;
58.}
59.}
60.}
61.Header *morecore(unsigned nu)
62.{
63.char *cp,*sbrk(int);
64.Header *hp;
65.void free(void *);
66.if(nu < NALLOC)
67.nu = NALLOC;
68.cp=sbrk(nu*sizeof(Header));
69.if(cp == (char*) -1)
70.return NULL;
71.hp = (Header*)cp;
72.hp->s.size = nu;
73.maxalloc = (hp->s.size >
maxalloc)?nu:maxalloc;
74.free((void*)(hp+1));
75.return freeptr;
76.} 77.void free(void*ap)
78.{
79.Header *bp;
80.Header *p;
81.bp = (Header*)ap - 1;
82.if(bp->s.size == 0 || bp->s.size >
maxalloc)
83.{
84.fprintf(stderr,"free,can't free %u
units\n",bp->s.size);
85.return;
86.}
87.for(p=freeptr;bp >p&&bps.next;p
=p->s.next)
88.{
89.if(p>p->s.next && (bp > p || bp s.next))
90.break;
91.}
92.if(bp+bp->s.size == p->s.next)
93.{
94.bp->s.size += p->s.next->s.size;
95.bp->s.next = p->s.next->s.next;
96.}
97.else
98.bp->s.next=p->s.next;
99.// p->s.next = bp;
100.if(p+p->s.size ==bp)
101.{
102.p->s.size+=bp->s.size;
103.p->s.next = bp->s.next;
104.}
105.else
106.p->s.next=bp;
107.freeptr = p;
108.}
109.unsigned bfree(char *p,unsigned n) 110.{
111.Header *hp;
112.hp = (Header*)p;
113.if(n < sizeof(Header))
114.return 0;
115.hp->s.size = (n + sizeof(Header) -
1)/sizeof(Header);
116.free((void*)(hp+1));
117.return hp->s.size;
118.}
119.void* calloc(unsigned n,unsigned size) 120.{
121.unsigned nb;
122.char *p,*q;
123.int i;
124.nb = n*size;
125.if((p = q = (char *) malloc(nb)) != NULL) 126.for(i=0;i=1)
32.strcpy(pattern,*argv);
33.else 34.printf("usage: find -x -n pattern
filename\n");
35.if(argc == 1)
36.fmatch(stdin,"",pattern,number,except);
37.else
38.while(--argc > 0)
39.if((fp = fopen(*++argv,"r")) == NULL)
40.{
41.fprintf(stderr,"%s can't open\n",*argv);
42.exit(1);
43.}
44.else
45.{
46.fmatch(fp,*argv,pattern,number,except);
47.fclose(fp);
48.}
49.exit(0);
50.}
51.void fmatch(FILE *fp,char *name,char
*pattern,int number,int except)
52.{
53.char line[MAXLINE];
54.long lineno = 0;
55.while(fgets(line,MAXLINE,fp) !=NULL)
56.{ lineno++;
57.if((strstr(line,pattern) != NULL) !=
except)
58.{
59.if(*name)
60.printf("%s:",name);
61.if(number)
62.printf("\t%ld:",lineno);
63.printf("%s\n",line);
64.}
65.} 66.}

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

1./*
2.Tue Aug 10 03:35:19 CST 2010*/
3.#include"stdio.h"
4.#include"stdlib.h"
5.#define MAXHEAD 5
6.#define MAXTAIL 3
7.#define MAXPAGE 66
8.#define MAXLINE 100
9.void fprint(FILE *fp,char *name);
10.int head(char *name, int pageno);
11.void main(int argc ,char *argv[])
12.{
13.FILE *fp;
14.if(argc == 1)
15.fprint(stdin,"");
16.else
17.{
18.while(--argc >0)
19.{
20.if((fp = fopen(*++argv,"r")) == NULL)
21.{
22.fprintf(stderr,"%s can't open\n",*argv);
23.exit(1);
24.}
25.else
26.{
27.fprint(fp,*argv);
28.fclose(fp);
29.} 30.}
31.}
32.exit(0);
33.}
34.void fprint(FILE *fp, char *name)
35.{
36.int lineno,pageno =1;
37.char lines[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.int head(char*name,int pageno)
54.{
55.int nl=3;
56.printf("\n\n");
57.printf("%s\t%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.else if((conp=strcmp(p->word,w)) == 0)
53.addin(p,nlines);
54.else if(conp >0)
55.p->left = addtree(p->left,w,nlines);
56.else
57.p->right = addtree(p->right,w,nlines);
58.return p;
59.}
60.void addin(struct tnode *p, int nlines)
61.{
62./* struct tnode *temp;
63.temp->lines = p->lines;
64.while(temp->lines->next !=NULL &&
temp->lines->lnum != nlines)
65.temp->lines = temp->lines->next;
66.if(temp->lines->lnum != nlines)
67.{
68.temp->lines->next = lalloc();
69.temp->lines->next->lnum = nlines;
70.temp->lines->next->next = NULL;
71.}*///本段为刚开始的时候自己写的,只是没
有设置struct nlist 变量直接用了struct
tnode 变量来操作,就是麻烦一点,感觉逻辑
上是对的,但是为什么运行时这个地方使得结
果不正确了呢?
72.struct nlist *temp;
73.temp = p->lines;
74.while(temp->next !=NULL && temp ->lnum !=
nlines)
75.temp = temp->next;
76.if(temp->lnum != nlines)
77.{
78.temp->next = lalloc();
79.temp->next->lnum = nlines;
80.temp->next->next = NULL;
81.}
82.}
83.void treeprint(struct tnode *p)
84.{
85.struct nlist *temp;
86.if(p != NULL)
87.{
88.treeprint(p->left);
89.printf("%s\t",p->word);
90.for(temp=p->lines ;temp != NULL;temp =
temp->next)
91.printf("%d\t",temp->lnum);
92.printf("\n");
93.treeprint(p->right);
94.}
95.}
96.struct tnode* talloc()
97.{
98.return (struct tnode *)
malloc( sizeof(struct tnode));
99.}
100.struct nlist* lalloc()
101.{
102.return (struct nlist *) malloc
(sizeof(struct nlist));
103.}
104.int noiseword(char *w) 105.{
106.static char* word[] = { 107."a",
108."an",
109."and",
110."are",
111."in",
112."is",
113."of",
114."or",
115."that",
116."the",
117."this",
118."to"
119.};
120.int cond, mid;
121.int low=0;
122.int high =
sizeof(word)/sizeof(word[0])-1;
123.while(low 0) 127.low = mid +1;

128.else if ( cond




上一篇:C程序设计语言部分章节习题
下一篇:C程序设计语言第次
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

雅宝知识库(yabaojy.com)--是一个聚焦知识付费的平台,包括在线教育、文档下载、作业答案、网课答案、考试资料、形考任务答案、行业资料、毕业论文、同时还包括问答平台、资料文库、课件下载等,是一个综合在线学习知识分享交流平台。
  • 企业微信

  • 官方微信

  • 商务合作