• C++ primer第三章作业


    3.1节

    练习3.1:

    使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习

    #ifdef 1
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(void)
    {
        int sum = 0;
        int i = 50;
        
        while (i <= 100) {
            sum += i;
            i++;
        }
    
        cout << sum << endl;
        return 0;
    }
    #endif
    
    #ifdef 2
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(void)
    {
        int n = 10;
        while (n >= 0) {
            cout << n << endl;
            n--;
        }
        return 0;
    }
    #endif
    
    #ifdef 3
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main(void)
    {
        int i, j;
        cout << "input two value" << endl;
        cin >> i >> j;
    
        int bigger, smaller;
        bigger = (i > j ? i : j);
        smaller = (i > j ? j : i);
    
        while ( smaller <= bigger ) {
            cout << smaller << endl;
            smaller++;
        } 
    
        return 0;
    }
    
    
    #endif
    

    3.2.2 节练习

    练习3.2:

    编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。

    #include <iostream>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(void)
    {
        string str;
    
    //    while (getline(cin, str))
    //        cout << str << endl;
    
        while (cin >> str)
            cout << str << endl;
    
        return 0;
    
    }
    

    练习3.3:

    请说明string类的输入运算符和getline函数分别是如何处理空白字符的。

    空白字符包含换行符,空格符,制表符等。
    cin从第一个非空白字符开始,将标准输入流的字符传递给指定字符串,并且以空字符为结束。
    getline从标准输入流读取用户输入的内容,以换行符作为结束(换行符也被读入流),并将流写入str(抛弃换行符)。

    练习3.4

    编写一段程序读入两个字符串,## 3.1节

    练习3.1:

    使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习

    #ifdef 1
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(void)
    {
        int sum = 0;
        int i = 50;
        
        while (i <= 100) {
            sum += i;
            i++;
        }
    
        cout << sum << endl;
        return 0;
    }
    #endif
    
    #ifdef 2
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main(void)
    {
        int n = 10;
        while (n >= 0) {
            cout << n << endl;
            n--;
        }
        return 0;
    }
    #endif
    
    #ifdef 3
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main(void)
    {
        int i, j;
        cout << "input two value" << endl;
        cin >> i >> j;
    
        int bigger, smaller;
        bigger = (i > j ? i : j);
        smaller = (i > j ? j : i);
    
        while ( smaller <= bigger ) {
            cout << smaller << endl;
            smaller++;
        } 
    
        return 0;
    }
    
    
    #endif
    

    3.2.2 节练习

    练习3.2:

    编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。

    #include <iostream>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(void)
    {
        string str;
    
    //    while (getline(cin, str))
    //        cout << str << endl;
    
        while (cin >> str)
            cout << str << endl;
    
        return 0;
    
    }
    

    练习3.3:

    请说明string类的输入运算符和getline函数分别是如何处理空白字符的。

    空白字符包含换行符,空格符,制表符等。
    cin从第一个非空白字符开始,将标准输入流的字符传递给指定字符串,并且以空字符为结束。
    getline从标准输入流读取用户输入的内容,以换行符作为结束(换行符也被读入流),并将流写入str(抛弃换行符)。

    练习3.4

    编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不相等,输出较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

    #include <iostream>
    #include <string>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(void)
    {
        string str1, str2;
        cin >> str1 >> str2;
        
        string max;
    
        if (str1 != str2) {
            max = (str1 > str2) ? str1 : str2;
            cout << max << endl;
        } 
    
        if (str1.size() != str2.size()) {
            max = (str1.size() > str2.size()) ? str1 : str2;
            cout << max << endl;
        } 
    
        return 0;
    }
    
    

    练习3.5

    编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。

    #include <iostream>
    #include <string>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    
    int main(void)
    {
        string str1, str2, str3;
    
        cin >> str1 >> str2 >> str3;
        string cat = str1 + " " + str2 + " " + str3;
    
        cout << cat << endl;
    
        return 0;
    }
    

    3.2.3 节练习

    练习3.6:

    编写一段程序,使用范围for语句将字符串内的所有字符用X代替。

        string str = "hello world";
        cout << str << endl;
    
        for (auto &c : str) {
            c = 'X';
        }
    
    

    练习3.7:

    就上一题完成的程序而言,如果将循环控制变量的类型设为char 将发生什么?先估计一下结果,然后实际编程验证。

    将循环变量改为char类型,字符串的值不会改变。


    练习3.8:

    分别用while循环和传统的for循环重写第一题的程序,你觉得哪种形式更好呢?为什么?

    我觉得区别不大

        string str = "hello world";
        cout << str << endl;
    
        //1
        for (auto &c : str) {
            c = 'X';
        }
    
        //2
        int i = 0;
        while (i < str.size()) {
             str[i] = 'X';
        }
        cout << str << endl;
    
        //3
       // int i;
       // for (i = 0; i < str.size(); i++)
       //     str[i] = 'X';
       // cout << str << endl;
    

    练习3.9:

    下面的程序有何作用?它合法吗?如果不合法,为什么?

    string s;
    cout << s[0] << endl;

    s为空,s[0]的结果未定义,书上的答案。具体还有待探索。

    练习3.10:

    编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。

    练习3.11:

    下面的范围for语句合法吗?如果合法,c的类型是什么?

    const string s = "Keep out!";
    for (auto &c : s) { /* ... */ }

    不合法,s是一个常量字符串,不能对其进行修改。

    3.3.1节练习

    练习3.12:

    下列vector对象的定义有什么不正确的吗?如果有,请指出来。对于正确的,描述起执行结果;对于不正确的,说明其错误的原因。
    (a) vector<vector> vec;
    (b) vector svec = ivec;
    (c) vector svec(10, "null");

    b选项不正确,类型不匹配。

    练习3.13:

    下列的vector对象各包含多少个元素?这些元素的值分别是多少?
    (a) vector v1;
    0个
    (b) vector v2(10);
    10个0
    (c) vector v3(10, 42);
    10个42
    (d) vector v4{10};
    1个10
    (e) vector v5{10, 42};
    2个元素,10和42
    (f) vector v6{10};
    1个10

    3.3.2节练习

    练习3.14:

    编写一段程序,用cin读入一组整数并把它们存入一个vector对象

  • 相关阅读:
    异步编程案例分享—后台线程事实通知任务进度
    ORACLE 常见错误
    设计模式——工厂模式
    sql 脚本编写之路 常用语句(一)
    C# 深入浅出 异步(八)
    设计模式——单例模式
    Sql Server 学习链接
    SqlServer 常用函数
    2016 ACM/ICPC Asia Regional Shenyang Online
    2016 ACM/ICPC Asia Regional Dalian Online
  • 原文地址:https://www.cnblogs.com/dennis-wong/p/9071516.html
Copyright © 2020-2023  润新知