【C++ Primer Plus学习记录】第6章编程练习

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

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

int main()
{
	char input;
	cout << "Please enter a character:";
	cin >> input;

	while (input != '@')
	{
		if (isdigit(input))   //isdigit()函数:检查参数 c 是否为阿拉伯数字0 到9。若参数c为阿拉伯数字0~9,则返回非0值,否则返回0。
		{
			cin >> input;
			continue;//continue语句用于循环中,让程序跳过循环体中余下的代码,并开始新一轮循环
		}
		else if (islower(input))  //islower()函数:判断参数c是否为小写字母。
		{
			input = toupper(input);  //toupper()函数:把小写字母转换为大写字母。
		}
		else
		{
			input = tolower(input);  // tolower()函数:把大写字母转换为小写字母。
		}
		cout << input;
		cin >> input;
	}
	system("pause");
	return 0;
}
#endif

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

#if 1
//编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。
//程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream>
#include<array>
#include<cctype>
using namespace std;

int main()
{
	cout << "Please input 10 donation: \n";
	array<double,10> kk;
	double sum = 0, ave;
	
	int i = 0;
	//for (i = 0; i < 10; i++)
		double ans = 0;
		//kk[i] = 0;
		char a;//char才可以接收非数字输入
		while (1){
			if (i >= 9)
			{
				break;
			}
			a = cin.get();//char才可以接收非数字输入
			if (a == '!')
			{
				//cout << ans << endl;
				//kk[i] = ans; 
				//cout << kk[i] << endl;
				//i = i + 1;
				break;
			}
			if (a == ' ')
			{
				//cout << ans << endl;
				//kk[i] = ans;
				//cout << kk[i] << endl;
				//i++;
				ans = 0;
				continue;
			}
			ans = ans * 10 + (a - '0');
			kk[i] = ans;
			cout << kk[i] << endl;
			sum = sum + kk[i];
			i++;
		}
		cout << "和为:" << sum << endl;
		ave = sum / i;
		cout << "平均值:" << ave << endl;
		cout << "大于平均值的有:";
		for (int j = 0; j <= i; j++)
		{
			if (kk[j] > ave)
			{
				cout << kk[j] << " ";
			}
		}

	system("pause");
	return 0;
}
#endif

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.

//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.

#if 1
#include<iostream>
using namespace std;
void showmenu();  //shaowmenu()函数声明

int main()
{
	char choice;
	showmenu();
	cin.get(choice);  //显示菜单,并读取用户输入,保存至choice变量中

	while (choice != 'c' && choice != 'p' && choice != 't' && choice != 'g')
	{
		//输入第一个字符时,需要敲回车键以进行下一步,而上一行的cin.get(choice);只能接收输入的第一个字符,因此回车键会在输入流中,需要cin.get();  把回车键这个输入处理掉。
		//回车键在代码过程中担任两个角色:1.作为一个输入字符;2.作为整个程序的一个字符结束的标志。
		cin.get();  
		cout << "please enter a,c,p,t,or g: ";
		cin.get(choice);
	}

	switch (choice)
	{
	case 'c':
		break;
	case 'p':
		break;
	case 't':
		cout << "As ample is a tree." << endl;
	case 'g':
		break;

	}

	system("pause");
	return 0;
}

void showmenu()
{
	cout << "Please enter one of the following choice:\n";
	cout << "c)carnivore \t\t\t p)pianist\n";
	cout << "t)tree\t\t\t\t g)game\n";
}//showmenu()函数只负责菜单传递信息的输出
#endif

4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
// Benevolent Order of Programmer 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 Programmer 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!

#if 1
#include<iostream>
using namespace std;

const int strsize = 40;
const int usersize = 5;

//结构是一种比数组更灵活的数据格式,因为同一个结构可以存储多种类型的数据
//Benevolent Order of Programmer 姓名结构体
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 bop_user[usersize] =
{
	{"Wimp Macho","Programmer","MIPS",0},
	{ "Raki Rhodes", "Junior Programmer","",1},
	{"Celia Laiter","","MIPS",2},
	{"Hoppy Hipman","Analyst Trainee","",1},
	{ "Pat Hand", "", "LOOPY",2 },
};//定义常量,定义结构体初始化bop数组信息

void showmenu();//showmenu()函数声明
void print_by_name(); //打印姓名函数声明
void print_by_pref();//打印偏好函数声明
void print_by_title();//打印头衔函数声明
void print_by_bopname();//打印秘密姓名函数声明
void create_info();

