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

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

时间:2015-05-24 23:31:37      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

第五版c++ primer plus,IDE是visual studio2013。

p180.2

 1 #include<iostream>
 2 #include<ctime>
 3 #include<cstdlib>
 4 
 5 int main(void){
 6     using namespace std;
 7     int i, j, count=0, sum = 0, pt[10];
 8     double mean;
 9     for (i = 0; i < 10; i++){
10         if (!(cin >> pt[i]))
11             break;
12         sum += pt[i];
13     }
14     mean = sum / i;
15     for (j = 0; j <= i; j++)
16         if (pt[j]>mean)
17             count++;
18     cout << "mean is " << mean << endl;
19     cout << "there " << count << " exceed mean\n";
20 
21     int time=10;
22     clock_t delay = time* CLOCKS_PER_SEC;
23     clock_t start = clock();
24     while (clock() - start < delay)
25         continue;    
26     
27     return 0;
28 }

p181.7

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cctype>
 4 #include<string>
 5 using namespace std;
 6 
 7 int main(){
 8     string word;
 9     int vowel = 0, con = 0, other=0;
10     while (cin >> word && word != "q"){
11         if (isalpha(word[0]))
12             switch (word[0]){
13             case A:
14             case a:
15                 vowel++;
16                 continue;
17             case E:
18             case e:
19                 vowel++;
20                 continue;
21             case I:
22             case i:
23                 vowel++;
24                 continue;
25             case O:
26             case o:
27                 vowel++;
28                 continue;
29             case U:
30             case u:
31                 vowel++;
32                 continue;
33             default: con++;
34         }
35         else other++;
36     }
37 
38     cout << vowel << "words beginning with vowels\n";
39     cout << con << "words beginning with consonants\n";
40     cout << other << "others\n";
41 
42     system("pause");
43     return 0;
44 }

p181.8

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cctype>
 4 #include<string>
 5 #include<fstream>
 6 using namespace std;
 7 
 8 int main(){
 9     string name;
10     cout << "enter the file name\n";
11     getline(cin, name);
12     ifstream inFile;
13     inFile.open(name);
14     if (!inFile.is_open()){
15         cout << "can‘t open the file\n";
16         exit(EXIT_FAILURE);
17     }
18     char ch;
19     int count = 0;
20     inFile >> ch;
21     while (inFile.good()){
22         count++;
23         inFile >> ch;
24     }
25     if (inFile.eof())
26         cout << "reading completed successfully\n";
27     else if (inFile.fail())
28         cout << "reading terminated by data dismatch\n";
29     if (count == 0)
30         cout << "no data processed\n";
31     else
32         cout << "count is " << count << endl;
33     inFile.close();
34     system("pause");
35     return 0;
36 }

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 }

 

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

标签:

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

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