Advertisement

C++ Primer Plus第六版第六章分支语句和逻辑运算符编程练习答案

阅读量:

1.

复制代码
    #include<iostream>
    #include<cctype>
    using namespace std;
    
    int main()
    {
    	char ch;
    
    	while (cin.get(ch) && ch != '@')
    	{
    		if (isupper(ch))
    		{
    			ch = tolower(ch);
    			cout << ch;
    		}
    		else if (islower(ch))
    		{
    			ch = toupper(ch);
    			cout << ch;
    		}
    	}
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

2.

复制代码
    #include<iostream>
    using namespace std;
    int main()
    {
    	double sum = 0, ave = 0;
    	int m = 0, i = 0;
    	double donation[10];
    	while (i < 10 && (cin >> donation[i]))
    		sum += donation[i++];
    	if (i == 0)
    		cout << "输入有误" << endl;
    	else
    		ave = sum / i;
    	for (int j = 0; j < i; ++j)
    		if (donation[j] > ave)
    			++m;
    	cout << "这些数字的平均值是: " << ave << endl;
    	cout << "有" << m << "个数字大于平均值" << endl;
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

3.

复制代码
    #include<iostream>
    using namespace std;
    
    int main()
    {
    cout << "Please enter one of the following choices:" << endl;
    cout << "c) carnivore           p) pianist" << endl;
    cout << "t) tree                g) game" << endl;
    char ch;
    while (cin >> ch)
    {
        switch(ch)
        {
            case 'c':
                cout << "A tiger is a carnivore.";
                return 0;
            case 'p':
                cout << "lang lang is a pianist.";
                return 0;
            case 't':
                cout << "A maple is a tree.";
                return 0;
            case 'g':
                cout << "lol is a game.";
                return 0;
            default :
                cout << "Please enter a c, p, t, or g: ";
                break;
        }
    }
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

4.

复制代码
    #include<iostream>
    using namespace std;
    const int strsize = 20;
    struct bop {
    	char fullname[strsize];
    	char title[strsize];
    	char bopname[strsize];
    	int preference;
    };
    
    int main()
    {
    	bop data[5] = {
    		{"Wimp Macho", "Student", "Macho", 0},
    		{"Raki Rhodes", "Junior Programmer", "Rhodes", 1},
    		{"Celia Laiter", "Student", "MIPS", 2},
    		{"Hoppy Hipman", "Analyst Trainee", "Hipman", 1},
    		{"Pat Hand", "Student", "LOOPY", 2}
    	};
    	cout << "Benevolent Order of Programmers 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" << endl;
    	cout << "Enter your choice: ";
    	char choice;
    	while (cin >> choice)
    	{
    		switch (choice)
    		{
    		case 'a':
    			for (int i = 0; i < 5; ++i)
    				cout << data[i].fullname << endl;
    			break;
    		case 'b':
    			for (int i = 0; i < 5; ++i)
    				cout << data[i].title << endl;
    			break;
    		case 'c':
    			for (int i = 0; i < 5; ++i)
    				cout << data[i].bopname << endl;
    			break;
    		case 'd':
    			for (int i = 0; i < 5; ++i)
    			{
    				if (data[i].preference == 0)
    					cout << data[i].fullname << endl;
    				else if (data[i].preference == 1)
    					cout << data[i].title << endl;
    				else
    					cout << data[i].bopname << endl;
    			}
    			break;
    		case 'q':
    			cout << "Bye!";
    			return 0;
    		default:
    			cout << "Please enter a, b, c, d or q: ";
    			break;
    		}
    	}
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

5.

复制代码
    #include<iostream>
    using namespace std;
    const int strsize = 20;
    
    int main()
    {
    	int n;
    	cout << "输入你的收入: ";
    	while ((cin >> n) && n >= 0)
    	{
    		if (n <= 5000)
    			cout << "不用交税\n";
    		else if (n <= 15000)
    			cout << "需要交税:" << (15000 - n) * 0.1 << endl;
    		else if (n <= 35000)
    			cout << "需要交税:" << 10000 * 0.10 + (n - 15000) * 0.15 << endl;
    		else
    			cout << "需要交税:" << 10000 * 0.10 + 20000 * 0.15 + (n - 35000) * 0.20 << endl;
    		cout << "输入另一个收入(输入字母或负数结束): ";
    	}
    	cout << "程序结束!" << endl;
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

6.

复制代码
    #include<iostream>
    #include <string>
    using namespace std;
    
    struct data {
    	string name;
    	double amount;
    };
    
    int main()
    {
    	int n;
    
    	cout << "请输入捐献者数目: ";
    	while (!(cin >> n) || n < 0)
    	{
    		cin.clear();//重置错误输入标记,程序才能继续读取输入
    		while (cin.get() != '\n')//读取行尾之前的所有输入,删除一行的错误输入
    			continue;
    		cout << "输入错误,请重新输入: ";
    	}
    	if (n == 0)
    	{
    		cout << "没有捐献者,程序结束!" << endl;
    		return 0;
    	}
    	cin.get();
    	struct data *pf = new struct data[n];
    	for (int i = 0; i < n; ++i)
    	{
    		cout << "输入第" << i + 1 << "个捐献者的姓名: ";
    		getline(cin, pf[i].name);
    		cout << "输入第" << i + 1 << "个捐献者捐献的金额: ";
    		while (!(cin >> pf[i].amount) || n < 0)
    		{
    			cin.clear();//重置错误输入标记,程序才能继续读取输入
    			while (cin.get() != '\n')//读取行尾之前的所有输入,删除一行的错误输入
    				continue;
    			cout << "输入错误,请重新输入: ";
    		}
    		cin.get();
    	}
    	cout << "重要捐款人:\n";
    	int temp = 0;
    	for (int i = 0; i < n; ++i)
    		if (pf[i].amount > 10000)
    		{
    			cout << pf[i].name << endl;
    			++temp;
    		}
    	if (temp == 0)
    		cout << "none" << endl;
    	temp = 0;
    	cout << "其他捐献人:\n";
    	for (int i = 0; i < n; ++i)
    		if (pf[i].amount <= 10000)
    		{
    			cout << pf[i].name << endl;
    			++temp;
    		}
    	if (temp == 0)
    		cout << "none" << endl;
    	delete []pf;
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

7.

复制代码
    #include<iostream>
    #include<cctype>
    #include<cstring>
    #include<string>
    using namespace std;
    int main()
    {
    	string s;
    	int vowels = 0, consonants = 0, others = 0;
    	cout << "Enter words (q to quit):\n";
    	while (cin >> s && (s != "q"))
    	{
    
    		if (isalpha(s[0]))
    		{
    			if (strchr("aeiou", s[0]))
    				++vowels;
    			else
    				++consonants;
    		}
    		else
    			++others;
    	}
    	cout << vowels << " words beginning with vowels\n";
    	cout << consonants << " words beginning with consonants\n";
    	cout << others << " others" << endl;
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

8.

复制代码
    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    const int SIZE = 60;
    using namespace std;
    int main()
    {
    	char filename[SIZE];
    	ifstream inFile;
    	cout << "输入你想要打开的文件名(如xx.txt):" << endl;
    	cin.getline(filename, SIZE);
    	inFile.open(filename);
    	if (!inFile.is_open())
    	{
    		cout << "无法打开文件,可能文件不存在或者路径不对";
    		exit(EXIT_FAILURE);
    	}
    	char ch;
    	int num = 0;
    	while (inFile >> ch)
    		++num;
    	cout << "该文件一共有" << num << "个字符." << endl;
    	inFile.close();
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

9.

复制代码
    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    using namespace std;
    const int SIZE = 60;
    struct data {
    	char name[SIZE];
    	double amount;
    };
    
    int main()
    {
    	int n;
    	char filename[SIZE];
    	ifstream inFile;
    
    	cout << "输入你想要打开的文件名(如xx.txt):" << endl;
    	cin.getline(filename, SIZE);
    	inFile.open(filename);
    	if (!inFile.is_open())
    	{
    		cout << "无法打开文件,可能文件不存在或者路径不对";
    		exit(EXIT_FAILURE);
    	}
    	cout << "请输入捐献者数目: ";
    	inFile >> n;
    	inFile.get();
    	cout << n << endl;
    	struct data *pf = new struct data[n];
    	for (int i = 0; i < n; ++i)
    	{
    		cout << "输入第" << i + 1 << "个捐献者的姓名: ";
    		inFile.getline(pf[i].name, SIZE);
    		cout << pf[i].name << endl;
    
    		cout << "输入第" << i + 1 << "个捐献者捐献的金额: ";
    		inFile >> pf[i].amount;
    		inFile.get();
    		cout << pf[i].amount << endl;
    
    	}
    	cout << "重要捐款人:\n";
    	int temp = 0;
    	for (int i = 0; i < n; ++i)
    		if (pf[i].amount > 10000)
    		{
    			cout << pf[i].name << endl;
    			++temp;
    		}
    	if (temp == 0)
    		cout << "none" << endl;
    	temp = 0;
    	cout << "其他捐献人:\n";
    	for (int i = 0; i < n; ++i)
    		if (pf[i].amount <= 10000)
    		{
    			cout << pf[i].name << endl;
    			++temp;
    		}
    	if (temp == 0)
    		cout << "none" << endl;
    	delete []pf;
    
    	return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

全部评论 (0)

还没有任何评论哟~