int main()
{
	char choice;
	showmenu();
	cout << "Enter your choice: ";
	cin.get(choice);//显示菜单读取用户输入

	while (choice != 'q')
	{
		switch (choice)
		{
		case 'a':
			print_by_name();
			break;
		case 'b':
			print_by_title();
			break;
		case 'c':
			print_by_bopname();
			break;
		case 'd':
			print_by_pref();
			break;
		default:
			cout << "Please enter character a,b,c,d,or q:" << endl;
		}
		cout << "Next choice:";
		cin.get(choice);
	}
	//将switch语句放在while循环中,可以反复循环给下那个功能
	cout << "Bye!" << endl;

	system("pause");
	return 0;
}

void showmenu()
{
	cout << "Benevolent Order Programmer Report" << endl;
	cout << "a. display by name\t\t b. display by title" << endl;
	cout << "c.display by bopname\t\t d.display by preference" << endl;
	cout << "q. quit" << endl;
}//显示菜单

void print_by_name()
{
	for (int i = 0; i < usersize; i++)
	{
		if (bop_user[i].fullname == 0)
			break;
		else
			cout << bop_user[i].fullname << endl;
	}
}

void print_by_pref()
{
	for (int i = 0; i < usersize; i++)
	{
		if (bop_user[i].fullname == 0)
			break;
		else
		{
			switch (bop_user[i].preference)
			{
			case 0:
				cout << bop_user[i].fullname << endl;
				break;
			case 1:
				cout << bop_user[i].title << endl;
				break;
			case 2:
				cout << bop_user[i].bopname << endl;
				break;
			}
		}
	}
}

void print_by_title()
{
	for (int i = 0; i < usersize; i++)
	{
		if (bop_user[i].fullname == 0)
			break;
		else
			cout << bop_user[i].title << endl;
	}
}

void print_by_bopname()
{
	for (int i = 0; i < usersize; i++)
	{
		if (bop_user[i].fullname == 0)
			break;
		else
			cout << bop_user[i].bopname << endl;
	}
}

/*
void create_info()
{
	cout << "Enter the user's full name: ";
	cin.getline(bop_user[i].fullname, strsize);
	cout << "Enter the user's title: ";
	cin.getline(bop_user[i].title, strsize);
	cout << "Enter the user's bopname: ";
	cin.getline(bop_user[i].bopname, strsize);
	cout << "Enter the user's preference: ";
	cin >> bop_user[i].preference;
	cout << "Next...(f for finished):";
	cin.get();
	if (cin.get() == 'f')
		break;
}*/
#endif

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

#if 1
#include<iostream>
using namespace std;

int main()
{
	float salary, tax;//定义salary为float类型,即salary不可能为非数字
	cout << "Please enter your salary: ";
	cin >> salary;

	while (salary > 0)
	{
		if (salary <= 5000)
		{
			tax = 0;
		}
		else if (salary <= 15000)
		{
			tax = (salary - 5000) * 0.10;
		}
		else if (salary <= 35000)
		{
			tax = (salary - 15000) * 0.15 + 10000 * 0.1;
		}
		else if(salary > 35000)
		{
			tax = (salary - 35000) * 0.2 + 20000 * 0.15 + 10000 * 0.1;
		}
		cout << "Your salary is " << salary << " trarps,and you should pay ";
		cout << tax << " trarps of tax." << endl;
		cout << "Enter your salary:";
		cin >> salary;
	}
	system("pause");
	return 0;
}
#endif

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

#if 1
#include<iostream>
#include<string>
using namespace std;

struct patrons
{
	string name;
	double fund;
};

int main()
{
	int patrons_number;
	patrons *ppatrons;
	cout << "How many patrons?";
	cin >> patrons_number;
	cin.get();
	//输入第一个字符时,需要敲回车键以进行下一步,而上一行的cin >> patrons_number;只能接收输入的第一个字符,因此回车键会在输入流中,需要cin.get();  把回车键这个输入处理掉。
		//回车键在代码过程中担任两个角色:1.作为一个输入字符;2.作为整个程序的一个字符结束的标志。
	ppatrons = new patrons[patrons_number];//建立动态数组
	int id = 0;
	bool empty = true;
	cout << "Starting to input patron's info:";
	while (id < patrons_number)
	{
		cout << "Enter the full name of patrons: ";
		getline(cin, ppatrons[id].name);
		cout << "Enter the fund of " << ppatrons[id].name << " :";
		cin >> ppatrons[id].fund;
		cin.get();
		id++;
		cout << "Continue to input,or press (f) to finished.";
		if (cin.get() == 'f')
			break;
	}//建立捐款人名单

	cout << "Grand Patrons" << endl;//该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。
	for (int i = 0; i < patrons_number; i++)
	{
		if (ppatrons[i].fund >= 10000)
		{
			cout << ppatrons[i].name << ": " << ppatrons[i].fund << endl;
			empty = false;
		}
	}
	if (empty)
		cout << "NONE" << endl;
	empty = false;

	cout << "Patrons" << endl;
	for (int i = 0; i < patrons_number; i++)
	{
		if (ppatrons[i].fund < 10000)
		{
			cout << ppatrons[i].name << ": " << ppatrons[i].fund << endl;
			empty = false;
		}
	}
	if (empty)
		cout << "NONE" << endl;
	//empty = false;

	system("pause");
	return 0;
}
#endif

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 consotants
2 others

