• C++ Primer Plus第二章编程练习答案


    答案仅供参考,实际运行效果取决于运行平台和运行软件

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. cout << "My name is sakuraaa0908 C++ Primer Plus." << endl;
    6. cout << "My address is in library." << endl;
    7. return 0;
    8. }

    2.编写一个C程序它要求用户输入一个以 long 为单位的距离,然后将它转换为码(- ng等于220码)

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. double long_distance;
    6. cout << "Enter a distance for long unit: ";
    7. cin >> long_distance;
    8. cout << long_distance << " long distance ";
    9. cout << "is equal to ";
    10. long_distance *= 220.0;
    11. cout << long_distance << " yard distance." << endl;
    12. return 0;
    13. }

    3.编写1个C++程序它使用3个用户定义的函数(括mai()),并生成下面的输出Three blind miceThree blind miceSee-how they runSeehow they run
    其中一个函数要调用两次,该函数生成前两行:另一-个函数也被调用两次,并生成其余的输出。

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

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

    如下所示:Enter your age: 29

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int age_total_months;
    6. cout << "Enter your age: ";
    7. cin >> age_total_months;
    8. cout << "Your age includes " << age_total_months * 12 << " months." << endl;
    9. return 0;
    10. }

    5.编写一个程序,其中的 main)调用一个用户定义的函数-以摄氏温度为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
    Please enter a Celsius value!20
    20 degrees Celsius is 68-degrees Fahrenheit
    下面是转换公式:华氏温度=1.8X摄氏温度+32.0

    1. #include
    2. using namespace std;
    3. double temperature(double temp);
    4. int main()
    5. {
    6. double celsius;
    7. cout << "Please enter a Celsius value: ";
    8. cin >> celsius;
    9. cout << celsius << " degrees Celsius is ";
    10. cout << temperature(celsius);
    11. cout << " degrees Fahrenheit." << endl;
    12. return 0;
    13. }
    14. double temperature(double temp)
    15. {
    16. return 1.8 * temp + 32.0;
    17. }

    6.编写一个程序其 main)调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
    Enter the number of light years: 4.2
    4.2 1ight years = 265608 astronomical units .
    天文单位是从地球到太阳的平均距离(约 150000000 公里或93000000 英里光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的大约离地球 4.2光年)。

    请使用 double 类型(参见程序清单2.4),转换公式为:
    1光年-63240 天文单位

    1. #include
    2. using namespace std;
    3. double transform(double temp);
    4. int main()
    5. {
    6. double light_years;
    7. cout << "Enter the number of light years: ";
    8. cin >> light_years;
    9. cout << light_years << " light years = ";
    10. cout << transform(light_years);
    11. cout << " astronomical units." << endl;
    12. return 0;
    13. }
    14. double transform(double temp)
    15. {
    16. return temp * 63240.0;
    17. }

    7,编写一个程序,要求用户输入小时数和分钟数。在 main()函数中,将这两个值传递给一个 void雨数,后者以下面这样的格式显示这两个值:
    Enter the number of hours: 9
    Enter the number of minutes: 28
    Time:9:28

    1. #include
    2. using namespace std;
    3. void show_time(int hour, int minute);
    4. int main()
    5. {
    6. int hour, minute;
    7. cout << "Enter the number of hours: ";
    8. cin >> hour;
    9. cout << "Enter the number of minutes: ";
    10. cin >> minute;
    11. show_time(hour, minute);
    12. return 0;
    13. }
    14. void show_time(int hour, int minute)
    15. {
    16. cout << "Time: " << hour << ":" << minute << endl;
    17. }
  • 相关阅读:
    多路输出调光无频闪36V48V60V恒流50v磁吸灯pwm调光ic SL8700
    zabbix mysql监控项
    【大模型系列】指令微调
    Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value;解决办法
    使用 CSS 的仿 GitHub 登录页面
    Mybatis工作流程及原理详解
    opencv交互式调整视觉算法参数(一)-图像阈值参数
    Java读取寄存器数据的方法
    Nanoprobes金脂质偶联物的相关应用
    Unity UI不被3D物体遮挡
  • 原文地址:https://blog.csdn.net/sakura0908/article/details/132696612