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;
}