第六章 复习题&编程题

自用,并非标准答案
自用,并非标准答案
自用,并非标准答案

文章目录

复习题

    // 1 多余判断
    // 2 打印出数字
    // 3 双倍输出字符, ct1 == 9 ,ct2 == 0,循环内部条件代码段根本无法进入。
    // 4
    int weight,x,donation,guest;
    char ch;
    115<=weight && weight<125;
    ch=='q' || ch=='Q';
    (x&1==0) && x!=26;
    (x&1==0) && x%26!=0;
    (1000<=donation && donation<=2000) || guest==1;
    ('A'<=ch && ch<='Z') || ('a'<=ch && ch<='z');
    // 5  相同
    // 6
    (x>0?x:-1);
    // 7
    int a_grade=0,b_grade=0,c_grade=0,d_grade=0,f_grade=0;
    switch(ch){
        case 'A': a_grade++;
        case 'B': b_grade++;
        case 'C': c_grade++;
        case 'D': d_grade++;
        default : f_grade++;
    }
    // 8 用首字母能更好的联想出单词,而不是用数字与之硬性匹配。
    // 9 
    int line = 0;
    while ((ch=cin.get()) && ch!='Q'){
        if (ch=='\n') ++line;
    }

编程题

3

cout<<"Please enter one of the following choices:"<<endl;
    cout<<"c) carnivore\tp) pianist\nt) tree\t\tg) game"<<endl;
    char ch;
    while(cin>>ch){
        switch(ch){
            case 'c' : cout<<"carnivore"<<endl;break;
            case 'p' : cout<<"pianist"<<endl;break;
            case 't' : cout<<"tree"<<endl;break;
            case 'g' : cout<<"game"<<endl;break;
            default : cout<<"Please enter a c, p, t, or g: ";
        }
    };

4
没看懂题

5

    long long tvarps = 0;
    while(cin>>tvarps && tvarps>=0){
        cout<<"tax:";
        if (tvarps<=5000)cout<<0;
        else if (tvarps>5000&&tvarps<=15000)cout<<(tvarps-5000)*0.1;
        else if (tvarps>15000&&tvarps<=35000)cout<<(tvarps-35000)*0.15+1000;
        else cout<<(tvarps-35000)*0.2+3000+1000;
        cout<<" tvarps"<<endl;
    }

6
当时只考虑姓名是一个单词的情况,多单词见 9

    int n;
    cout<<"input patrons pcs: ";
    cin>>n;
    struct person{
        string name;
        double money;
    };
    vector<person> arr(n);
    int i;
    for(i=0;i<n;++i){
        cout<<"input name: ";
        cin>>arr[i].name;
        cout<<"input money: ";
        cin>>arr[i].money;}

    int grand_cnt = 0;
    cout<<"Grand patrons:"<<endl;
    for(i=0;i<n;++i){
        if(arr[i].money>10000){
            ++grand_cnt;
            cout<<arr[i].name<<endl;
        }
    }
    if(!grand_cnt)cout<<"none"<<endl;

    cout<<"Patrons:"<<endl;
    if(grand_cnt==n)cout<<"none"<<endl;
    else{
        for(i=0;i<n;++i){
            if(arr[i].money<=10000){
                cout<<arr[i].name<<endl;
            }
        }
    }

7

    cout << "Enter words (q to quit):" << endl;
    string str;
    char ch;
    int vowels = 0, con = 0, other = 0;
    while (cin >> str && str != "q")
    {
        ch = str[0];
        if (isalpha(ch))
        {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                ++vowels;
            else
                ++con;
        }
        else
            ++other;
    }
    cout << vowels << " words beginning with vowels" << endl;
    cout << con << " words beginning with consonants" << endl;
    cout << other << " others" << endl;

8

    ifstream fr;
    fr.open("io.txt");
    int cnt = 0;
    char ch;
 // while ((ch = fr.get()) && !fr.eof())
 // while ((ch = fr.get()) && !fr.fail())
    while ((ch = fr.get()) && ch!=EOF)
    {
        ++cnt;
        cout << cnt <<'\t'<< char(ch)<<endl;
    }
    cout << cnt << endl;

9

ifstream fr;
    fr.open("io.txt");
    int n;
    fr >> n;
    fr.get();//吸收回车
    struct person
    {
        char name[20];
        double money;
    };
    vector<person> arr(n);
    int i;
    for (i = 0; i < n; ++i)
    {
        fr.get(arr[i].name, 19);
        fr >> arr[i].money;
        fr.get(); //吸收回车
    }

    int grand_cnt = 0;
    cout << "Grand patrons:" << endl;
    for (i = 0; i < n; ++i)
    {
        if (arr[i].money > 10000)
        {
            ++grand_cnt;
            cout << arr[i].name << endl;
        }
    }
    if (!grand_cnt)
        cout << "none" << endl;

    cout << "Patrons:" << endl;
    if (grand_cnt == n)
        cout << "none" << endl;
    else
    {
        for (i = 0; i < n; ++i)
        {
            if (arr[i].money <= 10000)
            {
                cout << arr[i].name << endl;
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值