6.编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或sting对象)和用来存储款项的 double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以 Patrons 开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include
#include
using namespace std;
struct Donor {
string name;
double amount;
};
int main() {
int num_donors;
cout > num_donors;
// 动态分配结构数组
Donor* donors = new Donor[num_donors];
// 输入捐款者信息
for (int i = 0; i > donors[i].name;
cout > donors[i].amount;
}
// 显示重要捐款人 (Grand Patrons)
cout 10000) {
cout << donors[i].name << " - $" << donors[i].amount << endl;
hasGrandPatrons = true;
}
}
if (!hasGrandPatrons) {
cout << "none\n";
}
// 显示其他捐款人 (Patrons)
cout << "\nPatrons:\n";
bool hasPatrons = false;
for (int i = 0; i < num_donors; i++) {
if (donors[i].amount <= 10000) {
cout << donors[i].name << " - $" << donors[i].amount << endl;
hasPatrons = true;
}
}
if (!hasPatrons) {
cout << "none\n";
}
// 释放动态分配的内存
delete[] donors;
return 0;
}
7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用 isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或 switch 语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn.q
5 words beginning with vowels
4 words beginning with consonants
2 others
#include
#include
#include
using namespace std;
int main() {
string word;
int vowels = 0, consonants = 0, others = 0;
cout > word && word != "q") {
if (!isalpha(word[0])) {
others++;
} else {
char first = tolower(word[0]);
if (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u') {
vowels++;
} else {
consonants++;
}
}
}
cout << vowels << " words beginning with vowels\n";
cout << consonants << " words beginning with consonants\n";
cout << others << " others\n";
return 0;
}
8.编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include
#include
using namespace std;
int main() {
string filename;
cout > filename;
ifstream inFile(filename);
if (!inFile.is_open()) {
cout << "Could not open file " << filename << endl;
return 1;
}
char ch;
int count = 0;
while (inFile.get(ch)) {
count++;
}
cout << "File " << filename << " contains " << count << " characters.\n";
inFile.close();
return 0;
}
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
#include
#include
#include
using namespace std;
struct Donor {
string name;
double amount;
};
int main() {
string filename;
cout > filename;
ifstream inFile(filename);
if (!inFile.is_open()) {
cout > num_donors;
inFile.ignore(); // 忽略换行符
// 动态分配结构数组
Donor* donors = new Donor[num_donors];
// 从文件读取捐款者信息
for (int i = 0; i > donors[i].amount;
inFile.ignore(); // 忽略换行符
}
// 显示重要捐款人 (Grand Patrons)
cout 10000) {
cout << donors[i].name << " - $" << donors[i].amount << endl;
hasGrandPatrons = true;
}
}
if (!hasGrandPatrons) {
cout << "none\n";
}
// 显示其他捐款人 (Patrons)
cout << "\nPatrons:\n";
bool hasPatrons = false;
for (int i = 0; i < num_donors; i++) {
if (donors[i].amount <= 10000) {
cout << donors[i].name << " - $" << donors[i].amount << endl;
hasPatrons = true;
}
}
if (!hasPatrons) {
cout << "none\n";
}
// 释放动态分配的内存
delete[] donors;
inFile.close();
return 0;
}