C++ Primer Pluse_6_课后题

#include <iostream>
#include <cctype>
#include <array>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>

using namespace std;

int t6_1()
{

	char ch;

	cout << "Enter text for analysis, and type # to terminate input.\n";
	cin.get(ch);
	while (ch != ‘@‘)
	{
		if (isdigit(ch))
		{
			cin.get(ch);
			continue;
		}
		else if (isupper(ch))
		{
			ch = tolower(ch);
		}
		else if (islower(ch))
		{
			ch = toupper(ch);
		}
		cout << ch;

		cin.get(ch);
	}
	cout << endl;
	system("pause");
	return 0;
}

const int MaxSize = 10;
int t6_2()
{
	array<double, MaxSize>donations = { 0 };

	cout << "input the donations:\n";

	cout << "donation#1:";
	int i = 0;
	while (i <MaxSize && cin >> donations[i])
	{
		if (++i <MaxSize)
		{
			cout << "donation#" << i + 1<<":";
		}
	}

	double total = 0;
	double average = 0;

	for (int j = 0; j < i; j++)
	{
		total += donations[j];
	}
	if (i==0)
	{
		cout << "No donations.\n";
	}
	else
	{
		average = total / i;
		cout << "Average is " << average << endl;
		cout << "Bigger than average: ";
		for (int j = 0; j < i; j++)
		{

			if (donations[j] > average)
			{
				cout << donations[j] << " ";
			}
		}
	}

	system("pause");
	return 0;
}

int t6_3()
{
	char ch;

	cout << "Please enter aone of the following choices:\n";
	cout << "c) carnivore\t\t\t p) pianist\n";
	cout << "t) tree\t\t\t\t g) game\n";
	cin.get(ch).get();

	while (true)
	{
		switch (ch)
		{
		case ‘c‘: cout << "A map is a carnivore.\n"; break;
		case ‘p‘: cout << "A map is a pianist.\n"; break;
		case ‘t‘: cout << "A map is a tree.\n"; break;
		case ‘g‘: cout << "A map is a game.\n"; break;
		default:
			cout << "Please enter a c, p, t, g:";
			break;
		}
		cin.get(ch).get();
	}

	system("pause");
	return 0;
}
const int strsize = 20;
typedef struct Bop{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
}bop;
int t6_4()
{
	bop b1 = { "bopp1", "title1", "bop1", 1 };
	bop b2 = { "bopp2", "title2", "bop2", 0 };
	bop b3 = { "bopp3", "title3", "bop3", 2 };

	cout << "BOP report.\n";
	cout << "a.display by name.\t\t\t b.display by title.\n";
	cout << "c.display by bopname.\t\t d.display by preference.\n";
	cout << "q.quit.\n";
	cout << "Enter your choice:";
	char ch;
	cin.get(ch).get();
	while (ch != ‘q‘)
	{
		switch (ch)
		{
		case ‘a‘:
			cout << b1.fullname << endl;
			cout << b2.fullname << endl;
			cout << b3.fullname << endl;
			break;
		case ‘b‘:
			cout << b1.title << endl;
			cout << b2.title << endl;
			cout << b3.title << endl;
			break;
		case ‘c‘:
			cout << b1.bopname << endl;
			cout << b2.bopname << endl;
			cout << b3.bopname << endl;
			break;
		case ‘d‘:
			if (b1.preference== 0)
			{
				cout << b1.fullname << endl;
			}
			else if (b1.preference ==1)
			{
				cout << b1.title << endl;
			}
			else
			{
				cout << b1.bopname << endl;
			}

			if (b2.preference == 0)
			{
				cout << b2.fullname << endl;
			}
			else if (b2.preference == 1)
			{
				cout << b2.title << endl;
			}
			else
			{
				cout << b2.bopname << endl;
			}

			if (b3.preference == 0)
			{
				cout << b3.fullname << endl;
			}
			else if (b3.preference == 1)
			{
				cout << b3.title << endl;
			}
			else
			{
				cout << b3.bopname << endl;
			}
			break;

		default:
			cout << "Please input a a, b, c, d ,q:";
		}
		cin.get(ch).get();
	}
	cout << "Bye!";

	system("pause");
	return 0;
}

