C++ Primer Plus第6版—第2章课后习题答案

//第1题
#include<iostream>

int main()
{
	using namespace std;
	cout << "Enter your name:wahaha" << endl;
	cout << "Enter your address:beijing" << endl;
	return 0;
}
//第2题
#include<iostream>

int main()
{
	using namespace std;
	cout << "输入一个以long为单位的距离:" << endl;
	int m,n;
	cin >> n;
	m = n * 220;
	cout << "转换结果为:" << m  << endl;
	return 0;
}
//第3题
#include<iostream>
void function1();
void function2();
using namespace std;

int main()
{
	function1();
	function1();
	function2();
	function2();
	return 0;
}
void function1()
{
	cout << "Three blind mice" << endl;
	return;
}
void function2()
{
	cout << "See how they run" << endl;
	return;
}
//第4题
#include<iostream>

int main()
{
	using namespace std;
	cout << "Enter your age:";
	int age;
	cin >> age;
	cout << "包含:" << age * 12 <<"个月"<< endl;
	return 0;
}
//第5题
#include<iostream>
using namespace std;
void function(double n);

int main()
{
	cout << "Please enter a Celsius value:";
	double n;
	cin >> n;
	function(n);
	return 0;
}
void function(double n)
{
	double m;
	m = 1.8*n + 32.0;
	cout << n << " degree Celsius is " << m << " degree Fahrenheit." << endl;
	return;
}
//如果写成以下方式,就会出现:无法从“void”转换为“double”的错误
#include<iostream>
using namespace std;
void function(double n);

int main()
{
	cout << "Please enter a Celsius value:";
	double m,n;
	cin >> n;
	m=function(n);
	cout << n << "degree Celsius is" << m << "degree Fahrenheit." << endl;
	return 0;
}
void function(double n)
{
	double a;
	a = 1.8*n + 32.0;
	return;
}
//第6题
#include<iostream>
using namespace std;
void function(double n);

int main()
{
	cout << "Enter the number of light years: ";
	double n;
	cin >> n;
	function(n);
	return 0;
}
void function(double n)
{
	double m;
	m = 63240*n;
	cout << n << " light years = " << m <<" astronmical units." << endl;
	return;
}
//第7题
#include<iostream>
using namespace std;
void function(int m,int n);

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

}
void function(int m,int n)
{
	cout << "time: "<< m << ":" <<n<< endl;
	return;
}

猜你喜欢

转载自blog.csdn.net/qq_43130414/article/details/86355069