第六章编程练习

1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。

#include <iostream>
#include <cctype>

int main(void)
{
	using std::cin;
	using std::cout;
	using std::endl;
	

	char input;

	cout << "Please enter a character:";
	cin >> input;

	while (input != '@')
	{
		if (isdigit(input))
		{
			cin >> input;
			continue;
		}
		else if(islower(input))
		{
			input = toupper(input);
		}
		else
		{
			input = tolower(input);
		}

		cout << input;
		cin >> input;
	}

	return 0;
}

2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

	array<double, 10> donation;

	double input;
	double average;
	double sum = 0.0;
	int count = 0;
	int bigger = 0;

	cout << "Please enter the double numerial: ";

	while ( cin >> input )
	{
		donation[count] = input;
		count++;
		if (count == 10)
		{
			break;
		}
		cout << "Please enter the next one: ";
	}

	for (int i = 0; i < count; i++)
	{
		sum += donation[i];
		average = sum / count;
	}

	for (int i = 0; i < count; i++)
	{
		if (donation[i] > average)
			bigger++;
	}

	cout << "You have entered " << count << " numbers, the average is " << average;;
	cout << ". And there are " << bigger << " numbers are bigger than the average" << ".\n";

3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单–每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c)carnivore p)pianist
t)tree g)game
f
Please enter a c, p, t, or g:q
Please enter a c, p, t, or g:t
A maple is a tree.

void ShowMenu(void);

using std::cin;
using std::cout;
using std::endl;

int main(void)
{
	char ch;
	ShowMenu();
	cin.get(ch);
	
	while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g')
	{
		cin.get();//消耗回车
		cout << "Please enter a character: c, p, t, g: ";
		cin.get(ch);
	}

	switch (ch)
	{
	case 'c':
		cout << "Carnivore." << endl;
		break;
	case 'p': 
		cout << "Pianist" << endl;
		break;
	case 't':
		cout << "A maple is a tree." << endl;
		break;
	case 'g': 
		cout << "Game" << endl;
		break;
	}

	return 0;
}

void ShowMenu(void)
{
	cout << "Please enter one of the following choices: " << endl;
	cout << "c)carnivore\t\tp)pianist" << endl;
	cout << "t)tree\t\t\tg)game" << endl;
}

4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
//Benevolent Order of Programmers name structure
struct bop
{
char fullname[STRSIZE]; //real name
char title[STRSIZE]; //job title
char bopname[STRSIZE]; //secret BOP name
int preference; //0 = fullname, 1 = title, 2 = bopname
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d.display by preference
q. quit
注意,“display by preference"并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员,例如,如果偏好为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d.display by preference
q. quit
Enter your choice:a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice:d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice:q
Bye!

const int STRSIZE = 40;
const int USERSIZE = 4;

//Benevolent Order of Programmers name structure
struct bop
{
	char fullname[STRSIZE];	//real name
	char title[STRSIZE];	//job title
	char bopname[STRSIZE];	//secret BOP name
	int preference;			//0 = fullname, 1 = title, 2 = bopname
};

bop user[USERSIZE] = 
{
	{"Rick", "level_a", "RR", 0},
	{"Jack", "level_b", "JJ", 1},
	{"Michal", "level_c", "MM", 2},
	{"Rose", "level_d", "RR", 0}
};

void DisplayName(void);
void PrintByFullname(void);
void PrintByTitle(void);
void PrintByBopname(void);
void PrintByPreference(void);

int main(void)
{
	char input;

	DisplayName();

	cin.get(input);

	while (input != 'q')
	{
		switch (input)
		{
		case 'a':
			PrintByFullname();
			break;
		case 'b':
			PrintByTitle();
			break;
		case 'c':
			PrintByBopname();
			break;
		case 'd':
			PrintByPreference();
			break;
		default:
			cout << "Please enter character a, b, c, d, q: " << endl;
			break;
		}

		cin.get();//消耗回车
		cout << "Next choice: ";
		cin.get(input);
	}

	cout << "Bye!" << endl;

	return 0;
}

void DisplayName(void)
{
	cout << "Benevolent Order of Programmer Report" << endl;
	cout << "a.display by name\t\tb.display by title" << endl;
	cout << "c.display by bopname\t\td.display by preference" << endl;
	cout << "q.quit" << endl;
}

void PrintByFullname(void)
{
	for (int i = 0; i < USERSIZE; i++)
	{
		cout << user[i].fullname << endl;
	}
	
}

void PrintByTitle(void)
{
	for (int i = 0; i < USERSIZE; i++)
	{
		cout << user[i].title << endl;
	}
}

void PrintByBopname(void)
{
	for (int i = 0; i < USERSIZE; i++)
	{
		cout << user[i].bopname << endl;
	}
}

void PrintByPreference(void)
{
	for (int i = 0; i < USERSIZE; i++)
	{
		switch (user[i].preference)
		{
		case 0:
			cout << user[i].fullname << endl;
			break;
		case 1:
			cout << user[i].title << endl;
			break;
		case 2:
			cout << user[i].bopname << endl;
			break;
		default:
			break;
		}
	}
}

5.在Neutronia王国,货币单位时tvarp,收入所得税的计算方式如下:
5000tvarp:不收税
5001-15000: 10%
15001-35000: 15%
35000tvarp以上: 20%
例如,收入为38000tvarps时,所得税为50000.00+100000.10+200000.15+30000.20,即4600tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

