c++primer 第三章编程练习答案

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();
}
时间: 2024-10-11 17:58:55

c++primer 第三章编程练习答案的相关文章

c++primer 第四章编程练习答案

4.13.1 #include<iostream> struct students { char firstname[20]; char lastname[20]; char grade; int age; }; int main() { using namespace std; students student1; cout << "What is your fistname? "; cin.get(student1.firstname, 20).get();

c++primer 第五章编程练习答案

5.9.1 #include<iostream> int main() { using namespace std; int one, two, temp, sum = 0; cout << "input first interger: "; cin >> one; cout << "input second interger: "; cin >> two; for (temp = one; temp &l

c++ primer plus(第6版)中文版 第九章编程练习答案

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

c++ primer plus(第6版)中文版 第十二章编程练习答案

第十二章编程练习答案 12.1根据以下类声明,完成类,并编小程序使用它 //12.1根据以下类声明,完成类,并编小程序使用它 #include <iostream> #include <cstring> using namespace std; class Cow{ char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm, const char * ho, double wt);

c++ primer plus(第6版)中文版 第七章编程练习答案

第七章编程练习答案 7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 //7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 #include <iostream> using namespace std; double average (unsigned x, unsigned y) { return (2.0 * x * y / (x + y)); } int main () { while (true) { unsigned x, y; cout

C Primer Plus (第五版) 第三章 编程练习

第三章    数据和C 编程练习: 通过实验的方法(即编写带有此类问题的程序)观察系统如何处理整数上溢.浮点数上溢和浮点数下溢的的情况. #include <stdio.h> int main(void) { /* 16位短整形 */ short int a = 32767; printf("a + 1 = %d\n", (short)(a + 1)); /* 32位整形 */ int b = 2147483647; printf("b + 1 = %d\n&qu

C语言程序设计:现代方法(第2版)第三章全部习题答案

前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解答,并参考GitHub上相关的分享和Chegg.com相关资料.因为并没有权威的答案来源,所以可能会存在错误的地方,如有错误还希望大家能够帮助指出. 第三章练习题和编程题答案 练习题 3.1节 1.下面的printf函数调用产生的输出分别是什么? (a)  printf("6d,%4d",

c++primer 第二章编程练习答案

2.7.1 #include<iostream> int main() { using namespace std; char name[20]; char address[20]; cout << "input name :"; cin >> name; cout << "input address:"; cin >> address; cin.get(); cout << "nam