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

C++ Primer Plus(第6版)Chapter 6 编程题答案

2017-12-25 20:52 483 查看

C++ Primer Plus(第6版)Chapter 6 编程题答案

第1题:

// task 1
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
char ch;
while (cin >> ch && ch != '@')
{
if (!isdigit(ch))
{
if (ch == tolower(ch))
cout << char(toupper(ch)) << endl;
else
cout << char(tolower(ch)) << endl;
}
}
cin.get();
cin.get();
return 0;
}


第2题:

// task 2
int main()
{
const int SIZE = 10;
double d[SIZE];
double sum, avg;
sum = avg = 0.0;
int size = 0;
cout << "Enter the double value most size 10: ";

while (size<SIZE && cin >> d[size])
{
sum += d[size];
size++;
}
if (size == 0)
{
cout << "No data!" << endl;
}
else
{
avg = sum / size;
int count = 0;
for (int i = 0; i < size; i++)
{
if (d[i] > avg)
count++;
}
cout << "The average is " << avg << "and there is " << count << " greater than average!\n";
}
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
return 0;
}


第3题:

// task 3
int main()
{
char ch;
cout << "Please enter one of the following choices: \n" << "c) carnivore \t\t\t" << "p) pianist\n" << "t) tree \t\t\t" << "g) game\n";
cin >> ch;
while (ch != 'c' && ch != 't' && ch != 'p' && ch != 'g')
{
cout << "Please enter a c, p, t, or g : ";
cin >> ch;
}
switch (ch)
{
case 'c':
cout << "you choose c" << endl;
break;
case 't':
cout << "A maple is a tree" << endl;
break;
case 'p':
cout << "You choose p" << endl;
break;
case 'g':
cout << "Good Game" << endl;
break;
}

cin.get();
cin.get();
return 0;
}


第4题:

// task 4
void show1();
void show2();
void show3();
void show4();
const int strsize = 20;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop bops[5] = {
{ "Wimp Macho", "President", "WM", 0 },
{ "Raki Rhodes", "Junior Programmer", "ChairMan", 1 },
{ "Celia Laiter", "Cleaner", "MIPS", 2 },
{ "Hoppy Hipman", "Analyst Trainee", "HH", 1 },
{ "Pat Hand", "Batman", "LOOPY", 2 }
};
int main()
{

cout << "Bebevolent Order of Programmers Report" << endl;
cout << "a. display by name\t\t b. display by title"<<endl;
cout << "c. display by bopname\t\t d. display by preference" << endl << "q. quit" << endl;
cout << "Enter your choices: "<
4000
/span>;
char ch;
while (cin >> ch && ch != 'q')
{
switch (ch)
{
case 'a':
show1();
break;
case 'b':
show2();
break;
case 'c':
show3();
break;
case 'd':
show4();
break;
default:
cout << "Enter again correctly: ";

}
}
cout << "Bye!";

cin.get();
cin.get();
return 0;
}
void show1()
{
for (int i = 0; i < 5; i++)
{
cout << bops[i].fullname << endl;
}
cout << "Next choice: ";
}
void show2()
{
for (int i = 0; i < 5; i++)
{
cout << bops[i].title << endl;
}
cout << "Next choice: ";
}
void show3()
{
for (int i = 0; i < 5; i++)
{
cout << bops[i].bopname << endl;
}
cout << "Next choice: ";
}
void show4()
{
for (int i = 0; i < 5; i++)
{
switch (bops[i].preference)
{
case 0:
cout << bops[i].fullname << endl;
break;
case 1:
cout << bops[i].title << endl;
break;
case 2:
cout << bops[i].bopname << endl;
break;

}
}
cout << "Next choices: ";
}


第5题:

