Chapter Review

  1. Having more than one integer type lets you choose the type that is best suited to a particular need. For example, you could use short to conserve space or long to guarantee storage capacity or to find that a particular type speed up a particular calculation.
  2. short ribs = 80; // or short int ribs = 80;
    unsigned int q = 42110; // or unsigned q = 42110;
    unsigned long ants = 3000000000; // or long long ants = 3000000000;
    Note: Don't count on int being large enough to hold 3,000,000,000. Also if your system supports universal list-initialization, you could use it:
    short rbis = {80};
    unsigned int q {42110};
    long long ants {3000000000};
  3. C++ provides no automatic safeguards to keep you from exceeding integer limits; you can use the climits header file to determine what the limits are.
  4. The constant 33L is type long, whereas the constant 33 is type int.
  5. The two statements are not really equivalent, although they have the same effect on some system. Most importantly, the first statement assigns the letter A to grade only on a system using the ASCII code, while the second statement also works for other codes. Second, 65 is a type int constant, whereas A is a type char constant.
  6. Here are four ways:
    char c = 88;
    cout << c << endl; // char type prints as character
    cout.put(char(88)); // put() prints char as character
    cout << char(88) << endl; // new-style type cast value to char
    cout << (char) 88 << endl; // old-style type cast value to char
  7. The answer depends on how large the two types are. If long is 4 bytes, there is no loss. Thats because the largest long value would be about 2 billion, which is 10 digits. Because double provides at least 13 significant figures, no rounding would be needed. The long long type, on the other hand, can reach 19 digits, which exceeds the 13 significant figures guaranteed for double.
  8. a. 8 * 9 + 2 is 72 + 2 is 74
    b. 6 * 3 / 4 is 18 / 4 is 4
    c. 3 / 4 * 6 is 0 * 6 is 0
    d. 6.0 * 3 / 4 is 18.0 / 4 is 4.5
    e. 15 % 4 is 3
  9. Either of the following would work for the first task:
    int pos = (int) x1 + (int) x2;
    int pos = int (x1) + int (x2);
    To add them as type double and then convert, you could do either of the following:
    int pos = (int) (x1 + x2);
    int pos = int (x1 + x2);
  10. a. int
    b. float
    c. char
    d. char32_t
    e. double

Programming Exercises

1

#include <iostream>
const int Factor{12};

int main()
{
    using namespace std;

    cout << "Input your height(inches): ";
    int height, feet, inches;
    cin >> height;
    feet = height / Factor;
    inches = height % Factor;
    cout << "Your height is equal to " << feet << " feet," << inches << " inches" << endl;

    return 0;
}

2

#include <iostream>

int main()
{
    using namespace std;

    const double Inches_per_foot = 12;
    const double Inches_per_meter{0.0254};
    const double Kilo_per_pound{2.2};

    cout << "Enter your height in feet and inches(example: 14 17): ";
    double feet;
    double inches;
    cin >> feet;
    cin >> inches;
    cout << "Enter your weight in pounds: ";
    double weight;
    cin >> weight;
    double i = (feet * Inches_per_foot + inches) * Inches_per_meter;
    cout << "Your BMI is: " << weight * Kilo_per_pound / (i * i) << endl;

    return 0;
}

3

#include <iostream>

int main()
{
    using namespace std;

    const double Mid{60.0};

    cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
    cout << "First, enter the degrees: ";
    int degrees;
    cin >> degrees;
    cout << "Next, enter the minutes of arc: ";
    int minutes;
    cin >> minutes;
    cout << "Finally, enter the seconds of arc: ";
    int seconds;
    cin >> seconds;
    cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = "
         << double (degrees) + minutes / Mid + seconds / (Mid * Mid) << endl;

    return 0;
}

4

#include <iostream>

int main()
{
    using namespace std;

    const int H_per_day{24};
    const int M_per_hour = {60};
    const int S_per_seconds = 60;

    cout << "Enter the number of seconds: ";
    long long seconds;
    cin >> seconds;
    long long d = seconds / (H_per_day * M_per_hour * S_per_seconds);
    long long h = seconds % (H_per_day * M_per_hour * S_per_seconds) / (M_per_hour * S_per_seconds);
    long long m = seconds % (H_per_day * M_per_hour * S_per_seconds) % (M_per_hour * S_per_seconds) / S_per_seconds;
    long long s = seconds % (H_per_day * M_per_hour * S_per_seconds) % (M_per_hour * S_per_seconds) % S_per_seconds;
    cout << seconds << " seconds = "
         << d << " days, "
         << h << " hours, "
         << m << " minutes, "
         << s << " seconds \n";

    return 0;
}

5

#include <iostream>

int main()
{
    using namespace std;

    cout << "Enter the world's population: ";
    long long p_of_world;
    cin >> p_of_world;
    cout << "Enter the population of the China: ";
    long long p_of_nation;
    cin >> p_of_nation;
    cout << "The population of the China is " << double (p_of_nation) / double (p_of_world) * 100.0
         << "% of the world population. \n";

    return 0;
}

6

#include <iostream>

