C++ primer plus编程第六章练习习题代码

本文通过几个具体的C++程序案例,介绍了如何处理字符输入、数组操作、条件选择、结构体应用及用户输入验证等问题,旨在帮助读者理解C++语言的基本语法和编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
#include <ctime>
#include <cstring>
#include <fstream>
#include <cctype>
using namespace std;

void test1()
{
	char ch;
	cin.get(ch);
	while (ch != '@')
	{
		if (islower(ch))   // 是小写
//			cout << char(ch - 32) ;
			cout << (char) toupper(ch);
		else if (isupper(ch))   //是大写
//			cout << (char)(ch + 32) ;
			cout << (char) tolower(ch);
		else if (isdigit(ch))   // 是数字
			;
		else
			cout << ch;
		cin >> ch;
	}
	cout << ch << endl;

	return;
}

运行结果:





void p6_2()
{
	double donation , sum = 0.0, averge = 0.0, array[10] = { 0.0 };
	int count = 0;
	cout << "Please enter words: ";
	while ((cin >> donation) &&  !isdigit(donation))
	{

		if (count < 10){
			array[count] = donation;
			sum += array[count];
			++count;
		}
		else
		{
			cout << "There are more than 10 words.\n";
			break;
		}

	}

	cout << "count  = " << count << endl;

	averge = sum / (count + 1);
	cout << "averge value = " << averge << endl;

	for (int j = 0; j <= count; j++)
		if (array[j] > averge){
			cout << "greater than averge: " << array[j] << endl;
		}


	return;
}

void p6_3()
{
	char ch;
	cout << "Please enter one of the following choices:\n";
	cout << "c) carnivore           p) pianist" << endl;
	cout << "t) tree                g) game"    << endl;
	while (cin >> ch){
		switch(ch)
		{
		case 'c':
			cout << "A maple is a carnivore." << endl;
			break;
		case 'p':
			cout << "A maple is a pianist." << endl;
			break;
		case 't':
			cout << "A maple is a tree." << endl;
			break;
		case 'g':
			cout << "A maple is a game." << endl;
			break;
		default:
			cout << "Please enter a c, p, t, or g: ";
			continue;
		}
	}
	return;
}

运行结果;




Wim

void p6_4()
{
	typedef struct bop
	{
		char fullname[size];
		char title[size];
		char bopname[size];
		int preference;  // 0 = fullname, 1 = title, 2 = bopname
	}tbop;

	tbop BOP[5] = {
			{"Wimp Macho", "teacher", "A", 0},
			{"Raki Rhodes", "Junior Progammer", "B", 1},
			{"Celia Laiter", "docter", "MIPS", 2},
			{"Hoppy Hipman", "Analyst Trainee", "C", 1},
			{"Pat Hand", "officer", "LOOPY", 2}
	};
	char ch;
	cout << "Benevolent Order of Programers Report\n";
	cout << "a. display by name     "  << "b. display by title\n";
	cout << "c. display by bopname   " << "d. display by preference\n";
	cout << "q. quit\n";

	cout << "Enter your choice: ";
	while (cin >> ch  && ch != 'q'){
		switch(ch)
		{
		case 'a':
			cout << BOP[0].fullname << endl;
			cout << BOP[1].fullname << endl;
			cout << BOP[2].fullname << endl;
			cout << BOP[3].fullname << endl;
			cout << BOP[4].fullname << endl;
			break;
		case 'b':
			cout << BOP[0].title << endl;
			cout << BOP[1].title << endl;
			cout << BOP[2].title << endl;
			cout << BOP[3].title << endl;
			cout << BOP[4].title << endl;
			break;
		case 'c':
			cout << BOP[0].bopname << endl;
			cout << BOP[1].bopname << endl;
			cout << BOP[2].bopname << endl;
			cout << BOP[3].bopname << endl;
			cout << BOP[4].bopname << endl;
			break;
		case 'd':
			for (int i = 0; i < 5; i++)
			{
				if(BOP[i].preference == 0)
					cout << BOP[i].fullname << endl;
				if(BOP[i].preference == 1)
					cout << BOP[i].title << endl;
				if(BOP[i].preference == 2)
					cout << BOP[i].bopname << endl;
			}
			break;
		default:
			break;
		}
		cout << "Next choice: ";
	}
	cout << "Bye!\n";
	return;
}

void p6_5()
{
	int income = 0;;
	double tax = 0.0;
	cout << "Please enter income of user: ";
	// 检测输入为负数或者非数字, 退出循环
	while (cin >> income && income >= 0 && !isdigit(income))
	{
		if (income >= 0 && income <= 5000)
			tax = 0.0;
		else if (income >= 5001 && income <= 15000)
			tax = (income - 5000) * 0.1;
		else if (income >= 15001 && income <= 35000)
			tax = 10000 * 0.1 + (income - 15000) * 0.15;
		else
			tax = 10000 * 0.1 + 15000 * 0.15 + (income - 35000) * 0.20;

		cout << "income of user is " << income << " tvarps.\n";
		cout << "tax of user is " << tax << " tvarps.\n";
	}
	return;
}

输出结果




void p6_6()
{
	struct Team {
		string name;
		double donation;
	};
	cout << "捐赠者的数量: " ;
	int num , count = 0;
	cin >> num;
	Team* team = new Team[num];
	for (int i = 0; i < num; i++)
	{
		cout << "输入第" << i+1 << "位姓名: ";
		cin >> team[i].name;
		cout << "输入第" << i+1 << "位款项: ";
		cin >> team[i].donation;
	}
	cout << "Grand Patrons\n";
	for (int i = 0; i < num; i++)
	{
		if (team[i].donation >= 10000){
			cout << team[i].name << "  " << team[i].donation << endl;
			count++;
		}
	}
	if (count == 0)
		cout << "none"  << endl;

	count = 0;
	cout << "Patrons"  << endl;
	for (int i = 0; i < num; i++)
	{
		if (team[i].donation < 10000){
			cout << team[i].name << "  " << team[i].donation << endl;
			count++;
		}
	}
	if (count == 0)
		cout << "none"  << endl;

	return;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值