// task 5
int main()
{
long income;
double tax;
cout << "Please enter your income: ";

while (cin >> income && income >= 0)
{
if (income>35000)
tax = 4000 + (income - 35000)*0.2;
else if (income > 15000)
tax = 1000 + (income - 15000)*0.15;
else if (income > 5000)
tax = (income - 5000)*0.1;
else
tax = 0;
cout << "The tax is " << tax << endl;
cout << "continue enter: ";
}

cin.get();
cin.get();
return 0;
}


第6题:

// task 6
struct donation
{
string name;
double money;

};
int main()
{
int num;
cout << "Enter the donator nums: ";
cin >> num;

int count = 0;
donation* pt = new donation[num];
for (int i = 0; i < num; i++)
{
cout << "Enter the " << i + 1 << "st donator's name: ";
cin.get();  //吃掉换行符
getline(cin, pt[i].name);
cout << "Enter the " << i + 1 << "st donator's money: ";
cin >> pt[i].money;

if (pt[i].money>10000)
count++;
}
if (count == 0)
{
cout << "None great than 10000" << endl;
}
else
{
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (pt[i].money>10000)
cout << pt[i].name << "\t\t" << pt[i].money << endl;
}
}
if (num == count)
{
cout << "None less than 10000" << endl;
}
else
{
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (pt[i].money <= 10000)
cout << pt[i].name << "\t\t" << pt[i].money << endl;
}
}

delete [] pt;

cin.get();
cin.get();
return 0;
}


第7题:

// task 7
int main()
{
int yuan, fu, other;
yuan = fu = other = 0;
string word;
cout << "Enter words (q to quit): " << endl;

while (cin >> word && word != "q")
{
if ( !isalpha(word[0]) )
other++;
else
{
switch (tolower(word[0]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
yuan++;
break;
default:
fu++;
break;
}
}

}
cout << yuan << " words beginning with vowels" << endl;
cout << fu << " words beginning with consonants" << endl;
cout << other << " others" << endl;
cin.get();
cin.get();
return 0;
}


第8题:

// task 8
int main()
{
int count = 0;
char ch;

ifstream read_f;
read_f.open("test.txt");
if (!read_f.is_open())
{
cout << "Could not open the file test.txt" << endl;
cout << "Program terminating" << endl;
exit(EXIT_FAILURE);
}
while (read_f >> ch && read_f.good()) //good()表示一切OK
{
count++;
}
if (read_f.eof())  //文件尾
cout << "End of file reached" << endl;
else if (read_f.fail())
cout << "Input terminated by data mismatch" << endl;
else
cout << "Input terminated for unknown reason" << endl;

cout << "Totally " << count << " characters!" << endl;
cin.get();
cin.get();
return 0;
}


第9题:

// task 9
struct donation
{
string name;
double money;

};
int main()
{

ifstream read_f;
read_f.open("test2.txt");
if (!read_f.is_open())
{
cout <<"Could not open the file!" << endl;
cout << "Program terminating..." << endl;
exit(EXIT_FAILURE);
}
int num;
read_f >> num;
read_f.get();  //吃掉换行符
donation* pt = new donation[num];
int count = 0;

for (int i = 0; i < num; i++)
{
getline(read_f, pt[i].name);
read_f >> pt[i].money;
if (pt[i].money>10000)
{
count++;
}
read_f.get(); //吃掉换行符
}

if (count == 0)
{
cout << "None great than 10000" << endl;
}
else
{
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (pt[i].money>10000)
cout << pt[i].name << "\t\t" << pt[i].money << endl;
}
}
if (num == count)
{
cout << "None less than 10000" << endl;
}
else
{
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (pt[i].money <= 10000)
cout << pt[i].name << "\t\t" << pt[i].money << endl;
}
}

delete[] pt; //释放内存,很重要

cin.get();
cin.get();
return 0;
}


:文本文件test.txt和test2.txt内容

test.txt:

abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGH


test2.txt:

4
Sam Stone
2000
Freida False
100500
Tammy Tubbs
5000
Rich Raptor
55000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++-primer