C++ Primer Plus(第6版)---编程作业完成(第六章)

第六章

#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
using namespace std;
//函数原型
void cc_type(void);
void donation(void);
void test(void);
void bop_plus(void);
void tvarp(void);
void denotion_plus(void);
void word_input(void);
int text_input(void);
void denote_txt(void);
//主函数
int main()
{
	//cc_type();

	//donation();

	//test();

	//bop_plus();

	//tvarp();

	//denotion_plus();

	//word_input();

	//text_input();

	denote_txt();
	return 0;
}

//-----------------作业一----------------------------
void cc_type(void)
{
	char ch;
	cout << "Enter the text:<@ is the end>" << endl;
	//cin.get()用来读取字符,可以读取回车符,空格,制表符,同样存在输入缓冲
	//按下回车符,才会传回程序中
	while ((ch = cin.get()) != '@')
	{
		if (!(isdigit(ch)))//不是数字
		{
			if (isupper(ch))//大写字母
				ch = ch - 'A' + 'a';
			else if (islower(ch))//小写字母
				ch = ch - 'a' + 'A';
			else
				;
			cout << ch;
		}
	}
	return;
}

//-----------------------作业二-------------------------------------
void donation(void)
{
	const int Max = 10;
	double donation[Max];
	cout << "Enter the donation: " << endl;
	int i = 0;
	double sum = 0;
	cout << "# 1: ";
	while (i<Max && cin >> donation[i])
	{
		sum += donation[i];
		cout << donation[i];
		i++;
		cout << "\n# " << i + 1<<": ";
	}
	cout << "sum is : " << sum << endl;
	cout << "average is : " << sum / i << endl;
	return;
}


//------------------作业三-------------------------------------------
void menu(void)
{
	cout << "Please enter one of the following choices:\n";
	cout << "a) carnivore\tp)pianist\n";
	cout << "t)tree\tg)game\n";
}
void test(void)
{
	menu();
	char ch;
	cin >> ch;
	while (ch != 'a' && ch != 'p' && ch != 't' && ch != 'g')
	{
		cout << "Please enter a c,p,t,g:" << endl;
		cin >> ch;
	}
		switch (ch)
		{
		case 'a':cout << "carnivore" << endl;
					break;
		case 'p': cout << "pianist" << endl;
					break;
		case 't':cout << "tree" << endl;
					break;
		case 'g': cout << "game" << endl;
					break;
		}
	return;
}



//--------------作业四------------------------------
const int Strsize = 128;
const int Num = 5;
struct bop
{
	char fullname[Strsize];
	char title[Strsize];
	char bopname[Strsize];
	int perference;
};
void menu_bop(void)
{
	cout << "a.display by name\tb.display by title\n";
	cout << "c.display by bopname\td.display by preference\n";
	cout << "q.quit\n";
	return;
}
void print(bop name[Num],int n)
{
	for (int i = 0;i < Num;i++) {
		switch (n)
		{
		case 0:cout << name[i].fullname << endl;
			break;
		case 1:cout << name[i].title << endl;
			break;
		case 2:cout << name[i].bopname << endl;
			break;
		case 3: {
			if(name[i].perference==0)
				cout << name[i].fullname << endl;
			else if(name[i].perference == 1)
				cout << name[i].title << endl;
			else 
				cout << name[i].bopname << endl;
		}
			 break;
		}	
	}
}
void bop_plus(void)
{
	bop arr[Num]=
	{
		{"AAA","aaa","Aa",0},
		{"BBB","bbb","Bb",1},
		{"CCC","ccc","Cc",2},
		{"DDD","ddd","Dd",0},
		{"EEE","eee","Ee",1}
	};
	menu_bop();
	cout << "Enter your chioce: ";
	char ch;
	cin >> ch;
	while (ch != 'q')
	{
		switch (ch)
		{
		case 'a': print(arr, 0);
			break;
		case 'b': print(arr, 1);
			break;
		case 'c': print(arr, 2);
			break;
		case 'd':print(arr, 3);
			break;
		default:cout << "Try again\n";
			break;
		}
		cout << "Next choice:";
		cin >> ch;
	}
	cout << "Good bye!\n";
	return;
}


//--------------------------作业五----------------------------
void tvarp(void)
{
	double wage;
	cout << "Enter your wages: ";
	double taxi = 0;
	while (cin >> wage && wage >=0)
	{
		if (wage < 5000)
			taxi = 0;
		else if(wage >= 5001 && wage < 15000)
			taxi = (wage - 5000) * 0.1;
		else if (wage >= 15001 && wage < 35000)
			taxi = (wage - 15000) * 0.15+10000*0.1;
		else
			taxi = (wage - 35000) * 0.2 + 10000 * 0.1+20000*0.15;
		cout << "taxi= " << taxi << endl;
		cout << "Enter your wages: ";
	}
	return;	
}


