您的位置:首页 > 编程语言 > C语言/C++

c++ primer plus第六章习题答案

2014-11-25 20:53 537 查看
直接上代码:

第一题

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
char flag;

do
{
cin >> flag;
if(flag >= 97 && flag <= 122)
{
flag = flag - 32;
cout << flag;
}
else if(flag >= 65 && flag <= 90)
{
flag = flag + 32;
cout << flag;
}
else if(isdigit(flag))
{
}
else
{
cout << flag;
}
}while(flag != 64);

return 0;
}


第二题

#include <iostream>
#include <cctype>
using namespace std;

const int ArSize = 10;

int main()
{
double vec[ArSize];
double sum = 0, avr;
int flag = 0;
int count = 0;

cout << "Enter #" << flag+1 << " donation: ";
while(flag < ArSize && cin >> vec[flag])
{
sum = sum + vec[flag];
flag ++;
cout << "Enter #" << flag+1 << " donation: ";
}

avr = sum / flag;

if(!cin)
{
cin.clear();
}

for(int i = 0; i < flag; i++ )
{
if(vec[i] > avr)
count ++;
}

cout << "Average of "<< flag << " numbers is " << avr << endl;
cout << count << " numbers is greater than the average.\n";

return 0;
}


第三题

#include <iostream>
using namespace std;

int main()
{
char choice;

cout << "Please enter one of the following choices: " << endl;
cout << "c) carnivore            p) pianist" << endl;
cout << "t) tree                 g) game" << endl;
cout << "Please enter a c, p, t, or g: ";

while(1)
{
cin >> choice;
switch(choice)
{
case 'c':
cout << "I love you so much!\n";
break;
case 'p':
cout << "But sometimes you like a little pig...\n";
break;
case 't':
cout << "When I'm going to leave, don't cry.\n";
break;
case 'g':
cout << "Never say goodbye, I'll come with you until the sun is dry.\n";
break;
default:
cout << "Please enter a c, p, t, or g: ";
continue;
}
break;
}
}


第四题

#include <iostream>
#include <cstring>
using namespace std;

const int strsize = 20;

typedef struct
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
}bop;

void Option(bop member[]);
void Deal(bop member[]);

int main()
{
bop member[5] = {
{"Wimp Macho","Pigman","PIG",0},
{"Raki Rhodes","Junior Programmer","JP",1},
{"Celia Laiter","Dogman","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","AT",1},
{"Pat Hand","Rabbitman","LOOPY",2},
};

Option(member);

return 0;
}

void Option(bop member[])
{
char choice;

cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name    " << "b. display by title" << endl
<<  "c. display by bopname " << "d. display by preference " << endl
<<  "q. quit" << endl;

cout << "Enter your choice: ";
cin >> choice;

do{
switch(choice)
{
case 'a':
for(int i = 0; i < 5; i++ )
{
cout << member[i].fullname << endl;
}
break;
case 'b':
for(int i = 0; i < 5; i++ )
{
cout << member[i].title << endl;
}
break;
case 'c':
for(int i = 0; i < 5; i++ )
{
cout << member[i].bopname << endl;
}
break;
case 'd':
Deal(member);
break;
default:
cout << "Enter is wrong!" << endl;
}

cout << "Next choice: ";
cin >> choice;

}while(choice != 'q');

cout << "Bye!\n";
}

void Deal(bop member[])
{
for(int i = 0; i < 5; i++ )
{
switch(member[i].preference)
{
case 0:
cout << member[i].fullname << endl;
break;
case 1:
cout << member[i].title << endl;
break;
case 2:
cout << member[i].bopname << endl;
break;
default:
cout << "That'll never gonna be happen!";
}
}
}


第五题

#include <iostream>
using namespace std;

int main()
{
int salary;
double tax;

cout << "Enter your salary: ";

while(cin >> salary && salary >= 0)
{
if(salary <= 5000)
{
tax = 0;
}
else if(salary > 5000 && salary <= 15000)
{
tax = (salary - 5000) * 0.1;
}
else if(salary > 15000 && salary <= 35000)
{
tax = 10000 * 0.1 + (salary - 15000) * 0.15;
}
else
{
tax = 10000 * 0.1 + 20000 * 0.15 + (salary - 35000) * 0.2;
}
cout << "Your tax is " << tax << endl;
cout << "Enter your salary: ";
}
return 0;
}


