C++ primer 课后练习题

第二章:开始学习C++

        一、复习题

        1. C++程序的模块叫 函数

        2. 用来包含头文件

        3. 编译指令 用来确定对象的命名空间

        4. cout << “Hello, world” << endl;

        5. int cheeses;

        6. cheeses = 32;

        7. cin >> cheeses;

        8. cout << “We have ” << cheeses << ” varieties of cheeses,”;

        9. 返回类型 函数名 函数参数

        10. 返回类型为void时

        11.可能未包含头文件;可能未使用编译指令;可能未声明对象的空间(std::)

        二、编程练习

        1.

#include <iostream>
using namespace std;

int main()
{

    cout << "David" << endl;
    cout << "FZU" << endl;

    return 0;
}

        2.

#include <iostream>
using namespace std;

int main()
{
    cout << "plz enter the long" << endl;

    int _long;

    cin >> _long;

    cout << "res = " << _long*220 << endl;

    return 0;
}

        3.

#include <iostream>
using namespace std;

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

void func2()
{
    cout << "See how they run" << endl;
    cout << "See how they run" << endl;
}

int main()
{
    func1();
    func1();
    func2();
    return 0;
}

        4.

#include <iostream>
using namespace std;

int main()
{
    cout << "enter your age: " << endl;

    int age;

    cin >> age;

    cout << age*12 << endl;

    return 0;
}

        5.

#include <iostream>
using namespace std;

int main()
{
    cout << "plz enter a Celsius value : " << endl;
    int t;
    cin >> t;
    cout << (1.8*t + 32) << endl;
    return 0;
}

        6.

#include <iostream>
using namespace std;

int main()
{
    cout << "plz enter light years : " << endl;
    double t;
    cin >> t;
    cout << t*63240 << endl;
    return 0;
}

        7.

#include <iostream>
using namespace std;

int main()
{
    cout << "plz enter hours : " << endl;
    int h;
    cin >> h;
    cout << "plz enter mins : " << endl;
    int mins;
    cin >> mins;
    cout << h << ":" << mins << endl;
}