手机版 欢迎访问it开发者社区(www.mfbz.cn)网站

当前位置: > 开发

C++ Primer Plus(第六版)中文版编程练习答案

时间:2021/5/27 21:51:39|来源:|点击: 次

47 C++ Primer Plus(第六版)第六章 编程练习答案

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

1

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<cctype>

int main()
{
	char ch;
	cout << "Enter ch : \n";
	while (cin.get(ch) && ch != '@')
	{
		if (!isdigit(ch))
		{
			if (islower(ch))
			{
				cout << char(toupper(ch)) << " ";
			}
			else if (isupper(ch))
			{
				cout << char(tolower(ch)) << " ";
			}
		}
	}
	
	system("pause");
	return 0;
}

2

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<cctype>

const int ArrSize = 10;

int main()
{
	double arr[ArrSize];
	int donation;
	int n = 0;
	int average = 0;
	int sum = 0;
	int count = 0;
	cout << "Enter donation:";
	while (n < ArrSize && (cin >> donation))
	{
		arr[n] = donation;
		++n;
		sum += donation;
		if(n<ArrSize)
			cout << "Next : ";
	}
	average = sum / n;
	for (int i = 0; i < n; i++)
	{
		if (arr[i] > average)
			++count;
	}
	cout << "average = " << average << " count = " << count << endl;

	system("pause");
	return 0;
}

3

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

void ShowMenu();

int main()
{
	cout << "Please enter a choice :\n";
	char choice;
	ShowMenu();
	while (cin>>choice && true)
	{
		//cin >> choice;
		switch (choice)
		{
		case 'c':
			cout << "carnivore" << endl;
			break;
		case 'p':
			cout << "pianist" << endl;
			break;
		case 't':
			cout << "tree" << endl;
			break;
		case 'g':
			cout << "game" << endl;
			break;
		case '@':
			cout << "欢迎下次使用!" << endl;
			system("pause");
			return 0;
			break;
		default:
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Please enter c, p, t or g (@ to quit): ";

		}
	}


	/*char ch;
	string c = "carnivore";
	string p = "pianist";
	string t = "tree";
	string g = "game";

	cout << "Please enter one of the following choices:\n";
	cout << "c) " << c << "        " << "p) " << p << "\n";
	cout << "t) " << t << "        " << "g) " << g << "\n";
	while (cin >> ch && ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {
		cout << "Please enter a c, p, t or g: ";
	}
	switch (ch) {
	case 'c':cout << "A maple is a " << c << endl;
		break;
	case 'p':cout << "A maple is a " << p << endl;
		break;
	case 't':cout << "A maple is a " << t << endl;
		break;
	case 'g':cout << "A maple is a " << g << endl;
		break;
	}*/
	
	system("pause");
	return 0;
}

void ShowMenu()
{
	cout << "c) carnivore  p) pianist" << endl
		<< "t) tree  g) game" << endl;
}

4

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

const int ArrSize = 20;

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

void ShowMenu()
{
	cout << "Benevolent Order of Programmers Report" << endl
		<< " a. display by name      b. display by title" << endl
		<< " c. display by bopname   d. display by preference" << endl
		<< " q. quit" << endl;
}

int main()
{
	bop arr[] = { { "Tom","professor","T",1},
		{ "Jerry","doctor","J",2},
		{ "Jack","graduate","J",3},
		{ "Mary","college","T",1} };

	ShowMenu();
	char choice;
	int count = 0;
	int len = sizeof(arr) / sizeof(struct bop); //结构体中元素个数
	cout << "Enter your choice:";
	//cin >> choice;
	while (cin>>choice)
	{
		switch (choice)
		{
		case 'a':
			for (int i = 0; i < len; i++)
			{
				cout << arr[i].fullname << endl;
			}
			cout << "Next choice:";
			break;
		case 'b':
			for (int i = 0; i < len; i++)
			{
				cout << arr[i].title << endl;
			}
			cout << "Next choice:";
			break;
		case 'c':
			for (int i = 0; i < len; i++)
			{
				cout << arr[i].bopname << endl;
			}
			cout << "Next choice:";
			break;
		case 'd':
			cout << "Enter preference:";
			int pref;
			cin >> pref;
			for (int i = 0; i < len; i++)
			{
				if (arr[i].preference == pref)
				{
					cout << arr[i].title << endl;
					++count;
				}
			}
			if (count == 0)
				cout << "No person's preference is " << pref << endl;
			

			/*for (int i = 0; i < len; i++) {
				if (arr[i].preference == 0)
					cout << arr[i].fullname << endl;
				else if (arr[i].preference == 1)
					cout << arr[i].title << endl;
				else
					cout << arr[i].bopname << endl;
			}*/
			
			
			cout << "Next choice:";
			break;
		case 'q':
			cout << "欢迎下次使用! Bye!" << endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "That's not a choice.\n";
			cout << "Enter your choice:";
			//cin >> choice;
		}
	}

	system("pause");
	return 0;
}

5

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

