c++ primer plus 第三章课后习题

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

自己写的

#include<iostream>
using namespace std;

int main() {
    
    
	const int ft_per_in = 12;

	int in;
	cout << "Enter your height in inches:";
	cin >> in;
	
	int foot = in / ft_per_in;
	int link = in % ft_per_in;

	cout << in << " inches are " << foot << " foot, " << inch << " link(s).\n";
	system("pause");
	return 0;

}

网上的参考

#include<iostream>
using namespace std;

int main()
{
    
    
	const int INCH_PER_FEET = 12;
	int height;

	cout << "Please enter your height in inch: ___\b\b\b";
	cin >> height;
	cout << "Your height is " << height / INCH_PER_FEET << " feet and ";
	cout << height % INCH_PER_FEET << " inches." << endl;

	return 0;
}

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

自己写的

#include<iostream>
using namespace std;

int main() {
    
    
	const int ft_per_in = 12;
	cout << "Enter your height in XXfeet XXinch(es):" << endl;
	double ft;
	double in;
	cout << "feet:";
	cin >> ft;

	cout << "inch(es)";
	cin >> in;

	cout << "Your height is:" << ft << " foot(s)," << in << " inch(es)" << endl;

	double height = (ft * ft_per_in + in) * 0.0254;
	cout << "Your height is:" << height << "m" << endl;

	const double kg_per_ig = 2.2;
	cout << "Enter your weight in pounds:";
	double pounds;
	cin >> pounds;
	cout << "Your weight is:" << pounds <<" pounds"<< endl;

	double weight = pounds / kg_per_ig;
	cout << "Your weight is:"<< weight << "kg" << endl;

	double BMI = weight / (height * height);
	cout << "BMI = " << BMI << endl;
	system("pause");
	return 0;
}

网上的参考1

#include <iostream>

const int FOOT_TO_INCH = 12;
const float INCH_TO_METER = 0.0254;
const double KILLOGRAM_TO_POUND = 2.2;

int main(void)
{
    
    
	using namespace std;

	int height_foot, height_inch;
	double weight_pound, height, weight, bmi;

	cout << "Please enter your height foot: ";
	cin >> height_foot;
	cout << "Please enter your height inch:";
	cin >> height_inch;
	cout << "Please enter your weight in pounds:";
	cin >> weight_pound;

	height = (height_foot * FOOT_TO_INCH + height_inch) * INCH_TO_METER;
	weight = weight_pound / KILLOGRAM_TO_POUND;

	bmi = weight / (height * height);
	cout << "Your bmi is " << bmi << endl;


	return 0;
}

在这里插入图片描述

网上的参考2

#include <iostream>
using namespace std;

int main()
{
    
    
	const int INCH_PER_FEET = 12;         //1英尺为12英寸;
	const double POUND_PER_KG = 2.2;      //1千克为2.2磅;
	const double METER_PER_INCH = 0.0254; //1英寸为0.0254米;
	double height_feet, height_inch, weight, meter;

	cout << "Please enter your height in feet and inch: ";
	cin >> height_feet >> height_inch;
	cout << "Please enter your weight in pound: ";
	cin >> weight;

	cout << "Your height is " << height_feet * INCH_PER_FEET + height_inch << " inches." << endl;
	meter = (height_feet * INCH_PER_FEET + height_inch) * METER_PER_INCH;
	cout << "Your BMI is " << (weight / POUND_PER_KG) / (meter * meter) << "." << endl;

	return 0;
}

3.编写一个程序,要求用户以度分秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分为60秒,请以符号常量的方式表示这些值。对于每个输入量,应使用一个独立的变量存储它。下面是该程序运行时的情况:
Enter a latitude in degree ,minutes,and seconds;
First,enter the degree:37
Next,enter the minutes of arc:51
Finally,enter the seconds of arc:19
37 degree,51 minutes,19 seconds = 37.8553 degrees

自己写的

#include <iostream>
const int DEGREE_TO_MINUTE = 60;
const int DEGREE_TO_SECOND = 3600;

int main(void)
{
    
    
	using namespace std;

	int degree, minute, second;
	double degree_style;
	cout << "Enter a latitude in degrees,minutes,and seconds" << endl;
	cout << "First,enter the degree: ";
	cin >> degree;

	cout << "Next,enter the minutes of arc:";
	cin >> minute;
	
	cout << "Finally,enter the seconds of arc:";
	cin >> second;

	degree_style = degree + (float)minute / DEGREE_TO_MINUTE + (double)second / DEGREE_TO_SECOND;
	
	cout << degree << "degrees," << minute << " minutes, " << second << " seconds =" << degree_style << " degrees" << endl;
	return 0;
}

