C++ Primer Plus 第三章-处理数据 编程练习

3.7编程练习

第一题

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

#include"iostream"
using namespace std;
int main()    //39.37
{
    cout << "Your height(in):______\b\b\b\b\b\b";    //退格转义符 使光标前移
    int In;    cin >> In;
    const int Ft_per_In = 12;        // 1 ft = 12 in
    int in = In % 12;
    int ft = In / 12;
    cout << "feet: " << ft << endl << "inches: " << in << endl;
    return 0;
}

第二题

编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重.(使用3个变量来存储这些信息.)

该程序报告其BMI(Body Mass Index,体重指数).为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸 = 0.0254米).然后,将以磅为单位的体重转换为以千克为单位的体重(1千克 = 2.2磅)

最后,计算相应的BMI---体重(千克)除以身高(米)的平方.用符号常量表示各种转换因子.

#include"iostream"
using namespace std;
 
int main(void)
{
    cout << "Your height(feet and inches)\n"; 
    cout << "feet: ";                int feet;         cin >> feet;
    cout << "inches: ";                int inches;        cin >> inches;
    cout << "Your weight(pounds): ";float pounds;    cin >> pounds;
    const int Ft_per_In = 12;
    const float In_per_M = 0.0254;
    const float Kg_per_Pound = 2.2;
    inches = (feet * Ft_per_In) + inches;
     cout << "\nYour height is " << inches << " feet.\n";
     float meter = inches * In_per_M;        cout << "meter: " << meter << endl;
     float kg = pounds / Kg_per_Pound;    cout << "kg: " << kg << endl;
     float BMI = kg / (meter*meter);    
     cout << "Your BMI is " << BMI << ".\n";
    return 0;
}

第三题

编写一个程序,要求用户以度,分,秒的方式输入一个纬度,然后以度为单位显示该维度.

1度为60分,1分等于60秒,请以符号常量的方式表示这些值.

对于每个输入值,应使用一个独立的变量存储它,下面是该程序运行的情况:

Enter a latitude,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 = 31.8553 degrees

#include"iostream"
using namespace std;
int main(void){
    float Degrees;
    float degrees,minutes,seconds;
    const int Deg_per_Min = 60;
    const int Min_per_Sec = 60;
    cout << "Enter a latitude,minutes,and seconds: \n";
    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;
    minutes = minutes + seconds / Min_per_Sec;
    Degrees = degrees + minutes / Deg_per_Min;
    cout << degrees << " degrees, " << int (minutes) << " minutes, "
         << seconds << " seconds = " << Degrees << " degrees.";
    
    return 0;
}

第四题

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

Enter the number of seconds: 31600000

31600000 seconds = 365days, 17hour, 46minutes, 40seconds

#include"iostream"
using namespace std;
int main(){
    const int day_hour = 24;
    const int hour_min = 60;
    const int min_second = 60;
    long long S;    //输入的总秒数
    cout << "Enter the number of seconds: ";
    cin >> S;
    int day = S / min_second / hour_min / day_hour;
    int hour = S / min_second / hour_min % day_hour;
    int minute = S / min_second % min_second;
    int second = S % min_second;
    cout << S << " seconds = " << day << " days, " 
         << hour << " hours, " << minute << " minutes,"
         << second << " seconds" << endl;   
    
    return 0;
} 

第五题

编写一个程序,要求用户输入全球当前人口和美国当前的人口(或其他国家的人口).将这些信息存储在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(void){
long long World_p;//6898758899
long long US_p;//310783781
cout << "Enter the world 's population: ";cin >> World_p;
cout << "Enter the population of the CHN: ";cin >> US_p;
double per = (double)US_p / (double)World_p;
per = per*100;
cout << "The poplution of the CHN is " << per << "% of the World popluation" << endl;
return 0;
}

第六题

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

#include"iostream"
using namespace std;
int main(void){
    long long mile;
    int gal;
    cout << "Driving mileage: ";    cin >> mile;
    cout << "Amout of gasoline used: ";    cin >> gal;
    float g_m = (float)mile / (float)gal;
    cout << "Mileage per gallon: " << g_m << endl;;
    system("pause");
    // 一下是计算每100公里的耗油量(升) 
    long long K;
    int L;
    cout << "Driving kilometers: "; cin >> K;
    cout << "Amout of gasoline used(litre): ";    cin >> L;
    float K100_L = ((float)L/(float)K) * 100; 
    cout << "The fuel consumption per 100 kilometers is " << K100_L << " liter.\n";
    return 0;
}

第七题

编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量---每加仑多少公里.注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反.

100公里等于62.14英里,1加仑等于3.785升.因此,19mpg大约合12.41/100km,27mpg大约合8.71/100km.

#include"iostream"
using namespace std;
int main(void){
    cout << "Please enter kilometers: ";    float kilometers;    cin >> kilometers;
    cout << "Please enter fuel consumption(liter): ";    float liter;    cin >> liter;
    float liter_per_100kilometers = liter / (kilometers / 100);
    cout << endl << liter_per_100kilometers << " liter per 100 kilometers.\n";
    //转换 
    const float mile_per_100kilometers = 62.14;
    float mile;
    mile = (kilometers / 100) * mile_per_100kilometers;
    const float liter_per_gllon = 3.785;
    float gllon;
    gllon = liter / liter_per_gllon;
    float mpg = mile / gllon;
    cout << mpg << " miles per gllon"<< endl << endl; 
    cout << liter << "/" << kilometers << "km = " << mpg << "mpg\n";
    
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值