int t6_5()
{
	double wage = 0;
	double tax = 0;

	while (cin>>wage && wage >=0)
	{
		if (wage <= 5000)
		{
			tax = 0;
		}
		else if (wage > 5000 && wage <= 1500)
		{
			tax = 5000 * 0 + (wage - 5000)*0.1;
		}
		else if (wage > 1500 && wage <= 3500)
		{
			tax = 5000 * 0 + 10000 * 0.1 + (wage - 15000)*0.15;
		}
		else
		{
			tax = 5000 * 0 + 10000 * 0.1 + 20000 * 0.15 + (wage - 35000)*0.2;
		}

		cout << "tax = " << tax << endl;
	}

	system("pause");
	return 0;
}

typedef struct Donor
{
	string name;
	double money;
}donor;
int t6_6()
{
	int donors = 0;

	cout << "Enter the number of donors.\n";
	cin >> donors;
	cin.get();
	donor *donorSet = new donor[donors];

	for (int i = 0; i < donors; i++)
	{
		cout << "Enter donor#" << i + 1 << ":\n";
		getline(cin, donorSet[i].name);
		cin >> donorSet[i].money;
		cin.get();
	}

	cout << "\nGrand Patrons:\n";
	int flag = 0;
	for (int i = 0; i < donors; i++)
	{
		if (donorSet[i].money > 10000)
		{
			cout << donorSet[i].name << endl;
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << "None.\n";
	}

	cout << "Patrons:\n";
	flag = 0;
	for (int i = 0; i < donors; i++)
	{
		if (donorSet[i].money <= 10000)
		{
			cout << donorSet[i].name << endl;
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << "None.\n";
	}

	system("pause");
	return 0;
}

int t6_7()
{
	int vowelCount = 0;
	int consonantsCount = 0;
	char ch;

	cout << "Enter words (q to quit):\n";
	cin.get(ch);
	while (ch != ‘q‘)
	{
		if (isalpha(ch))
		{
			if (tolower(ch) == ‘a‘ || tolower(ch) == ‘e‘ || tolower(ch) == ‘i‘ || tolower(ch) == ‘o‘ || tolower(ch) == ‘u‘)
			{
				vowelCount++;
			}
			else
				consonantsCount++;

			cin.get(ch);
			while (ch != ‘ ‘&& ch != ‘\n‘)
			{
				cin.get(ch);
			}
		}
		else
			cin.get(ch);
	}

	cout << vowelCount << " words beginning with vowels.\n";
	cout << consonantsCount << " words beginning with consonants.\n";
	//system("pause");
	return 0;
}

const int SIZE = 60;
int test6_8()
{
	char filename[SIZE];
	ifstream inFile;
	cout << "Enter the name of the data file:";
	cin.getline(filename,SIZE);
	inFile.open(filename);

	if (!inFile.is_open())//failed to open file
	{
		cout << "Could not open the file.\n";
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}

	char ch;
	int count = 0;

	inFile >> ch;
	while (inFile.good())
	{
		count++;
		inFile >> ch;
		cout << ch;
	}

	if (inFile.eof())
	{
		cout << "End of the file.\n";
	}
	else if (inFile.fail())
	{
		cout << "Input terminated by data mismatch.\n";
	}
	else
		cout << "Input terminated for unknown reason.\n";

	inFile.close();

	cout << "file has " << count << "characters.\n";

	return 0;
}

int test6_9()
{
	char filename[SIZE];
	ifstream inFile;
	cout << "Enter the file name: ";
	cin.getline(filename, SIZE);

	inFile.open(filename);
	if (!inFile.is_open())
	{
		cout << "File cannot be opened.\n";
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}

	int donors = 0;

	cout << "Enter the number of donors.\n";
	inFile >> donors;
	inFile.get();
	donor *donorSet = new donor[donors];

	for (int i = 0; i < donors; i++)
	{
		cout << "Enter donor#" << i + 1 << ":\n";
		getline(inFile, donorSet[i].name);
		inFile >> donorSet[i].money;
		inFile.get();
	}

	if (inFile.eof())
	{
		cout << "file end.\n";
	}
	else if (inFile.fail())
	{
		cout << "Input file terminated by mismatch.\n";
	}
	else
		cout << "Input file terminated by unknown reasons.\n";

	inFile.close();

	cout << "\nGrand Patrons:\n";
	int flag = 0;
	for (int i = 0; i < donors; i++)
	{
		if (donorSet[i].money > 10000)
		{
			cout << donorSet[i].name << endl;
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << "None.\n";
	}

	cout << "Patrons:\n";
	flag = 0;
	for (int i = 0; i < donors; i++)
	{
		if (donorSet[i].money <= 10000)
		{
			cout << donorSet[i].name << endl;
			flag = 1;
		}
	}
	if (flag == 0)
	{
		cout << "None.\n";
	}

	return 0;
}

int main()
{
	test6_9();
	system("pause");
	return 0;
}

  

时间: 2024-08-02 15:17:37

C++ Primer Pluse_6_课后题的相关文章

C++ Primer Pluse_8_课后题

#include <iostream> #include <string> #include<cstring> using namespace std; void showStr(const char * str, int & n) { cout << str << endl; if (n > 0) { showStr(str, --n); } } int test8_1() { char str[] = "hello c

C++ Primer Pluse_7_课后题

#include <iostream> using namespace std; double Sum2(double x, double y) { double sum = 0; if (x + y < 0.0000000001) { cout << "x, y 的调和数为无穷大:\n"; system("pause"); exit(0); } sum = 2.0*x*y / (x + y); return sum; } void t

C++ Primer 第五版 部分课后题答案

当时刚学C++的时候买了这本书,一开始前面看的一知半解,索性就先缓缓,等学完学校的C++课程(中途自己也写了不少c++的代码),一段时间之后又拿起这本书去看,感觉还是挺有滋味的,这本书对我印象中的C++做了很大的扩展,个人认为这本书不太适合刚学C++就去看,而是写了一定的代码,对C++有一个大体的了解之后再去看会很有味道.在看书的过程中自己也写了上面的课后练习题,现在整理一下,也跟大家分享一下,下面是9~12 15~16章的课后题编程题的答案 (第八章之前的都没保存/(ㄒoㄒ)/~~): 当时保

《算法导论》读书笔记--第三章函数的增长 课后题

本章的课后题看一下即可,比较平凡. 3.1渐近记号 引用一下别人的答案,非常感谢: 原文地址:http://www.cnblogs.com/timebug/archive/2010/03/25/1694286.html |概念回顾| 当输入规模大到使只有运行时间的增长量级有关时,就使在研究算法的渐进效率. 几个重要渐进记号的定义: Θ(g(n))={ f(n): 存在正常数c1,c2和n0,使对所有的n>=n0,有0<=c1g(n)<=f(n)<=c2g(n) } O(g(n))=

课后题3,4

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 练习2 { class Program { static void Main(string[] args) { //输入3个数,将这三个数从大到小排列打印出来: Console.Write("请输入第一个数:"); int a =Convert .ToInt32 (Console.ReadLine

[Python]小甲鱼Python视频第030课(文件系统:介绍一个高大上的东西)课后题及参考解答

# -*- coding: utf-8 -*- """ Created on Fri Mar 8 15:49:32 2019 @author: Administrator """ """ 动动手: 0. 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: 1. 编写一个程序,计算当前文件夹下所有文件的大小,程序实现如图 2. 编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在.如遇到文件

c++面向对象程序设计 课后题 答案 谭浩强 第四章

c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag;

郭懋正《实变函数与泛函分析》课后题答案

LATEX 精美排版,郭懋正<实变函数与泛函分析课后题答案>,每一题均有详细解答,120元一份,也可单独购买每一题,一题2元,付款后联系邮箱:                    [email protected] . 原文地址:https://www.cnblogs.com/zhangwenbiao/p/12109194.html

《数据库系统概论(第5版)》课后习答案 王珊、萨师煊编著版 课后题解析 高等教育出版社出版 答

<数据库系统概论(第5版)>课后习答案 王珊.萨师煊编著版 课后题解析 高等教育出版社出版 答案与解析 <数据库系统概论(第5版)> 王珊.萨师煊编著版 第二篇 第1章 课后答案与解析 完整答案在页面最下方 前言第一篇 基 础 篇 课后习题答案与解析第1章 绪论 课后习题答案与解析1.1 数据库系统概述1.2 数据模型1.3 数据库系统的结构1.4 数据库系统的组成1.5 小结习题本章参考文献第2章 关系数据库 课后习题答案与解析2.1 关系数据结构及形式化定义2.2 关系操作2.