1. 程式人生 > >C++ Primer Plus第六版第六章程式設計練習答案

C++ Primer Plus第六版第六章程式設計練習答案

6.1

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

int main()
{
	using namespace std;
	char ch;
	cout << "enter  characters<end in @>:";
	while ((ch=cin.get())!='@')//測試條件不能為(cin>>ch)!='@',cin返回值為cin物件、或者在需要bool型別的地方返回bool值;帶引數的cin.get(ch)函式返回值也為流物件;只有不帶引數的cin.get()函式返回值為int型
	{
		if (isdigit(ch))
			continue;
		else 
		{
			cout << "the charicter is: " << ch<<endl;
			if (islower(ch))
				cout << "It's upper case is " << char(toupper(ch)) << endl;//將toupper返回值強制轉換為char用於輸出該字元
			else if (isupper(ch))
				cout << "It's lower case is: " << char(tolower(ch))<< endl;
			else
				continue;
			
		}
	}
	
	return 0;
}

6.2

#include<stdafx.h>
#include<iostream>
const int SIZE = 10;

int main()
{
	using namespace std;
	double donation[SIZE];
	double sum = 0;
	double aver;
	cout << "enter numbers<at most ten and end with a non-number>: " << endl;
	int count = 0;
	while (count<10&&(cin>>donation[count]))
	{
		sum += donation[count];
		++count;
	}
	aver = sum / count;
	cout << "you total enter " << count << "numbers.\n"
		<< "the average is: " << aver;
	return 0;
}

 6.3

#include<stdafx.h>
#include<iostream>
using namespace std;
int main()
{
	
	char ch;
	cout << "Please enter one of the following choices:\n"
		<< "c) carnivore           P) pianist\n"
		<< "t) tress               g) game\n";
	bool wrong = false;//用於控制迴圈,當輸入不正確的重新輸入並判斷
	while (cin>>ch&&!wrong)//當輸入正確才會結束迴圈
	{
       switch (ch)
	   {
	     case 'c':
	     case'C ':
		       cout << "A maple is a carnivore.";
		       wrong = true;//輸入正確控制while迴圈結束
		       break;//switch開關語句不加break會按順序執行每條語句,因此在每種情況有多條語句時也不用加大括號
	     case 'p':
	     case'P':
			 cout << "A maple is a pianist.";
		     wrong = true;
		     break;
	     case't':
	     case'T':
			 cout << "A maple is a tress.";
		     wrong = true;
		     break;
	     case'g':
	     case'G':
			 cout << "A maple is a game.";
			 wrong = true;
		     break;
		 default:
			 cout << "Please enter a c, p, t, or g: ";
			 wrong = false;//輸入錯誤再次執行while語句
			 break;
		      
	    }
	}
	
	return 0;
}

6.4

#include<stdafx.h>
#include<iostream>
#include<iomanip>
const int strsize = 20;
const int number = 5;//存放人員數
using namespace std;
struct bop
{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int perference;
};
int main()
{
	bop list[number] =
	{
	  {"Wimp Macho","Senior programmer","WM",0},
	  {"Raki Rhodes","Junior programmer","RR",1},
	  {"Celia Laiter","Analyst ","MIPS",2},
	  {"Hoppy Hipman","Analyst Trainee","HHi",1},
	  {"Pat Hand","Juior programmer","LOOPY",2}
	};
	char ch;
	cout << "Benevolent Order of Programmers Report\n";
	cout.flags(ios::left);//設定輸出左對齊(預設為右對齊)
	cout << setw(25) << "a.display by name" << "b.display by title\n";//sew()用於設定其後的每個輸出所佔的位元組寬度,僅對緊跟其後的一個輸出有效,這裡只對a選項有效佔25位元組
	cout<<setw(25) << "c.display by bopname" << "d.display by preference\n";
	cout << "q.quite\n";
	cout << "enter your choice: ";
	cin >> ch;
	while (ch!='q')
	{
		if (ch == 'a')
			for (int i = 0; i < number; i++)
			{
				cout << list[i].fullname << endl;
			}
		else if (ch == 'b')
			for (int i = 0; i < number; i++)
			{
				cout << list[i].title << endl;
			}
		else if (ch == 'c')
			for (int i = 0; i < number; i++)
			{
				cout << list[i].bopname;
			}
		else  if (ch == 'd')
		{
			for (int i = 0; i < number; i++)
			{
				switch (list[i].perference)
				{
				case 0:cout << list[i].fullname << endl; break;
				case 1:cout << list[i].title << endl; break;
				case 2:cout << list[i].bopname << endl; break;
				}
			}
		}
		else
		{
			cout << "Please enter a,b,c,d or q: ";
			cin >> ch;
			break;
		}
		cout << "next choice: ";
		cin >> ch;
	}
	cout << "Bye";
	return 0;
}

6.5

#include<stdafx.h>
#include<iostream>
#include<iomanip>
const double tax_rate1 = 0.1;
const double tax_rate2 = 0.15;
const double tax_rate3 = 0.2;
using namespace std;

