C++ Primer Plus(第6版)习题(第二章)

1..编写一个C++程序,它显示您的姓名和地址。

#include<iostream>
using namespace std;

int main()
{
    string name,address;
    cout << "Please enter your name and address:";
    cin >> name >> address;
    cout << "Your name is "<< name
         << " and your address is "<< address
         << endl;

    return 0; 
}

2.编写一个C++程序,要求用户输入一个以 long 为单位的距离,然后将它转换为码 。

  (1 long = 220 码)

#include<iostream>
using namespace std;

int main()
{
    cout << "Please enter a distance in long: ";
    double long;
    cin >> long;
    double yard = long * 220;
    cout << long << " long = "<< yard << " yard."<< endl;

    return 0;
}

 3.编写一个C++程序,使用3个用户定义的函数(包括main()),并生产下面的输出:

Three blind mice

Three blind mice

See how they run

#include<iostream>
void str_1();
void str_2(void);
using namespace std;

int main()
{
    str_1();
    str_1();
    str_2();
    str_2();

    return 0;
}

void str_1()
{
    cout << "Three blind mice."<< endl;
}
void str_2(void)
{
    cout << "See how they run."<< endl;
}

 

猜你喜欢

转载自www.cnblogs.com/Charons/p/11225007.html