//---------------------------------作业六--------------------------------
struct donor
{
	string name;
	double money;
};
void denotion_plus(void)
{
	int num = 0;
	cout << "Enter the num: ";
	cin >> num;
	cin.get();//此处有回车符
	donor* pd = new donor[num];
	for (int i = 0;i < num;i++)
	{
		cout << "# " << i + 1 << ": ";
		cout << "Enter the name: ";
		getline(cin, pd[i].name);
		cout << "Enter the money: ";
		cin >> pd[i].money;//不处理回车符,会被留在输入行
		cin.get();
	}
	//重要捐款人
	cout << "Grand Patrons: " << endl;
	int count_g = 0;
	for (int j = 0;j < num;j++)
	{
		if (pd[j].money > 10000)
		{
			cout << "name: " << pd[j].name << "\tmoney: " << pd[j].money << endl;
			count_g++;
			//cout << count << endl;;
		}	
	}
	if (0 == count_g)//两个if没有先后
		cout << "None" << endl;
	//其他捐款人
	cout << "Patrons: " << endl;
	int count_p = 0;
	for (int j = 0;j < num;j++)
	{
		if (pd[j].money <= 10000)
		{
			cout << "name: " << pd[j].name << "\tmoney: " << pd[j].money << endl;
			count_p++;
		}
	}
	if (0 == count_p)
		cout << "None" << endl;
	delete [ ] pd;
	return;
}


//--------------------------------作业七-------------------------
void word_input(void)
{
	string str;
	int others = 0;
	int vowel = 0;
	int consonant = 0;

	cout << "Enter words(q to quit): ";
	while (cin >> str && str!= "q")
	{
		if (!(isalpha(str[0]))){
			others++;//其他字符
		}
		else {
			switch (str[0])
			{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':vowel++;//元音字符
				break;
			default:consonant++;//辅音字符
				break;
			}
		}
	}
	cout << "元音: " << vowel << endl;
	cout << "辅音: " << consonant << endl;
	cout << "其他: " << others << endl;
	return;
}



//------------------------------作业八-----------------------------------
//读取文本文件
int text_input(void)
{
	//读取文件名
	const int Size = 128;
	cout << "Enter the filename: " << endl;
	char filename[Size];
	cin >> filename;

	//创建对象//与cin相似,不读取空格符,制表符,换行符
	ifstream inFile;
	//关联文件
	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;
	}
	//表明具体原因
	if (inFile.eof())
		cout << "End of file reached.\n";
	else if (inFile.fail())
		cout << "Input terminated by data mismatch.\n";
	else
		cout << "Input termintated for unknown reason.\n";
	if (0 == count)
	{
		cout << "No data.\n";
	}
	else
	{
		cout << "Count is : " << count << endl;
	}
	//关闭文件
	inFile.close();
	return 0;
}


//-------------------作业九------------------------------------------------------
void denote_txt(void)
{
	//读取文件名
	const int Size = 128;
	cout << "Enter the filename: " << endl;
	char filename[Size];
	cin >> filename;

	//创建对象//与cin相似,不读取空格符,制表符,换行符
	ifstream inFile;
	//关联文件
	inFile.open(filename);
	//打开文件检查
	if (!inFile.is_open())
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	//读取文件
	//读取人数
	int num = 0;
	inFile >> num;
	inFile.get();//读取数字后的换行符
	donor* pd = new donor[num];
	//循环读取姓名和捐款数字
	while (inFile.good())//成功读取的话
	{
		for (int i = 0;i < num;i++) {
			//姓名,getline 读取一行,且丢弃换行符
			getline(inFile,pd[i].name);
			//数字
			inFile >> pd[i].money;
			inFile.get();
		}	
	}
	//重要捐款人
	cout << "Grand Patrons: " << endl;
	int count_g = 0;
	for (int j = 0;j < num;j++)
	{
		if (pd[j].money > 10000)
		{
			cout << "name: " << pd[j].name << "\tmoney: " << pd[j].money << endl;
			count_g++;
			//cout << count << endl;;
		}
	}
	if (0 == count_g)//两个if没有先后
		cout << "None" << endl;
	//其他捐款人
	cout << "Patrons: " << endl;
	int count_p = 0;
	for (int j = 0;j < num;j++)
	{
		if (pd[j].money <= 10000)
		{
			cout << "name: " << pd[j].name << "\tmoney: " << pd[j].money << endl;
			count_p++;
		}
	}
	if (0 == count_p)
		cout << "None" << endl;

	inFile.close();//关闭文件
	delete [ ] pd;//释放指针
	return;
}

如有错误,欢迎指正!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值