C++ Primer Plus 第六版 第二章课后编程练习答案

1.

#include<iostream>

void main() //void表示无return
{
	using namespace std;
	cout << "Tom" << endl;
	cout << "Chengdu" << endl;	
}

注:如果遇到VS 2017 运行C++程序时控制台输出窗口一闪而过的问题,那是运行完毕了,不过没来得及显示。对此,可以通过在网上搜教程改vs设置来解决(推荐),也可以在句尾加一条指令system("pause");

2.

#include<iostream>

void main(int Long, int Ma)
{
	using namespace std; 
	cout << "Please input distance in Long;";
	cin >> Long; //输入
	Ma = 220 * Long;
	cout << "Distance in Ma is " << Ma << endl;
	//system("pause");
}

3.

#include<iostream>
void f1()
{
	using namespace std;
	cout << "Three blind mice" << endl;
}
void f2()
{
	using namespace std;
	cout << "See how they run" << endl;
}
void main()
{
	f1();
	f1();
	f2();
	f2();
}

4.

#include<iostream>
void main(int age,int months)
{
	using namespace std;
	cout << "Please input your age:";
	cin >> age;
	months = age * 12;
	cout << "Your age contains "<<months<<" months.";
}

5.

#include<iostream>

float f(float);

void main(float temp0 ,float temp1)
{
	using namespace std;
	cout << "Please enter a Celsius value: ";
	cin >> temp0;
	temp1 = f(temp0);
	cout << temp0 << " degrees Celsius is " << temp1 << " degrees Fahrenheit." << endl;
}
float f(float a)
{
	return 1.8*a + 32.0;
}

6.

#include<iostream>

void main(double LightYear, double astronomical)
{
	using namespace std;
	cout << "Enter the number of light years: ";
	cin >> LightYear;
	astronomical = LightYear * 63240;
	cout << LightYear << " light years = " << astronomical << " astronomical units." << endl;
}

7.

#include<iostream>
void main(int hour,int minutes)
{
	using namespace std;
	cout << "Enter the number of hours: ";
	cin >> hour;
	cout << "Enter the number of minutes: ";
	cin >> minutes;
	cout << "Time:  " << hour << ":" << minutes;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值