C++primer plus第六版第三章编程题代码

第一题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    const int FootToInc = 12;
    int inch_put, inch_out,feet_out;
    cout << "请输入身高:___\b\b\b";
    cin >> inch_put;
    feet_out = inch_put / FootToInc;
    inch_out = inch_put % FootToInc;
    cout<<"您的身高是:" << feet_out << "英尺" << inch_out << "英寸" << endl;
}

第二题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    const int FootToInch = 12;
    const double InchToMeter = 0.0254;
    const double PoundToKilo = 2.2;
    double Inch_input, Foot_input, Pound_input, height, weight,bmi;
    cout << "Please input your height in foot and inch:" << endl;
    cout << "First input the foot:" << endl;
    cin >> Foot_input;
    cout << "Second input the inch:" << endl;
    cin >> Inch_input;
    cout << "Please input your weight in pound:" << endl;
    cin >> Pound_input;
    height = (Foot_input * FootToInch + Inch_input)*InchToMeter;
    weight = Pound_input / PoundToKilo;
    bmi = weight / height / height;
    cout << "Your BMI is:" << bmi << endl;
}

第三题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    double results;
    int arc_degrees,arc_minutes, arc_seconds;
    cout << "Enter a latitude in degrees, minutes, and seconds: " << endl;
    cout << "First, enter the degrees: " ;
    cin >> arc_degrees;
    cout << "Next, enter the minutes of arc: ";
    cin >> arc_minutes;
    cout << "Finally, enter the seconds of arc: ";
    cin >> arc_seconds;
    results = arc_degrees + arc_minutes / 60 + arc_seconds / 3600;
    cout << arc_degrees << "degrees, " << arc_minutes << "minutes, " << arc_seconds << "seconds = " << results << " degrees";

}

第四题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    long long second_input;
    int days, hours, minutes, seconds;
    cout << "Enter the number of seconds: ";
    cin >> second_input;
    days = second_input / (24 * 60 * 60);
    hours = (second_input % (24 * 60 * 60) )/ (60 * 60);
    minutes = (second_input % (60 * 60)) / 60;
    seconds = second_input % 60;
    cout << second_input << " seconds = " << days << " days " << hours << " hours " << minutes << " minutes" << seconds << " seconds" << endl;
}

第五题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    long long world_pop, us_pop;
    double percent;
    cout << "Enter the world's population: ";
    cin >> world_pop;
    cout << "Enter the population of the US: ";
    cin >> us_pop;
    percent = double (us_pop) / (world_pop)*100;
    cout << "The population of the US is " << percent << "% of the world population." << endl;
}

第六题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    double kilo, gallon, kilo_per_gallon;
    cout << "Enter the distances: ";
    cin >> kilo;
    cout << "Enter the gallon: ";
    cin >> gallon;
    kilo_per_gallon = gallon / kilo;
    cout << "每100公里的耗油量(升):" << kilo_per_gallon * 100 << endl;
}

第七题:

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    double us_fuel, eu_fuel;
    cout << "Enter the fuel consumption in EU standard: ";
    cin >> eu_fuel;
    us_fuel = eu_fuel / 19 * 12.41;
    cout << "The fuel consumption in US standard is: " << us_fuel << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_38224589/article/details/81485617