C++ Primer第三章课后编程题

本文介绍了一个包含六个代码示例的程序,涵盖了单位转换、物理计算、地理坐标转换、时间单位转换、燃油效率计算和距离单位换算等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



1、
代码:
#include<iostream>
int main()
{
    using namespace std;
    const int EXT = 5;
    cout << "请输入身高:___\b\b\b";
    int height;
    cin >> height;
    cout << "身高英尺是:" << height*EXT << endl;
    return 0;
}
运行结果:

2、
代码:
#include<iostream>
int main()
{
    using namespace std;
    const int INCH = 12;
    const double MI = 2.54E-2;
    const double KG = 2.2;
    cout << "请输入英尺身高:____\b\b\b\b";
    int height;
    cin >> height;
    cout << endl;
    cout << "请输入英寸身高:____\b\b\b\b";
    int heightCun;
    cin >> heightCun;
    cout << endl;
    cout << "请输入体重(磅):____\b\b\b\b";
    int weight;
    cin >> weight;
    cout << endl;
    double newHeight = height*MI + (heightCun/INCH)*MI;
    double newweight = weight/KG;
    cout << "您的身高为:" << newHeight << "米" << endl;
    cout << "您的体重为:" << newweight << "千克" <<endl;
    return 0;
}
运行结果:

3、
代码:
#include<iostream>
int main()
{
    using namespace std;
    const int EXTDEGREE = 60;
    const int EXTMINUTES = 60;
    cout << "Enter a latitude in degrees, minutes, and seconds:\n";
    cout << "Forst, enter the degrees:";
    int degrees;
    cin >> degrees;
    cout << endl;
    cout << "Next, enter the minutes of arc:";
    int minutes;
    cin >> minutes;
    cout << endl;
    cout << "Finally, enter te seconds of arc:";
    int seconds;
    cin >> seconds;
    double outcome = degrees + (seconds/EXTMINUTES + minutes)/EXTDEGREE;
    cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << outcome << " degrees" <<endl;
    return 0;
}
运行结果:

4、
代码:
#include<iostream>
int main()
{
    using namespace std;
    const int DAYHOUR = 24;
    const int HOURMIN = 60;
    const int MINSECONDS = 60;
    cout << "Enter the number of Sencods:";
    int seconds;
    cin >> seconds;
    cout << endl;
    int days = seconds/(DAYHOUR*HOURMIN*MINSECONDS);
    int afterDaysOverSeconds = (seconds-days*DAYHOUR*HOURMIN*MINSECONDS)/MINSECONDS;
    int minutes = afterDaysOverSeconds/MINSECONDS;
    int overSeconds = afterDaysOverSeconds-minutes*MINSECONDS;
    cout << seconds << " seconds = " << days << " days, " << minutes << " minutes, " << overSeconds << " seconds" << endl;
    return 0;
}
运行结果:

5、
代码:
#include<iostream>
int main()
{
    using namespace std;
    cout << "请输入驱车里程(英里):";
    float mileage;
    cin >> mileage;
    cout << endl;
    cout << "请输入耗油量(加仑):";
    float gasoline;
    cin >> gasoline;
    float fuel = mileage/gasoline;
    cout << "每加仑行驶里程为:" << fuel << " 英里" << endl;
    return 0;
}
运行结果:

6、
代码:
#include<iostream>
int main()
{
    using namespace std;
    const float MILETOKM = 100/62.14;
    const float GALTOL = 3.875;
    cout << "输入汽车每一百公里耗油量(L):";
    float gasoline;
    cin >> gasoline;
    float mile = 100/MILETOKM;
    float ltogal = gasoline/GALTOL;
    cout << "换算结果是:每加仑行驶 " << ltogal/mile << "英里" << endl;
    return 0;
}
运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值