C ++ Primer (第五版) 第五章习题练习

学习笔记所用
本文部分内容参考了:C++Primer第五版——习题答案+详解(完整版)

5.1

最简单的语句是空语句,空语句中只含有一个单独的分号;
用处:语法上需要一条语句但是逻辑上不需要。

5.2

块:块就是复合语句,用花括号括起来的语句何生命的序列。
用处:语法上需要一条语句,但是逻辑上需要多条语句。

5.3

#include <iostream>
using namespace std;

int main() {
	int sum = 0, val = 1;
	while (val<=10)
	{
		sum += val, ++val;
	}
	cout << sum;
	return 1;
}

5.4

a:iter没有进行初始化
b:if语句中的status不在作用域内

5.7

a: 缺少分号。
b: 缺少花括号
c: ival在第二个if语句中没有声明
d: ==

5.8

悬垂else:怎么知道某个给定的else是和那个if匹配
解决:使用花括号来控制执行路径。

5.9

#include <iostream>
using namespace std;

int main()
{
    char c;
    int cnt = 0;
    while (cin >> c) {
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            cnt++;
    }
    cout << cnt;
    return 0;
}

5.10

#include <iostream>
using namespace std;

int main()
{
    char c;
    int cnt = 0;
    while (cin >> c) {
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'|| c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
            cnt++;
    }
    cout << cnt;
    return 0;
}

5.12
C++Primer第五版 第五章习题答案(11~20)
5.13

a: 每个case后边都少了break;
b: ix不应该初始化
c: 1,3,5,7,9不是整形常量表达式
d: ival,jval,kval不是常量表达式

5.14

#include <iostream>
#include <string>
using namespace std;
//how now now now brown cow cow cow cow
//how now brown cow 

int main()
{
    string s;
    string smax, temp;
    int max = 0;
    int num;
    while (cin >> s) {
        if (smax.empty()) {//处理第一个字符串
            smax = s; max = 1;//默认第一个是出现最多的单词 
            temp = s; num = 1;//temp保存上一个单词,num保存当前单词连续出现了几次
        }
        else {
            if (s == temp) {
                num++;
                if (num > max) {
                    smax = s; max = num;
                }
            }
            else {
                temp = s; num = 1;
            }
        }
    }
    if (max > 1)
        cout << "出现最多次数的单词是:" << smax << "出现次数是:" << max;
    else
        cout << "任何单词都没有连续出现过!";
    return 0;
}

5.15

a: 含义:当ix不等于sz的时候,ix就递增。  但是循环只有ix!=sz的时候才进入,所以if永远不可能成立
b: 缺少一个;来表示空的init-statement
c: 当ix不等一sz的时候,ix和sz都递增,那么如果初始化sz不是0,并且循环体不做处理,那么将会进入死循环

5.16

while:常用在不确定到底要迭代多少次时。
for:for语句更加灵活。
这两个可以相互转换,熟练掌握一个,并且能够互相转换感觉就可以使用了。

5.17

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    vector <int> v1 = { 0,1,1,2 }, v2 = {0,1,1,2,3,5,8};
    int i=0, n;
    n = (v1.size() < v2.size()) ? v1.size() : v2.size();//控制循环次数,始终是最短的那个
    auto b1 = v1.begin(), b2 = v2.begin();
    bool flag = true;//标识符
    for (i = 0; i < n; i++) {
        if (*b1 != *b2) {
            flag = false;
            break;
        }
        b1++; b2++;
    }
    if (flag) cout << "真";
    else cout << "假";
    return 0;
}

5.18

a:输入两个数并且输出它们的和。但是do部分缺少花括号'{}'
b: 如果在do里边不使用ival就没有错误,否则有错误。警告:尽量不要在while后的condition部分声明变量
c: 会显示condition部分的ival未定义。要在循环体外定义

5.19

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string s1, s2, pd;
    do {
        cout << "请输入两个字符串:";
        cin >> s1 >> s2;
        if (s1.size() < s2.size()) {
            cout << s1;
        }
        else
            cout << s2;
        cin >> pd;
    } while (!pd.empty()&&pd[0]=='y');
    return 1;
}

5.20

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//ad ada rrdf a
//how now now 

int main()
{
    string s1, temp;
    while (true) {
        if (cin >> s1) {
            if (temp == s1) {
                cout << "重复单词是:" << s1;
                break;
            }
            temp = s1;
        }
        else {
            cout << "没有任何单词是连续重复出现的!";
            break;
        }
    }
    return 1;
}

5.21

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//ada ada rrdf ab Av Av

int main()
{
    string s1, temp;
    while (true) {
        if (cin >> s1) {
            if (temp == s1) {
                if (s1[0] < 'A' || s1[0]>'Z') continue;//如果不是大写字母,则不用执行下边的break
                cout << "重复单词是:" << s1;
                break;
            }
            temp = s1;
        }
        else {
            cout << "没有任何单词是连续重复出现的!";
            break;
        }
    }
    return 1;
}

5.22

while (true) {
        int sz = get_size();
        if (sz > 0)break;
    }

5.23

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//ada ada rrdf ab Av Av

int main()
{
    float a, b;
    while (cin >> a >> b) {
        cout << "a/b=" << a / b << endl;
    }
    return 1;
}

5.24

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    float a, b;
    while (cin >> a >> b) {
        if (b == 0) {
            throw range_error("除数不能为0!");
        }
        cout << "a/b=" << a / b << endl;
    }
    return 1;
}

5.25

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    float a, b;
    while (cin >> a >> b) {
        if (b == 0) {            
            try {
                throw range_error("除数不能为0!");
            }
            catch (range_error err) {
            	cout << err.what()<<endl;
                cout << "请重新输入b:";
                cin >> b;
            }
        }
        cout << "a/b=" << a / b << endl;
    }
    return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值