int main()
{
    using namespace std;

    cout << "Enter you have driven in miles: ";
    int mile;
    cin >> mile;
    cout << "Enter you have costed of gasoline: ";
    int gas;
    cin >> gas;
    cout << double (mile) / double (gas) << " miles per gallon your car has gotten. \n";

    return 0;
}

7

#include <iostream>

int main()
{
    using namespace std;

    const double Miles_per_kilo{6.214e-1};
    const double Liters_per_gallon = 3.875;

    cout << "Enter European style (liters per 100 kilometers): ";
    double eu;
    cin >> eu;
    int temp = int ((1.0 / eu) * (Miles_per_kilo * 100.0) * Liters_per_gallon);
    cout << eu << " l/100 km = " << temp << " mpg \n";
    return 0;
}

c++-pimer-plus-6th-chapter03的更多相关文章

  1. The 6th tip of DB Query Analyzer

      The 6th tip of DB Query Analyzer MA Gen feng (Guangdong Unitoll Services incorporated, Guangzhou ...

  2. [转载]ECMA-262 6th Edition / Draft August 24, 2014 Draft ECMAScript Language Specification

    http://people.mozilla.org/~jorendorff/es6-draft.html#sec-23.4 Draft Report Errors and Issues at: htt ...

  3. chapter03

    import scala.collection.mutable.ArrayBuffer /** * Created by EX-CHENZECHAO001 on 2018-03-29. */class ...

  4. Chapter03 Java变量

    Chapter03 变量 目录 Chapter03 变量 3.1 为什么需要变量 3.1.1 一个程序就是一个世界 3.1.2 变量是程序的基本组成单位 3.1.3 简单原理图 3.2 变(变化)量( ...

  5. ​Si2151/41 6th Generation Silicon TV Tuner ICs

    ​ The Si2151/41 are the industry's most advanced silicon TV tuner ICs supporting all worldwide terre ...

  6. Codeforces Round #361 Jul.6th B题 ☺译

    最近迈克忙着考前复习,他希望通过出门浮躁来冷静一下.迈克所在的城市包含N个可以浮躁的地方,分别编号为1..N.通常迈克在家也很浮躁,所以说他家属于可以浮躁的地方并且编号为1.迈克从家出发,去一些可以浮 ...

  7. Codeforces Round #361 Jul.6th A题 ☺译

    A.迈克和手机 当迈克在沙滩上游泳的时候,他意外的把他的手机扔进了水里.不过你甭担心因为他立马买了个便宜些的代替品,这个代替品是老款九键键盘,这个键盘只有十个等大的数字按键,按以下方式排列: 1 2 ...

  8. October 6th 2016 Week 41st Thursday

    The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...

  9. September 6th 2016 Week 37th Tuesday

    I only wish to face the sea, with spring flowers blossoming. 我只愿面朝大海,春暖花开. That scenery is beautiful ...

  10. July 6th, Week 28th Wednesday, 2016

    Diligence is the mother of good fortune. 勤勉是好运之母. The mother of good fortune can be diligence, conti ...

随机推荐

  1. android studio设计模式和文本模式切换

  2. js归并排序

    js归并排序 function mergeSort (arr){ if (arr.length < 2){ //控制语句,结束递归 return arr; } var middle = Math ...

  3. P3810 【模板】三维偏序(陌上花开)(cdq分治)

    思路 看到这种偏序类的题目,而且不要求强制在线,可以立刻想到cdq分治 注意这题有一个问题,就是询问的是小于等于而不是小于,如果相等的话两个元素会相互贡献,而cdq的特点是右区间不能对左边有影响,所以 ...

  4. 物体检测算法 SSD 的训练和测试

    物体检测算法 SSD 的训练和测试 GitHub:https://github.com/stoneyang/caffe_ssd Paper: https://arxiv.org/abs/1512.02 ...

  5. FI CO 常用表

    FI CO 常用表     最近写FICO的报表写得有点多,许多Table记不住,用F1查找又有点费事,不如把表单写下来,以后用到,直接在这上面找得了. 1,账目表主数据  SKA1  SKB1  S ...

  6. python实现八皇后问题

    import random def judge(state, nextX): #判断是否和之前的皇后状态有冲突 nextY = len(state) for i in range(nextY): if ...

  7. C#Listview添加数据,选中最后一行,滚屏

    this.listView.Items.Add(lvi); this.listView.EnsureVisible(this.listView.Items.Count - 1); this.listV ...

  8. 前端单页面富应用(SPA)的实现

    一. 什么是单页面富应用? 单页面应用:Single Page Application 概念:Web应用即使不刷新也在不同的页面间切换,解决浏览器前进.后退等机制被破坏等问题.并且页面访问会被浏览器保 ...

  9. Codeforces 769D k-Интересные пары чисел

    题目链接:http://codeforces.com/contest/769/problem/D 搜索题 考虑这些数的值域较小,直接${O(2^{k})}$次方枚举每个数字二进制位上是否改变,剪枝一下 ...

  10. virtualbox中的虚拟机和windows共享文件夹

    http://www.jianshu.com/p/4e3c8b06cb06 为什么要共享文件夹? 在工作的过程当中会使用到不同的软件开发环境,php的,python的,nodejs的为了隔离这些应用环 ...