[C++ Primer Plus] 第3章、处理数据(二)课后习题

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

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     const int change=100;//转换因子
 6     cout<<"请输入您的身高(单位为厘米):___\b\b\b";
 7     int height,meter,cm;
 8     cin>>height;
 9     meter=height/change;
10     cm=height%change;
11     cout<<"您的身高为"<<meter<<""<<cm<<"公分(厘米)"<<endl;
12     system("pause");
13 }

注意:使用下划线来指示输入位置主要利用转义字符\b

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

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     const int FootToInch=12;//1英尺等于12英寸
 6     const double  InchToMeter=0.0254;//1英寸=0.0254米
 7     const double  PoundToKg=1/2.2;
 8     int foot,inch,pound;
 9     double meter,kg,BMI;
10     cout<<"请以几英尺几英寸的方式输入您的身高。\n";
11     cout<<"请输入英尺的值:";
12     cin>>foot;
13     cout<<"请输入英寸的值:";
14     cin>>inch;
15     cout<<"请输入您的体重(磅):";
16     cin>>pound;
17     meter=foot*FootToInch*InchToMeter;
18     kg=pound*PoundToKg;
19     BMI=kg/meter/meter;
20     cout<<"您的身高为"<<meter<<"米,您的体重为"<<kg<<"千克,您的BMI为"<<BMI<<endl;
21     system("pause");
22 }

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

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     const int rate=60;
 6     double degree,minute,second,result;
 7     cout<<"Enter a latitude in degrees, minutes, and seconds:"<<endl;
 8     cout<<"First, enter the degrees: ";
 9     cin>>degree;
10     cout<<"Next, enter the minutes of arc:";
11     cin>>minute;
12     cout<<"Finally, enter the seconds of arc:";
13     cin>>second;
14     result=(second/change+minute)/change+degree;
15     cout<<degree<<" degrees,"<<minute<<" minutes,"<<second<<" seconds="<<result<<endl;
16     system("pause");
17 }

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

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     const int rate1=24;
 6     const int rate2=60;
 7     long secondTotal,minuteTotal;
 8     int day,hour,minute,second;
 9     cout<<"Enter the number of seconds:";
10     cin>>secondTotal;
11     minuteTotal=secondTotal/60;
12     second=secondTotal%60;
13     minute=minuteTotal%60;
14     hour=minuteTotal/60%24;
15     day=minuteTotal/60/24;
16     cout<<secondTotal<<" seconds="<<day<<" days,"<<hour<<" hours,"<<minute<<" minutes,"<<second<<" seconds"<<endl;
17     system("pause");
18 }

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在longlong变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
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.

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     long long People_world,People_US;
 6     double rate;
 7     cout<<"Enter the world's population:";
 8     cin>>People_world;
 9     cout<<"Enter the population of the US:";
10     cin>>People_US;
11     rate=People_US*1.0/People_world;
12     cout<<"The population of the US is "<<rate*100<<"% of the world population."<<endl;
13     system("pause");
14 }

注意:11行代码需要乘以1.0转成浮点数

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

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     double mile,gallon,km,liter;
 6     cout<<"请输入驱车里程(英里):";
 7     cin>>mile;
 8     cout<<"请输入耗油量(加仑):";
 9     cin>>gallon;
10     km=mile*1.609344;
11     liter=gallon*3.875;
12     cout<<"汽车耗油量为:一加仑里程"<<mile/gallon<<"英里,即每100公里的耗油量"<<liter/km*100<<"升。"<<endl;
13     system("pause");
14 }

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

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void main(){
 5     double US,Europe;
 6     cout<<"请输入耗油量(每100公里消耗的汽油量(升)):";
 7     cin>>Europe;
 8     US=62.14/(Europe/3.875);
 9     cout<<"汽车耗油量为:每加仑"<<US<<"英里"<<endl;
10     system("pause");
11 }

 

转载于:https://www.cnblogs.com/little-monkey/p/7397204.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值