码迷,mamicode.com
首页 > 编程语言 > 详细

C++程序设计原理与实践 第二十七章部分答案

时间:2014-11-11 00:43:25      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   使用   

bubuko.com,布布扣
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<assert.h>
  4 
  5 
  6 struct List
  7 {
  8     struct Link*first;
  9     struct Link*last;
 10 };
 11 
 12 struct Link
 13 {
 14     struct Link*pre;
 15     struct Link*suc;
 16 };
 17 
 18 void init(struct List*lst)
 19 {
 20     assert(lst);
 21     lst->first=lst->last=0;
 22 }
 23 
 24 struct List* create()
 25 {
 26     struct List* lst=(struct List*)malloc(sizeof(struct List*));
 27     init(lst);
 28     return lst;
 29 }
 30 
 31 void clear(struct List* lst)
 32 {
 33     assert(lst);
 34     {
 35         struct Link*curr=lst->first;
 36         while(curr){
 37             struct Link* next=curr->suc;
 38             free(curr);
 39             curr=next;
 40         }
 41     
 42         lst->first=lst->last=0;
 43     }
 44 }
 45 
 46 void destory(struct List*lst)
 47 {
 48     assert(lst);
 49     clear(lst);
 50     free(lst);
 51 }
 52 
 53 void push_back(struct List*lst,struct Link*p)
 54 {
 55     assert(lst);
 56     {
 57         struct Link* last=lst->last;
 58         if(last){
 59             last->suc=p;
 60             p->pre=last;
 61         }
 62         else
 63         {
 64             lst->first=p;
 65             p->pre=0;
 66         }
 67         lst->last=p;
 68         p->suc=0;
 69     }
 70 }
 71 
 72 void push_front(struct List*lst,struct Link*p)
 73 {
 74     assert(lst);
 75     {
 76         struct Link* first=lst->first;
 77         if(first){
 78             first->pre=p;
 79             p->suc=first;
 80         }
 81         else
 82         {
 83             lst->last=p;
 84             p->suc=0;
 85         }
 86         lst->first=p;
 87         p->pre=0;
 88     }
 89 }
 90 
 91 void insert(struct List*lst,struct Link*p,struct Link*q)
 92 {
 93     assert(lst);
 94     if(q==0)
 95         return;
 96     struct Link* x=lst->first;
 97     while(x->suc!=0&&x!=p)
 98     {
 99         x=x->suc;
100     }
101     if(x==p)
102     {
103         if(x==lst->first)
104         {
105             push_front(lst,q);
106         }
107         else
108         {
109             p->pre->suc=q;
110             q->pre=q->pre;
111             q->suc=p;
112             p->pre=q;
113         }
114     }
115     else
116     {
117         printf("no the Link\n");
118     }
119 }
120 
121 struct Link*erase(struct List*lst,struct Link*p)
122 {
123     assert(lst);
124     if(p==0)
125         return 0;
126     if(p==lst->first)
127     {
128         if(p->suc)
129         {
130             lst->first=p->suc;
131             p->suc->pre=0;
132             return p->suc;
133         }
134         else
135         {
136             lst->first=lst->last=0;
137             return 0;
138         }
139     }
140     else if(p==lst->last)
141     {
142         if(p->pre)
143         {
144             lst->last=p->pre;
145             p->pre->suc=0;
146         }
147         else
148         {
149             lst->first=lst->last=0;
150             return 0;
151         }
152     }
153     else
154     {
155         p->suc->pre=p->pre;
156         p->pre->suc=p->suc;
157         return p->suc;
158     }
159 }
160 
161 struct Link* advance(struct Link*p,int n)
162 
163 {
164     while(n>0)
165     {
166         p=p->suc;
167         --n;
168     }
169     return p;
170 }
习题2
bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<assert.h>
 4 #include<string.h>
 5 
 6 int Comp(const void*p1,const void*p2)
 7 {
 8     return strcmp((char*)p2,(char*)p1);
 9 }
