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";
swi