C++ Primer Plus 第3章-数据处理

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

#include <iostream>
using namespace std;

const int c_factors = 12;

int main()
{
    int height;
    cout << "Input your height(inch):______\b\b\b\b";
    cin >> height;
    cout << "Your height is " << height / c_factors << " feet" << " and " << height % c_factors << " inches";
}

 

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

#include <iostream>
using namespace std;

const int ft_to_in_factors = 12;
const double in_to_m_factors = 0.0254;
const double kg_to_pd_factors = 2.2;

int main() {
    int in, ft;
    int pound;
    double height,weight;
    double BMI;
    cout << "Input your feet and inches: ";
    cin >> ft >> in;
    cout << "Input your weight:";
    cin >> pound;
    in = ft * ft_to_in_factors + in;
    height = in * in_to_m_factors;
    weight = pound / kg_to_pd_factors;
    BMI = weight / (height * height);
    cout << "Your BMI is " << BMI<<endl;

}

 

 

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;

int main() {
    double degree, minute, second;
    double deg;
    cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
    cout << "First, enter the degrees : ";
    cin >> degree;
    cout << "Next, enter the minutes of arc :";
    cin >> minute;
    cout << "Finally, enter the seconds of arc : ";
    cin >> second;
    deg = degree + (minute / 60) + (second / 3600);
    cout << degree << " degrees, " << minute << " minutes, "<<second << " seconds = " << deg << " degrees"<<endl;
}

 

4.编写一个程序,要求用户以整数方式输入秒数(使用long 或long long 变量存储),然后以天、小
时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟
有多少秒。该程序的输出应与下面类似:
Enter the number of seconds: 31600000
31600000 seconds= 365 days, 17 hours, 46 minutes, 40 seconds

#include <iostream>
using namespace std;

int main() {
    long input_second;
    int day,hour,minute,second;
    int temp;
    cout << "Enter the number of seconds: ";
    cin >> input_second;
    day = input_second / 86400;
    temp = input_second % 86400;
    hour = temp / 3600;
    temp = temp % 3600;
    minute = temp / 60;
    second = temp % 60;
    cout << input_second << "seconds= " << day << " days, " << hour << " hours, " << minute << " minutes, " << second << " seconds" << endl;
}

 

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 w_population, u_population;
    cout << "Enter the world's population: ";
    cin >> w_population;
    cout << "Enter the population of the US :";
    cin >> u_population;
    double percentage;
    percentage = 100 * (double)u_population / (double)w_population;
    cout<<"The population of the US is "<< percentage <<"% of the world population.";
}

 

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

#include <iostream>
using namespace std;

int main() {
    double mile, gallon;
    double km, L;
    double k1, k2;
    cout << "Input miles and gallons:";
    cin >> mile >> gallon;
    k1 = mile/gallon;
    cout << "Fuel consumption per gallon " << k1 << " mile."<<endl;
    cout << "Input km and L:";
    cin >> km >> L;
    k2 = 100*L/km;
    cout << "The fuel consumption per 100 kilometers is " << k2 << " L." << endl;
}

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

#include <iostream>
using namespace std;

int main() {
    float gallon,mpg;
    cout << "Enter the amount of gasoline consumed per 100 kilometers:";
    cin >> gallon;
    mpg = 62.4 / (gallon / 3.785);
    cout << gallon << "/100km = " << mpg << "mpg"<<endl;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值