• 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.
    ";
    	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:
    ";
    	
    	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.
    ";
    	}
    	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:
    ";
    	cout << "c) carnivore			 p) pianist
    ";
    	cout << "t) tree				 g) game
    ";
    	cin.get(ch).get();
    
    	while (true)
    	{
    		switch (ch)
    		{
    		case 'c': cout << "A map is a carnivore.
    "; break;
    		case 'p': cout << "A map is a pianist.
    "; break;
    		case 't': cout << "A map is a tree.
    "; break;
    		case 'g': cout << "A map is a game.
    "; 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.
    ";
    	cout << "a.display by name.			 b.display by title.
    ";
    	cout << "c.display by bopname.		 d.display by preference.
    ";
    	cout << "q.quit.
    ";
    	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.
    ";
    	cin >> donors;
    	cin.get();
    	donor *donorSet = new donor[donors];
    
    	for (int i = 0; i < donors; i++)
    	{
    		cout << "Enter donor#" << i + 1 << ":
    ";
    		getline(cin, donorSet[i].name);
    		cin >> donorSet[i].money;
    		cin.get();
    	}
    	
    	cout << "
    Grand Patrons:
    ";
    	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.
    ";
    	}
    
    	cout << "Patrons:
    ";
    	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.
    ";
    	}
    
    	system("pause");
    	return 0;
    }
    
    int t6_7()
    {
    	int vowelCount = 0;
    	int consonantsCount = 0;
    	char ch;
    
    	cout << "Enter words (q to quit):
    ";
    	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 != '
    ')
    			{
    				cin.get(ch);
    			}
    		}
    		else
    			cin.get(ch);
    	}
    
    	cout << vowelCount << " words beginning with vowels.
    ";
    	cout << consonantsCount << " words beginning with consonants.
    ";
    	//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.
    ";
    		cout << "Program terminating.
    ";
    		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.
    ";
    	}
    	else if (inFile.fail())
    	{
    		cout << "Input terminated by data mismatch.
    ";
    	}
    	else
    		cout << "Input terminated for unknown reason.
    ";
    
    	inFile.close();
    
    	cout << "file has " << count << "characters.
    ";
    
    	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.
    ";
    		cout << "Program terminating.
    ";
    		exit(EXIT_FAILURE);
    	}
    
    	int donors = 0;
    
    	cout << "Enter the number of donors.
    ";
    	inFile >> donors;
    	inFile.get();
    	donor *donorSet = new donor[donors];
    
    	for (int i = 0; i < donors; i++)
    	{
    		cout << "Enter donor#" << i + 1 << ":
    ";
    		getline(inFile, donorSet[i].name);
    		inFile >> donorSet[i].money;
    		inFile.get();
    	}
    	
    	if (inFile.eof())
    	{
    		cout << "file end.
    ";
    	}
    	else if (inFile.fail())
    	{
    		cout << "Input file terminated by mismatch.
    ";
    	}
    	else
    		cout << "Input file terminated by unknown reasons.
    ";
    
    	inFile.close();
    
    	cout << "
    Grand Patrons:
    ";
    	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.
    ";
    	}
    
    	cout << "Patrons:
    ";
    	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.
    ";
    	}
    
    
    	return 0;
    }
    
    int main()
    {
    	test6_9();
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    python---redis缓存页面前戏之剖析render源码
    python---redis的python使用
    python---redis中文操作与系统中文检测
    python---redis在windows安装以及测试
    python---基于memcache的自定义session类
    python---memcache使用操作
    python---memcache基本使用以及内部原理
    googlecast
    Android-L-Samples
    libsuperuser
  • 原文地址:https://www.cnblogs.com/my-cat/p/6107538.html
Copyright © 2020-2023  润新知