C++ Primer Plus 第六版 第六章练习答案
-
if else
循环; -
?:
条件运算符; -
switch
语句; -
break
、continue
循环; -
提高读取的鲁棒性;
-
文件输入与输出。
#include <iostream>
using namespace std;
//practice 6.1
#include <cctype>
void p6_1(void) {
char ch;
while ((ch = cin.get()) != '@') { //没读到的部分会留在内存里
if (isdigit(ch))
continue;
else if (islower(ch))
cout << char(toupper(ch));
else if (isupper(ch))
cout << char(tolower(ch));
else if (ch=='\n') //为了展示好看
cout << endl;
}
return;
}
//practice 6.2
#include <array>
void p6_2(void) {
array<double, 10> arr = { 0 }; //这步初始化可有可无,最好写上
int index = 0, larger = 0;
double total = 0;
cout << "Please enter ten values, and Non-digital to exit: " << endl;
while (index<10 && (cin >> arr[index])) { //由于arr是double型,若输入的不是数字,(cin >> arr[index])为False
total += arr[index];
index++;
}
double avg = total / index;
for (int i = 0; i < index; i++)
if (arr[i] > avg)
larger++;
cout << "The average value is " << avg << ", and there are "
<< larger << " values larger than agerage!" << endl;
return;
}
//practice 6.3
#include <iomanip>
void display() {
cout << "Please enter one of the following choices: " << endl;
cout.flags(ios::left);
cout << setw(20) << "c) carnivore" << "p) pianist" << endl;
cout << setw(20) << "t) tree" << "g) game" << endl;
}
void p6_3(void){
char ch;
display();
while (cin >> ch){
cin.get(); //清空内存残余
switch (ch){
case 'c':
cout << "Tyrannosaurus rex is a carnivore." << endl;
display();
break;
case 'p':
cout << "Langlang is a pianist." << endl;
display();
break;
case 't':
cout << "A maple is a tree." << endl;
display();
break;
case 'g':
cout << "Dota2 is a game." << endl;
display();
break;
default:
cout << "Please enter a c, p, t or g: ";
break;
}
}
}
//practice 6.4
#include <iomanip>
const int strsize = 20;
const int Arr_size = 5;
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
};
void p6_4(void) {
char choice;
const bop bopArray[Arr_size] = {
{"Wimp Macho", "n", "n", 0},
{"Raki Rhodes", "Junior Programmer", "n", 1},
{"Celia Laiter", "n", "MIPS", 2},
{"Hoppy Hipman", "Analyst Trainee", "n", 1},
{"Pat Hand", "n", "LOOPY", 2} };
cout << "Benevolent Order of Proframers Report" << endl;
cout.flags(ios::left);
cout << setw(30) << "a. display by name" << "b. display by title" << endl;
cout << setw(30) << "c. display by bopname" << "d. display by preference" << endl;
cout << left << setw(30) << "q. quit" << endl; //flags两种写法
cout << "Enter your choice:";
while (cin >> choice && choice!='q') {
cin.get(); //清空内存残余
switch (choice) {
case 'a':
for (int i = 0; i < Arr_size; i++)
cout << bopArray[i].fullname << endl;
break;
case 'b':
for (int i = 0; i < Arr_size; i++)
cout << bopArray[i].title << endl;
break;
case 'c':
for (int i = 0; i < Arr_size; i++)
cout << bopArray[i].bopname << endl;
break;
case 'd':
for (int i = 0; i < Arr_size; i++) {
switch (bopArray[i].preference) {
case 0:
cout << bopArray[i].fullname << endl;
break;
case 1:
cout << bopArray[i].title << endl;
break;
case 2:
cout << bopArray[i].bopname << endl;
break;
}
}
break;
default:
cout << "Please enter a a, b, c, d, q: ";
break;
}
cout << "Next choice:";
}
cout << "Bye!" << endl;
}
// practice 6.5
const double rate1 = 0.1;
const double rate2 = 0.15;
const double rate3 = 0.2;
void p6_5(void){
double income = 0.0, tax = 0.0;
cout << "Please enter your income:";
while ((cin >> income) && (income > 0)){
if (income <= 5000)
tax = 0.0;
else if (5000 < income && income <= 15000)
tax = (income - 5000) * rate1;
else if (15000 < income && income <= 35000)
tax = (15000 - 5000) * rate1 + (income - 15000) * rate2;
else
tax = (15000 - 5000) * rate1 + (35000 - 15000) * rate2 + (income - 35000) * rate3;
cout << "Your income = " << income << ", tax = " << tax << endl;
}
return;
}
// practice 6.6
#include<string>
struct Info{
string name;
double payment;
};
void p6_6(void) {
int number = 0, tmp = 0;
cout << "Enter the number of contributors:";
cin >> number;
cin.get();
Info* contributors = new Info[number];
for (int i = 0; i < number; i++) {
cout << "Enter the name of the number " << i + 1 << " contributor: ";
getline(cin, contributors[i].name);
cout << "Enter the donation: ";
cin >> contributors[i].payment;
cin.get();
}
cout << "Grand Pators:" << endl;
for (int i = 0; i < number; i++)
if (contributors[i].payment > 10000){
cout << "Name: " << contributors[i].name << endl;
cout << "Amount: " << contributors[i].payment << endl;
tmp++;
}
if (tmp==0)
cout << "none" << endl;
tmp = 0;
cout << "Pators:" << endl;
for (int i = 0; i < number; i++)
if (contributors[i].payment <= 10000) {
cout << "Name: " << contributors[i].name << endl;
cout << "Amount: " << contributors[i].payment << endl;
tmp++;
}
if (tmp == 0)
cout << "none" << endl;
delete[] contributors;
return;
}
// practice 6.7
#include <iomanip>
#include<cctype>
void p6_7(void){
string input;
int vowels = 0, consonants = 0, others = 0;
cout << "Enter words (q to quit): " << endl;
while (cin >> input && input != "q"){
if (isalpha(input[0])){
if (input[0] == 'a' || input[0] == 'o' || input[0] == 'e' || input[0] == 'i' || input[0] == 'u')
vowels++;
else
consonants++;
}
else
others++;
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " otners" << endl;
return;
}
// practice 6.8
#include<fstream>
void p6_8(void){
string FileName;
ifstream File;
unsigned int num = 0;
char ch;
cout << "Enter the file name:";
getline(cin, FileName);
File.open(FileName);
while ((ch = File.get()) != EOF)
if (ch != '\n') //之前这句写的是if ((ch = File.get()) != '\n'),目的是为了不计算回车这个键
num++; //上面那句就是一个易错点,因为这样会一次性读取两个字符。
cout << "There are " << num << " characters in " << FileName << " file." << endl;
}
// practice 6.9
// 结构体结构如6.6所示
void p6_9(void){
int number = 0, tmp = 0;
string FileName;
ifstream File;
cout << "Enter the file name:";
getline(cin, FileName);
File.open(FileName);
File >> number;
File.get();
Info* contributors = new Info[number];
for (int i = 0; i < number; i++) {
getline(File, contributors[i].name);
File >> contributors[i].payment;
File.get();
}
cout << "Grand Pators:" << endl;
for (int i = 0; i < number; i++)
if (contributors[i].payment > 10000) {
cout << "Name: " << contributors[i].name << endl;
cout << "Amount: " << contributors[i].payment << endl;
tmp++;
}
if (tmp == 0)
cout << "none" << endl;
tmp = 0;
cout << "Pators:" << endl;
for (int i = 0; i < number; i++)
if (contributors[i].payment <= 10000) {
cout << "Name: " << contributors[i].name << endl;
cout << "Amount: " << contributors[i].payment << endl;
tmp++;
}
if (tmp == 0)
cout << "none" << endl;
delete[] contributors;
return;
}
int main(int argc, char **argv) {
cout << "=====================================\n"
<< "============ Chapter6: ============\n"
<< "=====================================\n\n";
cout << "==>> Practice 6_1:\n";
p6_1();
cout << endl;
cout << "==>> Practice 6_2:\n";
p6_2();
cout << endl;
cout << "==>> Practice 6_3:\n";
p6_3();
cout << endl;
cout << "==>> Practice 6_4:\n";
p6_4();
cout << endl;
cout << "==>> Practice 6_5:\n";
p6_5();
cout << endl;
cout << "==>> Practice 6_6:\n";
p6_6();
cout << endl;
cout << "==>> Practice 6_7:\n";
p6_7();
cout << endl;
cout << "==>> Practice 6_8:\n";
p6_8();
cout << endl;
cout << "==>> Practice 6_9:\n";
p6_9();
cout << endl;
}