10 
11 int main()
12 {
13     
14     int i=0;
15     char c[5][10];
16     for(i=0;i<5;i++)
17         scanf("%s",c[i]);
18     printf("\n\n");
19     for(i=0;i<5;i++)
20         printf("%s\n",c[i]);
21     printf("\n\n");
22     qsort(c,5,sizeof(c[0]),Comp);
23     for(i=0;i<5;i++)
24         printf("%s\n",c[i]);
25     while(1);
26     return 0;
27 
28 }
习题9(1)
bubuko.com,布布扣
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<assert.h>
  4 #include<string.h>
  5 
  6 
  7 struct List
  8 {
  9     struct Link*first;
 10     struct Link*last;
 11 };
 12 
 13 struct Link
 14 {
 15     struct Link*pre;
 16     struct Link*suc;
 17     char* c;
 18 };
 19 
 20 void init(struct List*lst)
 21 {
 22     assert(lst);
 23     lst->first=lst->last=0;
 24 }
 25 
 26 struct List* create()
 27 {
 28     struct List* lst=(struct List*)malloc(sizeof(struct List*));
 29     init(lst);
 30     return lst;
 31 }
 32 
 33 void clear(struct List* lst)
 34 {
 35     assert(lst);
 36     {
 37         struct Link*curr=lst->first;
 38         while(curr){
 39             struct Link* next=curr->suc;
 40             free(curr);
 41             curr=next;
 42         }
 43     
 44         lst->first=lst->last=0;
 45     }
 46 }
 47 
 48 void destory(struct List*lst)
 49 {
 50     assert(lst);
 51     clear(lst);
 52     free(lst);
 53 }
 54 
 55 void push_back(struct List*lst,struct Link*p)
 56 {
 57     assert(lst);
 58     {
 59         struct Link* last=lst->last;
 60         if(last){
 61             last->suc=p;
 62             p->pre=last;
 63         }
 64         else
 65         {
 66             lst->first=p;
 67             p->pre=0;
 68         }
 69         lst->last=p;
 70         p->suc=0;
 71     }
 72 }
 73 
 74 void push_front(struct List*lst,struct Link*p)
 75 {
 76     assert(lst);
 77     {
 78         struct Link* first=lst->first;
 79         if(first){
 80             first->pre=p;
 81             p->suc=first;
 82         }
 83         else
 84         {
 85             lst->last=p;
 86             p->suc=0;
 87         }
 88         lst->first=p;
 89         p->pre=0;
 90     }
 91 }
 92 
 93 void insert(struct List*lst,struct Link*p,struct Link*q)
 94 {
 95     assert(lst);
 96     if(q==0)
 97         return;
 98     struct Link* x=lst->first;
 99     while(x->suc!=0&&x!=p)
100     {
101         x=x->suc;
102     }
103     if(x==p)
104     {
105         if(x==lst->first)
106         {
107             push_front(lst,q);
108         }
109         else
110         {
111             p->pre->suc=q;
112             q->pre=q->pre;
113             q->suc=p;
114             p->pre=q;
115         }
116     }
117     else
118     {
119         printf("no the Link\n");
120     }
121 }
122 
123 struct Link*erase(struct List*lst,struct Link*p)
124 {
125     assert(lst);
126     if(p==0)
127         return 0;
128     if(p==lst->first)
129     {
130         if(p->suc)
131         {
132             lst->first=p->suc;
133             p->suc->pre=0;
134             return p->suc;
135         }
136         else
137         {
138             lst->first=lst->last=0;
139             return 0;
140         }
141     }
142     else if(p==lst->last)
143     {
144         if(p->pre)
145         {
146             lst->last=p->pre;
147             p->pre->suc=0;
148         }
149         else
150         {
151             lst->first=lst->last=0;
152             return 0;
153         }
154     }
155     else
156     {
157         p->suc->pre=p->pre;
158         p->pre->suc=p->suc;
159         return p->suc;
160     }
161 }
162 
163 struct Link* advance(struct Link*p,int n)
164 
165 {
166     while(n>0)
167     {
168         p=p->suc;
169         --n;
170     }
171     return p;
172 }
173 
174 
175 
176 struct Link* make_string(char*n)
177 {
178     struct Link*p=(struct Link*)malloc(sizeof(struct Link));
179     p->c=n;
180     return p;
181 }
182 
183 void fn(struct List*lst,struct Link*p)
184 {
185     struct Link*q=lst->first;
186     if(q==0)
187         push_back(lst,p);
188     else
189     {
190         while(q!=0 )
191         {
192             if(strcmp(q->c,p->c)>0)
193             {
194                 insert(lst,q,p);
195                 return ;
196             }
197             q=q->suc;
198         }
199         if(q==0)
200             push_back(lst,p);
201     }
202 }
203 
204 int main()
205 {
206 
207     struct List* strings=create();
208     /*int i=0;
209     for(;i<2;i++)
210     {
211         char* c=(char*)malloc(10);
212         scanf("%s",c);
213         struct Link*p=make_string(c);
214         push_back(strings,p);
215     }
216     struct Link*q=strings->first;
217     for(;q!=0;q=q->suc)
218     {
219         printf("%s\n",q->c);
220     }*/
221     int i=2;
222     for(i=0;i<4;i++)
223     {
224         char* c=(char*)malloc(10);
225         scanf("%s",c);
226         struct Link*p=make_string(c);
227         fn(strings,p);
228     }
229     struct Link*q=strings->first;
230     for(;q!=0;q=q->suc)
231     {
232         printf("%s\n",q->c);
233     }
234 
235 
236 
237     while(1);
238     return 0;
239 
240 }
习题9(2)
bubuko.com,布布扣
 1 //只实现了push_back
 2 
 3 
 4 #include<stdio.h>
 5 #include<stdlib.h>
 6 #include<assert.h>
 7 #include<string.h>
 8 
 9 
