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

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

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. string name,address;
  6. cout << "Please enter your name and address:";
  7. cin >> name >> address;
  8. cout << "Your name is "<< name
  9. << " and your address is "<< address
  10. << endl;
  11. return 0;
#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 码)

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "Please enter a distance in long: ";
  6. double long;
  7. cin >> long;
  8. double yard = long * 220;
  9. cout << long << " long = "<< yard << " yard."<< endl;
  10. return 0;
  11. }
#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

其中一个函数要调用两次,生产前两行;另一个函数也被调用两次,生产其余输出。

  1. #include<iostream>
  2. void str_1();
  3. void str_2(void);
  4. using namespace std;
  5. int main()
  6. {
  7. str_1();
  8. str_1();
  9. str_2();
  10. str_2();
  11. return 0;
  12. }
  13. void str_1()
  14. {
  15. cout << "Three blind mice."<< endl;
  16. }
  17. void str_2(void)
  18. {
  19. cout << "See how they run."<< endl;
  20. }
#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

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "Please enter your age: ";
  6. int age;
  7. cin >> age;
  8. int contains = age * 12;
  9. cout << "Your age is "<< age
  10. << " You've been through "<< contains
  11. << " months."<< endl;
  12. return 0;
  13. }
#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.

  1. #include<iostream>
  2. double fah(double);
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "Please enter a Celsius value:";
  7. double cel;
  8. cin >> cel;
  9. fah(cel);
  10. cout << cel << " degrees Celsius is "
  11. << fah(cel) << " degrees Fahrenheit."
  12. << endl;
  13. return 0;
  14. }
  15. double fah(double n)
  16. {
  17. double fah = n * 1.8 + 32.0;
  18. return fah;
  19. }
#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 天文单位

  1. #include<iostream>
  2. double astronomical(double);
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "Enter the number of light years:";
  7. double light_year;
  8. cin >> light_year;
  9. astronomical(light_year);
  10. cout << light_year << " light years = "
  11. << astronomical(light_year)
  12. << " astronomical units."
  13. << endl;
  14. return 0;
  15. }
  16. double astronomical(double n)
  17. {
  18. double ast = n * 63240;
  19. return ast;
  20. }
#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

  1. #include<iostream>
  2. void time(double,double);
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "Enter the number of hours: ";
  7. double hour;
  8. cin >> hour;
  9. cout << "Enter the number of minutes: ";
  10. double minute;
  11. cin >> minute;
  12. time(hour,minute);
  13. return 0;
  14. }
  15. void time(double h,double m)
  16. {
  17. cout << "Time: "<< h <<":"<< m << endl;
  18. }
#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;
}

作者:

喜欢围棋和编程。

 
发布于 分类 编程标签

发表回复

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