6.11练习题

#include<iostream>
using namespace std;
//1大小写转换
/*#include<cctype>
int main() {
	char ch;
	cout << "Please enter what you want:\n";
	cin >> ch;
	while (ch != '@') {
		if (isdigit(ch)) {
			cout << "Numbers are forbiden, please enter available words:\n";
			//break;
		}
		//小写转大写
		else if (islower(ch)) {
			ch -= 32;
			cout << ch;
				//<<endl;
		}
		//大写转小写
		else if (isupper(ch)) {
			ch += 32;
			cout << ch;
		}
		//cout << endl;
		cin>>ch;
	}
	cout << "\nOkay, bye.";
	return 0;
}*/

//2数组输入结算平均值
/*#include<cctype>
//const int Max = 10; 
int main() {
	int Max = 10;
	double *ch=new double[Max];
	double donation = 0.0;
	double total = 0.0;
	int count = 0;
	cout << "Please enter the donation.\n";
	for (int i = 0; i < Max; i++) {
		cout << "The #" << i + 1 << " : ";
		cin >> donation;
		if (!isalpha (donation)) {
			ch[i] = donation;
			continue;
		}
		break;
	}
	for (int i = 0; i < Max; i++) {
		if (ch[i]!=0) {
			total += ch[i];
			count++;
		}
	}
	cout << "There are " << count << " donations and ";
	cout << "the average donation is " << total / count;
	return 0;
}*/

//3switch
/*void menu();
int main() {
	char ch;
	menu();
	cin >> ch;
	while (ch!='\n') {
		switch (ch) {
		case 'c':cout << "cats are carnivore";
			break;
		case 'p':cout << "a great pianist";
			break;
		case 't':cout << "a maple is a tree";
			break;
		case 'g':cout << "a interesting game";
			break;
		default:cout << "Please enter c, p, t or g:\n";
			menu();
			break;
		}
		cin >> ch;
		continue;
	}
	return 0;
}
void menu() {
	cout << "Please enter one of the following choices: "
		<< "\nc) carnivore\t\t\tp)pianist\nt)tree\t\t\tg)game\n";
}*/

//4
/*const int strsize = 20;
const int Max = 5;
struct bop {
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};
void menu();
void getFullname(bop bp[]);
void getTitle(bop bp[]);
void getBopname(bop bp[]);
void getPreference(bop bp[]);
//这样定义形参实质是将结构体数组当做指针
int main() {
	bop member[Max] = {
		{"Wimp Macho","Doctor","WANDA",0},
		{"Raju Rhodes","Junior Programmer","INFO",1},
		{"Celia Laiter","CEO","MIPS",2},
		{"Hoppy Hipman","Account","BIG",1},
		{"Pat Hand","CFO","LOOPY",2}
	};
	char ch;
	menu();
	cin >> ch;
	while (ch != 'q') {
		switch (ch) {
		case 'a':getFullname(member);
			break;
		case 'b':getTitle(member);
			break;
		case 'c':getBopname(member);
			break;
		case 'd':getPreference(member);
			break;
		default:cout << "This is unavailable.\n";
		}
		menu();
		cin >> ch;
	}
	cout << "Quit.\n";
	delete member;
	return 0;
	
}
void menu() {
	cout << "Benevolent Order of Programmers Report\n"
		"a. display by name\t\tb. display by title\nc.display by bopname"
		"\t\td. display by preference\nq. quit\n";
}
void getFullname(bop bp[]) {
	for (int i = 0; i < Max; i++) {
		cout << bp[i].fullname << endl;
	}
}
void getTitle(bop bp[]) {
	for (int i = 0; i < Max; i++) {
		cout << bp[i].title << endl;
	}

}
void getBopname(bop bp[]) {
	for (int i = 0; i < Max; i++) {
		cout << bp[i].bopname << endl;
	}
}
void getPreference(bop bp[]) {
	for (int i = 0; i < Max; i++) {
		int ch = bp[i].preference;
		while (ch != 2) {
			switch (ch) {
			case 0:cout << bp[i].fullname << endl;
				break;
			default:cout << bp[i].title << endl;
				break;
			}
			cout << bp[i].bopname << endl;
			break;
		}
	}
}*/

//5
/*#include<cctype>
int main() {
	int income;
	cout << "You shall be honor to pay tax.\n";
	//cin >> income;
	while (cin>>income) {
		int tax ;
		if (income >= 0&&income<=5000) {
			tax = 0;
			cout << tax << endl;
		}
		else if (income > 5000 && income <= 15000) {
			tax = 0.1*(income - 5000);
			cout << tax << endl;
		}
		else if (income > 15000 && income <= 35000) {
			tax = 10000 * 0.1 + (income - 15000)*0.15;
			cout << tax << endl;
		}
		else if (income > 35000) {
			tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000)*0.2;
			cout << tax << endl;
		}
		else {
			cout << "This shouldn't be a joke.\n";
			break;
		}
	}
	cout << "Please enter your income.\n";
	return 0;
}*/

