c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013。我只标注了题号,具体的题目找下书上对应内容吧。
p110.8
#include<iostream>
#include<string>
#include<cstring>
char* getname(char*); struct pissza {
char *pt;
int diameter;
int weight;
}; using namespace std;
int main(void){
pissza will;
getname(will.pt);
cout << "enter a diameter\n";
cin >> will.diameter;
cout << "enter a weight\n";
cin >> will.weight;
cout << will.pt << " has a pissza " << will.diameter
<< " m " << will.weight << " g\n";
cin.get();
cin.get();
return ;
} char* getname(char*pt){
char name[];
cout << "enter a name\n";
cin.getline(name, );
pt = new char[strlen(name) + ];
strcpy(pt, name);
return pt;
}
p180.2
#include<iostream>
#include<ctime>
#include<cstdlib> int main(void){
using namespace std;
int i, j, count=, sum = , pt[];
double mean;
for (i = ; i < ; i++){
if (!(cin >> pt[i]))
break;
sum += pt[i];
}
mean = sum / i;
for (j = ; j <= i; j++)
if (pt[j]>mean)
count++;
cout << "mean is " << mean << endl;
cout << "there " << count << " exceed mean\n"; int time=;
clock_t delay = time* CLOCKS_PER_SEC;
clock_t start = clock();
while (clock() - start < delay)
continue; return ;
}
p181.7
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<string>
using namespace std; int main(){
string word;
int vowel = , con = , other=;
while (cin >> word && word != "q"){
if (isalpha(word[]))
switch (word[]){
case 'A':
case 'a':
vowel++;
continue;
case 'E':
case 'e':
vowel++;
continue;
case 'I':
case 'i':
vowel++;
continue;
case 'O':
case 'o':
vowel++;
continue;
case 'U':
case 'u':
vowel++;
continue;
default: con++;
}
else other++;
} cout << vowel << "words beginning with vowels\n";
cout << con << "words beginning with consonants\n";
cout << other << "others\n"; system("pause");
return ;
}
p181.8
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<string>
#include<fstream>
using namespace std; int main(){
string name;
cout << "enter the file name\n";
getline(cin, name);
ifstream inFile;
inFile.open(name);
if (!inFile.is_open()){
cout << "can't open the file\n";
exit(EXIT_FAILURE);
}
char ch;
int count = ;
inFile >> ch;
while (inFile.good()){
count++;
inFile >> ch;
}
if (inFile.eof())
cout << "reading completed successfully\n";
else if (inFile.fail())
cout << "reading terminated by data dismatch\n";
if (count == )
cout << "no data processed\n";
else
cout << "count is " << count << endl;
inFile.close();
system("pause");
return ;
}