c++primerplus(第六版)编程题——第3章(数据类型)

 

声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。

工程命名和文件命名可以命名成易识别的名字,当然你也可以随便自定义,只是作者本人偏好:

工程名:cprimerplus6th_chapter0_conten.pro
文件名:cprimerplus6th_exercise_0_0( )

主函数:

int main( int argc, char **argv)
{
    //cprimerplus6th_exercise3_7_1();
    //cprimerplus6th_exercise3_7_2();
    //cprimerplus6th_exercise_3_7_3();
    //cprimerplus6th_exercise3_7_4();
    //cprimerplus6th_exercise3_7_5();
    //cprimerplus6th_exercise3_7_6();
    cprimerplus6th_exercise3_7_7();
    return 0;    
}

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

#include <iostream>

using namespace std;

void cprimerplus6th_exercise3_7_1()
{
    const int inch_to_foot = 12; // 1 foot = 12 inchs 
    cout << "Please enter your height(inchs):___\b\b\b";
    int height_inch;
    cin >> height_inch;
    int height_foot;
    height_foot = height_inch / inch_to_foot;
    height_inch = height_inch % inch_to_foot;
    cout << "your height is " << height_foot << " foot " << height_inch << " inch";
    
}

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

void cprimerplus_exercise3_7_2()
{
    const int inch_to_foot = 12; // 1 foot = 12 inchs
    const double meter_to_inch = 0.0254; // 1 inch = 0.0254 meters
    const double pound_to_kg   = 2.2;// 1 kg = 2.2 pound

    int heigt_inch, height_foot;
    cout << "please enter your height(X foot X inchs):"<<endl;
    cout << "first enter your height in foot" << endl;
    cin >> height_foot;
    cout << "then enter your height in inch"<< endl;
    cin >> heigt_inch;
    cout << "your height is " << height_foot << " foot " << heigt_inch << "inches"<<endl;
    double height_meters;
    height_meters = (height_foot * inch_to_foot + heigt_inch) * meter_to_inch;

    double weight_pound;
    double weight_kg;
    cout << "Please enter your weight(pounds):" <<endl;
    cin >> weight_pound ;
    weight_kg = weight_pound * pound_to_kg;

    double body_mass_index;
    body_mass_index = weight_kg/(height_meters * height_meters);
    cout << \n << "your body mass index(BMI) is " << body_mass_index <<endl;
}

3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后

void cprimerplus_exercise_3_7_3()
{
    const double minute_to_degree = 60.0; // 1 degree = 60 minutes
    const double second_to_minute = 60.0; // 1 minute = 60 seconds

    int degrees, minutes, seconds;
    cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
    cout << "enter the degrees:";
    cin >> degrees;
    cout << "Next, enter the minutes of arc:" << endl;
    cin >> minutes;
    cout << "Finally, enter the seconds of arc:";
    cin >> seconds;
    double total_degrees;
    total_degrees = degrees + minutes/minute_to_degree +        seconds/(minute_to_degree * second_to_minute);

    cout << degrees << "degrees, " << minutes << "minutes, "<< seconds         << "seconds = " << total_degrees << "degrees"<<endl;
}

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

void cprimerplus_exercise3_7_4()
{
    const int second_to_minute = 60; // 1 minute = 60 seconds;
    const int minute_to_hour = 60;   // 1 hour = 60 minutes
    const int hour_to_day = 24;      // 1 day = 24 hours

    cout << "Enter the number of seconds:";
    long seconds;
    cin >> seconds;
    int days, hours, minutes, second;
    days = seconds / (second_to_minute * minute_to_hour * hour_to_day);
    hours = (seconds % (second_to_minute * minute_to_hour * hour_to_day))/(second_to_minute * minute_to_hour);
    minutes = ((seconds % (second_to_minute * minute_to_hour * hour_to_day))%(second_to_minute * minute_to_hour)) / second_to_minute;
    second = ((seconds % (second_to_minute * minute_to_hour * hour_to_day))%(second_to_minute * minute_to_hour)) % second_to_minute;

    cout << seconds <<" seconds = "<< days << " days, " << hours <<" hours, " << minutes << " minutes, "<< second << " seconds"<< endl;
}

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。

void cprimerplus_exercise3_7_5()
{
    cout << "Enter the world‘s population:";
    long long world_popu;
    cin >> world_popu;

    cout << "Enter the population of the US:";
    long long us_popu;
    cin >> us_popu;

    double percentage;
    percentage = (us_popu * 100) / world_popu;

    cout << "The population of the US is " << percentage << "% of the world population!" << endl; //输出
}

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

void cprimerplus_exercise3_7_6()
{
    cout << "Enter your driven miles(miles):";
    float miles;
    cin >> miles;
    cout << \n << "Enter your gallons of gasoline(gallons):";
    float gallons;
    cin >> gallons;
    cout << \n << " your car got" << miles/gallons << "miles per gallon\n";

}

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

void cprimerplus_exercise3_7_7()
{
    const double km100_to_miles = 62.14;
    const double liters_per_galllon = 3.875;
    double euro_rating, us_rating;
    cout << "Enter fuel consumption in liters per 100km:" <<endl;
    cin >>euro_rating;
    us_rating = (km100_to_miles * liters_per_galllon)/euro_rating;
    cout << euro_rating << "liters pper 100km is" << us_rating << "miles per gallon.\n";
}

c++primerplus(第六版)编程题——第3章(数据类型),布布扣,bubuko.com

c++primerplus(第六版)编程题——第3章(数据类型)

上一篇:为 API Gateway REST API 资源启用 CORS


下一篇:Spring学习笔记 2014-7-9