int main()
{
	int tvarp;
	cout << "Enter your tvarp:";
	int tax = 0;
	while (cin >> tvarp && tvarp >= 0)
	{
		if (tvarp <= 5000)
			tax = 0;
		else if (tvarp <= 15000)
			tax = (tvarp - 5000) * 0.1;
		else if (tvarp <= 35000)
			tax = 1000 + (tvarp - 15000) * 0.15;
		else
			tax = 1000 + 3000 + (tvarp - 35000) * 0.2;
		cout << "Your tax is:" << tax << endl;
		cout << "Next tvarp:";
	}

	system("pause");
	return 0;
}

6

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

struct Person
{
public:
	/*Person(string name, double money)
	{
		this->m_Name = name;
		this->m_Money = money;
	}*/

	string m_Name;
	double m_Money;
};

int main()
{
	int num;
	int gpa = 0;
	int pa = 0;
	cout << "Enter number of person:";
	cin >> num;
	cin.get();
	Person * p = new Person[num];

	for (int i = 0; i < num; i++)
	{
		cout << "Enter person " << i + 1 << " : ";
		//cin >> p[i].m_Name;
		getline(cin, p[i].m_Name);
		cout << "Enter the money of person " << i + 1 << " : ";
		cin >> p[i].m_Money;
		cin.get();
	}

	cout << "Grand Patrons:" << endl;
	for (int i = 0; i < num; i++)
	{
		if (p[i].m_Money > 10000)
		{
			cout << "Person name : " << p[i].m_Name
				<< " Person money : " << p[i].m_Money << endl;
			++gpa;
		}
	}
	if (gpa == 0)
		cout << "none!" << endl;

	cout << "Patrons:" << endl;
	for (int i = 0; i < num; i++)
	{
		if (p[i].m_Money <= 10000)
		{
			cout << "Person name : " << p[i].m_Name
				<< " Person money : " << p[i].m_Money << endl;
			++pa;
		}
	}
	if (pa == 0)
		cout << "none!" << endl;
	delete []p;

	system("pause");
	return 0;
}

7

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<cctype>

int main()
{
	char words[20];
	int vowel = 0;//元音
	int consonants = 0; //辅音
	int other = 0; //其他
	cout << "Enter words (q to quit):";
	while (cin>>words)
	{
		if (strcmp(words, "q") == 0)
			break;
		char ch = words[0];
		if (isalpha(ch))
		{
			if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
				++vowel;
			else
				++consonants;
		}
		else
			++other;
	}
	cout << "vowel : " << vowel << endl 
		<< "consonants : " << consonants << endl
		<< "other : " << other << endl;

	system("pause");
	return 0;
}

8

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<fstream>

const int Size = 60;

int main()
{
	char filename[Size];
	ifstream inFile;
	cout << "Enter filename:";
	cin.getline(filename, Size);
	inFile.open(filename);

	if (! inFile.is_open())
	{
		cout << "Could not open the file !" << endl;
		cout << "Program terminating!\n";
		exit(EXIT_FAILURE);
	}
	char ch;
	int count = 0;
	inFile >> ch;
	while (inFile.good())
	{
		++count;
		inFile >> ch;
	}

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

	if (count == 0)
		cout << "No char processed!\n";
	else
		cout << "char number :" << count << endl;

	inFile.close();

	system("pause");
	return 0;
}

9

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<fstream>

const int Size = 60;

struct Person
{
public:
	/*Person(string name, double money)
	{
		this->m_Name = name;
		this->m_Money = money;
	}*/

	string m_Name;
	double m_Money;
};

int main()
{
	int num;
	int gpa = 0;
	int pa = 0;

	char filename[Size];
	cout << "Enter filename:";
	ifstream inFile;
	cin.getline(filename, Size);
	inFile.open(filename);

	if (!inFile.is_open())
	{
		cout << "Could not open the file " << filename << endl;
		cout << "Progrom terminating.\n";
		exit(EXIT_FAILURE);
	}

	inFile >> num;
	inFile.get();
	//cin.get(); //这里不能用,从键盘输入时可以
	
	Person* p = new Person[num];

	for (int i = 0; i < num; i++)
	{
		getline(inFile,p[i].m_Name);
		inFile >> p[i].m_Money;
		inFile.get();
		//cin.get(); //这里不能用
	}

	cout << "Grand Patrons:" << endl;
	for (int i = 0; i < num; i++)
	{
		if (p[i].m_Money > 10000)
		{
			cout << "Person name : " << p[i].m_Name
				<< " Person money : " << p[i].m_Money << endl;
			++gpa;
		}
	}
	if (gpa == 0)
		cout << "none!" << endl;

	cout << "Patrons:" << endl;
	for (int i = 0; i < num; i++)
	{
		if (p[i].m_Money <= 10000)
		{
			cout << "Person name : " << p[i].m_Name
				<< " Person money : " << p[i].m_Money << endl;
			++pa;
		}
	}
	if (pa == 0)
		cout << "none!" << endl;
	delete[]p;
	inFile.close();

	system("pause");
	return 0;
}

Copyright © 2002-2019 某某自媒体运营 版权所有