C++Primer第五版——习题答案详解

开始刷《C++Primer》这本书,会在博客上更新课后习题。

水平有限,如有有误之处,希望大家不吝指教!

不断学习中,欢迎交流!



第一二章课后题

练习1.3

#include<iostream>
int main(){
    std::cout<<"Hello world"<<std::endl;
    return 0;
}

练习1.4

#include<iostream>
int main(){
    std::cout << "Input two numbers: " << std::endl;
    int a, b;
    std::cin >> a >> b;
    std::cout << a <<" * "<< b << " = " << a * b << std::endl;
}

练习1.5

#include<iostream>
int main(){
    std::cout << "Input two numbers: " << std::endl;
    int a, b;
    std::cin >> a >> b;
    std::cout << a;
    std::cout<<" * ";
    std::cout<< b ;
    std::cout<< " = " ;
    std::cout<< a * b ;
    std::cout<< std::endl;
}

练习1.6
不合法,第一行有分号表示语句结束,改为如下:

#include<iostream>
int main(){
    std::cout << "Input two numbers: " << std::endl;
    int a, b;
    std::cin >> a >> b;
    std::cout << "The sum of "<< a << " and " << b<< " is " << a + b <<std::endl;
    return 0;
}

练习1.7

#include<iostream>
int main(){
    /*
    /* */注释不能嵌套!
    */
    return 0;
}

练习1.8
第三行错误,因前双引号被注释掉了,后双引号不匹配。

#include<iostream>
int main(){
    std::cout << "/*"<<std::endl;
    std::cout << "*/"<<std::endl;
    //std::cout << /* "*/" */<<std::endl;
    std::cout << /* "*/" /* "/*" */<<std::endl;
    return 0;
}

练习1.9

 #include<iostream>
int main(){
    int sum = 0, val = 50;
    while (val <= 100){
        sum += val;
        ++val;
    }
    std::cout << sum << std::endl;
    return 0;
}

练习1.10

#include<iostream>
int main(){
    int sum = 0, val = 10;
    while (val >= 0){
        sum += val;
        --val;
    }
    std::cout << sum << std::endl;
    return 0;
}

练习1.11

#include<iostream>
int main(){
    int a, b;
    std::cin >> a >> b;
    while (a <= b){
        std::cout << a << " ";
        ++a;
    }
    return 0;
}

练习1.12
程序的功能是求[-100,100]范围内的整数的和,sum的终值为0

练习1.14
已知循环次数的时候用for简便,未知时用while简便。

练习1.16

#include<iostream>
int main(){
    int a, sum = 0;
    while(std::cin >> a){
        sum += a;
    }
    std::cout << sum;
    return 0;
}

练习1.19

#include<iostream>
int main(){
    int a, b;
    std::cin >> a >> b;
    if( a > b ){
        int temp = a;
        a = b;
        b = temp;
    }
    while (a <= b){
        std::cout << a << " ";
        ++a;
    }
    return 0;
}

练习1.20

#include <iostream>
#include "Sales_item.h"
int main(){
    Sales_item book;
    while(std::cin >> book){
        std::cout << "Record: " << book <<std::endl;
    }
    return 0;
}

练习1.21

#include <iostream>
#include "Sales_item.h"
int main(){
    Sales_item book1, book2;
    std::cin >> book1 >> book2;
    std::cout << book1 + book2 <<std::endl;
    return 0;
}

练习2.8

#include<iostream>
int main(){
    cout<<"2M"<<'\n';
    cout<<'2'<<'\t'<<'M'<<'\n';
}

练习2.9
a.需要在cin前定义变量名
b.3.14强制转换为Int有精度损失
c.wage未定义
d.同b

练习2.15
a.定义合法但有精度损失
b.引用类型的初始值必须是一个对象
c.正确
d.同b

练习2.17
10 10

练习2.27
a.不合法,引用r的赋值对象必须是一个对象
b.合法,将p2设置为一个常量指针,初始化为i2对象的地址
c.合法,将i设为常量-1,r设置为常量的引用
d.合法,将p3设为指向常量的常量指针,初始化为i2的地址
e.合法,将p1设为指向常量的指针,初始化为i2的地址
f.不合法,常量指针必须初始化
g.合法

练习2.28
a.不合法,常量指针必须初始化
b.不合法,同a
c.不合法,常量ic未初始化
d.不合法,同a
e.合法。

原文地址:https://www.cnblogs.com/Mered1th/p/10480841.html

时间: 2024-07-31 09:42:20

C++Primer第五版——习题答案详解的相关文章

C++Primer第五版——习题答案详解(五)

习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using namespace std; int fact(int x) { if (x == 1) return x; else return x * fact(x - 1); } int main() { int x; cout << "Please input a number:\n"

C++Primer第五版——习题答案详解(七)

习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &is) { string s; while (is >> s) { cout << s << endl; } is.clear(); return is; } 练习8.2 #include<iostream> #include<string>

C++Primer第五版——习题答案目录

目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS2017,答案用markdown写的. 第1章 开始&&第2章 变量和基本类型 ? 第3章 字符串.向量和数组 ? 第4章 表达式 ? 第5章 语句 ? 第6章 函数 ? 第7章 类 ? 第8章 IO库 ? ? ? ? ? ? ? ? ? ? ? ? ? ? 不断学习中,欢迎交流! 原文地址:

c++ Primer 第五版习题答案第三章

3.2 编写程序,从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词. void readByLine () {    string line; ?    while (getline (cin, line)) {        cout << line << endl;   }   } void readByWord () {    string word; ?    while (cin >> word) {        cout << wo

c++ Primer 第五版习题答案第二章

练习2.1 Q: 类型int.long.long long和short的区别是什么,无符号和带符号类型的区别是什么?float和double的区别是什么? A:int. long. long long和short尺寸不同,表示的数据范围不同.无符号只能表示0和正数,无符号还可以表示负数.float为单精度浮点数,double为双精度,一般来说,float占4字节,double占8字节. 练习2.2 Q: 计算按揭贷款时,对于利率.本金和付款分别应选择何种数据类型?说明你的理由. A: 利率应该用

C++Primer第五版习题解答---第一章

C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #include<iostream> int main() { std::cout << "hello, world" << std::endl; return 0; } 练习1.4: #include<iostream> int main() { int

《C++Primer》第五版习题详细答案--目录

作者:cosefy ps: 答案是个人学习过程的记录,仅作参考. <C++Primer>第五版习题答案目录 第一章:引用 第二章:变量和基本类型 第三章:字符串,向量和数组 第四章:表达式 原文地址:https://www.cnblogs.com/cosefy/p/12180771.html

Python核心编程(第二版) 第五章习题答案

5-1.整型.讲讲Python普通整型和长整型的区别. 答:Python 的标准整数类型是最通用的数字类型.在大多数 32 位机器上,标准整数类型的取值范围是-2**31到 2**31-1,也就是-2,147,483,648 到 2,147,483,647.如果在 64 位机器上使用 64 位编译器编译 Python,那么在这个系统上的整数将是 64 位. Python 的长整数类型能表达的数值仅仅与你的机器支持的(虚拟)内存大小有关. 5-2.操作符.(a)写一个函数,计算并返回两个数的乘积.

《C++ Primer 第五版》练习9.51参考答案

//Date.h #include <map> #include <string> #include <vector> using namespace std; struct Date {         explicit Date(const string & info){//检测输入格式,尝试初始化,若失败则进行errorInit             if(mymap.empty()){               initMap();