编程练习--3

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

#include "iostream"
using namespace std;
const int feet_to_inche=12;

int main()
{
    int height;
    cout<<"Please enter your height:________\b\b\b\b\b\b\b\b ";
    cin>>height;
    cout<<"you height is "<<height/12<<" feets and "<<height%12<<" inches";
    return 0;
}
2.编写一个小程序,要求以几英尺几英寸的方式输入身高,并以镑为单位输入其体重(使用三个变量来存储这些信息),该程序报告其BMI(Body Mass Index,体重指数),为了计算BMI,改程序以英寸的方式指出用户身高,然后将转换为米为单位,然后以磅为单位的体重转换为千克为单位的体重,最后计算相应的BML,体重(千克)/身高的平方;
 
 
#include "iostream"
using namespace std;
const int inche_to_meter=0.0254;
const int meet_to_inche=12;
const float pound_to_kg=0.45;
double height_tran_unit(double a,double b);
double weight(double c);
double BML(double e,double f)

int main()
{
    double height_meet,height_inche,height_meter,weight_pound,weight_kg;
    cout<<"Please enter your height meets: ";
    cin>>height_meet;
    cout<<" and inches: ";
    cin>>height_inche;
    cout<<" inches ";
    cout<<"Please enter your weight: ";
    cin>>weight_pound;
    cout<<" pounds";
    height_meter=height_tran_unit(height_meet,height_inche);
    weight_kg=weight(weight_pound);
    cout<<"your BML is "<<BML(height_meter,weight_kg);
    return 0;
}

double height_tran_unit(double a,double b)
{
    return (a*12+b)*0.0254;
}

double weight(double c)
{
    return 0.45*c;
}

double BML(double e,double f)
{
    return f/(e*e);
}


 
 

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"
using namespace std;
const int deg_tran_min=60;
const int min_tran_sec=60;

double trans(double a,double b,double c)
{
    double d=c+(b/min_tran_sec)+a/(deg_tran_min*min_tran_sec);
    return d;
}

int main()
{
    double degrees,minutes,seconds,deg;
    cout<<"Enter a latitude in degrees,minutes,and 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;
    deg=trans(seconds,minutes,degrees);
    cout<<degrees<<" degrees, "<<minutes<<" minutes,"<<seconds<<" seconds = "<<deg<<" degrees.";
    return 0;
}

4.编写一个程序,要求用户以整数方式输入秒数(使用long或着long long变量储存)然后以天、小时、分钟和秒的方式显示这段时间。使用常量符号来表示每天有多少小时、每小时有多少分钟,以及每分钟有多少秒。该程序以下面的方式输出:

Enter the number of the seconds:31600000

31600000 seconds=365 days,17 hours,46 minutes,40 seconds;

扫描二维码关注公众号,回复: 1049616 查看本文章
#include "iostream"
using namespace std;
const int hours_of_days=24;
const int min_of_hours=60;
const int sec_of_min=60;

int main()
{
    long long sec;
    int days,hours,minutes,seconds;
    cout<<"Enter the number of seconds: ";
    cin>>sec;
    days=(int)sec/(sec_of_min*min_of_hours*hours_of_days);
    hours=(int)(sec-days*hours_of_days*min_of_hours*sec_of_min)/(sec_of_min*min_of_hours);
    minutes=sec%(sec_of_min*min_of_hours)/sec_of_min;
    seconds=sec%(sec_of_min);
    cout<<sec<<" seconds = "<<days<<" days "<<hours<<" hours "<<minutes<<" minutes "<<seconds<<" seconds ";
    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"
using namespace std;

int main()
{
    long long popu_of_world,popu_of_us;
    double prec;
    cout<<"Enter the world's populations: ";
    cin>>popu_of_world;
    cout<<"Enter the population of the us: ";
    cin>>popu_of_us;
    prec=(double)popu_of_us/popu_of_world*100;
    cout<<"The population of the US is "<<prec<<" % of the world population.";
    cin.get();
    cin.get();
    return 0;
}

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

#include "iostream"
using namespace std;

int main()
{
    int How_Km,How_Li;
    double Km_per_Li;
    double km_100_consu_li;
    cout<<"Please enter the km:";
    cin>>How_Km;
    cout<<"Please enter the consume oil liter : ";
    cin>>How_Li;
    Km_per_Li=(double)How_Km/How_Li;
    cout<<"The distance traveled by a liter of fuel is "<<Km_per_Li<<endl;
    km_100_consu_li=100/Km_per_Li;
    cout<<"100 KM consume oil is "<<km_100_consu_li;
    return 0;
    cin.get();
    cin.get();
}

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

#include "iostream"
using namespace std;
double Eur_tran_US_style(double a);

int main()
{
    double hud_KM_csu_oil;
    double US_style;
    cout<<"Plese enter 100KM consume oil: ";
    cin>>hud_KM_csu_oil;
    US_style=Eur_tran_US_style(hud_KM_csu_oil);
    cout<<"US style is:"<<US_style;
    cin.get();
    cin.get();
    return 0;
    
}

double Eur_tran_US_style(double a)
{
    return 62.14/(a/3.875);
}

猜你喜欢

转载自blog.csdn.net/lily559/article/details/80228508