第六章习题——C++ Primer Plus

这是一个写作业的系列,旨在po出运行通过的代码。前五章较为简单,于是没有写。从第六章开始,我会完成这一系列的作业。提高自己coding的熟练度。

编译环境:Visual Sudio 2017

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

//6.1
#include "stdafx.h"//笔记本电脑在VS运行时应该首先加入这个头文件,否则iostream无法工作
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
	
	cout << "Enter data until an @ to terminate the input.\n";
	char ch;
	while (cin.get(ch) && ch != '@')
	{
		if (ch >= 'a'&&ch <= 'z')
			ch = toupper(ch);
		else if (ch >= 'A'&&ch <= 'Z')
			ch = tolower(ch);
		cout << ch;
	}
	cout << "\nInput complete.\n";
	system("pause");
    return 0;
}

 

2. 编写一个程序,最多将10个donation值读入到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

#include "stdafx.h"
#include<iostream>
#include<array>
using namespace std;

int main()
{

	//double arr[10];//可以选择使用数组
	array<double, 10>arr;
	int i = 0, j, num;
	double sum = 0, average = 0,temp;
	cout << "输入最多10个donation值:(非数字时将结束输入)\n";
	while ((cin >> temp) && (i<10))
	{
		arr[i] = temp;		
		sum += arr[i++];
	}
	average = sum / i;
	for (j = 0, num = 0; j < i; j++)
	{
		if (arr[j] > average)
			num++;
	}
	cout << "平均值:" << average<< " ,有 "<< num<< " 个数字超过平均值"<< endl;
	system("pause");
	return 0;
}

 

3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	char ch;
	cout << "Please enter one of the following choices:\n";
	cout << "c)carnivore\t" << "p) pianist"<<endl;
	cout << "t)tree\t\t" << "g) game"<<endl;
	while (cin >> ch) {
		switch (ch)
		{
		case 'c':	cout << "Danger!\n";
			break;
		case 'p':	cout << "Enjoy the music.\n";
			break;
		case 't':	cout << "A maple is a tree.\n";
			break;
		case 'g':	cout << "No child don't love game!\n";
			break;
		default: cout << "Please enter a c, p, t, or g:";
			continue;
		}
	}
	system("pause");
	return 0;   
}

 

4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。

#include "stdafx.h"
#include <iostream>

const int strsize = 30;
struct bop{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	char preference[strsize];
};
using namespace std;
int main()
{
	bop information[5]{
		{"Wimp Macho","Professor","W.M","Wimp Macho" },
		{"Raki Rhodes", "Engineer", "Chock", "Junior Programmer" },
		{"Celia Laiter","Associate professor","Great C","MIPS"},
		{"Hoppy Hipman","Lecturer","HipHop","Analyst Trainee"},
		{ "Pat Hand","Student","RatCat","LOOPY"},
	};
	char ch;
	int i;
	cout << "a. diplay by name\t" << "b. display by title" << endl;
	cout << "c. display by bopname\t" << "d. display by preference" << endl;
	cout << "q. quit" << endl;
	cout << "Enter your choice:" ;
	while (cin>>ch&&ch != 'q')
	{
		switch(ch)
		{
			case 'a':
			for (i = 0; i < 5; i++)
				{
				cout << information[i].fullname << endl;
				}
				break;
			case 'b':
			for (i = 0; i < 5; i++)
				{
					cout << information[i].title << endl;
				}
				break;
			case 'c':
				for (i = 0; i < 5; i++)
				{
					cout << information[i].bopname << endl;
				}
				break;
			case 'd':
				for (i = 0; i < 5; i++)
				{
					cout << information[i].preference << endl;
				}
				break;
			default:
				cout << "Please recheck your input." << endl;
				cout << "Enter your choice(a,b,c,d,q)";

				continue;
		}
		cout << "Next choice:";
	}
	system("pause");
    return 0;
}

 

5. 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:

5000 tvarps”:不收税

5001~15000 tvarps:10%

15001~35000 tvarps:15%

35000以上: 20%

例如,收入为38000 tvarps时,所得税为5000*0.00 + 10000*0.10+20000*0.15+3000*0.20,即4600tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

写这题的代码时,我将不同的纳税区间定义成了常量,便于程序的修改。

#include "stdafx.h"
#include <iostream>

const int line1 = 5000;//第一条征税线
const int line2 = 15000;//第二条征税线
const int line3 = 35000;//第一条征税线

const int step1 = line1;	//第一征税区间
const int step2 = line2 - line1;//第一征税区间
const int step3 = line3 - line2;//第三征税区间

const float rate1 = 0.00;	//第一征税区间税率
const float rate2 = 0.10;	//第二征税区间税率
const float rate3 = 0.15;	//第三征税区间税率
const float rate4 = 0.20;   //第四征税区间税率

using namespace std;
int main()
{
	int income; 
	float tax = 0;
	cout << "请输入您的收入:";

	while (cin >> income&&income >= 0)
	{
		
		if (income > step1)
		{
			tax += step1*rate1;
			income -= step1;

			if (income > step2)
			{

				tax += step2*rate2;
				income -= step2;

				if (income > step3)
				{
					tax += step3*rate3;
					income -= step3;

					if (income > 0)
						tax += income*rate4;
				}
				else tax += income*rate3;
			}
			else tax +=  income*rate2;
		}
		else tax += income*rate1;

		cout << "经计算,您所需要缴纳税款为" << tax << "tvarps。\n";
		cout << "请输入您的收入:";
	} 
    return 0;
}

 

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

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

