C++ primer(第五版)第1章习题答案

初学C++ primer,记录整理,使用ubuntu14.04和GNU编译器,答案参考于github

第一章  开始

1.1

文件命名约定:参考这里(https://www.cnblogs.com/yang666/p/6515623.html)

GCC 5.4  头文件后缀: h, hh, hpp, H, tcc (for shared template code);

         源文件后缀: cp, cpp, CPP,  C,  cc,  cxx,  c++.

1.2

C++ primer(第五版)第1章习题答案_第1张图片

返回值为255,含义是超出了退出状态的范围,具体参考这里

 

1.3

#include 

int main()
{
    std::cout << "Hello, World" << std::endl;
    return 0;
}

 

1.4

#include 

int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The product of " << v1 << " and " << v2
              << " is " << v1 * v2 << std::endl;
    return 0;
}

 

1.5

#include 

int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The product of ";
    std::cout << v1;
    std::cout << " and ";
    std::cout << v2;
    std::cout << " is ";
    std::cout << v1 * v2;
    std::cout << std::endl;
    return 0;
}

 

1.6

不合法,去掉前两个分号,分号代表语句结束,会导致后两个输出运算符左侧没有ostream对象

 

1.7

/*
* comment pairs /* */ cannot nest.
* ''cannot nest'' is considered source code,
* as is the rest of the program
*/
int main()
{
  return 0;
}
 

 

 

1.8

C++ primer(第五版)第1章习题答案_第2张图片

第三句不合法,在分号前添加双引号即可

 

1.9

#include 

int main()
{
    int sum = 0, val = 50;
    while (val <= 100) {
        sum += val;
        ++val;
    }
    std::cout << "Sum of  50 to 100 inclusive is  " << sum << std::endl;

    return 0;
}

 

1.10

#include 

int main()
{
    int val = 10;
    while (val >= 0) {
        std::cout << val << std::endl;
        --val;
    }

    return 0;
}

 

1.11

#include 

int main()
{
    int small = 0, big = 0;
    std::cout << "please input two integers:";
    std::cin >> small >> big;

    if (small > big) {
        int tmp = small;
        small = big;
        big = tmp;
    }

    while (small <= big) {
        std::cout << small << " ";
        ++small;
    }
    std::cout << std::endl;

    return 0;
}

 

1.12

 -100到100的和,sum为0

 

1.13

 

Ex1.9:

#include 

int main()
{
    int sum = 0;
    for (int i = 50; i <= 100; ++i)
        sum += i;

    std::cout << "the sum is: " << sum << std::endl;

    return 0;
}

Ex1.10:

#include 

int main()
{
    for (int i = 10; i >= 0; --i)
        std::cout << i << " ";
    std::cout << std::endl;

    return 0;
}

Ex1.11:

#include 

int main()
{
    int small = 0, big = 0;
    std::cout << "please input two integers:";
    std::cin >> small >> big;

    if (small > big) {
        int tmp = small;
        small = big;
        big = tmp;
    }

    for (int i = small; i <= big; ++i)
        std::cout << i << " ";
    std::cout << std::endl;

    return 0;
}

 

1.14

注意:for语句中定义的变量在循环结束之后是不可以使用的。

 

1.15

语法错误

C++ primer(第五版)第1章习题答案_第3张图片

类型错误

声明错误

C++ primer(第五版)第1章习题答案_第4张图片

 

1.16

#include 

int main()
{
    int sum = 0;
    for (int value = 0; std::cin >> value; )
        sum += value;
    std::cout << sum << std::endl;
    return 0;
}

 

1.17

都相等,输出总共次数,都不重复,输出每个值重复一次。

 

1.18

C++ primer(第五版)第1章习题答案_第5张图片

 

1.19

见1.10

 

1.20

#include 
#include "./1/Sales_item.h"

int main()
{
    Sales_item item;
    while (std::cin >> item) std::cout << item << std::endl;

    return 0;
}

我这边需要指定-std=c++0x参数

 

1.21

#include 
#include "./1/Sales_item.h"

int main()
{
    Sales_item item1, item2;
    std::cin >> item1 >> item2;
    if (item1.isbn() == item2.isbn()) {
        std::cout << item1 + item2 << std::endl;
        return 0;
    }
    else {
        std::cerr << "Data must refer to same ISBN." << std::endl;
        return -1;
    }
}

 

1.22

#include 
#include "./1/Sales_item.h"

int main()
{
    Sales_item total;
    if (std::cin >> total) {
        Sales_item trans;
        while (std::cin >> trans) {
            if (total.isbn() == trans.isbn())
                total += trans;
            else {
                std::cout << total << std::endl;
                total = trans;
            }
        }
        std::cout << total << std::endl;
    }
    else {
        std::cerr << "No data?!" << std::endl;
        return -1;
    }

    return 0;
}

 

1.23

#include 
#include "../include/Sales_item.h"

int main()
{
    Sales_item currItem, valItem;
    if (std::cin >> currItem) {
        int cnt = 1;
        while (std::cin >> valItem) {
            if (valItem.isbn() == currItem.isbn())
                ++cnt;
            else {
                std::cout << currItem << " occurs " << cnt << " times "
                          << std::endl;
                currItem = valItem;
                cnt = 1;
            }
        }

        std::cout << currItem << " occurs " << cnt << " times " << std::endl;
    }
    return 0;
}

 

1.24

C++ primer(第五版)第1章习题答案_第6张图片

 

1.25

C++ primer(第五版)第1章习题答案_第7张图片

 

你可能感兴趣的:(c++)