C++第三章习题

在这里插入图片描述

  1. 这退格好使欸
#include <iostream>

int main()
{
    using namespace std;
    cout << "Enter your height in inches (enter an integer):___\b\b\b";//英寸
    int inches;
    const int inch_per_feet = 12;//1英尺=12英寸
    cin >> inches;
    cout << inches << " inches is " << inches / inch_per_feet << " feet and " << inches % inch_per_feet << " inches.\n";
    return 0;
}
Enter your height in inches (enter an integer):98_
98 inches is 8 feet and 2 inches.
#include <iostream>
int main()
{
    using namespace std;
    const int inch_per_feet = 12;
    const float meter_per_inch = 0.0254;
    const float pound_per_kg = 2.2;
    cout << "Enter your height in feet and inches, first enter the number of feet:\n";
    int height_feet, height_inch, height_inches;
    float weight_pound, weight_kg, height_meter;
    cin >> height_feet;
    cout << "Then the number of inches:\n";
    cin >> height_inch;
    height_inches = height_feet * inch_per_feet + height_inch;
    height_meter = height_inches * meter_per_inch;

    cout << "Your height is " << height_inches << " inches.\n";
    cout << "Now enter your weight in pounds:\n";
    cin >> weight_pound;
    weight_kg = weight_pound / pound_per_kg;
    cout << "Your BMI (Body Mass Index) is " << weight_kg / (height_meter * height_meter) << endl;
    return 0;
}
Enter your height in feet and inches, first enter the number of feet:
5
Then the number of inches:
2
Your height is 62 inches.
Now enter your weight in pounds:
127
Your BMI (Body Mass Index) is 23.2772
#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter a latitude in degrees, minutes, and seconds:\n";
    float degrees, minutes, seconds;
    const int min_per_deg = 60;
    const int sec_per_min = 60;
    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;
    float deg;
    deg = degrees + (seconds / sec_per_min + minutes) / min_per_deg;
    cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << deg <<
    " degress.\n";
    return 0;
}
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 degress.

在这里插入图片描述

#include <iostream>
int main()
{
    using namespace std;
    long long secs;
    int day, hour, minute, second, day_, hour_;
    const int hours_per_day = 24;
    const int minutes_per_hour = 60;
    const int seconds_per_minute = 60;
    cout << "Enter the number of seconds:";
    cin >> secs;
    day = secs / (hours_per_day * minutes_per_hour * seconds_per_minute);
    day_ = secs % (hours_per_day * minutes_per_hour * seconds_per_minute);
    hour = day_ / (minutes_per_hour * seconds_per_minute);
    hour_ = day_ % (minutes_per_hour * seconds_per_minute);
    minute = hour_ / seconds_per_minute;
    second = hour_ % seconds_per_minute;
    cout << secs << " seconds = " << day << " days, " << hour
         << " hours, " << minute << " minutes, " << second << " seconds\n";
    return 0;
}
Enter the number of seconds:31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

5 错误示范(类型转换错误,还没写乘以100)

#include <iostream>
int main()
{
    using namespace std;
    long long global, american;
    cout << "Enter the world's population:";
    cin >> global;
    cout << "Enter the population of the US:";
    cin >> american;
    cout << "The population of the US is " << float(american / global) << "% of the world population.\n";
    return 0;
}
Enter the world's population:6898758899
Enter the population of the US:310783781
The population of the US is 0% of the world population.

正确版本,只改return前面最后一句

cout << "The population of the US is " << float(american) / float(global) * 100 << "% of the world population.\n";
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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值