int main()
{
	int income;
	double tax ;
	cout << "enter your income: ";
	while (cin>>income&&income>0)//當輸入補位數字時cin>>income返回false
	{
		if (income <= 5000)
			tax = 0;
		else if (income > 5000 && income <= 15000)
			tax = (income - 5000)*tax_rate1;
		else if (income > 1500 && income <= 3500)
			tax = 1000 * tax_rate1 + (income - 1500)*tax_rate2;
		else
			tax = 1000 * tax_rate1 + 2000 * tax_rate2 + (income - 3500)*tax_rate3;
		cout << "your income tax is: " << tax<<endl;
		cout << "enter your income: ";
	}
	return 0;
}

6.6

#include<stdafx.h>
#include<iostream>
#include<string>
using namespace std;//定義名稱空間要在結構體之前否則不能識別string,系統將不會將其視為結構體的成員之一
struct donar
{
	string name;
	double money;
};


int main()
{
	int number;
	int G_patr=0;
	int patr=0;
	cout << "Please enter the naumber of donars:";
	cin >> number;
	donar *P = new donar[number];
	cout << "Please enter the message of each donar.\n";
	for (int i = 0; i < number; i++)
	{
		cout << "name #"<<i+1<<": ";
		cin.get();//前面輸入過數字,換行符還存在緩衝區中,不將其吸收將影響下面輸入字元
	    getline(cin,(P+i)->name);//cin不能輸入多個單詞
		cout << "money #" << i + 1 << ": ";
		cin >> (P + i)->money;
	}
	cout << "Grand Patrons:\n";
	for (int i = 0; i < number; i++)
	{
		if ((P+i)->money>10000)
		{
			++G_patr;
			cout << (P + i)->name << endl;
		}
	}
	if (!G_patr)
		cout << "none\n";
	cout << "Patrions:\n";
	for (int i = 0; i < number; i++)
	{
		if ((P + i)->money <10000)
		{
			++patr;
			cout << (P + i)->name << endl;
		}
	}
	if (!patr)
		cout << "none\n";
	return 0;
}

6.7

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


int main()
{
	string word;
	int vowel = 0;
	int cons = 0;
	int others = 0;
	cout << "enter words<end in q>: \n";
	cin >> word;
	while (word!="q")//不能直接用word!=‘q’為測試條件,word時string類類似於字串,而‘q’是字元;
	{
		if (isalpha(word[0]))//cctype函式引數為字元而不是字串不能用word

		{
			if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
				++vowel;
			else
				++cons;
		}
		else
			++others;
		cin >> word;//不加此條語句word的值將一直是輸入的第一個單詞,其他的單詞將留在緩衝佇列裡面,且輸入換行符以後將不能再輸入
	}
	cout << vowel << " words beginning with vowels\n"
		<< cons << " words beginning with consonants\n"
		<< others << " others";
	return 0;
}

6.8

/*首先需要在可執行檔案所在的資料夾中建立一個名為scores的文字檔案,
並輸入資料*/
#include<stdafx.h>
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
	double number;
	int count = 0;
	ifstream inFile;
	inFile.open("scores.txt");
	if (!inFile.is_open())
	{
		cout << "can't opent the file!\n";
		exit(EXIT_FAILURE);
	}
	inFile >> number;
	while (inFile.good())
	{
		++count;
		inFile >> number;
	}
	if (inFile.eof())
		cout << "Reach the end of file.\n";
	else if (inFile.fail())
		cout << "Input terminated by data mismatch.\n";
	else
		cout << "Input terminated for unknown reason.\n";
	cout << "Iteams read " << count;
	return 0;
}

6.9

#include<stdafx.h>
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
struct donar
{
	string name;
	double money;
};
int main()
{
	double number;
	int G_patr= 0;
	int patr = 0;
	ifstream inFile;
	inFile.open("mesage.txt");
	if (!inFile.is_open())
	{
		cout << "can't opent the file!\n";
		exit(EXIT_FAILURE);
	}
	inFile >> number;
	donar *p = new donar[number];
	for (int i = 0; i < number&&inFile.good(); i++)
	{
		inFile.get();//數字和字元的混合輸入,檔案中數字和字元中間有換行符,不將其吸收將不能正確輸入名字
		getline(inFile,(p+i)->name);//名字由多個單片語成,不能直接用inFile>>只能將一次單詞讀入,原理同cin
		inFile >> (p+i)->money;
		
	}
	if (inFile.eof())//檢查是否輸入完成全部資料
		cout << "Reach the end of file.\n";
	else if (inFile.fail())
		cout << "Input terminated by data mismatch.\n";
	else
		cout << "Input terminated for unknown reason.\n";
	cout << "Grand Patrons:\n";
	for (int i = 0; i < number; i++)
	{
		if ((p + i)->money > 10000)
		{
			++G_patr;
			cout << (p + i)->name<<endl;
		}
	}
	if (G_patr == 0)
		cout << "none\n";
	cout << "Patrons:\n";
	for (int i = 0; i < number; i++)
	{
		if ((p + i)->money <=10000)
		{
			++patr;
			cout << (p + i)->name << endl;
		}
	}
	if (patr == 0)
		cout << "none\n";
	return 0;
}