每日学点c++:处理数据篇(3)——习题篇

第三章习题

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

# include <iostream>

int main()
{
    using namespace std;
    const int scale = 12;
    int height;
    
    cout << " Enter your height:____\b\b\b\b\b ";
    cin >> height;
    int foot = height / scale;
    int inches = height % scale;
    cout << "your foot of height is : " << foot << " feet;" 
    << " your inchs of height is : " << inches << " inches;";
    return 0;
}  

结果显示:

Enter your height: 50____
your foot of height is : 4 feet;your inchs of height is : 2 inches;请按任意键继续. . .

2.编写一个小程序,要求以几英寸几英尺的方式输入身高,并以磅·为单位输入其体重。(使用三个变量来存储这些信息。)该程序报告其BMI。计算公式:体重(千克)处于身高(米)的平方。(单位换算:1 feet = 12 inchs;
1 inchs = 0.254 m;1 Kg = 2.2 bang;)

程序清单:

# include <iostream>

int main()
{
    using namespace std;
    const int hs = 12;
    const float ws = 2.2;
    const float inchestom = 0.0254;
    float feet;
    float inches;
    float pands;

    cout << " Enter your feet:____\b\b\b\b\b ";
    cin >> feet;
    cout << " Enter your inches:____\b\b\b\b\b";
    cin >> inches;
    cout << " Enter your pands:____\b\b\b\b\b ";
    cin >> pands;
    int inche = feet * hs + inches;
    float height = inche * inchestom;
    float weight = pands * ws;
    float height2 = height * height;
    float BMI = weight / height2;
    cout << "your foot of BMI is :  " << BMI;
    return 0;
}

输出结果:

 Enter your feet:_22_
 Enter your inches:_2__
 Enter your pands:_ 22
your foot of BMI is :  1.06027请按任意键继续. . .

3.编写一个程序,要求用户以度、分、秒的方式输入一个维度,然后以度为单位显示该维度。1度为60分,1分为60秒,请以符号常量的方式表示这些值。对于每个输入值,应该使用一个独立的变量存储它。
程序清单:

# include <iostream>

int main()
{
    using namespace std;
    const int scale = 60;
    float degrees;
    float minutes;
    float seconds;

    cout << " Enter a latitude in degrees, minutes, and seconds: " << endl;
    cout << " First, enter the degrees: ";
    cin >> degrees;
    cout << " Next, enter the minutes: ";
    cin >> minutes;
    cout << " Finally, enter the seconds: ";
    cin >> seconds;
    float mtd = minutes / scale;
    float setm = seconds / scale / scale;
    float alltodegrees = degrees + mtd + setm;
    cout << degrees << " degrees," << minutes << " minutes," << seconds << " seconds = " << alltodegrees << " degrees";
    return 0;
}  

结果输出:

 Enter a latitude in degrees, minutes, and seconds:
 First, enter the degrees: 37
 Next, enter the minutes: 51
 Finally, enter the seconds: 19
37 degrees,51 minutes,19 seconds = 37.8553请按任意键继续. . .

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

程序清单:


# include <iostream>

int main()
{
    using namespace std;
    const int dh = 24;
    const int hm = 60;
    const int ms = 60;
    long seconds;
    
    cout << " Enter the number of seconds: ";
    cin >> seconds;
    int day = seconds / (60 * 60 * 24);
    int days = seconds % (60 * 60 * 24);
    int hours = days / (60 * 60);
    int hourss = days % (60 * 60);
    int minutes = hourss / 60;
    int secondss = hourss % 60;
    cout << seconds << " seconds = " << day << " days," << hours << " hours," << minutes << " minutes," << secondss << " seconds";
    return 0;
}

结果输出:

 Enter the number of seconds: 31600000
31600000 seconds = 365 days,17 hours,46 minutes,40 seconds请按任意键继续. . .

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

程序清单:

# include <iostream>

int main()
{
    using namespace std;
    long long ap; // 美国人口
    long long wp; // 世界人口
    
    cout << " Enter the world's population: ";
    cin >> wp;
    cout << " Enter the population of the US: ";
    cin >> ap;
    double scales = 100 * (float)ap / wp;
    cout << "The population of the US is " << scales << "% of the world population.";
    return 0;
} 

结果输出:

 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.请按任意键继续. . .

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

# include <iostream>

int main()
{
    using namespace std;
    float mile;
    float gallon;
    cout << "Enter the mile of driven: ";
    cin >> mile;
    cout << "Enter the Fuel consumption: ";
    cin >> gallon;
    float scale = mile / gallon;
    cout << " the Fuel consumption per kilomer: " << scale;
    
    return 0;
}  

7.编写一个程序,让程序要求用户以欧洲风格输出耗油量——即每100公里的耗油量(升)。然后将其转化为美国风格的耗油量——每加仑多少英里。注意:除了单位不同,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里=62.14英里,1加仑=3.875升。
程序清单:

# include <iostream>

int main()
{
    using namespace std;
    float liter;
    float Kilometer;
    cout << "Enter the liter: ";
    cin >> liter;
    cout << "Enter the kilometer: ";
    cin >> Kilometer;
    float mile = 62.14 * Kilometer / 100;
    float gallon = liter / 3.875;
    float Eurpean = liter / Kilometer;
    float America = mile / gallon;
    cout << " the America Fuel consumption : " << America \
    << "mpg approximately equal to " << Eurpean << "/100km";
    
    return 0;
}  

输出结果:

Enter the liter: 12.41
Enter the kilometer: 100
 the America Fuel consumption : 19mpg approximately equal to 12.41/100km请按任意键继续. . .
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页