	double income = 0, tax = 0;

	cout << "Please enter your income:";
	
	while ((cin >> income) && income > 0 )
	{
		//5000收入 0税率
		if (income <= 5000)
		{
			tax = 0;
		}
		else if (income <= 15000)
		{
			tax = (income - 5000) * 0.10;
		}
		else if (income <= 35000)
		{
			tax = 10000 * 0.1 + (income - 15000) * 0.15;
		}
		else
		{
			tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.20;
		}

		cout << "Your tax is " << tax << endl;

		cout << "Please enter your next income: ";
	}

6.编写一个程序,记录捐助给“维护合法权利团队”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息存储在一个动态分配的结构数组中。每个结构有两个成员:用来存储姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patron开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。

struct Patrons
{
	string name;
	double donation;
};

int main(void)
{
	int number;
	Patrons* p_donation;
	bool empty = true;

	cout << "Please enter the number of donors:";
	cin >> number;

	p_donation = new Patrons[number];

	for (int i = 0; i < number; i++)
	{
		cout << "donor #" << i + 1 << endl;
		cout << "Enter the name of donor:";
		cin >> p_donation[i].name;
		cout << "Enter the donation amount:";
		cin >> p_donation[i].donation;
	}

	cout << "***********Grand Patrons***********:" << endl;
	for (int i = 0; i < number; i++)
	{
		if (p_donation[i].donation >= 10000)
		{
			cout << p_donation[i].name << "\t" << p_donation[i].donation << endl;
			empty = false;
		}
	}
	if (empty == true)
	{
		cout << "none!" << endl;
	}

	empty = true;

	cout << "**************Patrons**************:" << endl;
	for (int i = 0; i < number; i++)
	{
		if (p_donation[i].donation < 10000)
		{
			cout << p_donation[i].name << "\t" << p_donation[i].donation << endl;
			empty = false;
		}
	}
	if (empty == true)
	{
		cout << "none!" << endl;
	}

	return 0;
}

7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以元音字母和其他字母打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words(q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others

	string words;
	int vowels = 0, consonants = 0, others = 0;

	cout << "Please enter words(q to quit):" << endl;;
	while ((cin >> words) && words != "q")
	{
		if (isalpha(words[0]))
		{
			switch (words[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++;
				break;
			}
		}
		else
		{
			others++;
		}
	}

	cout << "vowels:" << vowels << endl;
	cout << "consonants:" << consonants << endl;
	cout << "others:" << others << endl;

8.编写一个程序,它打开一个文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。

	ifstream in_file;
	string file_name;
	char ch;
	int count = 0;

	cout << "Enter the file name:";
	getline(cin, file_name);//or cin.getline(file_name,SIZE_OF_NAME);
	in_file.open(file_name);
	if (!in_file.is_open())
	{
		cout << "Failed to open the file!" << endl;
		exit(EXIT_FAILURE);
	}

	while (!in_file.eof())
	{
		in_file >> ch;
		count++;
	}
	
	//eof这个文件结束标志是在文件的最后一个字符之后的,当读入最后一个字符的时候文件并没有读到标志,
	//只有下一次读的时候才会读到。
	//解决方法:
	//while (in_file.peek() != EOF)
	//{
	//	in_file >> ch;
	//	count++;
	//}
	
	cout << file_name << " has " << count << " characters." << endl;

	in_file.close();

9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;

struct Patrons
{
	string name;
	double donation;
};

int main(void)
{
	int number;
	Patrons* p_donation;
	bool empty = true;
	ifstream in_file;
	string file_name;
	int i = 0;

	cout << "Enter the name of the file:" << endl;
	getline(cin, file_name);
	in_file.open(file_name);

	if (!in_file.is_open())
	{
		cout << "Failed to open the file!" << endl;
		exit(EXIT_FAILURE);
	}

	in_file >> number;
	if (number <= 0)
	{
		exit(EXIT_FAILURE);
	}

	p_donation = new Patrons[number];

	in_file.get();//消耗换行

	while ((!in_file.eof()) && (i < number))
	{
		getline(in_file, p_donation[i].name);
		cout << "Name:" << p_donation[i].name << endl;
		in_file >> p_donation[i].donation;
		cout << "Donation:" << p_donation[i].donation << endl;
		i++;
		in_file.get();
	}

	cout << "***********Grand Patrons***********:" << endl;
	for (int i = 0; i < number; i++)
	{
		if (p_donation[i].donation >= 10000)
		{
			cout << p_donation[i].name << "\t" << p_donation[i].donation << endl;
			empty = false;
		}
	}
	if (empty == true)
	{
		cout << "none!" << endl;
	}

	empty = true;

	cout << "**************Patrons**************:" << endl;
	for (int i = 0; i < number; i++)
	{
		if (p_donation[i].donation < 10000)
		{
			cout << p_donation[i].name << "\t" << p_donation[i].donation << endl;
			empty = false;
		}
	}
	if (empty == true)
	{
		cout << "none!" << endl;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值