//6
/*#include<string>
const int Max = 5;
struct donater {
	string name;
	double donation;
};
void getGPatrons(donater dn[]);
int main() {
	cout << "Please enter the donaters' information.\n";
	donater *dn = new donater[Max];
	for (int i = 0; i < Max; i++) {
		cout << "Donater No." << i + 1 << ": ";
		cin >> dn[i].name >> dn[i].donation;
	}
	getGPatrons(dn);
	return 0;
}
void getGPatrons(donater dn[]) {
	cout << "Grand Patrons\n";
	int count = 0;
	for (int i = 0; i < Max; i++) {
		if (dn[i].donation > 10000) {
			cout << dn[i].name << endl;
			count++;
		}
	}
	if (count == 0) {
		cout << "None" << endl;
	}
	count = 0;
	cout << "Patrons\n";
	for (int i = 0; i < Max; i++) {
		//int count = 0;
		if (dn[i].donation <= 10000) {
			cout << dn[i].name << endl;
			count++;
		}
//		else if (count == 0) {
//		cout << "None" << endl;
//		}
	}
	if (count == 0) {
		cout << "None" << endl;
	}
}*/

//7
/*int main()
{
	char str[20];
	int vowel = 0,consonants = 0,others = 0;
	cout << "Enter words (q to quit):" << endl;
	while (cin >> str)
	{
		if (strcmp(str, "q") == 0)
			break;
		char ch = str[0];
		if (isalpha(ch))
		{
			switch (ch)
			{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				vowel++;
				break;
			default:
				consonants++;
			}
//			if (ch == 'a' || 'e' || 'i' || 'o' || 'u') {
//				vowel++;
//			}
//			else {
//				consonants++;
//			}
//			并不会按首字母统计
		}
		else
			others++;
	}
	cout << vowel << " words beginning with vowels\n";
	cout << consonants << " words beginning with consonants\n";
	cout << others << " others\n";
	return 0;
}*/

//8读取文件,统计字符
//统计字符采用char
//统计单词采用string
//统计首字母采用char[]
/*#include<fstream>
#include<cstdlib>
#include<string>
const int SIZE = 60;
int main() {
	ifstream inFile;
	char filename[SIZE];
	cin.getline(filename, SIZE);
	inFile.open(filename);
	if (!inFile.is_open()) {
		cout << "Couldn't open the file. Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	char ch;
	int count = 0;
	while (inFile>>ch) {
		cout << ch << endl;
		count++;
	}
	if (inFile.eof()) {
		cout << "End of file reached.\n";
	}
	else if (inFile.fail()) {
		cout << "Input terminated by data mismatch.\n";
	}
	else {
		cout << "Input terminated for unknow reason.\n";
	}
	if (count == 0) {
		cout << "No data processed.\n";
	}
	else {
		cout << "\nTotal chars: " << count;
	}
	inFile.close();
	return 0;
}*/

//9将第6题改为从文件中获取初始信息
#include<fstream>
#include<cstdlib>
#include<string>
//const int MEMBERS = 4;
const int MAX = 100;
struct donater {
	string name;
	int donation;
};
void getGPatrons(donater dn[]);
int main() {
	//string *name=new string[MEMBERS];
	//int *donation = new int[MEMBERS];
	donater *members = new donater[MAX];
	ifstream inFile;
	inFile.open("9.txt");
	if (!inFile.is_open())
	{
		cout << "Could not open the file." ;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	for (int i = 0; i < MAX; i++) {
		getline(inFile, members[i].name);
		//inFile >> members[i].name;
		inFile >> members[i].donation;
	}
	//while (!inFile.eof()) {
		//for (int i = 0; i < MEMBERS; i++) {
		//	inFile >> name[i];
		//}
		//for (int i = 0; i < MEMBERS; i++) {
		//	inFile >> donation[i];
		//}
		/*for (int i = 0; i < MAX; i++) {
			getline(inFile,members[i].name);
			inFile >> members[i].donation;
		}*/
	//}
	getGPatrons(members);
	//delete members;
	inFile.close();
	return 0;
}
void getGPatrons(donater dn[]) {
	cout << "Grand Patrons\n";
	int count = 0;
	for (int i = 0; i < MAX; i++) {
		if (dn[i].donation > 10000) {
			cout << dn[i].name << endl;
			count++;
		}
	}
	if (count == 0) {
		cout << "None" << endl;
	}
	count = 0;
	cout << "Patrons\n";
	for (int i = 0; i < MAX; i++) {
		//int count = 0;
		if (dn[i].donation <= 10000) {
			cout << dn[i].name << endl;
			count++;
		}
		//		else if (count == 0) {
		//		cout << "None" << endl;
		//		}
	}
	if (count == 0) {
		cout << "None" << endl;
	}
}

 

转载于:https://my.oschina.net/Almon/blog/3046560

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值