C++ Primer Plus第六章课后编程答案

1.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;

int main()
{
	using namespace std;
	cout << "Enter text for analysis. and type @"
		"to terminate input.\n";
	char ch;
	string str;
	cin.get(ch);
	while (ch != '@')
	{
		if (islower(ch))
		{	
			ch = toupper(ch);
			cout << ch;
		}
		else if (isupper(ch))
		{			
			ch = tolower(ch);
			cout << ch;
		}
		else if (!isdigit(ch))
		{
			cout << ch;
		}
		cin.get(ch);
	}
	getline(cin, str);
	cin.get();
	return 0;
}

2.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;
const int MAX = 10;

int main()
{
	using namespace std;
	double donation[MAX];
	string str;
	cout << "Please enter your donations.\n";
	cout << "You must enter " << MAX << "donations.\n";
	cout << "donation #1: ";
	int i = 0;
	while (i < MAX&&cin >> donation[i]){
		if (++i < MAX)
			cout << "donation #" << i + 1 << ": ";
	}
	double total = 0.0;
	for (int j = 0; j < i; j++)
		total += donation[j];
	if (i == 0)
		cout << "No donarion\n";
	else
		cout << total / i << "= average donation of"
		<< i << " donation\n";
	cout << "Done.\n";
	cin.clear();
	getline(cin, str);
	cin.get();
	return 0;
}


3.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;
const int MAX = 10;
using namespace std;
void showmenu();

int main()
{
	using namespace std;
	char choice;
	int done = 0;
	showmenu();
	cin >> choice;
	while (done != 1)
	{
		switch (choice)
		{
		case 'c' :	cout << "A maple is a carnivore"; 
						done = 1;	
						break;
		case 'p':	cout << "A maple is a pianist";
						done = 1;
						break;
		case 't':	cout << "A maple is a tree";
						done = 1;
						break;
		case 'g':	cout << "A maple is a tree";
						done = 1;
						break;
		default:	cout << "Please enter a c, p, t, or g: ";
		}
		if (done == 1)
			break;
		cin >> choice;
	}
	cin.get();
	cin.get();
	return 0;
}

void showmenu()
{
	cout << "Please enter one of the following choices:\n"
					 "c) canivore							p) pianist\n"
					 "t) tree								g) game\n";	
}


4.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;
const int strsize = 20;
const int MAX = 10;
struct bop{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};
using namespace std;
void showmenu();

int main()
{
	using namespace std;
	char choice;
	int done = 0;
	bop arr[3] = 
	{
		{ "Wimp Macho", "Junior Programmer", "MIPS",1 },
		{ "Raki Rhodes", "Senior Programmer","LOOPY",0 },
		{ "Celia Laiter", "Analyst Trainee", "Intel",2 }
	};
	showmenu();
	cout << "Enter your choice: ";
	cin >> choice;
	while (choice != 'q')
	{
		switch (choice)
		{
			case 'a':	for (int i = 0; i < 3; i++)
								cout << arr[i].fullname << endl;
							break;
			case 'b':	for (int i = 0; i < 3; i++)
								cout << arr[i].title << endl;
							break;
			case 'c':	for (int i = 0; i < 3; i++)
								cout << arr[i].bopname << endl;
							break;
			case 'd':	for (int i = 0; i < 3; i++)
								switch (arr[i].preference)
								{
									case 0:	cout << arr[i].fullname << endl;
										break;
									case 1:	cout << arr[i].title << endl;
										break;
									case 2:	cout << arr[i].bopname << endl;
										break;
									default:	break;
								}
							break;
			default:	cout << "That's not a choice.\n";
		}
		cout << "Next choice: ";
		cin >> choice;
	}
	cin.get();
	cin.get();
	return 0;
}

void showmenu()
{
	cout << "Benevolent Order of Programmers Report\n"
		"a) display by name			b) display by title\n"
		"c) display by bopname			d) display by preference\n"
		"q) quit\n";
}

5.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;
const int strsize = 20;
const int MAX = 10;
using namespace std;

int main()
{
	using namespace std;
	double income,tax;
	cout << "income: ";
	while (cin >> income && (income >= 0))
	{
		if (income <= 5000)
		{
			tax = 0;
			cout << "income: " << income << "\ttax: " << tax << endl;
		}
		else if (income > 5000 && income <= 15000)
		{
			tax = (income - 5000)*0.1;
			income = income- (income - 5000)*0.1;		
			cout << "income: " << income << "\ttax: " << tax << endl;
		}
		else if (income > 15000 && income <= 35000)
		{
			tax = (income - 15000)*0.15 + 10000 * 0.1;
			income = income - (income - 15000)*0.15 - 10000 * 0.1;
			cout << "income: " << income << "\ttax: " << tax << endl;
		}
		else
		{
			tax = (income - 35000)*0.2 + 20000 * 0.15 + 10000 * 0.1;
			income = income - (income - 35000)*0.2 - 20000 * 0.15 - 10000 * 0.1;
			cout << "income: " << income << "\ttax: " << tax << endl;
		}
		cout << "income: ";
	}
	cin.get();
	cin.get();
	return 0;
}

