C++ primer plus第6版第三章作业

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

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    int feet,inch,pound;
    const int inches_per_feet= 12;
    const double meters_per_inch= 0.0254;
    const double pounds_per_kg= 2.2;
    cout<<"Please enter your height(feet): ";
    cin>>feet;
    cout<<"Please enter your height(inch): ";
    cin>>inch;
    cout<<"Please enter your weight: ";
    cin>>pound;
    double BMI=(pound/pounds_per_kg)/pow(((feet*inches_per_feet+inch)*meters_per_inch),2);
    cout<<"Your BMI is: "<<setprecision(2)<<std::fixed<<BMI<<endl;
    return 0;
}

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

#include <iostream>
#include <iomanip>
#include <cmath>

int main()
{
    using namespace std;
    cout<<"Enter a latitude in degrees,minutes and seconds.\n";
    int degree,minute,second;
    cout<<"First,enter the degrees: ";
    cin>>degree;
    cout<<"Next,enter the minutes: ";
    cin>>minute;
    cout<<"Finally,enter the seconds: ";
    cin>>second;
    const double exchange= 60;
    double latitude= degree+(minute/exchange)+second/pow(exchange,2);
    cout<<degree<<" degrees,"<<minute<<" minutes,"<<second<<" seconds = "<<latitude<<" degrees"<<endl;
    return 0;
}
4.编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of seconds:31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

#include <iostream>
#include <cmath>

int main()
{
    using namespace std;
    cout<<"Enter the number of second: ";
    long long sec;
    cin>>sec;
    int day,hour,minute,second;
    const int exc1= 24;
    const int exc2= 60;
    second= sec%exc2;
    minute= ((sec-second)/exc2)%exc2;
    hour= ((((sec-second)/exc2)-minute)/exc2)%exc1;
    day= int(sec/(exc1*exc2*exc2));
    cout<<sec<<" seconds = "<<day<<" days, "<<hour<<" hours, "
       <<minute<<" minutes, "<<second<<" seconds"<<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>

int main()
{
    using namespace std;
    unsigned long long wp,usp;
    cout<<"Enter the world's population: ";
    cin>>wp;
    cout<<"Enter the population of the US: ";
    cin>>usp;
    long double ldwp,ldusp;
    ldwp=wp;
    ldusp=usp;
    double percent= double(ldusp/ldwp)*100;
    cout<<"The population of the US is "<<percent<<"% of the world population."<<endl;
    return 0;
}

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

#include <iostream>

int main()
{
    using namespace std;
    cout<<"Please enter the oil consumption in euro(L/km): ";
    double euoc;
    cin>>euoc;
    const double mile_km= 62.14;
    const double gallon_L= 3.875;
    int usoc= 1/(euoc/(mile_km*gallon_L));
    cout<<"The oil consumption in the US(miles/gallon) is "<<usoc<<endl;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值