/*
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 consotants
2 others
*/
#if 1
#include<iostream>
#include<string>
using namespace std;

int main()
{
	char words[40];
	int vowel, consonant, others;
	vowel = consonant = others = 0;

	cout << "Enter words(q to quit): " << endl;
	cin >> words;

	while (strcmp(words, "q") != 0)//比较
	{
		if (!isalpha(words[0]))
		{
			others++;
		}
		else
		{
			switch (words[0])
			{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				vowel++;
				break;
			default:
				consonant++;
			}
		}
		cin >> words;
	}
	
	cout << vowel << " words beginning with vowel." << endl;
	cout << consonant << " words beginning with consonants." << endl;
	cout << others << "others" << endl;

	system("pause");
	return 0;
}
#endif

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

/*
8.编写一个程序,它打开一个文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
*/
#if 1
#include<iostream>
#include<fstream>
#include<cstdlib>//函数exit()的原型是在头文件cstdlib中定义的,在该头文件中,还定义了一个用于同操作系统通信的参数值EXIT_FAILURE。函数exit()终止程序。
using namespace std;
const int SIZE = 60;

int main()
{
	//ofstream outFile;//声明一个文件输出对象
	//outFile.open("carinfo.tat");//将上述对象与特定文件关联起来
	char filename[SIZE];
	ifstream inFile;//声明一个文件输入对象
	
	cout << "Enter name of data file: ";
	cin.getline(filename, SIZE);//输入文件名
	inFile.open(filename);//关联文件
	
	//检查文件是否被成功打开
	if (!inFile.is_open())   //failed to open file
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	 }

	char read_char;//字符
	int count = 0;//number of items read
	while (!inFile.eof())//直到到达文件末尾
	{
		inFile >> read_char;//逐个读字符
		count++;
	}
	cout << "该文件包含" << count << "个字符。" << endl;
	inFile.close();

	system("pause");
	return 0;
}
#endif

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

#if 1
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int SIZE = 60;

struct patrons
{
	string name;
	double fund;
};

int main()
{
	//int patrons_number;
	//cout << "How many patrons?";
	//cin >> patrons_number;
	//cin.get();
	//输入第一个字符时,需要敲回车键以进行下一步,而上一行的cin >> patrons_number;只能接收输入的第一个字符,因此回车键会在输入流中,需要cin.get();  把回车键这个输入处理掉。
	//回车键在代码过程中担任两个角色:1.作为一个输入字符;2.作为整个程序的一个字符结束的标志。
	
	char filename[SIZE];
	ifstream inFile;//声明一个文件输入对象

	cout << "Enter name of data file: ";
	cin.getline(filename, SIZE);//输入文件名
	inFile.open(filename);//关联文件

	//检查文件是否被成功打开
	if (!inFile.is_open())   //failed to open file
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}

	int patrons_number;
	//cout << "How many patrons?";
	//inFile >> patrons_number;
	//inFile.get();
	patrons *ppatrons;
	int id = 0;
	bool empty = true;

	inFile >> patrons_number;
	if (patrons_number <= 0)
	{
		exit(EXIT_FAILURE);
	}
	ppatrons = new patrons[patrons_number];
	inFile.get();

	//cout << "Starting to input patron's info:";
	while (!inFile.eof() && id < patrons_number)
	{
		getline(inFile, ppatrons[id].name);
		cout << "Enter the full name of patrons: " << ppatrons[id].name << endl;;
		inFile >> ppatrons[id].fund;
		cout << "Enter the fund of " << ppatrons[id].fund << endl;
		inFile.get();
		id++;
		//cout << "Continue to input,or press (f) to finished.";
		//if (cin.get() == 'f')
			//break;
	}
	inFile.close();

	cout << "Grand Patrons" << endl;//该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。
	for (int i = 0; i < patrons_number; i++)
	{
		if (ppatrons[i].fund >= 10000)
		{
			cout << ppatrons[i].name << ": " << ppatrons[i].fund << endl;
			empty = false;
		}
	}
	if (empty)
		cout << "NONE" << endl;
	empty = false;

	cout << "Patrons" << endl;
	for (int i = 0; i < patrons_number; i++)
	{
		if (ppatrons[i].fund < 10000)
		{
			cout << ppatrons[i].name << ": " << ppatrons[i].fund << endl;
			empty = false;
		}
	}
	if (empty)
		cout << "NONE" << endl;
	//empty = false;

	system("pause");
	return 0;
}
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值