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

本文提供了七个C++编程练习,涵盖了基本的函数使用、用户输入处理及数值转换。练习涉及输出个人信息、单位转换、多次调用函数实现特定输出格式、年龄与月份的关系、摄氏度到华氏度转换、光年到天文单位转换以及时间显示等。这些题目旨在巩固C++基础知识和提高用户交互能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


总结

函数由函数头和函数体组成。函数头指出函数的返回值(如果有的话)的类型和参数类型。
函数先声明(或先定义),后使用。


提示:以下是本篇文章正文内容,下面案例可供参考

一、第一题

题目:编写一个C++程序,它显示您的姓名和地址

#include <iostream>
int main() {
	std::cout << "中盾" << std::endl;
	std::cout << "中国" << std::endl;
	return 0;
}

在这里插入图片描述

二、第二题

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

#include <iostream>
int main() {
	float longDistance;
	std::cout << "请输入一个以long为单位的距离: ";
	std::cin >> longDistance;
	std::cout << "转换为码为: "<<longDistance * 220 << " " << "码";
	return 0;
}

在这里插入图片描述
在这里插入图片描述

三、第三题

题目:编写一个C++程序,它使用3个用户定义的函数(包括maiin()),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行:另一个函数也被调用两次,并生成其余的输出。

#include <iostream>
void ThreeMice();
void SeeRun();
int main() {
	ThreeMice();
	ThreeMice();
	SeeRun();
	SeeRun();
	return 0;
}
void ThreeMice() {
	std::cout << "Three blind mice" << std::endl;
}
void SeeRun() {
	std::cout << "See how they run" << std::endl;
}

在这里插入图片描述

四、第四题

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

#include <iostream>
int main() {
 int ageInput;
 std::cout << "请输入年龄:";
 std::cin >> ageInput;
 while (ageInput < 0) {
  std::cout << "请正确输入这辈子的年龄:";
  std::cin >> ageInput;
 }
 std::cout << "该年龄包括" << ageInput*12 << "个月。";
 return 0;
}

五、第五题

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

#include <iostream>
int main() {
 float degreesInput;
 std::cout << "Please enter a Celsius value: ";
 std::cin >> degreesInput;
 std::cout <<degreesInput<< " degrees Celsius is "
  <<1.8*degreesInput + 32.0 <<" degrees Fahrenheit.";
 return 0;
}

在这里插入图片描述

六、第六题

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

1光年=63240天文单位

#include <iostream>
double LightUnits(double lightYears_);
int main() {
	double lightYears;
	std::cout << "Enter the number of light years: ";
	std::cin >> lightYears;
	std::cout << lightYears << " light years = " << LightUnits(lightYears) << " astronmical units.";
	return 0;
}
double LightUnits(double lightYears_) {
	return lightYears_ * 63240;
}

在这里插入图片描述

七、第七题

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

#include <iostream>
void ShowTime(int hours_, int minutes_);
int main() {
	int hours = 0;
	int minutes = 0;
	std::cout << "Enter the number of hours: ";
	std::cin >> hours;
	std::cout << "Enter the number of minutes : ";
	std::cin >> minutes;
	ShowTime(hours, minutes);
	return 0;
}
void ShowTime(int hours_, int minutes_) {
	std::cout << "Time: " << hours_ << ":" << minutes_;
}

在这里插入图片描述

谢谢观看,祝顺利。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值