《C++Primer》第五版习题答案第五章--【学习笔记】

《C++Primer》第五版习题答案--第五章【学习笔记】

ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考。
作者:cosefy
Date: 2020/1/15

第三章:语句

练习5.3:

代码可读性降低了。

while(val<=10)
    sum+=val,++val;

练习5.4:

  • iter未初始化。
  • if语句中的status超过作用范围,且status在while中进行了判断。

练习5.5:

#include<iostream>
#include<vector>

using namespace std;
int main() 
{
    //vector<string> str{ "甲","乙","丙" };
    int score;
    string rank;
    while (cin >> score)
    {
        if (score < 60)
            rank = "丙";
        else
            if (score < 90)
                rank = "乙";
            else
                rank = "甲";
        cout << rank << endl;
        return 0;
    }
}

练习5.6:

rank = score > 90 ? "甲" : score > 60 ? "乙":"丙";

练习5.7:

  • ival2后少了分号。
  • 忘记加上花括号
  • 第二个if语句中的ival超过了作用范围,且第一个if语句已经进行了判断。
  • if语句中条件永远为False。

练习5.9:

#include<iostream>
#include<vector>

using namespace std;
int main() 
{
    int a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count = 0;
    int other_count = 0;
    char c;
    while (cin >> c)
        if (c == 'a')
            ++a_count;
        else if (c == 'e')
            ++e_count;
        else if (c == 'i')
            ++i_count;
        else if (c == 'o')
            ++o_count;
        else if (c == 'u')
            ++u_count;
        else
            ++other_count;

    cout << "a: " << a_count << endl;
    cout << "e: " << e_count << endl; 
    cout << "i: " << i_count << endl; 
    cout << "o: " << o_count << endl;
    cout << "u: " << u_count << endl;
    return 0;
}

练习5.10:

代码形式如下所示:

case 'a':
case'A':
    ++a_count;
    break;

练习5.11:

修改while判断语句为:

while (cin >>noskipws>> c)

这样不会忽略空白,另外加上统计制表符等的case语句就可以了。

练习5.12:

保留上一个字符,如果当前字符为'f'或'i'或'l',则判断上个字符是否是'f'。

练习5.13:

  • 忘记break
  • ix定义位置错误
  • case后面接整型常量表达式
  • case后面是整型常量,可以用const修饰ival,jval,kval。

练习5.14:

#include<iostream>
#include<vector>
using namespace std;
int main() 
{
    string now_word, last_word,record_word;
    int max = 1,count=1,flag=0;
    while (cin >> now_word)
    {
        if (now_word == last_word) {
            ++count;
            last_word = now_word;
        }
        else
        {
            if (max < count)
            {
                max = count;
                record_word = last_word;
            }
            count = 1;
            last_word = now_word;
            flag = 1;  //标记出现了不重复的单词
        }
    }
    if (flag==0)
        cout << "仅有单词"<<now_word << "出现了" << count << "次" << endl;
    else
        cout << record_word << "出现了"<<max << "次" << endl;
    return 0;
}

练习5.15:

  • ix定义位置错误,应在for循环外定义
  • 缺少一个分号
  • 无限循环

练习5.17:

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

int main() 
{
    vector<int>v1{ 1,0,2,2,3,5 };
    vector<int>v2{ 1,0,2,2, };
    auto it1 = v1.begin();
    auto it2 = v2.begin();
    for (; it2 != v2.end(); ++it1, it2++)
        if (*it2 != *it1)
            break;
    cout << (it2 == v2.end() ? "YES" : "NO");
    return 0;
}

练习5.18:

  • do语句块忘记花括号
  • while条件语句不用来定义变量
  • 变量应定义在循环体外

练习5.20:

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

int main() 
{
    string last_word, accur_word;
    bool flag = false;
    while (cin >> accur_word)
    {
        if (accur_word == last_word)
        {
            cout << accur_word << endl;
            break;
        }
        else
        {
            last_word = accur_word;
            flag = true;
        }
    }
    if (!flag)
        cout << "不连续重复" << endl;
    return 0;
}

练习5.23:

#include<iostream>
using namespace std;
int main() 
{
    int m, n;
    cin >> m >> n;
    cout << "m/n:"<<m / n << endl;
    return 0;
}

练习5.24:


练习5.25:

#include<iostream>
using namespace std;
int main() 
{
    int m, n;
    while (cin >> m >> n)
    {
        try 
        {
            if (n == 0)
                throw runtime_error("除数不可为0");
            cout << "m/n:" << m / n << endl;
        }
        catch (runtime_error err)
        {
            cout << err.what()
                << "\nTry Again? Enter Y or N" << endl;
            char c;
            cin >> c;
            if (!cin||c == 'N')
                break;          
        }
        
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cosefy/p/12198957.html