在这里插入图片描述
网上的参考

#include <iostream>
using namespace std;

int main()
{
    
    
	const double MINUTE_PER_DEGREE = 60.0;
	const double SECOND_PER_MINUTE = 60.0;
	double degree, minute, second, total;

	cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
	cout << "First, enter the degrees: ";
	cin >> degree;
	cout << "Next, enter the minutes of arc: ";
	cin >> minute;
	cout << "Finally, enter the seconds of arc: ";
	cin >> second;

	total = degree + (minute / MINUTE_PER_DEGREE) + (second / (MINUTE_PER_DEGREE * SECOND_PER_MINUTE));
	cout << degree << " degrees, ";
	cout << minute << " minutes, ";
	cout << second << " seconds  = ";
	cout << total << " degrees" << endl;

	return 0;
}

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long 变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
Enter the world’s population :6898758899
Enter the population of the US:310783781
The population of the us is 4.50492% of the world population

自己写的

#include <iostream>
using namespace std;
int main() {
    
    
	float World_Population, Amercian_Population, Percentage;
	cout << "Enter the world's population:";
	cin >> World_Population;

	cout << "Enter the population of the U.S.:";
	cin >> Amercian_Population;

	Percentage = (Amercian_Population / World_Population);
	cout << Percentage << endl;

	cout << "The population of the us is " << Percentage * 100 
		<< "% of the world population." << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

网上的参考

#include <iostream>
using namespace std;

int main()
{
    
    
	long long world_population;
	long long china_population;

	cout << "Enter the world's population: ";
	cin >> world_population;
	cout << "Enter the population of the China: ";
	cin >> china_population;

	cout << "The population of the China is ";
	cout << double(china_population) / world_population * 100LL;
	cout << "% of the world population." << endl;

	return 0;
}

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

自己写的

#include <iostream>
using namespace std;
int main() {
    
    
	long long yingli, jialun;
	cout << "Enter the yinli:";
	cin >> yingli;

	cout << "Enter the jialun:";
	cin >> jialun;

	double meijialunlicheng =(double)yingli / jialun;
	cout << "The haoyouliang is " << meijialunlicheng*100 << endl;

	system("pause");
	return 0;
}

网上的参考

#include <iostream>
using namespace std;

int main()
{
    
    
**加粗样式**	double kilometer, liter;

	cout << "Please enter your driving distance(km): ";
	cin >> kilometer;
	cout << "Please enter your gas consumption(liter): ";
	cin >> liter;

	cout << "You consume " << liter / kilometer * 100;
	cout << " liter gas for driving per 100 km." << endl;

	return 0;
}

7.编写一个程序,要求用户按欧洲分割输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量–每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.785升。因此,19mpg大约合12.4l/100km,27mpg大约合8.71/100km。

自己写的

#include <iostream>
using namespace std;

int main()
{
    
    
	double kilometer, liter, mile, gallon;
	const double kilometer_per_mile = 0.6214;
	const double gallon_per_liter = 3.785;

	cout << "每100公里消耗的汽油量(升):" << endl;
	cin >> liter;
	gallon = liter / gallon_per_liter;
	mile = 100 * kilometer_per_mile;
	cout << "每加仑可以跑" << mile / gallon << "英里" << endl;


	system("pause");
	return 0;
}

在这里插入图片描述

网上的参考

#include <iostream>
using namespace std;

int main()
{
    
    
	const double MILE_PER_KM = 0.6214;     //1公里是0.6214英里;
	const double LITER_PER_GALLON = 3.875; //1加仑是3.875升;
	double kilometer, liter;

	cout << "Please enter your driving distance(km): ";
	cin >> kilometer;
	cout << "Please enter your gas consumption(liter): ";
	cin >> liter;

	cout << "You consume " << liter / kilometer * 100;
	cout << " liter gas for driving per 100 km (European style)." << endl;
	cout << "You drive " << kilometer * MILE_PER_KM / liter * LITER_PER_GALLON;
	cout << " miles for per gallon (American style)." << endl;

	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Yang_Wanli/article/details/119727668