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

c++ primer plus 习题答案(2)

时间:2015-05-26 18:20:17      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

p221.8

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstring>
 4 using namespace std;
 5 const int SLEN = 30;
 6 struct student{
 7     char fullname[SLEN];
 8     char hobby[SLEN];
 9     int ooplevel;
10 };
11 int getinfo(student pa[], int n);
12 void display1(student st);
13 void display2(const student *ps);
14 void display3(const student pa[], int n);
15 
16 int main(){
17     cout << "enter class size:";
18     int class_size;
19     cin >> class_size;
20     while (cin.get() != \n)
21         continue;
22     student *ptr_stu = new student[class_size];
23     int entered = getinfo(ptr_stu, class_size);
24     for (int i = 0; i < entered; i++){
25         display1(ptr_stu[i]);
26         display2(&ptr_stu[i]);
27     }
28     display3(ptr_stu, entered);
29     delete[]ptr_stu;
30     cout << "done\n";
31     system("pause");
32     return 0;
33 }
34 
35 int getinfo(student pa[], int n){
36     int i = 0, count = 0;
37     while (i < n && *pa[i].fullname != \0){
38         cin.getline(pa[i].fullname, SLEN);
39         cin.getline(pa[i].hobby, SLEN);
40         cin >> pa[i].ooplevel;
41         cin.get();
42         count++;
43         i++;
44     }
45     return count;
46 }
47 
48 void display1(student st){
49     cout << "st.fullname" << st.fullname <<
50         endl << "st.hobby" << st.hobby <<
51         endl << "st.ooplevel" << st.ooplevel <<
52         endl;
53 }
54 
55 void display2(const student *ps){
56     cout << "ps->fullname" << ps->fullname <<
57         endl << "ps->hobby" << ps->hobby <<
58         endl << "ps->ooplevel" << ps->ooplevel <<
59         endl;
60 }
61 
62 void display3(const student pa[], int n){
63     for (int i = 0; i < n; i++){
64         cout << "pa[i].fullname" << pa[i].fullname
65             << endl << "pa[i].hobby" << pa[i].hobby
66             << endl << "pa[i].ooplevel" << pa[i].ooplevel
67             << endl;
68     }
69 }

p259.2

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstring>
 4 using namespace std;
 5 struct candybar {
 6     char brand[81];
 7     float weight;
 8     int calory;
 9 };
10 void assign(candybar &, char br[81]="millennium munch", float w=2.85, int c=350);
11 void show_menu(candybar&);
12 
13 int main(){
14     candybar tasty;
15     assign(tasty);
16     show_menu(tasty);
17     system("pause");
18     return 0;
19 }
20 
21 void assign(candybar &delicious, char br[81], float w , int c){
22     strcpy(delicious.brand, br);
23     delicious.weight = w;
24     delicious.calory = c;
25 }
26 
27 void show_menu(candybar&delicious){
28     cout << delicious.brand << endl;
29     cout << delicious.weight << endl;
30     cout << delicious.calory << endl;
31 }

p259.4

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cctype>
 4 using namespace std;
 5 struct stringy{
 6     char* str;
 7     int ct;
 8 };
 9 void set(stringy &, const char*);
10 void show(const stringy &, int times = 1);
11 void show(const char*, int times = 1);
12 
13 int main(){
14     stringy beany;
15     char testing[] = "reality isn‘t what it used to be.";
16     set(beany, testing);
17     show(beany);
18     show(beany, 2);
19     delete beany.str;
20     testing[0] = D;
21     testing[1] = u;
22     show(testing);
23     show(testing, 3);
24     show("Done!");
25     system("pause");
26     return 0;
27 }
28 
29 void set(stringy &beany, const char* testing){
30     int num = strlen(testing);
31     beany.str = new char[num + 1];
32     strcpy(beany.str, testing);
33     beany.ct = num;
34 }
35 
36 void show(const stringy &beany, int times){
37     for (int i = 0; i < times; i++)
38         cout << beany.str << endl
39             << beany.ct << endl;
40 }
41 
42 void show(const char* testing, int times){
43     for (int i = 0; i < times; i++)
44         cout << testing << endl;
45 }

p260.6

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstring>
 4 using namespace std;
 5 template<class T>
 6 T maxn(T ar[], int num);
 7 template<> char *maxn(char *pt[], int num);
 8 
 9 int main(){
10     int num1[6];
11     double num2[4];
12     cout << "enter the member of two array\n";
13     for (int i = 0; i < 6; i++)
14         cin >> num1[i];
15     for (int i = 0; i < 4; i++)
16         cin >> num2[i];
17     int imax = maxn(num1, 6);
18     double dmax = maxn(num2, 4);
19     cout << "imax is" << imax << endl
20         << "dmax is" << dmax << endl;
21     char *str[5];
22     str[0] = "his eyes came round";
23     str[1] = "it all seems very unchanged";
24     str[2] = "which contain of old the pipes";
25     str[3] = "yes, billy, i know";
26     str[4] = "will you be please to dine";
27     char *address = maxn(str, 5);
28     cout << (int*)address << " " <<
29         address << endl;
30     system("pause");
31     return 0;
32 }
33 
34 template<class T>
35 T maxn(T ar[], int num){
36     T max=ar[0];
37     for (int i = 1; i < num; i++)
38         if (max < ar[i])
39             max = ar[i];
40     return max;
41 }
42 
43 template<> char *maxn(char *pt[], int num){
44     int length[5], i;
45     for (i = 0; i < 5; i++)
46         length[i] = strlen(pt[i]);
47     int max = length[0];
48     for (i = 0; i < 5; i++)
49         if (max < length[i])
50             max = length[i];
51     for (i = 0; i < 5; i++)
52         if (max == length[i])
53             break;
54     return pt[i];            
55 }

c++ primer plus 习题答案(2)

标签:

原文地址:http://www.cnblogs.com/coding-time/p/4530824.html

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