"C++ primer plus English version sixth edition" Chapter 2

"C++ primer plus English version 6th edition" speaks loudly. It is recommended to buy this book, this is the upper and lower volumes. Moreover, the publication time is the latest in 2015. When I bought it, I found that the errata on the Internet had basically been corrected, and there were very few mistakes that did not affect the understanding. Good! And I found that the choice of words and sentences is very simple, and the reading is also smooth.
Electronic version (PDF):
C++ Primer Plus Sixth Edition.pdf 密码:910h
C++ Primer Plus Sixth Edition Chinese version of 密码:ihsq
Chpter Review 's answer reference book Appendix J
Programming Exercises mainly refer to the following posts:
C++-primer-plus (6th edition) Chinese version of programming exercises answers (complete version).pdf 密码:20rn
C++-Primer-Plus (sixth edition) programming exercises solutions (incomplete version).doc 密码:hxzs
charlesdong The code in the Programming Exercises
section below has been typed in and can be run. Environment: Xcode under macOS

Chapter Review

  1. They are called functions.
  2. It causes the contents of the iostream file to be substituted for this directive before final compilation.
  3. It makes definitions made in the std namespace available to a program.
  4. cout << "Hello, world" << endl;orcout << "Hello, world\n";
  5. int cheeses;
  6. cheeses = 32;
  7. cin >> cheeses;
  8. cout << "We have " << cheeses << " varieties of cheese\n";
  9. The function froop() expects to be called with one argument, which will be type double, and that the function will return a type int value. For instance, it could be used as follows:
    int gval = froop(3.14159);
    The function rattle() has no return value and expects an int argument. For instance, it could be used as follows:
    rattle(37);
    The function prune() returns an int and expects to be used without an argument. For instance, it could be used as follows:
    int residue = prune();
  10. You don't have to use return in a function when the function has the return type void. However, you can use it if you don't give a return value:
    return;
  11. The likely cause it that the function loses a statement using namespace std; to declare a directive of output.
    place using namespace std; above the main() function;
    place using std::cout; in the main() function;
    type as following: using std::cout << "Please enter your PIN: " << std::endl;

Programming Exercises

1

#include <iostream>

int main()
{
    using namespace std;
    
    cout << "Name: NaRiSu;\n"
         << "Address: BeiJing;" << endl;
         
    return 0;
}

2

#include <iostream>

int main()
{
    using namespace std;
    
    int furlong;
    cin >> furlong;
    cout << furlong << " furlong(s) = "
         << 220 * furlong << " yard(s)" << endl;
    
    return 0;
}

3

#include <iostream>
void f1();
void f2();

using namespace std;

int main()
{   
    f1();
    f1();
    f2();
    f2();
    
    return 0;
}

void f1()
{
    cout << "Three blind mice" << endl;
}

void f2()
{
    cout << "See how they run\n"; // 也可以用f1()函数中的endl形式。
    
    return; // void f2()表明无返回值,故可以这样写;也可以如f1()不写return语句。
}

4

#include <iostream>

int main()
{
    using namespace std;
    
    cout << "Enter your age: ";
    int age;
    cin >> age;
    cout << "Your age in months is " << 12 * age << "." << endl;
    
    return 0;
}

5

#include <iostream>
double fahrenheit(double);

int main()
{
    using namespace std;
    
    cout << "Please enter a Celsius value: ";
    int celsius;
    cin >> celsius;
    cout << celsius << " degrees Celsius is "
         << fahrenheit(celsius) << " degrees Fahrenheit." << endl;
    
    return 0;
}

double fahrenheit(double n)
{
    return 1.8 * n + 32.0;
}

6

#include <iostream>
double astronomical(double);

int main()
{
    using namespace std;
    
    cout << "Enter the number of light years: ";
    double light;
    cin >> light;
    cout << light << " light years = "
         << astronomical(light) << " astronomical units." << endl;
    
    return 0;
}

double astronomical(double n)
{
    return 63240 * n;
}

7

#include <iostream>
void f(int, int);

int main()
{
    using namespace std;
    
    cout << "Enter the number of hours: ";
    int hour;
    cin >> hour;
    cout << "Enter the number of minutes: ";
    int minute;
    cin >> minute;
    f(hour, minute);
    
    return 0;
}

void f(int h, int m)
{
    using namespace std;
    
    cout << "Time: " << h << " : " << m << endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027974&siteId=291194637