6.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;
const int strsize = 20;
const int MAX = 10;
struct donator
{
	char name[ArSize];
	double money;
};

int main()
{
	using namespace std;
	int n;
	cout << "donators number: ";
	cin >> n;
	donator* ps = new donator[n];
	for (int i = 0; i < n; i++)
	{
		cout << "donator " << i + 1 << "'s name: ";
		cin >> ps[i].name;
		cout << "donator " << i + 1 << "'s money: ";
		cin >> ps[i].money;
	}
	int k = 0;
	cout << "Grand Patrons" << endl;
	for (int i = 0; i < n; i++)
	{
		if (ps[i].money >= 10000)
		{
			k++;
			cout << ps[i].name << "\t" << ps[i].money << endl;
		}
	}
	if (k == 0)
		cout << "none!" << endl;
	else
		k = 0;
	cout << "Patrons: " << endl;
	for (int i = 0; i < n; i++)
	{
		if (ps[i].money < 10000)
		{
			k++;
			cout << ps[i].name << "\t" << ps[i].money << endl;
		}
	}
	if (k == 0)
		cout << "none!" << endl;
	cin.get();
	cin.get();
	return 0;
}


7.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
const int ArSize = 20;
const int strsize = 20;
const int MAX = 10;

int main()
{
	char str[ArSize];
	int v = 0, c = 0, o = 0;
	using namespace std;
	cout << "Enter words (q to quit) :" << endl;
	while (cin >> str&&strcmp(str, "q") != 0)
	{
		if (isalpha(str[0]))
			switch (tolower(str[0]))
			{
			case 'a':	v++;
				break;
			case 'e':	v++;
				break;
			case 'i':	v++;
				break;
			case 'o':	v++;
				break;
			case 'u':	v++;
				break;
			default:	c++;
			}
		else
			o++;
	}
	cout << v << " words beginning with vowels\n" << c << " words beginning with consonants\n" << o << " others" << endl;
	cin.get();
	cin.get();
	return 0;
}

8.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
#include <fstream>
#include <cstdlib>
const int ArSize = 20;
const int strsize = 20;
const int MAX = 10;
const int SIZE = 60;

int main()
{
	using namespace std;
	char filename[SIZE];
	ifstream inFile;
	cout << "Enter name of data file: ";
	cin.getline(filename, SIZE);
	inFile.open(filename);
	if (!inFile.is_open())
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	char ch;
	int count = 0;

	inFile >> ch;
	while (inFile.good())
	{
		++count;
		inFile >> ch;
	}
	cin.clear();
	inFile.close();
	cout << count << " characters" << endl;
	cin.get();
	cin.get();
	return 0;
}

9.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
#include <cctype>
#include <fstream>
#include <cstdlib>
const int ArSize = 20;
const int strsize = 20;
const int MAX = 10;
const int SIZE = 60;
struct donator
{
	char name[ArSize];
	double money;
};

int main()
{
	using namespace std;
	int n;
	char filename[SIZE];
	char temp[SIZE];
	ifstream inFile;
	cout << "donators number: ";
	cin >> n;
	cin.get();
	donator* ps = new donator[n];
	cout << "Enter the filename: ";
	cin.getline(filename, SIZE);
	inFile.open(filename);
	if (!inFile.is_open())
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	for (int i = 0; i < n; i++)
	{
		inFile.getline(ps[i].name, ArSize);
		inFile >> ps[i].money;
		inFile.getline(ps[i + 1].name, ArSize);
	}
	int k = 0;
	cout << "Grand Patrons" << endl;
	for (int i = 0; i < n; i++)
	{
		if (ps[i].money >= 10000)
		{
			k++;
			cout << ps[i].name << "\t" << ps[i].money << endl;
		}
	}
	if (k == 0)
		cout << "none!" << endl;
	else
		k = 0;
	cout << "Patrons: " << endl;
	for (int i = 0; i < n; i++)
	{
		if (ps[i].money < 10000)
		{
			k++;
			cout << ps[i].name << "\t" << ps[i].money << endl;
		}
	}
	if (k == 0)
		cout << "none!" << endl;
	cin.get();
	cin.get();
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值