第六章 编程练习(6-9)

编程练习 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从用户输入改成文件读取了,没舍特别的,这里就偷个懒了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值