《C++ primer plus》6

C++ primer plus 6

6.3

Method 1

#include <iostream>
using namespace std;

int main()
{
    char temp = '0';
    cout << "Please enter one of the follonwing choices:\n"
        << "c) carnivore\tp) pianist\nt) tree \tg) game\n";
    cin >> temp;
    while ((temp != 'c') && (temp != 'p') && (temp != 't') && (temp != 'g')) {
        cout << "Please enter a c,p,t or g: ";
        cin >> temp;
    }

    switch(temp) {
    case 'c':
        cout << "C !!\n";
        break;
    case 'p':
        cout << "P !!\n";
        break;
    case 't':
        cout << "T !!\n";
        break;
    case 'g':
        cout << "G !!\n";
        break;
    }
}

Method 2

#include <iostream>
using namespace std;
int main(void)
{
	char ch;
	cout << "Please enter one of the following choices :" << endl;
	cout << "c) carnivore " << "\t p) pianist" << endl;
	cout<<  " t) tree" << "     \tg) game" << endl;
	
	while (cin >> ch)
	{
		switch (ch)
		{
		case 'c':
			cout << "tiger is a carnivore." << endl;
			break;
		case 'p':
			cout << "Xiaoming is a pianist." << endl;
			break;
		case 't':
			cout << "A maple is a tree." << endl;
			break;
		case 'g':
			cout << "Golf is a game." << endl;
			break;
		default:
			cout << "Please enter a c, p, t, or g: ";
 
		}
	}
 
	system("pause");
	return 0;
}

6.4

#include <iostream>
using namespace std;
const int strsize = 20;
struct bop {
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};

int main()
{
	bop bopArr[] = {
		{"Wimp Macho","Driver","W",2},
		{"Raki Rhodes","Actor","R",1},
		{"Celia Laiter","Teacher","C",1}
	};
	cout << "BOP report" << endl;
	cout << "a. display by name\t b.display by title\n"
		<< "c. display by bopname\t d. display by preference\n"
		<< "q. quit\n";
	
	char temp;
	cout << "Enter your choice: ";
	while (cin>>temp) {
		if (temp == 'q') {
			cout << "Bye! " << endl;
			break;
		}
		switch (temp) {
		case 'a':
			for (int i = 0; i < 3; i++) {
				cout << bopArr[i].fullname << endl;
			}
			break;
		case 'b':
			for (int i = 0; i < 3; i++) {
				cout << bopArr[i].title << endl;
			}
			break;
		case 'c':
			for (int i = 0; i < 3; i++) {
				cout << bopArr[i].bopname << endl;
			}
			break;
		case 'd':
			for (int i = 0; i < 3; i++) {
				if(bopArr[i].preference==0)
					cout << bopArr[i].fullname << endl;
				else if(bopArr[i].preference == 1)
					cout << bopArr[i].title << endl;
				else if (bopArr[i].preference == 2)
					cout << bopArr[i].bopname << endl;
			}
			break;
		default:
			"please enter the right char! \n";
		}
		cout << "Next choice: ";
	}
}


6.5

#include <iostream>
using namespace std;
int main()
{
	double income = 0,tax=0;
	cout << "Enter incomes: " << endl;
	cin >> income;
	while (income >= 0 && !(cin.fail())) {
		if (income <= 5000)
			tax = 0;
		else if (income <= 15000)
			tax = 10000 * 0.10;
		else if (income <= 35000)
			tax = 10000 * 0.10 + 20000 * 0.15;
		else
			tax = 10000 * 0.10 + 20000 * 0.15 + (income - 35000) * 0.20;
		cout << "Tax: " << tax << endl;
		cout<< "Enter incomes: " << endl;
		cin >> income;
	}
  
}

6.6

#include <iostream>
#include <string>
using namespace std;
//用来储存姓名的字符数组(或string对象)和用来存储款项的double成员
struct information
{
	string name;
	double money;
};
 
//显示所有捐款超过10000的捐款者的姓名及其捐款数额。
void show_Grand(information* info, int num)
{
	cout << "Grand Patrons:" << endl;
	int n_Grand = 0;
	for (int j = 0; j < num; j++)
	{
		if (info[j].money> 10000)
		{
			cout << info[j].name << "\t" << info[j].money << endl;
			n_Grand++;
		}
	}
	if (n_Grand == 0)
	{
		cout << "none" << endl;
	}
}
 
//程序将列出其他的捐款者,该列表要以Patrons开头。
void show_other(information* info, int num)
{
	cout << "Patrons:" << endl;
	int n_other = 0;
	for (int j = 0; j < num; j++)
	{
		if (info[j].money <= 10000)
		{
			cout << info[j].name << "\t" << info[j].money << endl;
			n_other++;
		}
	}
	if (n_other == 0)
	{
		cout << "none" << endl;
	}
 
}
int main(void)
{
	int num;       //捐献者数目
	cout << "Pleser enter the number of donors: ";
	cin >> num;
	cin.get();    //清除缓存 后面还要输入字母
	information* info = new information[num]; //这些信息被储存在一个动态分配的结构数组中。
	
											  //要求用户输入每一个捐献者的姓名和款项。
	for (int i = 0; i < num; i++)
	{
		cout << "Please enter the " << i + 1 << "-th name: ";
		getline(cin, info[i].name);
		cout << "Please enter the " << i + 1 << "-th money:";
		cin >> info[i].money;
		cin.get();
	}
	show_Grand(info, num);
	show_other(info, num);
 
	delete info;
}

6.7

//单词 == 字符数组
#include <iostream>
#include <cctype>
using namespace std;
const int ArrSize = 30;
int main(void)
{
	char ch;
	char str[ArrSize];
	int count_other = 0;
	int count_vowel = 0;
	int count_consonant = 0;
	cout << "Enter words(q to quit) :" << endl;
	while (cin >> str)
	{
		if (strcmp(str, "q") == 0)       //单独一个字母q就退出
			break;
		ch = str[0];
		if (isalpha(ch))
		{
 
			switch (ch)
			{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				count_vowel++;
				break;
			default:
				count_consonant++;
			}
		}
		else
			count_other++;
	}
	cout << count_vowel << " words beginning with vowels\n";
	cout << count_consonant << " words beginning with consonants\n";
	cout << count_other << " others\n";
 
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值