C++primer plus第六章编程练习

6、

#include "stdafx.h"
#include<iostream>
using namespace std;
const int ArSize = 40;
struct information
{
	char name[ArSize];
	double fund;
};
int main()
{
	cout << "please enter the number of donor:";
	int number;
	cin >> number;
	information *s = new information[number];
	for (int i = 0; i < number; i++)
		{
			cout << "please enter the " << i + 1 << "th donor's name: ";
			cin.sync();
			cin.getline(s[i].name,ArSize);
			cout << "please enter the " << i + 1 << "th donor's fund: ";
			cin >> s[i].fund;
		}
	int temp = 0;
		cout << "Grand Patrons:\n";
		for (int i = 0; i < number; i++)
		{
			if (s[i].fund > 10000)
			{
				cout << s[i].name << " " << s[i].fund << endl;
				temp++;
			}
		}
		if (temp == 0)cout << "none\n";
		temp = 0;
		cout << "Patrons:\n";
		for (int i = 0; i < number; i++)
		{
			if (s[i].fund <= 10000)
			{
				cout << s[i].name << endl;
				temp++;
			}
		}
		if (temp == 0)cout << "none\n";
	
	delete [] s;
	return 0;
}

7、

#include "stdafx.h"
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	cout << "Enter words(q to quit):\n";
	string word;
	int vowel = 0;
	int consonant = 0;
	int others = 0;
	getline(cin, word);
	while (word != "q")
	{
		if (isalpha(word[0]))
		{
			switch (word[0])
			{
			case 'a':case'A':
			case 'e':case'E':
			case 'i':case'I':
			case 'o':case'O':
			case 'u':case'U':
				vowel++; break;
			default:consonant++; break;
			}
		}
		else
			others++;
		//cin.sync();
		getline(cin, word);
	}
	cout << vowel << " words beginning with vowels\n";
	cout << consonant << " words beginning with consonants\n";
	cout << others << " others\n";
	
	return 0;
}
8、

#include "stdafx.h"
#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
	ifstream inFile;
	string filename;
	cout << "please enter filenamde:";
	cin>>filename;
	inFile.open(filename+".txt");
	if (!inFile.is_open())
	{
		cout << "could not open the file" << filename << 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 unkown reason.\n";
	if (count == 0)cout << "No data processed.\n";
	else cout << "The file have " << count << " chars";
	return 0;
}

9、

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
const int ArSize = 40;
struct information
{
	char name[ArSize];
	double fund;
};
int main()
{
	//读取文件中内容
	ifstream inFile;
	string filename;
	cout << "please enter filenamde:";
	cin >> filename;
	inFile.open(filename + ".txt");
	if (!inFile.is_open())
	{
		cout << "could not open the file" << filename << endl;
		cout << "program terminating.\n";
		exit(EXIT_FAILURE);
	}
	int number;
	inFile >> number;
	cout << "number:" << number << endl;
	information *s = new information[number];
	int i;
	for (i = 0; i < number; i++)
	{
		inFile.get();
		inFile.getline(s[i].name, ArSize);
		inFile >> s[i].fund;
	}
	inFile.close();

	//显示
	if (number == 0)cout << "No data processed.\n";
	else
	{
		int temp = 0;
		//显示重要捐款者
		cout << "Grand Patrons:\n";
		for (int i = 0; i < number; i++)
		{
			if (s[i].fund > 10000)
			{
				cout << s[i].name << " " << s[i].fund << endl;
				temp++;
			}
		}
		if (temp == 0)cout << "none\n";
		temp = 0;
		//显示其他捐款者
		cout << "Patrons:\n";
		for (int i = 0; i < number; i++)
		{
			if (s[i].fund <= 10000)
			{
				cout << s[i].name << endl;
				temp++;
			}
		}
		if (temp == 0)cout << "none\n";
	}
	delete[]s;

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值