C++ Primer Plus第三章课后习题总结

发布于:2025-02-10 ⋅ 阅读:(51) ⋅ 点赞:(0)

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

#include <iostream>
using namespace std;

int main(){
	//1 feet = 12 inches; 
	int height;
	cout << "Please enter your height:(inch)________\b\b\b\b\b\b";
	cin >> height;
	int feet = height / 12;
	int inch = height % 12;
	cout << endl;
	cout << "Your height is " << feet << " feet, " << inch << " inches.";
	return 0;
}

3.7.2 要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重(使用三个变量来存储这些信息)该程序报告其BMI。为了计BMI,该程序以英寸的方式指出用户的身高,并将以英寸为单位的身高转换为以米为单位的身高,将以磅为单位的体重转换为以千克为单位。用符号常量表示各种转换因子

#include <iostream>
using namespace std;
int main(){
	const double inchPerFeet = 12;
	const double meterPerInch = 0.0254;//1英寸=0.0254米
	const double kgPerPound = 0.454;//1磅=0.454千克
	double feet, inches, pounds;
	
	cout << "Please enter your height:" << endl;
	cout << "feet: ________\b\b\b\b\b\b";
	cin >> feet;
	cout << "inches: ________\b\b\b\b\b\b";
	cin >> inches;
	
	cout << "Please enter your weight: " << endl;
	cout << "pounds: ________\b\b\b\b\b\b";
	cin >> pounds; 
	
	double totalHeight = feet * inchPerFeet + inches;
	double height = totalHeight * meterPerInch;
	double weight = pounds * kgPerPound;
	double bmi = weight / (height * height);
	
	cout << "Your BMI is: " << bmi << endl;
	return 0;
}

 之前将这部分声明写成了feet, inches, pounds的类型声明成了int,但是输入inches时是double类型的,这直接导致无法获取pounds键盘键入 

后查询到原因如下:
在C++中,当使用 cin 输入流来获取用户输入时,如果输入的数据类型与声明的变量类型不匹配,就会导致输入失败。在你的情况下,你声明了 int 类型的变量 feet、inches 和 pounds,但是如果你尝试输入一个 double 类型的值(例如带有小数的数字),那么 cin 将无法将该值赋给 int 类型的变量,因为它期望一个整数。当 cin 遇到类型不匹配的情况时,它会进入失败状态(fail state),并且会丢弃后续的输入直到遇到下一个换行符。这也会阻止后续的 cin 操作,除非你清除输入流的错误标志。

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

#include <iostream>
using namespace std;

int main(){
	const double minPerDegree = 60;
	const double secondPerMin = 60;
	int degrees, minutes, seconds;
	cout << "Enter a latitude in degrees, minutes, and seconds:" << endl; 
	cout << "First, enter the degrees: ";
	cin >> degrees;
	cout << "Next, enter the minutes of arc: ";
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: ";
	cin >> seconds;
	
	double degreesTotal = degrees + minutes / minPerDegree + seconds / (minPerDegree * secondPerMin);
	cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds ";
	cout << "= " << degreesTotal << " degrees" << endl;
	return 0;
}

在执行代码的时候,发现最后的结果输出还是整数(其实是因为最后在输出的时候变量名写成了degrees,这能对吗??),为了让输出格式变成浮点数,查询到可以使用cout的成员函数setf来规范输出,下面语句的含义是,以一般形式输出浮点数,小数点后精确到4位(四舍五入)

cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(4);

有关cout成员函数setf的具体使用可以参考下面:

C++ 中的 cout.setf() 函数-CSDN博客

3.7.4 编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。
该程序的输出应与下面类似:
Enter the number of seconds:3160000
3160000 seconds = 365 days,17 hours,46 minutes,40 seconds

#include <iostream>
using namespace std;
int main(){

	const int secondPerMin = 60;
	const int minPerHour = 60;
	const int hourPerDay = 24; 
	long second;
	cout << "Enter the number of seconds: ";
	cin >> second;
	int day = second / secondPerMin / minPerHour / hourPerDay;
	int hour = (second % (secondPerMin * minPerHour * hourPerDay)) / secondPerMin / minPerHour;
	int minute = (second % (secondPerMin * minPerHour)) / secondPerMin;
	int seconds = second % secondPerMin;
	
	cout << second << " seconds = " << day << " days, ";
	cout << hour << " hours, " << minute << " minutes, " << seconds << " seconds.";
	return 0;
}

3.7.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(){
	//这道题目的重点是如何将longlong类型的整数在计算时输出位double类型的浮点数 
	long long poplWord, poplUs;
	cout << "Enter the world's population: ";
	cin >> poplWord;
	cout << "Enter the population of the US: ";
	cin >> poplUs;
	double proportion = double(poplUs) / double(poplWord);
	cout << "The population of the US is " << proportion * 100 << "% of the world population." << endl;
	
	return 0;
}

 注意格式转换!!

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

#include <iostream>
using namespace std;
int main(){
	int mile, gas;
	cout << "Enter your driving distance:(miles) ";
	cin >> mile;
	cout << "Enter the quantity of gasoline used:(gallons) ";
	cin >> gas;
	double milesPerGallon = double(mile) / double(gas);
	cout << "The fuel consumption of a car is one gallon of mileage is ";
	cout << milesPerGallon << " per gallon." << endl;
	return 0;
}

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

#include <iostream>
using namespace std;

int main(){	
	double gasPerKil;
	cout << "The fuel consumption of a car is one gallon of mileage is:(Fuel consumption per 100 kilometers) ";
	cout << endl;
	cin >> gasPerKil;
	double milesPerGallon = 62.14 / (gasPerKil * 3.875);
	cout << "The fuel consumption of a car is one gallon of mileage is:(Miles per gallon) ";
	cout << endl;
	cout << milesPerGallon << " per gallon." << endl;
	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到