声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。

工程命名和文件命名可以命名成易识别的名字,当然你也可以随便自定义,只是作者本人偏好:

  1. 工程名:cprimerplus6th_chapter0_conten.pro
  2. 文件名:cprimerplus6th_exercise_0_0( )

主函数:

  1. int main( int argc, char **argv)
  2. {
  3. //cprimerplus6th_exercise3_7_1();
  4. //cprimerplus6th_exercise3_7_2();
  5. //cprimerplus6th_exercise_3_7_3();
  6. //cprimerplus6th_exercise3_7_4();
  7. //cprimerplus6th_exercise3_7_5();
  8. //cprimerplus6th_exercise3_7_6();
  9. cprimerplus6th_exercise3_7_7();
  10. return 0;
  11. }

1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const 符号常量来表示转换因子。

#include <iostream>

using namespace std;

  1. void cprimerplus6th_exercise3_7_1()
  2. {
  3. const int inch_to_foot = 12; // 1 foot = 12 inchs
  1. cout << "Please enter your height(inchs):___\b\b\b";
  2. int height_inch;
  3. cin >> height_inch;
  4. int height_foot;
  5. height_foot = height_inch / inch_to_foot;
  6. height_inch = height_inch % inch_to_foot;
  7. cout << "your height is " << height_foot << " foot " << height_inch << " inch";
  8.  
  9. }

2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1kg=2.2磅)。最后,计算相应的BMI——体重(kg)除以身高(米)的平方。用符号常量表示各转换因子。

  1. void cprimerplus_exercise3_7_2()
  2. {
  3. const int inch_to_foot = 12; // 1 foot = 12 inchs
  4. const double meter_to_inch = 0.0254; // 1 inch = 0.0254 meters
  5. const double pound_to_kg = 2.2;// 1 kg = 2.2 pound
  6.  
  7. int heigt_inch, height_foot;
  8. cout << "please enter your height(X foot X inchs):"<<endl;
  9. cout << "first enter your height in foot" << endl;
  10. cin >> height_foot;
  11. cout << "then enter your height in inch"<< endl;
  12. cin >> heigt_inch;
  13. cout << "your height is " << height_foot << " foot " << heigt_inch << "inches"<<endl;
  14. double height_meters;
  15. height_meters = (height_foot * inch_to_foot + heigt_inch) * meter_to_inch;
  16.  
  17. double weight_pound;
  18. double weight_kg;
  19. cout << "Please enter your weight(pounds):" <<endl;
  20. cin >> weight_pound ;
  21. weight_kg = weight_pound * pound_to_kg;
  22.  
  23. double body_mass_index;
  24. body_mass_index = weight_kg/(height_meters * height_meters);
  25. cout << '\n' << "your body mass index(BMI) is " << body_mass_index <<endl;
  26. }

3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后

  1. void cprimerplus_exercise_3_7_3()
  2. {
  3. const double minute_to_degree = 60.0; // 1 degree = 60 minutes
  4. const double second_to_minute = 60.0; // 1 minute = 60 seconds
  5.  
  6. int degrees, minutes, seconds;
  7. cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
  8. cout << "enter the degrees:";
  9. cin >> degrees;
  10. cout << "Next, enter the minutes of arc:" << endl;
  11. cin >> minutes;
  12. cout << "Finally, enter the seconds of arc:";
  13. cin >> seconds;
  14. double total_degrees;
  15. total_degrees = degrees + minutes/minute_to_degree +\
  16. seconds/(minute_to_degree * second_to_minute);
  17.  
  18. cout << degrees << "degrees, " << minutes << "minutes, "<< seconds \
  19. << "seconds = " << total_degrees << "degrees"<<endl;
  20. }

4.编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量储存),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。

  1. void cprimerplus_exercise3_7_4()
  2. {
  3. const int second_to_minute = 60; // 1 minute = 60 seconds;
  4. const int minute_to_hour = 60; // 1 hour = 60 minutes
  5. const int hour_to_day = 24; // 1 day = 24 hours
  6.  
  7. cout << "Enter the number of seconds:";
  8. long seconds;
  9. cin >> seconds;
  10. int days, hours, minutes, second;
  11. days = seconds / (second_to_minute * minute_to_hour * hour_to_day);
  12. hours = (seconds % (second_to_minute * minute_to_hour * hour_to_day))/(second_to_minute * minute_to_hour);
  13. minutes = ((seconds % (second_to_minute * minute_to_hour * hour_to_day))%(second_to_minute * minute_to_hour)) / second_to_minute;
  14. second = ((seconds % (second_to_minute * minute_to_hour * hour_to_day))%(second_to_minute * minute_to_hour)) % second_to_minute;
  15.  
  16. cout << seconds <<" seconds = "<< days << " days, " << hours <<" hours, " << minutes << " minutes, "<< second << " seconds"<< endl;
  17. }

