C primer plus--ch11答案

C primer plus--ch11答案

gaowanchen photo By gaowanchen Comment Permalink
 1  
 2 /*Programming Exercise11-3*/
 3 #include<stdio.h>
 4 #define SIZE 20
 5 char *getword(char *array,int len);
 6 main()
 7 {
 8     char word[SIZE];
 9     char *st;
10     st=getword(word,SIZE-1);
11     printf("%s\n",word);
12 }
13 char *getword(char *array,int len)
14 {
15     int c;
16     int i=0;
17     while((c=getchar())!=' '&&c!='\n'&&c!='\t'&&i< len)
18     {
19         *array++=c;
20         i++;
21     }
22     *array='\0';
23     return array;
24 }
25 /*1.数组已经通过参数传递,并且修改,所以打印还是用s作为printf的一个参数*/
26 /*2.对于字符数组,最重要的就是结束符。在需要时一定要手动添加*/
 1  
 2 /*Programming Exercise11-2*/
 3 #include<stdio.h>
 4 #define SIZE 20
 5 char *store(char *array,int len);
 6 main()
 7 {
 8     char s[SIZE];
 9     char *st;
10     st=store(s,SIZE-1);
11     printf("%s\n",s);
12 }
13 char *store(char *array,int len)
14 {
15     int c;
16     int i=0;
17     while((c=getchar())!=' '&&c!='\n'&&c!='\t'&&i< len)
18     {
19         *array++=c;
20         i++;
21     }
22     *array='\0';
23     return array;
24 }
 1  
 2 /*Programming Exercise11-3*/
 3 #include<stdio.h>
 4 #define SIZE 20
 5 char *getword(char *array,int len);
 6 main()
 7 {
 8     char word[SIZE];
 9     char *st;
10     st=getword(word,SIZE-1);
11     printf("%s\n",word);
12 }
13 char *getword(char *array,int len)
14 {
15     int c;
16     int i=0;
17     while((c=getchar())!=' '&&c!='\n'&&c!='\t'&&i< len)
18     {
19         *array++=c;
20         i++;
21     }
22     *array='\0';
23     return array;
24 }
 1  
 2 /*Programming Exercise11-4*/
 3 #include<stdio.h>
 4 #define SIZE 20
 5 char *search(char *array,int chr);
 6 main()
 7 {
 8     char s[SIZE];
 9     char *st;
10     char c;
11     gets(s);
12     while(1)
13     {
14         __fpurge(stdin);
15         printf("What char u wanna search?\n");
16         scanf("%c",&c);
17         st=search(s,c);
18         if(st==NULL)
19             printf("There is no %c in %s\n",c,s);
20         else
21             printf("%p\n",st);
22     }
23 }
24 char *search(char *array,int chr)
25 {
26     int i;
27     for(i=0;array[i]!='\0';i++)
28         if(array[i]==chr)
29             return array+i;
30     return NULL;
31 }
32 /*使用__fpurge(stdin)清空缓冲区的回车*/
 1  
 2 /*Programming Exercise11-5*/
 3 #include<stdio.h>
 4 int is_within(char c,char *str);
 5 main()
 6 {
 7     char s[100];
 8     char c;
 9     printf("Please input the string\n");
10     gets(s);
11     while(1)
12     {
13         printf("what char u wanna search?\n");
14         __fpurge(stdin);
15         c=getchar();
16         if(is_within(c,s))
17             printf("IN\n");
18         else
19             printf("NOT IN\n");
20     }
21 }
22 int is_within(char c,char *str)
23 {
24     int i;
25     while(*str!=c||*str!=EOF)
26         str++;
27     return *str;
28  
29 }
30 /*使用__fpurge(stdin)清空缓冲区的回车*/
 1  
 2 /*Programming Exercise11-6*/
 3 #include<stdio.h>
 4 char *Strncpy(char *s1,char *s2,int n);
 5 main()
 6 {
 7     char s1[100],s2[100];
 8     int n;
 9     gets(s2);
10     printf("Please input n:\n");
11     scanf("%d",&n);
12     Strncpy(s1,s2,n);
13     puts(s1);
14 }
15 char *Strncpy(char *s1,char *s2,int n)
16 {
17     int i=0;
18     for(i=0;i < n;i++)
19         s1[i]=s2[i];
20     s1[i]='\0';
21  
22 }
 1  
 2 /*Programming Exercise11-7*/
 3 #include<stdio.h>
 4 #include<string.h>
 5 char *string_in(const char *s1,const char *s2);
 6 main()
 7 {
 8     char *ptr;
 9     ptr=string_in("transportation","transportation");
10     if(ptr)
11         puts(ptr);
12     else
13         puts("Not found");
14 }
15 char *string_in(const char *s1,const char *s2)
16 {
17     int len1,len2;
18     int no=1;
19     int times;
20     len2=strlen(s2);
21     len1=strlen(s1);
22     times=len1-len2+1;
23     printf("%d %d\n",len1,len2);
24     if(times>0)
25         while(no=strncmp(s1,s2,len2)&&times--)
26             s1++;
27     if(no)
28         return NULL;
29     else
30         return (char *) s1;
31 }
32 /*1.比较的次数自然要加1,否则当s1就是s2的时候会出现找不到*/
33 /*2.对s1进行自增运算,strncmp中的s1也就发生了变化*/
 1  
 2 /*Programming Exercise11-8*/
 3 #include<stdio.h>
 4 #include<string.h>
 5 void rev_string(char *s);
 6 main()
 7 {
 8     char a[100];
 9     gets(a);
10     rev_string(a);
11     puts(a);
12 }
13 void rev_string(char *s)
14 {
15     int i=0;
16     int len,temp;
17     len=strlen(s);
18     for(i=0;i < len/2;i++)
19     {
20         temp=s[i];
21         s[i]=s[len-1-i];
22         s[len-1-i]=temp;
23     }
24 }
25 /*1.不需要使用两个数组,一个数组就可以,也不需要指针*/
26 /*2.注意i<len/2,因为只需要把首尾对称的元素调换即可*/
 1  
 2 /*Programming Exercise11-9*/
 3 #include<stdio.h>
 4 #include<string.h>
 5 void no_space(char *s);
 6 main()
 7 {
 8     char a[100];
 9     gets(a);
10     no_space(a);
11     //puts(a);
12 }
13 void no_space(char *s)
14 {
15     int i;
16     int len;
17     char *pos;
18     char *start=s;
19     while(*s)
20     {
21         if(*s==' ')
22         {
23             pos=s;
24             do
25             {
26                 *pos=*(pos+1);
27                 pos++;
28             }while(*pos);
29             //printf("移动后结果:%s\n",start);
30         }
31         else
32             s++;
33     }
34 }
35 /*1.当有空格的时候,需要用后面的字符串覆盖前面字符串*/
36 /*2.当遇到空格时候,用新的指针pos指向当前位置,然后不断用后面字符覆盖前面*/
37 /*3.while(*pos)表明了,当遇到空格,后面覆盖前面是到字符串最终结尾为止的*/
38 /*4.相当于空格后面的字符串整体前移*/
  1  
  2 /*Programming Exercise11-10*/
  3 #include<stdio.h>
  4 #include<string.h>
  5 #define MAXLINES 10
  6 #define MAXLEN 100
  7 #define ALLOCSIZE 1000
  8 int Readlines(char *lineptr[],int nlines);
  9 void Writelines(char *lineptr[],int nlines);
 10 void qsort(void *v[],int left,int right,int (*comp)(void *,void *));
 11 void as_order(char *lineptr[],int lines);
 12 void s2l_order(char *lineptr[],int lines);
 13 void word_order(char *lineptr[],int lines);
 14 main()
 15 {
 16     char *list[MAXLINES];
 17     int flag=1;
 18     int n,lines;
 19     printf("----------------------------------------------\n\n");
 20     printf(" * Please input 10 strings\n");
 21     while((lines=Readlines(list,MAXLINES))<=0)//括号要有,赋值优先级低
 22         printf("Please input 10 strings\n");
 23     printf("\n* Input succeed!\n");
 24     printf("* U have input %d lines\n",lines);
 25     printf("\n----------------------------------------------\n");
 26     printf("1.按初始顺序输出\t2.按ASCII顺序输出\n");
 27     printf("3.按长度递增顺序输出\t4.按首单词长度顺序输出\n");
 28     printf("q.退出\n");
 29     printf("----------------------------------------------\n");
 30     __fpurge(stdin);
 31     printf("PLease chose ur function\n");
 32     flag=scanf("%d",&n);
 33     while(flag==1)
 34     {
 35         switch(n)
 36         {
 37             case 1:
 38                 printf("按初始顺序输出:\n");
 39                 Writelines(list,lines);
 40                 break;
 41             case 2:
 42                 printf("按ASCII顺序输出:\n");
 43                 as_order(list,lines);
 44                 break;
 45             case 3:
 46                 printf("按长度递增顺序输出:\n");
 47                 s2l_order(list,lines);
 48                 break;
 49             case 4:
 50                 printf("按首单词长度顺序输出:\n");
 51                 word_order(list,lines);
 52                 break;
 53             default:
 54                 printf("\nError:You should choose from the table!\n");
 55                 break;
 56         }
 57         printf("----------------------------------------------\n");
 58         printf("PLease chose ur function\n");
 59         flag=scanf("%d",&n);
 60     }
 61 }
 62  
 63 int Getline(char line[],int maxline);//读入字符串函数
 64 char *alloc(int);//储存空间分配函数
 65 int Readlines(char *lineptr[],int maxlines)
 66 {
 67     int len;
 68     int nlines;
 69     char *p;//永久储存空间的字符串指针
 70     char line[MAXLEN];
 71     nlines=0;
 72     while((len=Getline(line,MAXLEN))>0)
 73         if(nlines>=maxlines||(p=alloc(len))==NULL)
 74             return -1;//不做小于10行的判断,只规定不能超过10行
 75         else
 76         {
 77             line[len-1]='\0';   //删除换行符
 78             strcpy(p,line);     //把临时空间line中字符串复制到永久空间
 79             lineptr[nlines++]=p;//储存指针地址
 80         }
 81     return nlines;//允许小于10行的任意行输出
 82  
 83 }
 84 int Getline(char s[],int lim)
 85 {
 86     int c,i;
 87     for(i=0;i<(lim-1)&&(c=getchar())!=EOF&&c!='\n';i++)
 88         s[i]=c;
 89     if(c=='\n')
 90     {
 91         s[i]=c;
 92         ++i;
 93     }
 94     s[i]='\0';
 95     return i;
 96 }
 97 void Writelines(char *lineptr[],int nlines)
 98 {
 99     while(nlines-->0)
100     {
101         printf("%s\n",*lineptr++);
102     }
103  
104 }
105 static char allocbuf[ALLOCSIZE];//永久储存空间
106 static char *allocp=allocbuf;
107 char *alloc(int n)
108 {
109     if(allocbuf+ALLOCSIZE-allocp>=n)
110     {
111         allocp+=n;
112         return allocp-n;
113     }
114     else
115         return 0;
116 }
117 void qsort(void *v[],int left,int right,int (*comp)(void *,void *))
118 {
119     int i,last;
120     void swap(void *v[],int i,int j);
121     if(left>=right)
122         return ;
123     swap(v,left,(left+right)/2);
124     last=left;
125     for(i=left+1;i<=right;i++)
126         if((*comp)(v[i],v[left])<0)
127             swap(v,++last,i);
128     swap(v,left,last);
129     qsort(v,left,last-1,comp);
130     qsort(v,last+1,right,comp);
131 }
132 int lencmp(char v1[],char v2[])
133 {
134     int len1,len2;
135     len1=strlen(v1);
136     len2=strlen(v2);
137     if(len1>len2)
138         return 1;
139     else if(len1< len2)
140         return -1;
141     else
142         return 0;
143 }
144 int wordcmp(char v1[],char v2[])
145 {
146     int i,j;
147     i=j=0;
148     while(v1[i]!=' ')
149         i++;
150     while(v2[j]!=' ')
151         j++;
152     if(i>j)
153         return 1;
154     else if(i< j)
155         return -1;
156     else
157         return 0;
158 }
159 void swap(void *v[],int i,int j)
160 {
161     void *temp;
162     temp=v[i];
163     v[i]=v[j];
164     v[j]=temp;
165 }
166 void s2l_order(char *lineptr[],int lines)
167 {
168     qsort((void **)lineptr,0,lines-1,(int (*)(void *,void *))(lencmp));
169     Writelines(lineptr,lines);
170 }
171 void word_order(char *lineptr[],int lines)
172 {
173     qsort((void **)lineptr,0,lines-1,(int (*)(void *,void *))(wordcmp));
174     Writelines(lineptr,lines);
175 }
176 void as_order(char *lineptr[],int lines)
177 {
178     qsort((void **)lineptr,0,lines-1,(int (*)(void *,void *))(strcmp));
179     Writelines(lineptr,lines);
180 }
  1  
  2 /*Programming Exercise11-11*/
  3 #include<stdio.h>
  4 #include<ctype.h>
  5 int word_count(char *s);
  6 int upper_count(char *s);
  7 int lower_count(char *s);
  8 int punct_count(char *s);
  9 int digit_count(char *s);
 10 int isword(char *s);
 11 #define SIZE 100
 12 main()
 13 {
 14     int c;
 15     char s[SIZE];
 16     int i=0;
 17     int nword,nupper,nlower,npunct,ndigit;
 18     nword=nupper=nlower=npunct=ndigit=0;
 19     printf("Please input a string\n");
 20     while((c=getchar())!=EOF&&c!='\n')
 21     {
 22         s[i]=c;
 23         i++;
 24     }
 25     s[i]='\0';
 26     printf("输入的字符串:%s\n",s);
 27     nword=word_count(s);
 28     nupper=upper_count(s);
 29     nlower=lower_count(s);
 30     npunct=punct_count(s);
 31     ndigit=digit_count(s);
 32     printf("words:%d\n",nword);
 33     printf("upper-case:%d\n",nupper);
 34     printf("lower-case:%d\n",nlower);
 35     printf("punctuation:%d\n",npunct);
 36     printf("digit:%d\n",ndigit);
 37 }
 38 #define OUT 0
 39 #define IN 1
 40 int word_count(char *s)
 41 {
 42     int i=0;
 43     int count=0;
 44     int state=OUT;
 45     while(s[i]!='\0'&&s[i]==' ')
 46         i++;
 47     while(s[i]!='\0')
 48     {
 49         if((ispunct(s[i])&&s[i]!='-')||isblank(s[i])||s[i]=='\n')
 50             state=OUT;
 51         else if(state==OUT&&!isdigit(s[i]))
 52         {
 53             state=IN;
 54             count++;
 55         }
 56         i++;
 57     }
 58     return count;
 59 }
 60 int upper_count(char *s)
 61 {
 62     int i=0;
 63     int count=0;
 64     while(s[i]!='\0')
 65     {
 66         if(isupper(s[i]))
 67             count++;
 68         i++;
 69     }
 70     return count;
 71 }
 72 int lower_count(char *s)
 73 {
 74     int i=0;
 75     int count=0;
 76     while(s[i]!='\0')
 77     {
 78         if(islower(s[i]))
 79             count++;
 80         i++;
 81     }
 82     return count;
 83 }
 84 int punct_count(char *s)
 85 {
 86     int i=0;
 87     int count=0;
 88     while(s[i]!='\0')
 89     {
 90         if(ispunct(s[i]))
 91             count++;
 92         i++;
 93     }
 94     return count;
 95 }
 96 int digit_count(char *s)
 97 {
 98     int i=0;
 99     int count=0;
100     while(s[i]!='\0')
101     {
102         if(isdigit(s[i]))
103             count++;
104         i++;
105     }
106     return count;
107 }
 1  
 2 /*Programming Exercise11-12*/
 3 #include<stdio.h>
 4 int main(int argc,char *argv[])
 5 {
 6     int i=argc;
 7     while(i-->1)
 8         argv++;//首先把指针移动到倒数第二个位置
 9     while(argc-->1)
10         printf("%s ",*argv--);//逆序打印但是不能打印argv[0]
11     printf("\n");
12 }
 1  
 2 /*Programming Exercise11-13*/
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 int main(int argc,char *argv[])
 6 {
 7     double base,power,temp=1,result=1;//必须有初值
 8     base=atof(argv[1]);
 9     power=atof(argv[2]);
10     if(power==0)
11         result=1;
12     else if(power<0)
13     {
14         power=-power;//为了递减,需要变成正数
15         while(power-->0)
16             temp*=base;
17         result=1/temp;
18     }
19     else
20         while(power-->0)
21             result*=base;
22     printf("%.3f^%.3f=%.3f\n",base,power,result);
23 }
 1  
 2 /*Programming Exercise11-14*/
 3 #include<stdio.h>
 4 #include<ctype.h>
 5 #define SIZE 100
 6 int myatoi(char *s);
 7 main()
 8 {
 9     int num,c,i=0;
10     char s[SIZE];
11     printf("Please input a string\n");
12     while((c=getchar())!='\n')
13     {
14         s[i]=c;
15         i++;
16     }
17     s[i]='\0';
18     printf("the int number: \n");
19     if(num=myatoi(s))
20         printf("%d\n",num);
21     else
22         printf("the string is not a int number!\n");
23 }
24 int myatoi(char *s)
25 {
26     int sign=1;
27     int i=0;
28     int n=0;
29     if(s[i]=='-')
30     {
31         sign=-1;
32         i++;//只有当s[0]是负号的时候才跳过
33     }
34     else
35         sign=1;//否则仍让从s[0]开始
36     while(s[i]!='\0')
37     {
38         if(isdigit(s[i]))
39         {
40             n=n*10+s[i]-'0';
41         }
42         else
43             return 0;
44         i++;
45     }
46     return n*sign;
47 }
 1  
 2 /*Programming Exercise11-15*/
 3 #include<stdio.h>
 4 #include<ctype.h>
 5 #define SIZE 1000
 6 int main(int argc,char *argv[])
 7 {
 8     int c,i=0;
 9     char s[SIZE];
10     char *p;
11     while((c=getchar())!=EOF)
12     {
13         s[i]=c;
14         i++;
15     }
16     s[i]='\0';
17     p=s;
18     //printf("%c %c\n",*argv[1],*(argv[1]+1));
19     if(argc>1&&*argv[1]=='-')
20     {
21         if(*(argv[1]+1)=='p')
22         {
23             printf("按照原样输出:\n");
24             while(*p!='\0')
25                 putchar(*p++); 
26         }
27         else if(*(argv[1]+1)=='u')
28         {
29             printf("全部转换为大写字母:\n");
30             while(*p!='\0')
31                 putchar(toupper(*p++));
32         }
33         else if(*(argv[1]+1)=='l')
34         {
35             printf("全部转换为大写字母:\n");
36             while(*p!='\0')
37                 putchar(tolower(*p++));
38         }
39         else
40             printf("Only -p,-l,-u please!\n");
41     }
42     else
43         printf("请输入命令行参数-p,-l,-u\n");
44 }