C++primer plus之处理数据_编程练习

编程练习

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

int main()
{
	using namespace std;

	const double ShiftYz = 12;
	cout << "1 feet = " << ShiftYz << " inch" << endl;
	
	cout << "Enter you is the height:____\b\b\b";
	int height_in;//英寸身高
	cin >> height_in;
	double height_f = feets(height_in);//英尺身高
	cout << height_in << " inch = " << height_f << " feet" << endl;
	height_in = inchs(height_f);
	cout << height_f << " inch = " << height_in << " feet" << endl;
}

double feets(double fe)
{
	return fe / 12;
}
double inchs(double ins)
{
	return ins * 12;
}
  1. 编写程序,要求以几英尺几英寸的方式输入身高,并以磅为单位输入体重。(使用3个变量来存储这些信息)该程序报告其体重指数(BMI),该程序将以英寸的方式指出用户的身高(1英尺等于12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸等于0.025米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克等于2.2磅)。最后计算相应的BMI(BMI = 体重(千克)除以身高(米)的平方)。用符号常量表示各转换因子。
#include<iostream>
double weightKg(double);//体重千克
double heightMeter(double);//身高米
using namespace std;
int main()
{
	//cout.setf(ios_base::fixed, ios_base::floatfield);
	cout << "Enter is the height_f_in weight: " << endl;
	double height_f, height_in, weights;//英尺,英寸,体重
	cin >> height_f >> height_in >> weights;
	double height_fin = height_f + height_in * 12;//先将英寸转换为英尺,再相加获得英尺为单位的身高
	double heightMe = heightMeter(height_fin);
	double weightK = weightKg(weights);
	cout << "BMI:" << weightK / (heightMe * heightMe) << endl;
}
//英尺转换为米
double heightMeter(double douM)
{
	return douM * 0.0254;
}
//磅转换为千克(Kg)
double weightKg(double douK)
{
	return douK / 2.2;
}
  1. 编写程序,要求用户以度、分、秒的方式输入一个维度,然后以度为单位显示该维度。1度等于60分,1分等于60秒,用符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。输出应是下列情况:
Enter a latotude in degrees, minutes, and seconds:
First, enter the degress: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees + 51 minutes + 19 seconds = 37.8553 degrees
#include<iostream>

int main()
{
	using namespace std;
	//定义符号常量:度、分、秒
	const double LI_MI= 35.00;
	const double MIN = 23.00;
	const double SECS = 56.00;
	double degrees = LI_MI;
	double minutes = MIN;
	double seconds = SECS;
	cout << "Enter a latotude in degrees, minutes, and seconds:" << endl;
	cout << "First, enter the degress: ";
	cin >> degrees;
	cout << "Next, enter the minutes of arc: ";
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: ";
	cin >> seconds;
	double deg = degrees + minutes / 60 + seconds / 60 / 60;
	cout << degrees << " degrees + " << minutes << " minutes + " << seconds << " seconds = " << deg << " degrees ";
	return 0;
}
  1. 编译程序,要求客户以整数方式出入秒数(使用long或long long变量存储),然后以天,小时,分钟,秒数的方式显示这段时间。使用符号常量来表示每天有多少个小时,每小时有多少分钟及每分钟有多少秒。该程序输出应与下列相似:
#include<iostream>

int main()
{
	using namespace std;
	const long long HOUR = 24;//一天多少小时
	const long long MIN = 60;//一小时多少分钟
	const long long SEC = 60;//一分钟多少秒
	cout << "Enter the number od seconds: " << endl;
	long long seconds;
	cin >> seconds;
	long days = seconds * 0.998 / HOUR / MIN / SEC;
	long hours = seconds * 0.00199 / MIN / SEC;
	long secs = seconds * 0.000001;
	cout << seconds << " seconds = " 
		<< days << " days, " 
		<< hours << " hours, " 
		<< secs << " seconds" << endl;
	return 0;
}
  1. 编程程序,要求用户输入全球当前人口和其他一个国家当前人口。将这些信息存储在long long变量中,并让程序显示国家人口占全球人口的百分比。该程序输出与下列类似:
Enter the world's population: 6898758899
Enter the population of the india: 310783781
The population of the india is 4.504923% of world  population.
#include<iostream>
 int main()
 {
	 using namespace std;
	 cout.setf(ios_base::fixed, ios_base::floatfield);//保留小数点后6位
	 cout << "Enter the world's population: ";
	 long long popu;//全球人口
	 cin >> popu;
	 double pop = popu;
	 cout << "Enter the population of the india: ";
	 long long india;//印度人口
	 cin >> india;
	 double ind = india;
	 double percent = ind / pop * 100;
	 cout << "The population of the india is " << percent << "% of world  population." << endl;
 }
  1. 编写程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油为一加仑多少英里。
#include<iostream>
int main()
{
	using namespace std;
	cout << "Enter the mile: ";
	double mile;
	cin >> mile;
	cout << "Enter the gallon: ";
	double gallon;
	cin >> gallon;
	double gallonm = gallon / mile;
	cout << "The amount of fuel consumed per mile is: " << gallonm << endl;
	return 0;
}
  1. 编写程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量-----每加仑多少公里。100公里等于62.14英里,1加仑等于3.875升。
#include<iostream>
int main()
{
	using namespace std;
	//欧洲格式
	cout << "Enter in the mile: ";
	double mile;
	cin >> mile;
	cout << "Enter in the gallon: ";
	double gallon;
	cin >> gallon;
	double consume = mile / gallon;
	cout << "The amount of fuel consumed per mile is: " << consume << endl;
	//美国格式
	double kms = mile * 0.6214;//英里转换为公里
	double litre = gallon * 3.875;//加仑转换为升
	double consimeus = kms / litre;
	cout << "The amount of fuel consumed per km is:" << consimeus << endl;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值