《C程序设计语言》读书笔记及课后答案
通过一周多的学习,大体看完了《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)到现在为止没有能够解决。属于后种情况的我在每一章最后都加以说明。
用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.
}
设计编写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;fp<_iob+OPEN_MAX;fp++)
22.{
23.if((fp->flag &(_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 || 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所以编译老是出错,而且对于我标出的哪一点我还是有些疑问的。
设计编写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 8.putchar(*(buf+i)); 9.putchar('\n'); 10.} 11.void main() 12.{ 13.FILE *fp; 14.char mybuf[BUF_LEN]; 15.int i; 16.if((fp = fopen("t.txt","r")) == NULL) 17.{ 18.fprintf(stderr,"can't open file\n"); 19.exit(1); 20.} 21.setvbuf(fp,mybuf,_IOFBF,BUF_LEN); 22.printf("mybuf = %p\n",mybuf); 23.printf("curp = %p\tpos = %ld\n",fp->_IO_read_ptr,ftell(fp)); 24.for(i=0;i<2;++i) 25.{ 26.fgetc(fp); 27.Printbuf(mybuf,BUF_LEN); 28.printf("curp = %p\tpos = %ld\n",fp->_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;i<4;++i)的时候mybuf = 0xbfb199d5 curp = 0xbfb199d5 pos = 0 ABC curp = 0xbfb199d6 pos = 1 ABC curp = 0xbfb199d7 pos = 2 ABC curp = 0xbfb199d8 pos = 3 DEF curp = 0xbfb199d6 pos = 4 DEF curp = 0xbfb199d5 pos =3 DEF curp = 0xbfb199d6 pos =4 1./* 2.Wed Aug 11 06:24:34 CST 2010*/ 3.#include 4.#include 5.#include 6.//#include "syscalls.h" 7.#include 8.#include 9.#include"sys/dir.h" 10.#include"stdlib.h" 11.//#define NAME_MAX 14 12.#define MAX_PATH 1024 13.#define DIRSIZ 14 14.typedef struct{ 15.long ino; 16.char name[NAME_MAX +1]; 17.} Dirent; 18.typedef struct{ 19.int fd; 20.Dirent d; 21.}MYDIR;??不用MYDIR结构不行吗?用到fd 的时候直接设个变量代替不行吗? 22.struct mydirect { 23.ino_t d_ino; 24.char d_name[DIRSIZ]; 25.}; 26.MYDIR *myopendir(char *dirname); 27.Dirent *myreaddir(MYDIR *dfd); 28.void myclosedir(MYDIR *dfd); 29.//int stat(char *,struct stat *); 30.void dirwalk(char*,void (*fcn)(char*)); 31.void fsize(char *); 32.void main(int argc, char *argv[]) 33.{ 34.if(argc == 1) 35.fsize("."); 36.else 37.while(--argc>0) 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(https://www.360docs.net/doc/683416713.html,,dirbuf.d_name,DIRSIZ); https://www.360docs.net/doc/683416713.html,[DIRSIZ] = '\0'; 109.return &d; 110.} 111.return NULL; 112.} 本题的代码仍然是原书上的代码,是一个Linux中经典命令ls的简化版本,习题只是让修改一点,但是这段代码一直在机器上实现不了功能,我跟踪了一下过程发现总是在我标出的地方直接返回NULL了,但是程序在逻辑上我觉得没什么错误呀,还有为什么要设计MYDIR结构呢,我怎么觉得只要一个Dirent结构就够了呢?具体用到文件描述符时定义一个int型的不就行了吗?要让opendir,readdir等用MYDIR 结构操作我怎么感到多此一举呢?还有在readdir函数中借用了系统中的struct direct结构这又是为什么呢?直接用一个Dirent结构难道不行吗?这也就不用来回在转换再赋值了不是吗? 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&&bp =p->s.next) 88.{ 89.if(p>p->s.next && (bp > p || bp < p->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 127.*q++ = 0; 128.return (void*) p; 129.} 130.void main() 131.{ 132.int c,d; 133.char *p,*q; 134.char s[100]; 135.//c = bfree(s,100); 136.//printf("%d\n",c); 137.d = sizeof(Header); 138.printf("the sizeof Header is %d\n",d); 139.p = (char *) malloc(60); 140.q = (char *) calloc(6,sizeof(int)); 141.strcpy(q,"this is a test"); 142.strcpy(p,"hello,word!"); 143.printf("%s\n%s\n",p,q); 144.c = bfree(s,100); 145.printf("%d\n",c); 146.free((void*)p); 147.free((void*)q); 148.} 1./* 2.Tue Aug 10 16:57:57 CST 2010*/ 3.#include"stdio.h" 4.#include"string.h" 5.#include"ctype.h" 6.void main(int argc ,char *argv[]) 7.{ 8.int c; 9.if(strcmp(argv[1],"lower") == 0) 10.while((c = getchar()) != EOF) 11.putchar(tolower(c)); 12.else if (strcmp(argv[1],"upper") == 0) 13.while((c=getchar()) != EOF) 14.putchar(toupper(c)); 15.else 16.printf("usage: test7-1 [lower|upper];\n" ); 1./* 2.Tue Aug 10 06:58:06 CST 2010*/ 3.#include"stdio.h" 4.#include"ctype.h" 5.#define MAXLINE 10 6.#define OCTLEN 6 7.void main() 8.{ 9.int c,pos = 0; 10.int inc(int pos,int n); 11.while((c=getchar()) != EOF) 12.if(iscntrl(c) || c == ' ') 13.{ 14.pos = inc(pos,OCTLEN); 15.printf(" \\%03o ",c); 16.if(c == '\n') 17.{ 18.pos = 0; 19.putchar('\n'); 20.} 21.} 22.else 23.{ 24.putchar(c); 25.pos = inc(pos,1); 26.} 27.} 28.int inc(int pos ,int n) 29.{ 30.if(pos +n < MAXLINE) 31.return pos+n; 32.else 33.{ 34.putchar('\n'); 35.return n; 36.} 37.} 1./* 2.Tue Aug 10 05:38:01 CST 2010*/ 3.#include"stdio.h" 4.#include"stdarg.h" 5.#include"ctype.h" 6.#define LOCALFMT 100 7.void minprintf(char *fmt,...) 8.{ 9.va_list ap; 10.char *p,*sval; 11.char localfmt[LOCALFMT]; 12.int i,ival; 13.unsigned uval; 14.double dval; 15.va_start(ap,fmt); 16.for(p=fmt;*p;p++) 17.{ 18.if(*p != '%') 19.{ 20.putchar(*p); 21.continue; 22.} 23.i = 0; 24.localfmt[i++] = '%'; 25.while (!isalpha(*(p+1)) && *(p+1) ) 26.localfmt[i++] = *++p; 27.localfmt[i++] = *(p+1); 28.localfmt[i] = '\0'; 29.switch(*++p) 30.{ 31.case'd': 32.case'i': 33.ival = va_arg(ap,int); 34.printf(localfmt,ival); 35.break; 36.case'x': 37.case'X': 38.case'u': 39.case'o': 40.uval = va_arg(ap,unsigned); 41.printf(localfmt,uval); 42.break; 43.case'f': 44.dval = va_arg(ap,double); 45.printf(localfmt,dval); 46.break; 47.case's': 48.sval = va_arg(ap,char *); 49.printf(localfmt,sval); 50.break; 51.default: 52.// printf(localfmt); 53.break; 54.} 55.} 56.va_end(ap); 57.} 58.void minscanf(char *fmt,...) 59.{ 60.va_list ap; 61.char *p,*sval; 62.char localfmt[LOCALFMT]; 63.int i,*ival; 64.unsigned *uval; 65.double *dval; 66.va_start(ap,fmt); 67.for(p=fmt;*p;p++) 68.{ 69.if(*p != '%') 70.{ 71.putchar(*p); 72.continue; 73.} 74.i = 0; 75.localfmt[i++] = '%'; 76.while (!isalpha(*(p+1)) && *(p+1) ) 77.localfmt[i++] = *++p; 78.localfmt[i++] = *(p+1); 79.localfmt[i] = '\0'; 80.switch(*++p) 81.{ 82.case'd': 83.case'i': 84.ival = va_arg(ap,int *); 85.scanf(localfmt,ival); 86.break; 87.case'x': 88.case'X': 89.case'u': 90.case'o': 91.uval = va_arg(ap,unsigned*); 92.scanf(localfmt,uval); 93.break; 94.case'f': 95.dval = va_arg(ap,double*); 96.scanf(localfmt,dval); 97.break; 98.case's': 99.sval = va_arg(ap,char *); 100.scanf(localfmt,sval); 101.break; 102.default: 103.// printf(localfmt); 104.break; 105.} 106.} 107.va_end(ap); 108.} 109.void main() 110.{ 111.int i =3; 112.double f = 2.015; 113.char s[100] = "what are you doing "; 114.minprintf(":%3d:\t%.2f:%s\n",i,f,s); 115.minscanf("%d%f%s",&i,&f,s); 116.minprintf(":%3d:\t%.2f:%s\n",i,f,s); 117.} 1./* 2.Tue Aug 10 04:36:20 CST 2010*/ 3.#include"stdio.h" 4.#include"ctype.h" 5.#include"stdlib.h" 6.#define NUMBER 0 7.#define MAXVAL 100 8.int sp=0; 9.double stack[MAXVAL]; 10.void push(double f) 11.{ 12.if(sp 13.stack[sp++] = f; 14.else 15.printf("error:stack is overflow!"); 16.} 17.double pop() 18.{ 19.if(sp >0) 20.return stack[--sp]; 21.else 22.{ 23.printf("error:stack empty"); 24.return 0.0; 25.} 26.} 27.int getop(char *s) 28.{ 29.char c ; 30.int rc; 31.float f; 32.while( (rc = scanf("%c",&c)) != EOF) 33.if((s[0] = c) != ' ' && c != '\t') 34.break; 35.s[1] = '\0'; 36.if(rc == EOF) 37.return EOF; 38.else if(!isdigit(c) && c != '.') 39.return c; 40.ungetc(c,stdin); 41.scanf("%f",&f); 42.sprintf(s,"%f",f); 43.return NUMBER; 44.} 45.#define MAXOP 100 46.void main() 47.{ 48.int type , i; 49.double op; 50.char s[MAXOP]; 51.// double variable[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.case NUMBER: 59.push(atof(s)); 60.break; 61./*case 1: 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; 74.case'/': 75.op=pop(); 76.if(op != 0) 77.push(pop()/op); 78.else 79.printf("error:the operater is wrong\n"); 80.break; 81.case'%': 82.op = pop(); 83.if(op != 0.0) 84.push( (int)pop()%(int)op); 85.else 86.printf("error:wrong operater\n"); 87.break; 88.case'\n': 89.printf("\t%.8g\n",pop()); 90.break; 91.default: 92.// if( type >= 'A' && type <= 'Z') 93.// push(variable[type - 'A']); 94.// else 95.printf("error:Unkown command\n"); 96.break; 97.} 98.// var = type; 99.} 100.} 1./* 2.Tue Aug 10 01:09:30 CST 2010*/ 3.#include"stdio.h" 4.#include"stdlib.h" 5.#include"string.h" 6.#define MAXLEN 1000 7.void main(int argc ,char * argv[]) 8.{ 9.FILE *fp1,*fp2; 10.char line1[MAXLEN],line2[MAXLEN]; 11.if((fp1=fopen(argv[1],"r")) == NULL) 12.{ 13.fprintf(stderr,"%s can't open\n",argv[1]); 14.exit(1); 15.} 16.if((fp2=fopen(argv[2],"r")) == NULL) 17.{ 18.fprintf(stderr,"%s can't open\n",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) 25.{ 26.printf("%s\n%s\n",line1,line2); 27.break; 28.} 29.} 30.fclose(fp1); 31.fclose(fp2); 32.} 1./* 2.Tue Aug 10 02:35:29 CST 2010*/ 3.#include"stdio.h" 4.#include"string.h" 5.#include"stdlib.h" 6.#define MAXLEN 100 7.#define MAXLINE 1000 8.void fmatch(FILE *fp,char *line,char *pattern,int number,int except); 9.void main(int argc ,char *argv[]) 10.{ 11.//char line[MAXLINE]; 12.// long lineno = 0; 13.int c, except = 0, number = 0; 14.char pattern[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: illegal option!"); 28.argc = 0; 29.break; 30.} 31.if(argc>=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.} 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++ 59.printf("\n"); 60.return nl; 61.} 1./* 2.Mon Aug 9 16:51:17 CST 2010*/ 3.#include"stdio.h" 4.#include"string.h" 5.#include"stdlib.h" 6.#include"ctype.h" 7.#define MAXLEN 100 8.#define BUFSIZE 100 9.struct nlist{ 10.int lnum;//the line of number 11.struct nlist *next; 12.}; 13.struct tnode{ 14.struct nlist *lines; 15.char *word; 16.struct tnode *left; 17.struct tnode *right; 18.}; 19.struct tnode* addtree( struct tnode *, char *,int ); 20.void treeprint(struct tnode *); 21.char * mystrdup(char *s); 22.void addin(struct tnode *,int); 23.int mygetword(char *,int ); 24.struct tnode* talloc(); 25.struct nlist* lalloc(); 26.int noiseword(char *); 27.void main() 28.{ 29.struct tnode *root = NULL; 30.int nlines = 1; 31.char word[MAXLEN]; 32.while(mygetword(word,MAXLEN) != EOF) 33.{ 34.if(isalpha(word[0]) && noiseword(word) == -1) 35.root = addtree(root,word,nlines); 36.if(word[0] =='\n') 37.nlines++; 38.} 39.treeprint(root); 40.} 41.struct tnode* addtree(struct tnode *p,char *w,int nlines) 42.{ 43.int conp; 44.if(p == NULL) 45.{ 46.p = talloc(); 47.p->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 <= high) 124.{ 125.mid = (low + high)/2; 126.if((cond =strcmp(w,word[mid]))>0) 127.low = mid +1; 128.else if ( cond <0) 129.high = mid -1; 130.else 131.return mid; 132.} 133.return -1; 134.} 135.char *mystrdup(char *s) 136.{ 137.char *p; 138.p=(char *) malloc(strlen(s)+1); 139.if(p != NULL) 140.strcpy(p,s); 141.return p; 142.} 143.int mygetword(char *word,int lim) 144.{ 145.int c,getch(); 146.void ungetch(int); 147.char *w = word; 148.while(isspace(c=getch()) && c != '\n') 149.; 150.if( c != EOF) 151.*w++ = c; 152.if( !isalpha(c)) 153.{ 154.*w = '\0'; 155.return c; 156.} 157.for(;--lim > 0;w++) 158.if(!isalnum(*w = getch())) 159.{ 160.ungetch(*w); 161.break; 162.} 163.*w = '\0'; 164.return word[0]; 165.} 166.int bufsp = 0; 167.char buf[BUFSIZE]; 168.int getch() 169.{ 170.return bufsp>0?buf[--bufsp]:getchar(); 171.} 172.void ungetch(int c) 173.{ 174.if(bufsp >=BUFSIZE) 175.printf("error:Buf is overfow\n"); 176.else 177.buf[bufsp++] = c; 178.} 1./* 2.Sun Aug 8 23:18:28 CST 2010*/ 3.#include"ctype.h" 4.#include"string.h" 5.#include"stdio.h" 6.#include"stdlib.h" 7.#define MAXWORD 500 8.#define MAXLEN 100 9.#define BUFSIZE 500 10.struct tnode { 11.char *word; 12.int count; 13.struct tnode *left; 14.struct tnode *right; 15.}; 16.struct tnode wnode[MAXWORD]; 17.struct tnode *addtree(struct tnode*,char*); 18.int mygetword(char *,int); 19.void treestor(struct tnode*); 20.void myqsort(struct tnode wnode[],int left,int right); 21.struct tnode *talloc(void); 22.char *mystrdup(char*); 23.int bufsp = 0; 24.char buf[BUFSIZE]; 25.void main() 26.{ 27.struct tnode *root; 28.char word[MAXLEN]; 29.int i,len=0; 30.root = NULL; 31.while (mygetword(word,MAXLEN) != EOF) 32.{ 33.if(isalpha(word[0])) 34.root = addtree(root,word); 35.len++; 36.} 37.treestor(root); 38.myqsort(wnode,0,len-1);