5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。

  1. void cprimerplus_exercise3_7_5()
  2. {
  3. cout << "Enter the world's population:";
  4. long long world_popu;
  5. cin >> world_popu;
  6.  
  7. cout << "Enter the population of the US:";
  8. long long us_popu;
  9. cin >> us_popu;
  10.  
  11. double percentage;
  12. percentage = (us_popu * 100) / world_popu;
  13.  
  14. cout << "The population of the US is " << percentage << "% of the world population!" << endl; //输出
  15. }

6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为1加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果—即每100公里的耗油量(升)。

  1. void cprimerplus_exercise3_7_6()
  2. {
  3. cout << "Enter your driven miles(miles):";
  4. float miles;
  5. cin >> miles;
  6. cout << '\n' << "Enter your gallons of gasoline(gallons):";
  7. float gallons;
  8. cin >> gallons;
  9. cout << '\n' << " your car got" << miles/gallons << "miles per gallon\n";
  10.  
  11. }

7.编写一个程序,要求用户按欧洲的风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量---每加仑多少英里。注意:除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲的方法(燃料/距离)相反。100公里 = 64.12英里,1加仑 = 3.875升。因此,19mpg大约和12.4l/100km,127mpg大约合8.7l/100km。

  1. void cprimerplus_exercise3_7_7()
  2. {
  3. const double km100_to_miles = 62.14;
  4. const double liters_per_galllon = 3.875;
  5. double euro_rating, us_rating;
  6. cout << "Enter fuel consumption in liters per 100km:" <<endl;
  7. cin >>euro_rating;
  8. us_rating = (km100_to_miles * liters_per_galllon)/euro_rating;
  9. cout << euro_rating << "liters pper 100km is" << us_rating << "miles per gallon.\n";
  10. }

c++primerplus(第六版)编程题——第3章(数据类型)的更多相关文章

  1. c++primerplus(第六版)编程题——第4章(复合类型)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  2. c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  3. c++primerplus(第六版)编程题——第5章(循环和关系表达式)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  4. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)

    第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...

  5. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  6. c primer plus(五版)编程练习-第七章编程练习

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  7. C算法编程题(六)串的处理

    前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...

  8. 某软件大赛C#版考题整理——【编程题】

    三.编程题(4小题共40.0分)程序及结果写入对应文框内 1. 孪生素数查找程序. 所谓孪生素数指的是间隔为2 的相邻素数,就像孪生兄弟.最小的孪生素数是(3, 5),在100 以内的孪生素数还有 ( ...

  9. PMBOK(第六版) PMP备考知识总汇!

    记录本人学习PMBOK第六版的学习笔记. 备考知识总汇! PMBOK序章 PMP备考指南之相关事项介绍 PMP备考指南之第一章:引论 PMP备考指南之第二章:项目运作环境 PMP备考指南之第三章:项目 ...

随机推荐

  1. python for selenium 数据驱动测试

    # -*- coding:utf-8 -*- """ 数据驱动测试,从 csv 文件中读取数据 """ from selenium impo ...

  2. Unity3d 超级采样抗锯齿 Super Sampling Anti-Aliasing

    Super Sampling Anti-AliasingSSAA算是在众多抗锯齿算法中比较昂贵的一种了,年代也比较久远,但是方法比较简单,主要概括为两步1.    查找边缘2.    模糊边缘这是一种 ...

  3. 从m个数中取top n

    将题目具体一点,例如,从100个数中取出从大到小排前10的数 方法1:使用快速排序 因为快速排序一趟下来,小于K的数都在K的前面,大于K的数都在K的后面 如果,小于K的数有35个,大于K的数有64个 ...

  4. C#执行参数为游标 返回一个记录集的Oracle存储过程

    public DataTable SelectPay_Unit() { string returns = ""; DataTable objDataTable = new Data ...

  5. 未能导入activex控件,请确保它正确注册

    这个错误"未能导入activex控件,请确保它正确注册"昨天下午让我和我同事花费了3个小时来调试这个错误,在使用VS2010的winfrom编程时加入com组件的时候,报这个错误( ...

  6. UIApplication对象及其代理UIApplicationDelegate[转]

    在开发过程中我们需要一些全局对象来将程序的各个部分连接起来,这些全局对象中最重要的就是UIApplication对象.但在实际编程中我们并不直接和UIApplication对象打交道,而是和其代理打交 ...

  7. [Javascript] Using JSHint for Linting with Gulp

    gulpfile.js var gulp = require('gulp'); var jshint = require('gulp-jshint'); var stylish = require(' ...

  8. [Reactive Programming] RxJS dynamic behavior

    This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...

  9. Android 仿PhotoShop调色板应用(三) 主体界面绘制

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android 仿PhotoShop调色板应用(三) 主体界面绘制    关于PhotoShop调色板应用的实现我总结了两个最核心的部分:   1 ...

  10. java继承分析

    把java学完之后有開始了一遍突然发现对于继承还是不太理解所以就做了一个測试来分析一下 <span style="font-size:18px;">class A{ p ...