《C++ Primer》第五版课后习题解答_第一章(2)(12-25)

系统环境: windows 10 1703

编译环境:Visual studio 2017


1.12

此程序计算了从-100至100的所有证书之和,sum终值为0。


1.13

(1.9)

#include <iostream>

int main()
{
    int sum = 0;
    for (int i = 50; i <= 100; ++i)
    {
        sum += i;
    }
    std::cout << "The sum of 50 to 100 is: " << sum << std::endl;
    return 0;
}

(1.10)

#include <iostream>

int main()
{
    for (int i = 10; i >= 0; --i)
    {
        std::cout << i << std::endl;
    }
    return 0;
}
把循环条件里的大于号写成小于号了,看了半天没看出来。循环条件是,满足条件时循环继续,而不是跳出。


(1.11)

#include <iostream>

int main()
{
    int v1 = 0, v2 = 0;
    std::cout << "Enter two numbers:" << std::endl;
    std::cin >> v1 >> v2;
    if (v1 >= v2)
    {
        v1 = v1 + v2;
        v2 = v1 - v2;
        v1 = v1 - v2;
    }
    for (int i = v1; i <= v2; ++i)
    {
        std::cout << i << std::endl;
    }
    return 0;
}

1.14

for循环更加适用于循环次数已知的场景,while更加适用于循环次数为止的场景。


1.15

每次写错一行,观察错误信息。


1.16

#include <iostream>

int main()
{
    int sum = 0, value = 0;
    while (std::cin >> value)
    {
        sum += value;
    }
        std::cout << "Sum is: " << sum << std::endl;
        return 0;
}

1.17

输入所有值都是相等的,只输出一行,

ie. 

input 5 5 5 5 5 5 5

output 5 occurs 7 times;

输入没有重复值,输出与输入个数相等的行,

ie. 

input: 1 2 3

output: 1 occurs 1 times;

            2 occurs 1 times;

            3 occurs 1 times;


1.18

结果同上。


1.19

#include <iostream>

int main()
{
    int v1 = 0, v2 = 0;
    std::cout << "Enter two integers:" << "\n" << std::endl;
    std::cin >> v1 >> v2;
    if (v1 < v2)
    {
        v1 = v1 + v2;
        v2 = v1 - v2;
        v1 = v1 - v2;
    }
    for (int i = v1; i >= v2; --i)
    {
        std::cout << i << std::endl;
    }
    return 0;
}

iput: 3 8

output: 8 7 6 5 4 3


1.20

#include <iostream>
#include "Sales_item.h"

int main()
{
    Sales_item book;
    std::cin >> book;
    std::cout << book << std::endl;
    return 0;
}

input: 0-201-82470-1 4 45.39

output: 0-201-82470-1 4 181.56 45.39


1.21

#include <iostream>
#include "Sales_item.h"

int main()
{
    Sales_item book1, book2;
    std::cout << "Enter two ISBNs" << std::endl;
    std::cin >> book1 >> book2;
    if (book1.isbn() == book2.isbn())
    {
        std::cout << book1 + book2 << std::endl;
    } 
    else
    {
        std::cout << "The two books have different ISBN" << std::endl;
    }
    
    return 0;
}

input: Enter two ISBNs
  0-201-88954-4 2 15.00
  0-201-88954-4 5 12.00

output: 0-201-88954-4 7 90 12.8571


1.22

#include <iostream>
#include "Sales_item.h"

int main()
{
    Sales_item book, sum;
    std::cin >> sum; //必须添加此句,否则输出中无ISBN号
    while (std::cin >> book)
    {
        sum += book;
    }
    std::cout << sum << std::endl;
    
    return 0;
}

input: 0-201-88954-4 5 12.00
  0-201-88954-4 2 12.00

  0-201-88954-4 7 12.00

output: 0-201-88954-4 12 144 12

原本无法在输出中输出ISBN号,后来发现必须加上上文中注释所示段落。


1.23

#include <iostream>
#include "Sales_item.h"

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

1.24

0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-70353-X occurs 1 times.
0-201-88954-4 2 15.00
0-201-82470-1 occurs 1 times.
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-201-88954-4 occurs 4 times.
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-399-82477-1 occurs 5 times.
0-201-78345-X 2 25.00
^Z
0-201-78345-X occurs 6 times.


1.25

#include <iostream>
#include "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;
}

0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-70353-X 4 99.96 24.99
0-201-88954-4 2 15.00
0-201-82470-1 4 181.56 45.39
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-201-88954-4 16 198 12.375
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-399-82477-1 5 226.95 45.39
0-201-78345-X 2 25.00
^Z
0-201-78345-X 5 110 22

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值