c++ primer plus chapter6 习题

在这里插入图片描述

#include<iostream>
#include<cctype>
#include<string>

using namespace std;

int main()
{
	string str;
	cin >> str;
	int i = 0;
	while (str[i] != '@') {
		if (isdigit(str[i]) )
		else if (islower(str[i])) {
			str[i] = toupper(str[i]);
			cout << str[i];
		}
		else if (isupper(str[i])) {
			str[i] = tolower(str[i]);
			cout << str[i];
		}
		else {
			cout << str[i];
		}
		i++;
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<array>

using namespace std;

int main()
{
	array<double, 10> donation;
	double input;
	int count = 0;
	double sum = 0.0;
	double average;

	cout << "Please enter the double numerial: ";
	
	while ((cin >> input) && (count < 10)) {
		donation[count++] = input;
		cout << "Please enter the double numerial: ";
	}
	for (int i = 0; i < count; i++) {
		sum += donation[i];
	}
	average = sum / count;
	int i = 0;
	while (count--) {
		if (donation[count] > average)
			i++;
	}
	cout << "Average is: " << average << endl;
	cout << i << " numbers are bigger than average." << endl;
	return 0;
}

在这里插入图片描述

#include<iostream>

using namespace std;

int main()
{
	char ch;
	cout << "Please enter one of the following choices: " << endl;
	cout << "c) carnivore          p) pianist" << endl;
	cout << "t) tree               g) game" << endl;
	while (cin >> ch) {
		if (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {
			cout << "Please enter a c,p,t or g: ";
			continue;
		}
		switch (ch) {
			case 'c': cout << "A car." << endl; break;
			case 'p': cout << "A pepple." << endl; break;
			case 't': cout << "A maple is a tree." << endl; break;
			case 'g': cout << "A google." << endl; break;
		}
		cout << "Please enter a c,p,t or g: ";
	}
	return 0;
}

在这里插入图片描述

#include<iostream>

using namespace std;

const int strsize = 40;

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

int main()
{
	char choice;
	bop people[5] = { 
		{"Wimp macho", "Level_A", "ww", 0} ,
		{"Raki Rhodes", "Junior Programmer", "RR", 1},
		{"Celia Laiter", "Level_C", "MIPS", 2},
		{"Hoppy Hipman", "Analyst trainee", "HH", 1},
		{"Pat Hand", "Level_D", "LOOPY", 2}
	};
	cout << "Benevolent Order of Programmers Report " << endl;
	cout << "a. display by name      b. display by title" << endl;
	cout << "c. display by bopname   d. display by preference" << endl;
	cout << "q. quit" << endl;
	cout << "Enter your choice: ";
	while (cin >> choice) {
		switch (choice) {
		case 'a':
			for (int i = 0; i < 5; i++)
				cout << people[i].fullname << endl;
			break;
		case 'b':
			for (int i = 0; i < 5; i++)
				cout << people[i].title << endl;
			break;
		case 'c':
			for (int i = 0; i < 5; i++)
				cout << people[i].bopname << endl;
			break;
		case 'd':
			for (int i = 0; i < 5; i++) {
				switch (people[i].preference) {
				case 0: cout << people[i].fullname << endl; break;
				case 1: cout << people[i].title << endl; break;
				case 2: cout << people[i].bopname << endl; break;
				}
			}
			break;
		case 'q':
			cout << "Bye!" << endl;
			exit(0);
		}
		cout << "Next choice: ";
	}
	return 0;
}

在这里插入图片描述

#include<iostream>

using namespace std;

int main()
{
	int income;
	double tax;
	cout << "Please enter your income: ";
	while ((cin >> income) && (income >= 0)) {
		if (income <= 5000) {
			tax = income * 0.0;
		}
		else if (income <= 15000) {
			tax = (income - 5000) * 0.1;
		}
		else if (income <= 35000) {
			tax = 10000 * 0.1 + (income - 15000) * 0.15;
		}
		else {
			tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2;
		}
		cout << "your tax = " << tax << endl;
		cout << "Please enter your income: ";
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>

using namespace std;

struct Patrons {
	string name;
	double money;
};

int main()
{
	int num;
	cout << "Please enter the number of patrons: ";
	cin >> num;
	cin.get();
	Patrons* patron = new Patrons[num];
	cout << "Enter the information of patrons: " << endl;
	Patrons* grand_patron = new Patrons[num];
	Patrons* common_patron = new Patrons[num];
	int k = 0, j = 0;
	for (int i = 0; i < num; i++) {
		cout << "Please enter name #" << i + 1 << ": ";
		getline(cin, patron[i].name);
		cout << "Please enter the amount of donation #" << i + 1 << ": ";
		while (!(cin >> patron[i].money)) //处理错误输入;
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Please enter a number: ";
		}
		cin.get(); //吸收正确输入时的换行符;
		if (patron[i].money > 10000)
			grand_patron[k++] = patron[i];
		else
			common_patron[j++] = patron[i];
	}
	cout << "Grand Patrons" << endl;
	if (k == 0)
		cout << "none" << endl;
	for (int i = 0; i < k; i++) {
		cout << grand_patron[i].name << "   " << grand_patron[i].money << endl;
	}
	cout << "Patrons" << endl;
	if (j == 0)
		cout << "none" << endl;
	for (int i = 0; i < j; i++) {
		cout << common_patron[i].name << "   " << common_patron[i].money << endl;
	}
	delete[]common_patron;
	delete[]grand_patron;
	delete[]patron;
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
	string word;
	cout << "Enter words (q to quit): " << endl;
	int vowels = 0, consonants = 0, others = 0;
	while ((cin >> word) && (word != "q")){
		if (isalpha(word[0])) {
			switch (word[0]) {
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
			case 'A':
			case 'E':
			case 'I':
			case 'O':
			case 'U':
				vowels++;
				break;
			default:
				consonants++;
			}
		}
		else {
			others++;
		}
	}
	cout << vowels << " words beginning with vowels" << endl;
	cout << consonants << " words beginning with consonants" << endl;
	cout << others << " others" << endl;
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

using namespace std;

int main()
{
	ifstream inFile;
	string file_name;
	cout << "Enter the file name: ";
	getline(cin, file_name);
	inFile.open(file_name);
	if (!inFile.is_open()) {
		cout << "Failed to open the file!" << endl;
		exit(EXIT_FAILURE);
	}
	char ch;
	int count = 0;
	while (!inFile.eof()) {
		inFile >> ch;
		count++;
	}
	cout << file_name << " has " << count << " characters." << endl;
	inFile.close();
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>

using namespace std;

struct Patrons {
	string name;
	double money;
};

int main()
{
	ifstream inFile;
	string file_name;
	cout << "Enter the file name: ";
	getline(cin, file_name);
	inFile.open(file_name);
	if (!inFile.is_open()) {
		cout << "Failed to open the file!" << endl;
		exit(EXIT_FAILURE);
	}
	int num;
	inFile >> num;
	inFile.get();
	Patrons* patron = new Patrons[num];
	Patrons* grand_patron = new Patrons[num];
	Patrons* common_patron = new Patrons[num];
	int k = 0, j = 0;
	for (int i = 0; i < num; i++) {
		getline(inFile, patron[i].name);
		inFile >> patron[i].money;
		inFile.get(); //吸收正确输入时的换行符;
		if (patron[i].money > 10000)
			grand_patron[k++] = patron[i];
		else
			common_patron[j++] = patron[i];
	}
	cout << "Grand Patrons" << endl;
	if (k == 0)
		cout << "none" << endl;
	for (int i = 0; i < k; i++) {
		cout << grand_patron[i].name << "   " << grand_patron[i].money << endl;
	}
	cout << "Patrons" << endl;
	if (j == 0)
		cout << "none" << endl;
	for (int i = 0; i < j; i++) {
		cout << common_patron[i].name << "   " << common_patron[i].money << endl;
	}
	delete[]common_patron;
	delete[]grand_patron;
	delete[]patron;
	inFile.close();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定谔的喵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值