C++ Primer Plus 第六版(第六章)的课后习题答案,在main函数中把题号换掉就可以跑不同的程序,第九题的用例在附件中:
#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
using namespace std;
void p1()
{
char ch;
while( ch = cin.get())
{
if (ch == '@')
break;
else if (isdigit(ch))
continue;
else if(islower(ch))
ch = toupper(ch);
else if(isupper(ch))
ch = tolower(ch);
cout << ch;
}
}
bool is_double(string x)
{
double y;
try{
y = stod(x);
}
catch (const std::exception&){
return false;
}
return true;
}
void p2()
{
double donation,sum,average;
double Donation[10];
int size,above = 0;
string buf;
cout << "Please input the donation:"<< endl;
getline(cin, buf);
while(is_double(buf) && size<10)
{
donation = stod(buf);
Donation[size++] = donation;
sum += donation;
getline(cin,buf);
}
average = sum/size;
cout << "The average is " << average <<endl;
for(int i=0; i<=size; ++i)
{
if(Donation[i]>average)
above++;
}
cout << "There are " << above << " numbers larger than average."<<endl;
}
void p3()
{
char ch;
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
while(true)
{
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
if(ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
{
continue;
}
cout << "A maple is a";
switch(ch)
{
case 'c':
cout << "carnivore.";
break;
case 'p':
cout << "pianist.";
break;
case 't':
cout << "tree.";
break;
case 'g':
cout << "game.";
break;
}
break;
}
}
void p4()
{
const int strSize = 50;
struct bop
{
char fullname[strSize]; //real name
char title[strSize]; //job title
char bopname[strSize]; //secret BOP name
int preference; //0=fullname, 1=title, 2=bopname
};
vector<bop> bop_vector;
char ch;
bop_vector.push_back(bop{"Bob Smith", "chairman", "Bob", 0});
bop_vector.push_back(bop{"Anne Green", "vice chairman", "Ann", 2});
bop_vector.push_back(bop{"Helen Chen", "Junior Programmer", "Helen", 1});
bop_vector.push_back(bop{"Wimp Macho", "MIPS", "wimp", 2});
bop_vector.push_back(bop{"Raki Rhodes", "LOOPY", "raki", 1});
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display b.display by title" << endl;
cout << "c. display by bopname d. display by preference" << endl;
cout << "q. quit" << endl;
cout << "Enter your choice:";
cin >> ch;
while(true)
{
if(ch == 'q')
{
cout << "Bye!";
break;
}
switch(ch)
{
case 'a':
for(int i=0; i<bop_vector.size(); ++i)
{
cout<<bop_vector[i].fullname << endl;
}
break;
case 'b':
for(int i=0; i<bop_vector.size(); ++i)
{
cout<<bop_vector[i].title << endl;
}
break;
case 'c':
for(int i=0; i<bop_vector.size(); ++i)
{
cout<<bop_vector[i].bopname << endl;
}
break;
case 'd':
for(int i=0; i<bop_vector.size(); ++i)
{
int preference = bop_vector[i].preference;
switch (preference)
{
case 0:
cout << bop_vector[i].fullname << endl;
break;
case 1:
cout << bop_vector[i].title << endl;
break;
case 2:
cout << bop_vector[i].bopname << endl;
}
}
break;
}
cout << "Next choice:";
cin >> ch;
}
}
void p5()
{
int incoming;
cout << "Please input your incoming: ";
while( cin >> incoming && incoming>0 )
{
cout << "Your taxes is ";
if(incoming <= 5000)
cout << 0;
else if(incoming <= 15000 && incoming >5000)
cout << (incoming-5000)*0.1;
else if(incoming <= 35000 && incoming >15000)
cout << 15000*0.1+(incoming-15000)*0.15;
else
cout << 15000*0.1+20000*0.15+(incoming-35000)*0.2;
cout << "." << endl;
cout << "Please input your incoming: ";
}
}
void p6()
{
struct Donors
{
string name;
double money;
};
vector<Donors> donors_vector;
while(true)
{
string name;
double money;
char next;
cout << "Please input the patron's name: ";
getline(cin,name);
cout << "Please input the money of " << name << " : ";
(cin >> money).get();
donors_vector.push_back(Donors{name,money});
cout << "Next patron? (Y or N)" << endl;
(cin >> next).get();
if(next != 'Y' && next != 'y')
break;
}
int grand,normal = 0;
cout << "Grand Patrons:" << endl;
for(Donors x : donors_vector)
{
if(x.money > 10000)
{
cout << x.name << " : " << x.money << endl;
grand++;
}
}
if(grand == 0)
cout << "none" << endl;
cout << "Patrons:" << endl;
for(Donors x : donors_vector)
{
if(x.money <= 10000)
{
cout << x.name << " : " << x.money << endl;
normal++;
}
}
if(normal == 0)
cout << "none" << endl;
}
void p7()
{
char ch;
int vowel,consonants,others = 0;
cout << "Enter words (q to quit): " << endl;
while(cin.get(ch))
{
if(isalpha(ch))
{
if(ch == 'q')
break;
else if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowel++;
while(cin.get() != ' ');
}
else if(isalpha(ch))
{
consonants++;
while(cin.get() != ' ');
}
}
else
{
others++;
while(cin.get() != ' ');
}
}
cout << vowel << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
}
void p8()
{
ifstream inFile;
string filename;
cout << "Enter name of data file: ";
getline(cin,filename);
inFile.open(filename);
if(!inFile.is_open())
exit(EXIT_FAILURE);
char ch;
int numbers = 0;
inFile >> ch;
while(inFile.good())
{
numbers++;
inFile >> ch;
}
if(inFile.eof())
cout << numbers << " chars are in this file." << endl;
}
void p9()
{
string filename;
int number;
ifstream inFile;
struct Donors
{
string name;
double money;
};
vector<Donors> donors_vector;
cout << "Enter name of patrons file: ";
cin >> filename;
inFile.open(filename);
if(!inFile.is_open())
exit(EXIT_FAILURE);
(inFile >> number).get();
while(inFile.good())
{
string name;
double money;
getline(inFile,name);
(inFile >> money).get();
donors_vector.push_back(Donors{name,money});
}
if(inFile.eof())
{
int grand,normal = 0;
cout << "Grand Patrons:" << endl;
for(Donors x : donors_vector)
{
if(x.money > 10000)
{
cout << x.name << " : " << x.money << endl;
grand++;
}
}
if(grand == 0)
cout << "none" << endl;
cout << "Patrons:" << endl;
for(Donors x : donors_vector)
{
if(x.money <= 10000)
{
cout << x.name << " : " << x.money << endl;
normal++;
}
}
if(normal == 0)
cout << "none" << endl;
}
}
int main()
{
p1();//在这换题号
}
这篇博客提供了C++ Primer Plus第六版第六章的全部课后习题答案,读者可以在main函数修改题号以运行不同程序。特别地,第九题的测试用例已附在资源中。
&spm=1001.2101.3001.5002&articleId=105346449&d=1&t=3&u=0965629eaa21475a902bdd22f44045b0)
1063

被折叠的 条评论
为什么被折叠?



