C++ Primer Plus 第二章练习题答案

本文档展示了《C++ Primer Plus》第2章第7节的练习题解答,涵盖了基本的C++输入输出操作,如显示个人信息、单位转换、函数调用等。通过示例代码,演示了如何获取用户输入并进行计算,以及如何通过函数实现特定的功能,如将摄氏度转换为华氏度、光年转换为天文单位等。
摘要由CSDN通过智能技术生成

C++ primer plus 课后练习题

以下代码均在Linux下ubuntu 18.04测试过

2.7.1

/*编写一个C++程序,它显示您的姓名和地址*/

#include <iostream>

//using namespace std;

int main()
{
    //cout << "我的姓名是: 凯" << " 地址是: 江苏 南京" << endl;  
    std::cout << "我的姓名是: 凯" << " 地址是: 江苏 南京" << std::endl;
    return 0;
}

//运行结果为:我的姓名是: 凯 地址是: 江苏 南京

2.7.2

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

#include <iostream>

using namespace std;

int main()
{
    long num = 0;
    cout << "Please input your number:";
    cin >> num;
    cout << "转换为码为:" << num * 220 << "码" << endl;
}

// 运行结果如下
// Please input your number:2.5
// 转换为码为:440码

2.7.3

/*
编写一个C++程序,它使用3个用户定义的函数(包括main),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
*/
#include <iostream>

using namespace std;

void func1();
void func2();
void func3();

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

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

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

void func3()
{
    cout << "See how they run" << endl;
}
//运行结果如下:
// Three blind mice
// Three blind mice
// See how they run
// See how they run

2.7.4

/*编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age:29
*/

#include <iostream>

using namespace std;

int main()
{
    int age = 0;
    cout << "请输入您的年龄:";
    cin >> age;
    cout << "该年龄包含" << age * 12 << "个月" << endl;
    
    return 0;
}

// 运行结果如下:
// 请输入您的年龄:23
// 该年龄包含276个月

2.7.5

/*
编写一个程序,其中的main()调用一个用户定义的函数(以摄氏度为参数,并返回相应的华氏度)。
该程序按照下面的格式要求用户输入摄氏度温度值,并显示结果:
Please enter a Celsius value: 20
21 degrees Celsius is 68 degrees Fahrenheit
*/
#include <iostream>

using namespace std;

float func(int num)
{
    return num * 1.8 + 32;
}

int main()
{
    int num = 0;
    cout << "Please enter a Celsius value: ";
    cin >> num;
    float ret = func(num);
    cout << num << " degrees Celsius is " 
    << ret << " degrees Fahrenheit" << endl;
    
    return 0;
}

// 运行结果如下:
// Please enter a Celsius value: 21
// 21 degrees Celsius is 69.8 degrees Fahrenheit

2.7.6

/*
编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。
该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years:4.2
4.2 light years = 265608 astronomical units
*/

#include <iostream>

using namespace std;

float func(float years)
{
    return years * 63240;
}

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

// 运行结果如下:
// Enter the number of light years:4.2
// 4.2 light years = 265608 astronomical units

2.7.7

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

using namespace std;

void func(int hours, int minutes)
{
    cout << "Time: " << hours << ":" << minutes << endl; 
}

int main()
{
    int hours, minutes;
    cout << "Enter the number of hours:";
    cin >> hours;
    cout << "Enter the number of minutes:";
    cin >> minutes;
    func(hours, minutes);
    return 0;
}

// 运行结果如下:
// Enter the number of hours:9
// Enter the number of minutes:28
// Time: 9:28

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿凯kk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值