《C++ primer》第五版习题答案整理——第一章 开始

P5

练习1.1

我使用的编译器为visio studio2019

int main()

{

    return 0;

}

练习1.2

返回-1 和返回0 ,在单个函数中没看出有什么区别,系统也不会做什么处理。

P8

练习1.3

#include<iostream>

int main()

{

    std::cout << "Hollow world!!!!!!!!!!" << std::endl;

    return 0;

}

练习1.4

#include<iostream>

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<iostream>

int main()

{

    std::cout << "Enter two numbers:";

    std::cout << std::endl;

    int v1 = 0, v2 = 0;

    std::cin >> v1;

    std::cin >> v2;

    std::cout << " the sum 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对象,右侧运算对象是打印的值。

改正:

Std::cout<<”The sum of “<<v1<<”and”<<v2<<”is”<<v1+v2<<std::endl;

P9

练习1.7

int main()

{

    /*

        /*

        */

    */

    return 0;

}

输出结果编译错误 :在注释外找到“*/”

练习1.8

无合法(若在一个程序里)

S1 S2合法(若每行各为一程序)

P11

练习1.9

#include<iostream>

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<iostream>

int main()

{

    int sum = 0, val = 10;

    while (val >=1)

    {

         sum += val;

         --val;

    }

    std::cout << "Sum of 10 to 0 inclusive is " << sum << std::endl;

    return 0;

}

练习1.11

#include<iostream>

int main()

{

    std::cout << "请输入两个整数: " << std::endl;

    int v1 = 0, v2 = 0;

    std::cin >> v1 >> v2;

    if (v1 <= v2)

    {

         while (v1 <= v2)

         {

             std::cout << v1 << std::endl;

             ++v1;

         }

    }

    else

    {

         while (v1 >= v2)

         {

             std::cout << v2 << std::endl;

             ++v2;

         }

    }

    return 0;

}

P12

练习1.12

-100到100之间所有整数的和;sum的终值是0.

练习1.13

1.9

#include<iostream>

int main()

{

    int sum = 0;

    for (int val = 50; val <= 100; ++val)

    {

         sum += val;

    }

    std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;

    return 0;

}

1.10

#include<iostream>

int main()

{

    int sum = 0;

    for (int val = 10; val >=0; --val)

    {

         sum += val;

    }

    std::cout << "Sum of 10 to 0 inclusive is " << sum << std::endl;

    return 0;

}

1.11

#include<iostream>

int main()

{

    std::cout << "请输入两个整数: " << std::endl;

    int i = 0, j = 0;

    std::cin >> i >> j;

    if (i <= j)

    {

         for (i; i <= j; i++)

             std::cout << i << std::endl;

    }

    else

    {

         for (i; i >= j; i--)

             std::cout << i << std::endl;

    }

    return 0;

}

练习1.14

在for循环中,循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。两种形式各有优点,但它们在功能上是等价的,可以相互转换。

练习1.15

执行编译运行

P15

练习1.16

#include<iostream>

int main()

{

    int sum = 0, val = 0;

    while (std::cin >> val)//键盘输入文件结束符 Windows中 Ctrl+Z 然后按Enter或Return

    {

         sum += val;

    }

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

    return 0;

}

P16

练习1.17

如果所有值相等输出的 为输入元素的个数

如果所有值不重复输出为每个元素个数为一

练习1.18

执行编译运行

练习1.19

#include<iostream>

int main()

{

    int sum = 0, val = 10;

    while (val >=1)

    {

         sum += val;

         --val;

    }

    std::cout << "Sum of 10 to 0 inclusive is " << sum << std::endl;

    return 0;

}

P20

练习1.20

#include<iostream>

#include"Sales_item.h"//下载添加头文件"Sales_item.h"

int main()

{

    Sales_item book;

    while (std::cin >> book)

    {

         std::cout << book << std::endl;

    }

    return 0;

}

练习1.21

#include<iostream>

#include"Sales_item.h"//下载添加头文件"Sales_item.h"

int main()

{

    Sales_item item1, item2;

    std::cin >> item1 >> item2;

    std::cout << item1 + item2 << std::endl;

    return 0;

}

练习1.22

#include<iostream>

#include"Sales_item.h"//下载添加头文件"Sales_item.h"

int main()

{

    Sales_item book, total;

    while (std::cin >> book)

    {

         total += book;

    }

    std::cout << total << std::endl;

    return 0;

}

P21

练习1.23&练习1.24

#include<iostream>

#include"Sales_item.h"//下载添加头文件"Sales_item.h"

int main()//输入相同的ISBN必须连续,不可跳跃。

{

    Sales_item book;

    if (std::cin >> book)

    {

         Sales_item temp;

         while (std::cin >> temp)

         {

             if (book.isbn() == temp.isbn())

                  book += temp;

             else

             {

                  std::cout << book << std::endl;

                  book = temp;

             }

         }

         std::cout << book << std::endl;

    }

    return 0;

}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值