C++ Primer Plus 第六版 第三章编程练习答案参考(仅仅用作学习交流,若有错误,请联系作者,谢谢)

第一题:

#include <iostream>
using namespace std;
int main()
{
    const double inchtofoot=0.833333;
	cout<<"Please enter your height___\b\b\b";
	int inch;
	cin>>inch;
	cout<<"Your height is "<<int(inch*inchtofoot)<<" feet "
	<<inch*inchtofoot-int(inch*inchtofoot)<<" inches"<<endl;
	return 0;
}

第二题:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{ 
    double inch;
	double foot; 
	double pound;
	const int foottoinch=12;
	const double inchtometer=0.0254;
	const double poundtokg=2.2;
	cout<<"Please enter your height and weight:";
	cin>>inch;
	cin>>foot;
	cin>>pound;
	double meter;
	double kg;
	meter=(inch+foot*foottoinch)*inchtometer;
	cout<<meter<<endl;
	kg=pound/poundtokg;
	cout<<"Your BMI is "<<kg/(meter*meter)<<endl;
	return 0; 
}

第三题:

#include <iostream>
using namespace std;
int main()
{
	double degree,minute,second;
	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;
	cout<<degree<<" degrees, "<<minute<<" minutes, "<<second<<" seconds = ";
	degree=degree+minute/60+second/3600;
	cout<<degree<<" degrees"<<endl;
	return 0;
}

第四题:

#include <iostream>
using namespace std;
int main()
{
	long second;
	cout<<"Enter the number of seconds:";
	cin>>second;
	int day,hour,minute;
	day=second/(60*60*24);
	hour=second/(60*60)-day*24;
	minute=second/60-hour*60-day*24*60; 
	cout<<second<<" seconds = "<<day<<" days "<<hour<<" hours "<<minute<<" minutes ";
	second=second%60;
	cout<<second<<" seconds";
	return 0;
}

第五题:

#include <iostream>
using namespace std;
int main()
{
	long long population_w;
	long long population_us;
	cout<<"Enter the world's population:";
	cin>>population_w;
	cout<<"Enter thr population of US:";
	cin>>population_us;
	cout<<"The population of the US is "<<population_us*(double)100/population_w<<"% of the world population"<<endl;
	return 0;
}

第六题:

#include <iostream>
using namespace std;
int main()
{   
    double mile;
    double gallon;
    double km;
    double litre;
	cout<<"Enter the mile:";
	cin>>mile;
	cout<<"Enter the gallon:";
	cin>>gallon;
	cout<<"1 gallon "<<mile/gallon<<" mile"<<endl;
	cout<<"Enter the km:";
	cin>>km;
	cout<<"Enter the litre:";
	cin>>litre;
	cout<<"1 litre "<<km/litre<<" km"<<endl;
	return 0;
}

第七题:(待定)

#include <iostream>
using namespace std;
int main()
{
	double kpl;
	double mpg;
	cout<<"Enter the kpl:";
	cin>>kpl;
	mpg=62.14/(kpl/3.875);
	cout<<"the mpg is "<<mpg;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值