c++ Primer Plus(第六版)第三章习题,写代码之路

c++ Primer Plus(习题3.1)

/*输入身高,转换成英尺和英寸*/
#include<iostream> 
const int Feet_inch = 12;           //1 英尺=12英寸
int main() 
{
	using namespace std;
	cout << "Please enter your height in inches:___\b\b\b";			//  \b表示为退格字符
	int ht_inch;  cin >> ht_inch;
	int ht_feet = ht_inch/Feet_inch;					//除法转换数学问题
	int rm_inch = ht_inch%Feet_inch;					//取余  
	cout<<"Your height is "<<ht_feet<<" feet,and "<<rm_inch<<" inches\n";  
	return 0; 
}

c++ Primer Plus(习题3.2)

/*以几英寸,几英尺输入身高,以磅为单位输入体重,求出BMI*/
#include<iostream>
const int inch_per_feet = 12;
const double meter_per_inch = 0.0254;
const double pound_per_kilogram = 2.2; 
int main() 
{
	using namespace std;  
	cout << "First,enter your height of feet part:___\a\b\b\b";  
	int ht_feet; 
	cin>> ht_feet;  
	cout << "Second,enter your height of inch part:___\a\b\b\b";  
	int ht_inch; 
	cin >> ht_inch;  
	cout << "Now,please enter your weight in pound:___\a\b\b\b"; 
	double wt_pound; 
	cin >> wt_pound; 
	int inch; 
	inch = ht_feet*inch_per_feet + ht_inch;  
	double ht_meter;  
	ht_meter = inch*meter_per_inch;  
	double wt_kilogram;  
	wt_kilogram = wt_pound / pound_per_kilogram; 
	cout << endl;  
	cout << "Your pensonal body information as follows:" << endl;  
	cout << "Height: " << inch << " (inch)\n" 
		<< "Weight: " << ht_meter << " (meter)\n" 
		<< "体重: " << wt_kilogram << " (kilogram)\n";  
	double BMI;  BMI = wt_kilogram / (ht_meter*ht_meter);
	cout << "Your Body Mass Index(BMI) is  " << BMI << endl;  
	return 0;
}
c++ Primer Plus(习题3.3)

/*输入度,分,秒表示一个纬度,然后以度显示出来*/
#include<iostream>
const float D_f = 60;           //1度=60分
const float F_m= 60;           //1分=60秒
int main()
{
	using namespace std;
	cout << "Enter a latitude in degrees,minutes,and seconds:\n"
		<< "Frist,enter the degrees: \a";
	int degree;
	cin >> degree;
	cout << "enter the minutes of arc: \a";
	int min;
	cin >> min;
	cout << "enter the seconds ofarc: \a";
	int sec;
	cin >> sec;
	float degrees = degree + min / D_f + sec /(F_m*D_f);       //都转化成度,所以用除
	cout << degree << " degrees, "
		<< min << " minutes,"
		<< sec << " seconds " << "= " << degrees << " degrees.\n";
	return 0;


}
c++ Primer Plus(习题3.4)

/*用户输入秒数,等转化为,天,小时,分钟,秒表示*/
/*这么简答的问题也想了好久*/
#include<iostream> 
const int hours_per_day = 24; 
const int minutes_per_hour = 60; 
const int seconds_per_minute = 60; 
int main()
{
	using namespace std; 
	cout << "Enter the number of seconds:"; 
	long seconds; 
	cin >> seconds;  
	int Day, Hour, Minute, Second;  
	Day = seconds/seconds_per_minute/minutes_per_hour/hours_per_day;   //求天数
	Hour = seconds/seconds_per_minute/minutes_per_hour%hours_per_day;    //求小时数
	Minute = seconds/seconds_per_minute%minutes_per_hour;                  //求分钟数
	Second = seconds%seconds_per_minute;									//得秒数
	cout << seconds << "  seconds=  " << Day << " days," 
		<< Hour << " hours," << Minute << " minutes, "
		<<Second<<" seconds.\n";  
	return 0;
}
c++ Primer Plus(习题3.5)

/*用户输入中国人口数和全世界人口数*/
#include<iostream> 
int main()
{ 
using namespace std;
	cout << "Enter the world population:"; 
	long long world_population; 
	cin >> world_population;
	cout << "Enter the population of the China:";  
	long long China_population; 
	cin >> China_population;  
	double percentage;
	percentage = (double)China_population / world_population * 100;
	cout << "The population of the China is " 
		<< percentage << "% of the world population.\n";  
return 0;
}
c++ Primer Plus(习题3.6)
/*输入驱车历程和使用汽油数,求单位历程的耗油量*/
#include<iostream> 
int main()
{
	using namespace std;
	cout << "Computing by Us style:\n";
	cout << "Enter the miles of distance you have driven:";
	double m_distance;
	cin >> m_distance;
	cout << "Enter the gallons of gasoline you have used:";
	double m_gasoline;
	cin >> m_gasoline;
	cout << "Your car can run " << m_distance / m_gasoline << " miles per gallon\n";
	cout << "After that,omputing by European style:\n";
	cout << "Enter the distance in kilometers:";
	double k_distance;  cin >> k_distance;
	cout << "Enter the petrol in liters:";
	double k_gasoline;  cin >> k_gasoline;
	cout << "In European style:" << "your can used " << 100 * k_gasoline / k_distance << " liters of petrol per 100 kilometers\n";
	return 0;
}
c++ Primer Plus(习题3.7)

/*这题目真不新鲜,老套路2*/
#include<iostream> 
int main() 
{		using namespace std;  
		cout<<"Enter the automobile gasoline consumption figure in\n"  
			<<"European style(liters per 100 kilometers):";  
		double Euro_style;  
		cin>>Euro_style;  
		cout<<"Converts to U.S. style(miles per gallon):"<<endl; 
		cout<<Euro_style<<" L/100Km = "<<62.14*3.875/Euro_style<<" mpg\n";  
		return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值