《C++ Primer Plus》编程练习第六章 解析与答案

#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
    char input;
    cin.get(input);
    while (cin.get(input)&&input != '@')
    {
        if (!isdigit(input))
        {
            if(isupper(input))
            {
                input = input + 32;
            }
            else if(islower(input))
            {
                input = input - 32;
            }
            cout << input;
        }
    } 
    return 0; 
}
#include <iostream>
#include <fstream>
using namespace std;
int main() 
{
    double donation[10];
    cout << "Please input donation\n";
    int i = 0;
    double average = 0;
    int conut = 0;
    while (i<10 && cin>>donation[i])
    {
        average += donation[i];
        i++;
    }
    cout<<"Done : wrong or i>10\n";
    average = average/i;

    for(int j = 0;j < 10; j++)
    {
        if(donation[j] > average)
            conut++;
    }
    cout<< "AVERAGE: "<<average<<" conut:"<<conut<<endl;  
    return 0;
}
#include <iostream>
#include <fstream>
using namespace std;


int main() 
{
    char input;
    cout<< "Please enter one of the following choices:\n";
    cout<<"c) carnibore\t p) pianist\nt) tree\t g) game\n";

    while(cin.get(input))
    {
        switch (input)
        {
        case 'c':
            cout<<"A maple is a carnivore\n";
            break;
        case 'p':
            cout<<"A maple is a pianist\n";
            break;
        case 't':
            cout<<"A maple is a tree\n";
            break;
        case 'g':
            cout<<"A maple is a game\n";
            break;            
        default:
            cout<<"Please enter c,p,t,g:";
            cin.get();
            break;
        }
        if(input == 'c'||input == 'p'||input =='t'||input == 'g')
            break;
    }


    return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
const int strsize = 20;
const int people = 5;
struct  BOP
{
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;
};
int main() 
{
    char choice;
    BOP * bop = new BOP [people];
    for (int i=0; i < people; i++)
    {
        cout<< "NUM:"<<i+1<<endl;
        cout << "Please input fullname:";
        cin.getline((bop + i)->fullname,strsize);
        cout << "Please input title:";
        cin.getline((bop + i)->title,strsize);
        cout << "Please input bopname:";
        cin.getline((bop + i)->bopname,strsize);
        cout << "Please input preference:";
        cin>>(bop+i)->preference;
        cin.get();     
    }
    cout << "Enter your choice:";
    while (cin.get(choice)&&choice!='q')
    {
        cin.get();
        switch (choice)
        {
        case 'a': //fulname
            for(int i =0; i<people;i++)
                cout<<(bop + i)->fullname<<endl;
            break;
        case 'b': //fulname
            for(int i =0; i<people;i++)
                cout<<(bop + i)->title<<endl;
            break;
        case 'c': //fulname
            for(int i =0; i<people;i++)
                cout<<(bop + i)->bopname<<endl;
            break;
        case 'd': //fulname
            for(int i =0; i<people;i++)
            {
                switch ((bop + i)->preference)
                {
                case 0:
                    cout<<(bop + i)->fullname<<endl;
                    break;
                case 1:
                    cout<<(bop + i)->title<<endl;
                    break;
                case 2:
                    cout<<(bop + i)->bopname<<endl;
                    break;                                    
                default:
                    break;
                }   
            }
            break;                    
        default:
            cout<<"input choices error!!"<<endl;
            break;
        }
        cout<<"Next Choice:";
    }
    cout <<"Bye!\n"; 
    delete []bop;
    return 0;
}
#include <iostream>
using namespace std;

int main() 
{
    double tax,salary = 0.0;
    cout <<"Please input salary:\n";
    while (cin>>salary && salary>=0)
    {
        while (salary>0)
        {
            if(salary>35000)
            {   tax += (salary-35000)*0.2;
                salary = 35000;
            }
            else if(salary>15000)
            {
                tax += (salary - 15000)*0.15;
                salary = 15000;
            }
            else if(salary>5000)
            {
                tax += (salary - 5000)*0.1;
                salary = 5000;
            }
            else 
            {
                salary = 0;
            }  
        }
        cout<<"tax:"<<tax<<endl;
        tax = 0;   
    }
    return 0;
}
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

struct  DONATE
{
    string name;
    double money;
};

int main() 
{
    int num = 0;
    int conut = 0;
    string a;
    cout<<"Please input num:";
    cin>>num;
    cin.get();
    DONATE * donate = new DONATE[num];
    //输入
    for (int i = 0; i < num; i++)
    {
        cout<<"NUM:"<<i+1<<endl;
        cout<<"name: ";
        getline(cin,(donate+i)->name);
        cout<<"money: ";
        cin>>(donate+i)->money; 
        cin.get();
    }
    //输出
    cout<<"Grand Patrons:\nname\tmoner\n";
    for (int i = 0; i<num; i++)
    {
        
        if((donate+i)->money > 10000)
        {
            cout<<(donate+i)->name<<"\t"<<(donate+i)->money<<endl;
            conut++;
        }
        
        
    }
    if (!conut)
    {
        cout<<"None\n";
    }
    conut = 0;
    cout<<"Patrons:\nname\tmoner\n";
     for (int i = 0; i<num; i++)
    {
        
        if((donate+i)->money <= 10000)
        {
            cout<<(donate+i)->name<<"\t"<<(donate+i)->money;
            conut++;
        }
        
    }
    if (!conut)
    {
        cout<<"None\n";
    }
    delete []donate;


    return 0;
}
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() 
{
    string word;
    int num_a=0;
    int num_b=0;
    int num_c = 0;
    cout <<"Enter words (q to quit):"<<endl;
    while (cin>>word&&word != "q")
    {
        cout << word << endl;
        if(isalpha(word[0]))
        {
            if(word[0] == 'A'||word[0] == 'E'||word[0] == 'I'||word[0] == 'O'||word[0] == 'U'||
                word[0] == 'a'||word[0] == 'e'||word[0] == 'i'||word[0] == 'o'||word[0] == 'u')
                num_a++;
            else 
                num_b++;
        }
        else 
            num_c++;
    }
    cout <<"vowels:"<<num_a<<" consonants:"<<num_b<<" others:"<<num_c<<endl;
    return 0;
}
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() 
{
    ifstream inFile;
    int count = 0;
    inFile.open("scores.txt");
    char word;
    inFile.get(word);
    while (inFile.good())
    {
        ++count;
        inFile.get(word);
    }
    cout << "count:"<<count<<endl;
    return 0;
}
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;


struct  DONATE
{
    string name;
    double money;
};

int main() 
{
    ifstream inFile;
    inFile.open("scores.txt");
    if(!inFile.is_open())
    {
        cout << "Could not open the file";
        exit(EXIT_FAILURE);
    }
    int num = 0;
    int conut = 0;

    inFile >> num;
    inFile.get();
    DONATE * donate = new DONATE[num];
    //输入
    for (int i = 0; i < num; i++)
    {

        getline(inFile,(donate+i)->name);
        inFile>>(donate+i)->money; 
        inFile.get();
    }
    //输出
    cout<<"Grand Patrons:\nname\tmoner\n";
    for (int i = 0; i<num; i++)
    {
        
        if((donate+i)->money > 10000)
        {
            cout<<(donate+i)->name<<"\t"<<(donate+i)->money<<endl;
            conut++;
        }    
    }
    if (!conut)
    {
        cout<<"None\n";
    }
    conut = 0;
    cout<<"Patrons:\nname\tmoner\n";
     for (int i = 0; i<num; i++)
    {  
        if((donate+i)->money <= 10000)
        {
            cout<<(donate+i)->name<<"\t"<<(donate+i)->money<<endl;
            conut++;
        }   
    }
    if (!conut)
    {
        cout<<"None\n";
    }
    delete []donate;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值