编程练习 6
#include <iostream>
#include <string>
using namespace std;
struct donation {
string donor;
double amount;
};
int main()
{
cout << "Enter the number of donor: ";
int num;
cin >> num;
if (num == 0)
cout << "No donor recorded";
else
{
cin.get();
donation *arrDonation = new donation[num];
donation *arrDonation2 = arrDonation ;
cout << "Enter the detail of eacy donor: " << endl;
for (int i=0; i< num; i++)
{
cout << "Donor #" << i+1 << " : ";
getline(cin,arrDonation2->donor);
cout << "Donor #" << i+1 << " donated: ";
cin >> arrDonation2->amount;
arrDonation2 ++;
}
arrDonation2 = arrDonation; // reset the pointer
// display Grand Patrons, both name and amount
cout << "The list below shows the Grand Patrons: " << endl;
int numGrand = 0;
for (int i=0; i< num; i++)
{
if (arrDonation2->amount > 10000)
{
cout << arrDonation2->donor << "\t" << arrDonation2->amount << endl;
numGrand++;
}
arrDonation2++;
}
if (numGrand ==0)
cout << "None" << endl;
arrDonation2 = arrDonation; // reset the pointer
// display Grand Patrons, both name and amount
cout << "The list below shows the name of Patrons: " << endl;
int numPatrons = 0;
for (int i=0; i< num; i++)
{
if (arrDonation2->amount <= 10000)
{
cout << arrDonation2->donor << endl;
numPatrons++;
}
arrDonation2++;
}
if (numPatrons ==0)
cout << "None" << endl;
cin.get();
//delete [] arrDonation;
//delete [] arrDonation2;
}
system("pause");
return 0;
}
编程练习 7
#include <iostream>
#include <cctype>
using namespace std;
const int Size = 20;
int main()
{
int vowels = 0;
int consonants = 0;
int others = 0;
char word[Size];
char ch;
cout << "Enter words (q to quit):" << endl;
cin >> word;
while(strcmp(word,"q"))
{
//cin >> word;
ch = word[0];
if(!isalpha(ch))
others++;
else if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vowels++;
else
consonants++;
cin>>word;
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beinning with consonants" << endl;
cout << others << " others" << endl;
system("pause");
return 0;
}
编程练习 8
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream inFile;
ifstream fin;
int count=0;
inFile.open("file.txt");
if(!inFile.is_open())
{
cout<<"Open Failed!"<<endl;
}
char ch;
inFile>>ch;
while(inFile.good())
{
count++;
inFile>>ch;
}
if(inFile.eof())
{
cout<<"count="<<count<<endl;
}
system("pause");
return 0;
}
编程练习 9 就是把练习6从用户输入改成文件读取了,没舍特别的,这里就偷个懒了