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;
}

 4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下:

Enter you age:29

#include<iostream>
using namespace std;

int main()
{
    cout << "Please enter your age: ";
    int age;
    cin >> age;
    int contains = age * 12; 
    cout << "Your age is "<< age 
         << " You've been through "<< contains
         << " months."<< endl;
    
    return 0;
}

5.编写程序,其中的main()调用一个用户定义的函数(以摄氏温度为参考,并返回相应的华氏温度)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:

Please enter a Celsius value:20

20 degress Celsius is 68 degress Fahenheit.

转换公式:华氏温度 = 1.8X摄氏温度 + 32.

#include<iostream>
double fah(double);
using namespace std;

int main()
{
    cout << "Please enter a Celsius value:";
    double cel;
    cin >> cel;
    fah(cel);
    cout << cel << " degrees Celsius is "
         << fah(cel) << " degrees Fahrenheit."
         << endl;
    
    return 0;
}

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

6.编写程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:

Enter the number of light years:4.2

4.2 light years = 265608 astronmical units.

转换公式:1 光年 = 63240 天文单位

#include<iostream>
double astronomical(double);
using namespace std;

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

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

7.编写程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个 void函数,后者以下面格式显示这两个值:

Enter the number of hours: 9
Enter the number of minutes: 28
Time:9:28

#include<iostream>
void time(double,double);
using namespace std;

int main()
{
    cout << "Enter the number of hours: ";
    double hour;
    cin >> hour;
    cout << "Enter the number of minutes: ";
    double minute;
    cin >> minute;
    
    time(hour,minute);
    return 0;
}
void time(double h,double m)
{
    cout << "Time: "<< h <<":"<< m << endl;
}

作者:

喜欢围棋和编程。

 
发布于 分类 编程标签

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注