码迷,mamicode.com
首页 > 编程语言 > 详细

《C++ primer plus 英文版 第六版》Chapter 3

时间:2018-04-29 15:27:18      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:enter   mos   speed   eterm   provides   main   tom   lock   section   

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++ primer plus 英文版 第六版》Chapter 3

标签:enter   mos   speed   eterm   provides   main   tom   lock   section   

原文地址:https://www.cnblogs.com/narisu/p/8970967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!