10 struct List
11 {
12     int first;
13     int last;
14 };
15 
16 struct Link
17 {
18     int pre;
19     int suc;
20     char* c;
21 };
22 
23 void push_back(struct List*l,struct Link* link,int i)
24 {
25     //struct Link last=link[l->last];
26     if(l->last!=-1)
27     {
28         link[l->last].suc=i;
29         link[i].pre=l->last;
30     }
31     else
32     {
33         l->first=i;
34         link[i].pre=-1;
35     }
36     link[i].suc=-1;
37     l->last=i;
38 }
39 
40 int main()
41 {
42     struct List l;
43     l.first=l.last=-1;
44     struct Link* link=(struct Link*)malloc(100*sizeof(struct Link));
45     int i=0;
46     for(i=0;i<5;i+=2)
47     {
48         char* c=(char*)malloc(10);
49         scanf("%s",c);
50         link[i].c=c;
51         push_back(&l,link,i);
52     }
53 
54     i=l.first;
55     while(i!=-1)
56     {
57         printf("%s\n",link[i].c);
58         i=link[i].suc;
59     }
60 
61 
62 
63     while(1);
64     return 0;
65 
66 }
习题6
bubuko.com,布布扣
 1 struct string
 2 {
 3     char c[100];
 4 };
 5 
 6 void inin(string* s)
 7 {
 8     char c;
 9     int i=0;
10     while(isspace((c=getchar())))
11         s->c[i]=c;
12     s->c[i]=0;
13 }
习题13
bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<ctype.h>
 5 
 6 #ifndef C_Class 
 7 #define C_Class struct 
 8 #endif 
 9 
10 C_Class A { 
11     C_Class A *A_this; 
12     void (*Foo)(C_Class A *A_this); 
13 
14 }; 
15 
16 C_Class B{ //B继承了A 
17     C_Class B *B_this; //顺序很重要 
18     void (*Foo)(C_Class B *Bthis); //虚函数 
19 
20 }; 
21 
22 
23 void B_F2(C_Class B *Bthis) 
24 { 
25     printf("It is B_Fun\n "); 
26 } 
27 
28 void A_Foo(C_Class A *Athis) 
29 { 
30     printf("It is A\n ");//或者这里 
31     // exit(1); 
32     // printf("纯虚 不允许执行 ");//或者这里 
33 } 
34 
35 void B_Foo(C_Class B *Bthis) 
36 { 
37     printf("It is B\n "); 
38 } 
39 
40 void A_Creat(struct A* p) 
41 { 
42     p->Foo=A_Foo; 
43     p->A_this=p; 
44 } 
45 
46 
47 void B_Creat(struct B* p) 
48 { 
49     p->Foo=B_Foo; 
50     p->B_this=p; 
51 } 
52 
53 
54 int main(int argc, char* argv[]) 
55 { 
56     C_Class A *ma,a; 
57     C_Class B *mb,b; 
58 
59     A_Creat(&a);//实例化 
60     B_Creat(&b); 
61 
62     mb=&b; 
63     ma=&a; 
64     ma->Foo(ma);//A
65     mb->Foo(mb);//B
66     a.Foo(ma);//A
67     b.Foo(mb);//B
68 
69     ma=(C_Class A*)mb;//引入多态指针 
70 
71     ma->Foo(ma);//多态   //B
72 
73     ma->Foo(ma);//B
74     mb->Foo(mb);//B
75     a.Foo(ma);//A
76     b.Foo(mb);//B
77     //a.Foo(&a);//不是多态了 
78     B_F2(&b);//成员函数,因为效率问题不使用函数指针 
79     while(1);
80     return 0; 
81 } 
习题15 16

 

C++程序设计原理与实践 第二十七章部分答案

标签:des   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/yueba/p/4088540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!