struct fundrecord
{
	string Name;
	double Money;
};
int main()
{
	unsigned int num;
	int i,flag;
	cout << "'维护合法权利团体'资金记录" << endl;
	cout << "捐献者人数:";
	while (!(cin >> num))
	{
		cin.clear();
		cin.get();
		cout<<"请检查输入,它必须是一个正整数。\n";
		cout << "捐献者人数:";
		
	}
	
	fundrecord *ps = new fundrecord[num];
	for(i =0;i<num;i++)
	{
		cout << "第" << i + 1 << "个捐献者记录:\n";
		cout << "姓名:";
		cin >> ps[i].Name;
		cout << "款项:";
		cin >> ps[i].Money;
	}
	flag = 0;
	cout << "\nGrand Patrons:" << endl;
	for(i=0;i<num;i++)
	{	
		if(ps[i].Money>10000)
		{
			flag++;
			cout << ps[i].Name << ",\t$" << ps[i].Money <<"\n\n";
		}
		
	}
	if (flag == 0)
			cout << "None\n\n";
	flag=0;
	cout << "Patrons:" << endl;
	for (i = 0; i<num; i++)
	{
		if (ps[i].Money<=10000)
		{
			flag++;
			cout << ps[i].Name << ",\t$" << ps[i].Money <<"\n\n";
		}
		
	}
	if (flag == 0)
			cout << "None\n\n" ;
	delete [] ps;
	system("pause");
    return 0;
}

 

6.7 编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。

#include "stdafx.h"
#include <iostream>
#include<string>
using namespace std;

const int ArSize = 10;
bool isvowels(char);
int main()
{
	string word;
	char ch;
	int i = 0,j = 0, k = 0;//i个元音,j个辅音,k个其他
	cout<<"Enter words (q to quit):\n";
	cin >> word;
	while (word != "q")
	{
			ch = word[0];
			if (!isalpha(ch))
				k++;
			else
				if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'||
					ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
					i++;
				else
					j++;
			cin >> word;
		
	} 
	cout << i << " words beginning with vowels.\n";
	cout << j << " words beginning with consonants.\n";
	cout << k << " others.\n";
	system("pause");
    return 0;
}

 

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

我这里还有一个没有解决的问题:打不开绝对路径的文件。太奇怪了。

使用调试模式的话,打开的文件1.txt要放在.vcxproj同一个文件夹下;使用exe执行的话,打开的文件1.txt要放在exe同一个文件夹下。

#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	//char filename[SIZE];
	//ofstream outFile;
	//outFile.open("1.txt");
	ifstream inFile;
	//cout << "Enter name of data file: ";
	//cin.getline(filename, SIZE);
	inFile.open("1.txt");
	if (!inFile.is_open())
	{
		cout << "Could not open the file:"<<endl;//<< filename << endl;
		cout << "Program terminating.\n";
		system("pause");
		exit(EXIT_FAILURE);
	}
	
	char zifu;
	int count=0;//读取有多少个字符
	inFile >> zifu;
	while (inFile.good())
	{
		++count;
		inFile >> zifu;
	}
	if (inFile.eof())
		cout << "End of file reached.\n";
	else if (inFile.fail())
		cout << "Input terminated by data mismatch.\n";
	else cout << "Input terminated by unknown reason.\n";
	if (count == 0)
		cout << "No data processed.\n";
	else
		cout << "文件中有" << count << "个字符。\n";
	inFile.close();
	system("pause");
    return 0;
}

 

6.9  完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:

4

Sam Stone

2000

Freida Flass

100500

Tammy Tubbs

5000

Rich Raptor

55000

// 66.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include<fstream>
#include <iostream>
using namespace std;

struct fundrecord
{
	string Name;
	double Money;
};
int main()
{
	//打开文件
	ifstream inFile;
	inFile.open("list.txt");
	if (!inFile.is_open())
	{
		cout << "Could not open the file:" << endl;
		cout << "Program terminating.\n";
		system("pause");
		exit(EXIT_FAILURE);
	}

	//处理数据
	unsigned int num;
	int i, flag;
	cout << "'维护合法权利团体'资金记录" << endl;
	cout << "捐献者人数:";
	inFile >> num;
	cout << num<<endl;
	fundrecord *ps = new fundrecord[num];
	inFile.get();//注意添加这一项接收回车\n
	for (i = 0; i<num; i++)
	{
		cout << "第" << i + 1 << "个捐献者记录:\n";
		cout << "姓名:";
		getline(inFile,ps[i].Name);
		cout << ps[i].Name<<"\t";
		cout << "款项:";
		inFile >> ps[i].Money;
		cout<< ps[i].Money<<endl;
		inFile.get();
			
	}
	flag = 0;
	cout << "\nGrand Patrons:" << endl;
	for (i = 0; i<num; i++)
	{
		if (ps[i].Money>10000)
		{
			flag++;
			cout << ps[i].Name << ",\t$" << ps[i].Money << "\n\n";
		}

	}
	if (flag == 0)
		cout << "None\n\n";
	flag = 0;
	cout << "Patrons:" << endl;
	for (i = 0; i<num; i++)
	{
		if (ps[i].Money <= 10000)
		{
			flag++;
			cout << ps[i].Name << ",\t$" << ps[i].Money << "\n\n";
		}

	}
	if (flag == 0)
		cout << "None\n\n";
	delete[] ps;	
	
	if (inFile.eof())
		cout << "End of file reached.\n";
	else if (inFile.fail())
		cout << "Input terminated by data mismatch.\n";
	else cout << "Input terminated by unknown reason.\n";
	inFile.close();

	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值