c++Primer Plus 第六章课后习题

1

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

int main()
{
    char ch;
    while(cin.get(ch) && ch != '@'){
        if(isdigit(ch)){
            continue;
        }
        else if(isupper(ch)){
            cout<<char(tolower(ch));
        }
        else {
            cout<<ch;
        }
    }
    cout << "\nHello World!" << endl;
    return 0;
}

2

#include <iostream>

using namespace std;

int main()
{
    double numbers[10];
    double average=0;
    int OnAve=0;
    int i=0;
    cout<<"Enter the "<< i+1<<" number: ";
    while(i<10 && (cin>>numbers[i])){
        i++;
        cout<<"Enter the "<< i+1<<" number: ";
        average+=numbers[i-1];
    }
    average/=i;
    for(int j=0;j<i+1;j++){
        if(numbers[j]>average){
            OnAve++;
        }
    }
    cout<<"you enter "<<i<<" numbers."<<endl;
    cout<<"Averrage = "<<average<<endl;
    cout<<"greater than "<<average<<" have "<<OnAve<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

3

#include <iostream>

using namespace std;

bool isSelection(char ch){
    return ch=='t' || ch=='c' || ch=='p' || ch=='g';
}

int main()
{
    cout<<"Please enter one of the following choices:"<<endl;
    cout<<"c) carnivore\t\tp) pianist"<<endl;
    cout<<"t) tree\t\t\tg) game\nf"<<endl;
    char ch;
    do{
        cout<<"Please enter a c,p,t or g: ";
        cin>>ch;
        if(isSelection(ch)){
            switch(ch){
            case 'c':
                cout<<"a tiger is a carnivore."<<endl;
                break;
            case 'p':
                cout<<"Beethoven is a great pianist."<<endl;
                break;
            case 't':
                cout<<"A maple is a tree"<<endl;
                break;
            case 'g':
                cout<<"Hi-fi Rush is a good music game."<<endl;
                break;
            }
        }
        else{
            cin.clear();
            while(cin.get()!='\n'){
                continue;
            }
            cout<<"please enter the correct selection."<<endl;
        }
        while(cin.get()!='\n'){
            continue;
        }

    }while(true);
    cout << "Hello World!" << endl;
    return 0;
}

4

#include <iostream>

using namespace std;

bool isSelection(char ch){
    return ch=='a' || ch=='b' || ch=='c' || ch=='d';
}

const int strsize=50;

struct bop{
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preferrence;
    void showName(){
        cout<<fullname<<endl;
    }
    void showTitle(){
        cout<<title<<endl;
    }
    void showBopName(){
        cout<<bopname<<endl;
    }
    void showPreferrence(){
        switch(preferrence){
            case 0:
                showName();
                break;
            case 1:
                showTitle();
                break;
            case 2:
                showBopName();
                break;
            default:
                showName();
                break;
        }
    }
};

//创建结构体数组

struct bop Bop[]={
{"Wimp Macho","teacher","Wimp Macho",0},
{"Raki Rhodes","traveller","Junior Programmer",2},
{"Celia Laiter","hunter","MIPS",1}
};

int main()
{
    char ch;
    cout<<"Please enter one of the following choices:"<<endl;
    cout<<"a. display by name\tb. display by title"<<endl;
    cout<<"c. display by bopname\td. display by preference"<<endl;
    cout<<"q. quit"<<endl;
    do{
        cout<<"Enter you choice:";
        cin>>ch;
        if(isSelection(ch)){
            switch(ch){
            case 'a':
                for(int i=0;i<3;i++){
                    Bop[i].showName();
                }
                break;
            case 'b':
                for(int i=0;i<3;i++){
                    Bop[i].showTitle();
                }
                break;
            case 'c':
                for(int i=0;i<3;i++){
                    Bop[i].showBopName();
                }
                break;
            case 'd':
                for(int i=0;i<3;i++){
                    Bop[i].showPreferrence();
                }
                break;
            }
        }
        else if(ch=='q'){
            break;
        }
        else{
            while(cin.get()!='\n'){
                continue;
            }
            cout<<"Please enter the corrct selection"<<endl;
        }
        while(cin.get()!='\n'){
            continue;
        }
    }while(true);

    cout << "Hello World!" << endl;
    return 0;
}

5

#include <iostream>

using namespace std;

int main()
{
    int tvarps;
    cout<<"Please enter your income: ";
    while(cin>>tvarps){
        double tax=0.0;
        if(tvarps<0){
            break;
        }
        if(tvarps>35000){
            tax=(tvarps-35000)*0.2+20000*0.15+10000*0.1;
        }
        else if(tvarps>15000){
            tax=(tvarps-15000)*0.15+10000*0.1;
        }
        else if(tvarps>5000){
            tax=(tvarps-5000)*0.1;
        }
        else{
            tax=0.0;
        }
        cout<<"individual income tax: "<<tax<<endl;
        cout<<"Please enter your income: ";
    }
    cout << "Hello World!" << endl;
    return 0;
}

6

#include <iostream>

using namespace std;

struct donate{
    string name;
    double money;
    void show(){
        cout<<"Name: "<<name;
        cout<<"donation: "<<money<<endl;
    }
};


int main()
{
    int number;
    cout<<"Please enter the number of donator: ";
    cin>>number;
    struct donate *donators=new struct donate[number];

    for(int i=0;i<number;i++){
        cout<<"Name: ";
        cin>>(donators+i)->name;
        cout<<"Money: ";
        cin>>(donators+i)->money;
    }
    cout<<"Grand Patrons:"<<endl;
    for(int i=0;i<number;i++){
        if((donators+i)->money>=10000){
            (donators+i)->show();
        }
    }
    cout<<"Other Patrons:"<<endl;
    for(int i=0;i<number;i++){
        if((donators+i)->money<10000){
            (donators+i)->show();
        }
    }
    cout << "Hello World!" << endl;
    return 0;
}

7

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

bool isVowels(char ch){
    return ch=='a' || ch=='A' || ch=='e' ||ch=='E' ||ch=='i'||ch=='I'||ch=='O'||ch=='o'||ch=='U'||ch=='u';
}


int main()
{
    cout<<"Enter words (q to quit):"<<endl;
    char ch;
    char ch2='.';//记录前2
    char ch1=' ';//记录前1
    int numberOfVowels=0;
    int numberOfConsonants=-1;
    int numberOfOthers=0;
    while(cin.get(ch)){
        if((ch2==' '||ch2=='\n')&& ch1=='q'&&(ch=='\n')){
            break;
        }
        if(isalpha(ch)&&(ch1==' '||ch1=='\n')){
            if(isVowels(ch)){
                numberOfVowels++;
            }
            else{
                numberOfConsonants++;
            }
        }
        else if(!isalpha(ch)&&(ch1==' '||ch1=='\n')){
            numberOfOthers++;
        }

        ch2=ch1;
        ch1=ch;
    }
    cout<<numberOfVowels<<" words beginning with vowels"<<endl;
    cout<<numberOfConsonants<<" words beginning with consonants"<<endl;
    cout<<numberOfOthers<<" others"<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

8

 非逐字符读物文件

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

int main()
{
    ofstream of;
    int numberOfChars=0;
    of.open("demo1.txt");
    of<<"jisuanji\n jisuanji jisaunji sjaisjia";
    of.close();

    ifstream inf;
    inf.open("demo1.txt");
    if(!inf.is_open()){
        cout<<"open file erro";
        exit(EXIT_FAILURE);
    }
    string str;
    while(getline(inf,str)){
        numberOfChars+=str.length();
    }
    cout<<"number Of Chars :"<<numberOfChars<<endl;
    inf.close();
    cout << "Hello World!" << endl;
    return 0;
}

逐字符读取文件

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

int main()
{
    ofstream of;
    int numberOfChars=0;
    of.open("demo1.txt");
    of<<"jisuanji\n jisuanji jisaunji sjaisjia";
    of.close();

    ifstream inf;
    inf.open("demo1.txt");
    if(!inf.is_open()){
        cout<<"open file erro";
        exit(EXIT_FAILURE);
    }
    char ch;
    while(inf>>ch){
        cout<<ch;
        numberOfChars++;
    }
    cout<<"number of chars: "<<numberOfChars<<endl;
    return 0;
}

9

懒得写了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HWsir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值