C++Primer Plus (第六版)第三章作业笔记

C++Primer Plus (第六版)编程作业笔记(一)

第三章

1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。

#include <iostream>
using namespace std;

int main()
{
	double ft;
	const double trans = 12;
	int in;
	cout << "_";
	cin >> in;
	ft = in/trans;
	cout << "foot is:"<<ft<<endl;
	cout << "inch is:"<<in<<endl;
	system("pause");
	return 0;
} 

2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(BodyMassIndex,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI—体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。

#include <iostream>
using namespace std;
int main()
{
	double ft;
	const double trans = 12;
	int in;
	double pound;
	cout<< "ft:";
	cin >> ft;
	cout <<"in:";
	cin >> in;
	cout <<"pound:";
	cin >> pound;
	cout << "BMI = " <<(0.45*pound)/(((ft*trans+in)*0.0254)*((ft*trans+in)*0.0254));
	system("pause");
	return 0;
} 

3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。

#include <iostream>
using namespace std;

int main()
{
	double degree;
	double minute;
	int second;
	cout << "Enter a latitude in degrees, minutes, seconds:"<<endl;
	cout << "Degrees: ";
	cin >> degree;
	cout << "minutes: ";
	cin >> minute;
	cout << "seconds: ";
	cin >> second;
	cout << degree <<" degrees "<<minute<<" minutes " << second << " seconds = ";
	degree = degree+(minute+(second/60))/60;
	cout << degree <<endl;
	system("pause");
	return 0;
} 

4.编写一个程序,要求用户以整数方式输入秒数(使用long或longlong变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。

#include <iostream>
using namespace std;

int main()
{
	long seconds;
	int days, hours, minutes;
	const int trans_d_h = 24;
	const int trans_h_m = 60;
	const int trans_m_s = 60;
	cout << "enter the number of seconds: ";
	cin >> seconds;
	cout << seconds<< " seconds ="<<seconds/( trans_m_s* trans_h_m* trans_d_h)<<" days "<<seconds%( trans_m_s* trans_h_m* trans_d_h)/(trans_m_s * trans_h_m)<<" hours "<<seconds%( trans_m_s* trans_h_m* trans_d_h)%(trans_m_s * trans_h_m)/trans_h_m<<" minutes "<<seconds%( trans_m_s* trans_h_m* trans_d_h)%(trans_m_s * trans_h_m)%trans_h_m<< " seconds"<<endl;
	system("pause");
	return 0;
} 

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在longlong变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。

#include <iostream>
using namespace std;

int main()
{
	long long world_p;
	long long nation_p;
	cout << "enter the population of the world: ";
	cin >> world_p;
	cout << "enter the population of the nation: ";
	cin >> nation_p;
	double stem;
	stem = (double)nation_p / world_p*100;
	cout << "rate : "<<stem <<"% "<<endl;
	system("pause");
	return 0;
}

6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果—即每100公里的耗油量(升)。

#include <iostream>
using namespace std;

int main()
{
	double distance;
	double fuel;
	cout << "distance: ";
	cin >> distance;
	cout << "amount of fuel:";
	cin >> fuel;
	cout << distance/fuel <<" miles per galon"<<endl;
	system ("pause");
	return 0;
} 

最后那个题懒求得做了,差不多的

猜你喜欢

转载自blog.csdn.net/baidu_29452653/article/details/82818854
今日推荐