1. 程式人生 > >c++primer 第三章編程練習答案

c++primer 第三章編程練習答案

答案 c++ nal world mint fin blog logs eas

3.7.1

#include<iostream>
int main() {
    using namespace std;
    const int unit = 12;
    int height,inch,foot;
    cout << "please input your height __ inch\b\b\b\b\b\b\b";
    cin >> height;
    inch = height % unit;
    foot = height / unit;
    cin.get();
    cout << "your height is 
" << foot << " foot " << inch << " inch"; cin.get(); }

3.7.2

#include<iostream>
int main() {
    using namespace std;
    cout.setf(ios_base::fixed, ios_base::floatfield);
    const double inch_to_meter = 0.0254;
    const double kg_to_lb = 2.2;
    int inch, foot, lb;
    
//float height, weight; cout << "please input your height!\n"; cout << "foot:_\b"; cin >> foot; cout << "inch:_\b"; cin >> inch; cout << "please input your weight " << "___ lb\b\b\b\b\b\b"; cin >> lb; cin.get(); cout << "
your height is " << foot * 10 + inch << " inch\n"; cout << "your height is " << (foot * 10 + inch)*inch_to_meter << "m\n"; cout << "your weight is " << lb / 2.2 << "kg\n"; cout << "your BMI is " << pow((lb / 2.2) / ((foot * 10 + inch)*inch_to_meter), 2); //cout << pow(2, 3); cin.get(); }

3.7.3

#include<iostream>
int main() {
    using namespace std;
    const int degree_to_minute = 60;
    const int minute_to_second = 60;
    int degree, minute, second;
    double latitude;
    cout << "Enter a latitude in degree,minutes,and seconds:\n";
    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;
    cin.get();
    latitude = degree + double(minute) / degree_to_minute + double(second) / degree_to_minute / minute_to_second;
    cout << degree << " degrees," << minute << " minutes," << second << " seconds = " << latitude << " degrees";
    cin.get();
}

3.7.4

#include<iostream>
int main() {
    using namespace std;
    const int days_to_hours = 24;
    const int hours_to_minutes = 60;
    const int mintutes_to_seconds = 60;
    int days, hours, minutes, seconds,init_times;
    cout << "Enter the number of seconds: ";
    cin >> init_times;
    cin.get();
    days = init_times / mintutes_to_seconds / hours_to_minutes / days_to_hours;
    hours = init_times / mintutes_to_seconds / hours_to_minutes % days_to_hours;
    minutes = init_times / mintutes_to_seconds % hours_to_minutes;
    seconds = init_times % mintutes_to_seconds;
    cout << init_times << " seconds = " << days << " days," << hours << " hours," << minutes << " minutes," << seconds << " seconds.";
    cin.get();
}

3.7.5

#include<iostream>
int main() {
    using namespace std;
    long long world_num;
    long long us_num;
    cout << "Enter the world‘s population: ";
    cin >> world_num;
    cout << "Enter the population of the US: ";
    cin >> us_num;
    cin.get();
    cout << "The population of the US is " << double(us_num) / double(world_num)*100 << "% of the world population";
    cin.get();
}

c++primer 第三章編程練習答案