C++Primer Plus复习题(第六章)

/*复习题*/
/*看下面两个代码片段,第二种比第一种好在哪儿?
* Verssion 1 
* while(cin.get(ch)) //quit on eof
* {
*    if(ch == ' ' )
        spaces++;
    if(ch == '\n')
        newlines++;
}
    Version 2
    while(cin.get(h)) //quit on eof
    {
        if(ch == ' '){
            spaces++
        }else if(ch == '\n'){
            newlines++;
        }
    }


    可读性 2的时间复杂度更低,他只会判断一次,2更适合拓展
*/
/*
2.在程序清单6.2中,使用ch + 1,代替 ++ch 将会发送什么情况
6.2

#include<iostream>
using namespace std;

int main() {
    char ch;
    
    cin.get(ch);
    while (ch != '.') {
        if (ch =='\n') {
            cout << ch;
        }
        else {
        cout<<++ch;
            cout << ch + 1;
        }
        cin >> ch;
    }

    return 0;
}
一个打印的是字符
一个打印的是对于的ASCALL码

*/
/*
* 请认真考虑下面的程序
* 假设输入如下(请在每行末尾按回车键)
* HI!
* Send $10 or $20 now!
* 则其将输出什么(还记得吗,输入被缓冲)

#include <iostream>
using  namespace std;

int main() {
    char ch;
    int ct1 = 0, ct2 = 0;

    while ((ch = cin.get()) != '$') {
        cout << ch;
        ct1++;
        if (ch = '$') {//书上是=  不是 ==
            ct2++;
        }
        cout << ch;
    }
    cout << "ct1=" << ct1 << ", ct2==" << ct2 << endl;
    return 0;

    如果 是= ,则ct2=ct1 = 9 == 则ct1=9 ct2 = 0 
}
*/

/*
*创建表诉下述条件的逻辑表达式
* a.weight 大于或者等于 115 但是小于125   (wieghe>=115 && weight<125)
* b.ch为q或Q  strcmp('q',ch) || strcmp('Q',ch)
* c.x为偶数,但不是26    x>=0 && x%2=0 && x!=26
* d.x为偶数,但不是26的背书 x>=0 && x%2 = 0 && x%26!=0
* e.donation为1000-2000,或者guest为1   (denation>1000 && denation<2000) || guest == 1
* f.ch是小写字母或者大写字母,小写字母依次编码 大写字母依次 大小写没有一次  (ch>='a' && ch <='z')||(ch>='A' && ch<='Z') 
*/

/*5
* 在英语中,"I will not not speak" 与 I will speak  意思相同 
* !!x  与 x是否相同呢

* x是bool的话应该是相同的 其他的不同
*/


/*
*6.创建一个条件表达式  其值为变量的绝对值,
* 如果变量为正即其正,为负,值为-x
* if(x>=0){
    num = x;
}else{
    mum = -1*x;
}

*/

/*7.用switch改写下面的代码片段
* switch(ch){
*    case 'A':
*        a_grade++;
*        break;
* case 'B':
*        b_grade++;
*        break;'
* case 'C':
*        c_grade++;
*        break;
* case 'D':
*        d_grade++;
*        break;

    default:
    f_grade++;
}
    
*/

/*8.对于程序清单 6.10 与使用数字相比,使用字符表示菜单选项和case标签有何优点呢*/
/*可读性 + 易于维护  不需要重新编号之类的*/

/*重新编写下面的代码片段 不适应break和continue *
 int line = 0;
 char ch;
 while(cin.get(ch)){
    if(ch == 'Q')
        break;
    if(ch!='\n')
        continue;
    line++;
 }

 while(ch!='Q'){
    cin.get(ch);
    if(ch =='\n')
        line++;
 }


*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值