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

4万+




被折叠的 条评论
为什么被折叠?



