C++ Primer plus学习笔记 第三章编程lianxi

/*第一题*/
#include<iostream>
using namespace std;

int main()
{
    cout<<"Please enter your height ____(英寸)";
  	//注:1英寸=2.5400厘米,1英尺=12英寸。
  	//英尺 feet 英寸inches
    int heightInches;
    cin>>heightInches;

    int heightFeet;
    heightFeet = heightInches / InchesConverseToFeet;
    heightInches = heightInches % InchesConverseToFeet;

    cout<<"Your height is "<<heightFeet<<" feet "<<heightInches<<" inches"<<endl;
    return 0;
}



/*
第二题:计算BMI(Body Mass Index,体重指数)
*/
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    const int FeetConverseToInches = 12;
    const double InchesConverseToMeters = 0.0254;
    const double poundConverseToKilogram = 2.2;

    cout<<"Please enter your height ____feet____inches(separated by a blank space)";
    int heightFeet,heightInches,pound;
    cin>>heightFeet>>heightInches;
    int height = heightFeet * FeetConverseToInches + heightInches;
    cout<<"Your height is "<<height<<" inches"<<endl;
    double heightMeters = height * InchesConverseToMeters;
    cout<<"Your height is "<<heightMeters<<" Meters"<<endl;

    cout<<"Please enter your weight____pound ";
    cin>>pound;
    double weight = pound / poundConverseToKilogram;
    cout<<"Your weight is "<<weight<<" kg"<<endl;

    double BMI = weight / pow(heightMeters,2);
    cout<<"Your BMI is "<<BMI<<endl;
    return 0;
}


/*第三题*/
#include<iostream>
using namespace std;

int main()
{
    const double degreeToMinute = 60;
    const double MinuteToSecond = 60;
    cout<<"Enter a latitude in degrees,minutes, and seconds:"<<endl;
    int degrees,minutes,seconds;
    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 converseDegrees = degrees + minutes / degreeToMinute + seconds / (degreeToMinute * MinuteToSecond);
    cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds = "<<converseDegrees<<" degrees"<<endl;
    return 0;
}

/*第四题*/
#include<iostream>
using namespace std;

int main()
{
    const int hourOfDay = 24;
    const int minuteOfHour = 60;
    const int secondOfMinute = 60;
    long long Init_seconds;
    cout<<"Enter the number of seconds: ";
    cin>>Init_seconds;
    long days,hours,minutes,seconds;
    seconds = Init_seconds % secondOfMinute;//40=31600000%60
    minutes = Init_seconds / secondOfMinute;//526666=3160000/60
    minutes = minutes % minuteOfHour;//46=526666%60
    hours = (Init_seconds / secondOfMinute) / minuteOfHour;//8777=526666/60
    hours = hours % hourOfDay;//17=8777%24
    days = ((Init_seconds / secondOfMinute) / minuteOfHour) / hourOfDay;//365=8777/24
    cout<<Init_seconds<<" seconds = "<<days<<" days, "<<hours<<" hours, "<<minutes<<" minutes, "<<seconds<<" seconds."<<endl;
    return 0;
}

/*第五题*/
#include<iostream>
using namespace std;

int main()
{
    long long popOfWorld,popOfUS;
    cout<<"Enter the world's population: ";
    cin>>popOfWorld;
    cout<<"Enter the population of the US: ";
    cin>>popOfUS;
    double percentWorldUS = (double)popOfUS / popOfWorld;
    cout<<"The population of the US is "<<percentWorldUS*100<<"% of the world population";
    return 0;
}

/*第六题*/
#include<iostream>
using namespace std;

int main()
{
    cout<<"请选择单位 1.英里和加仑;2.公里和升"<<endl;
    int n;
    cin>>n;
    if(n==1)
    {
        cout<<"请输入驱车里程(英里)和使用汽油量(加仑):_________(用空格隔开)";
        double miles,gallon,LPK;//LPK是油耗的缩写
        cin>>miles>>gallon;
        LPK = miles / gallon;
        cout<<"油耗为 "<<LPK<<"(英里/加仑)"<<endl;
    }
    else if(n==2)
    {
        cout<<"请输入驱车里程(公里)和使用汽油量(升):_________(用空格隔开)";
        double km,litres,LPK;//LPK是油耗的缩写
        cin>>km>>litres;
     LPK = litres / (km/100);
     cout<<"油耗为 "<<LPK<<"(升/100公里)"<<endl;
    }
    return 0;
}

/*第七题*/
#include<iostream>
using namespace std;

int main()
{
    const double kmToMiles = 62.14;
    const double gallonToL = 3.875;
    cout<<"请输入汽车的耗油量(升/100公里)";
    double LPKOfE,LPKOfUS;
    cin>>LPKOfE;
    LPKOfUS = (gallonToL*kmToMiles)/LPKOfE;
    cout<<LPKOfE<<"L/100km等于"<<LPKOfUS<<"英里/加仑"<<endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值