第六题

#include <iostream>
#include <string>
using namespace std;

typedef struct
{
string name;
double donation;
}dona;

void Init(dona * pt, int num);
void Print(dona * pt, int num);

int main()
{
int num;

cout << "Enter the number: ";
cin >> num;
cin.get();

dona * pt = new dona [num];

Init(pt,num);

Print(pt,num);

delete [] pt;

return 0;
}

void Init(dona * pt, int num)
{
string temp_n;
double temp_m;
for(int i = 0; i < num; i++ )
{
cout << "Enter the name of #" << i+1 << ": ";
getline(cin, temp_n);
(pt+i)->name = temp_n;

cout << "Enter the money of #" << i+1 << ": ";
cin >> temp_m;
cin.get();
(pt+i)->donation = temp_m;
}
}

void Print(dona * pt, int num)
{
int mount1 = 0, mount2 = 0;
cout << "\nGrand Patrons:\n";
for(int i = 0; i < num; i++ )
{
if((pt+i)->donation > 10000)
{
cout << (pt+i)->name << ": " << (pt+i)->donation << endl;
mount1++;
}
}
if(mount1 == 0)
{
cout << "none\n";
}

cout << "\nPatrons:\n";
for(int i = 0; i < num; i++ )
{
if((pt+i)->donation <= 10000)
{
cout << (pt+i)->name << ": " << (pt+i)->donation << endl;
mount2++;
}
}
if(mount2 == 0)
{
cout << "none\n";
}
}


第七题

#include <iostream>
#include <cctype>
using namespace std;

const int ArSize = 20;

int main()
{
char word[ArSize];
int vowels = 0, consonants = 0, others = 0;

cout << "Enter words (q to quet): \n";

while(cin >> word && strcmp(word,"q"))
{
if(!isalpha(word[0]))
{
others++;
}
else
{
switch(word[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
default:
consonants++;
}
}
}

cout << vowels << " words beginning with vowels" << endl
<< consonants << " words beginning with consonants" << endl
<< others << " others" << endl;

return 0;
}


第八题

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char get;
int count = 0;

ofstream outFile;
ifstream inFile;

outFile.open("test.txt");
outFile << "abcde123" << endl;
outFile << "bye!";
outFile.close();

inFile.open("test.txt");

do{
inFile >> get;
if(inFile.good())
{
count++;
}
else
{
break;
}
}while(1);
inFile.close();

cout << count << " bytes" << endl;

return 0;
}


第九题

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

typedef struct
{
string name;
double donation;
}dot;

void Print(dot * pt, int num);

int main()
{
int num;

ofstream outFile;
ifstream inFile;

outFile.open("donation.txt");
outFile << 4 << endl;
outFile << "Sam Stone " << endl << 2000 << endl
<< "Freida Flass "  << endl << 100500 << endl
<< "Tammy Tubbs " << endl << 5000 << endl
<< "Rich Raptor " << endl << 55000 << endl;
outFile.close();

inFile.open("donation.txt");
inFile >> num;
inFile.get();

dot * pt = new dot [num];

for(int i = 0; i < num; i++ )
{
getline(inFile, (pt+i)->name);
inFile >> (pt+i)->donation;
inFile.get();
}
Print(pt, num);

delete [] pt;
return 0;
}

void Print(dot * pt, int num)
{
int mount1 = 0, mount2 = 0;
cout << "Grand Patrons:\n";
for(int i = 0; i < num; i++ )
{
if((pt+i)->donation > 10000)
{
cout << (pt+i)->name << ": " << (pt+i)->donation << endl;
mount1++;
}
}
if(mount1 == 0)
{
cout << "none\n";
}

cout << "\nPatrons:\n";
for(int i = 0; i < num; i++ )
{
if((pt+i)->donation <= 10000)
{
cout << (pt+i)->name << ": " << (pt+i)->donation << endl;
mount2++;
}
}
if(mount2 == 0)
{
cout << "none\n";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: