注:有的题设条件自己改动了一下,比如英寸英尺改成米和厘米,部分题目自己加了点额外要求。

1.要求用户输入身高(m),用下划线提示用户输入,将身高转化成“米“加“厘米”的形式输出,用const限定符设定转化因子。

#include<iostream>

using namespace std;

const int cvt = 1;

int main()
{
char s = '_'; double input_height; cout << "Enter your height(m):" << s << s; cin >> input_height; cout << "Your height is:" << int(input_height) / cvt << "m plus " << 100*(input_height - int(input_height)) << "cm." << endl; system("pause"); }

 

2.要求用户输入身高(m)和体重(kg),计算其BMI指数,并判断其健康程度。

#include<iostream>

using namespace std;

const double index1 = 18.5, index2 = 23.9, index3 = 27.0, index4 = 32.0;

int main()
{
double input_height,input_weight,BMI_index; cout << "Enter your height(m):" << endl; cin >> input_height; cout << "Enter your weight(kg):" << endl; cin >> input_weight; BMI_index = input_weight / (input_height*input_height); cout << "Your BMI index is " << BMI_index << endl; if (BMI_index < index1)
cout << "underweight\n";
else if ((BMI_index >= index1) && (BMI_index < index2))
cout << "normal weight\n";
else if ((BMI_index >= index2) && (BMI_index < index3))
cout << "overweight\n";
else if ((BMI_index >= index3) && (BMI_index < index4))
cout << "obese\n";
else
cout << "extrmely obese\n"; system("pause"); }

 

3.要求用户输入秒数,计算其天数+小时数+分钟数+秒数的结果并输出(如“31600000 秒 = 365 天,17小时,46分钟,40秒”)。

#include<iostream>

using namespace std;

const long S2M = 60,S2H = 3600,S2D = 86400; 

int main()
{
long long input_seconds; int seconds,minutes,hours,days; cout << "Enter the number of seconds:"; cin >> input_seconds; days = input_seconds / S2D;
hours = (input_seconds % S2D) / S2H;
minutes = ((input_seconds % S2D) % S2H) / S2M;
seconds = ((input_seconds % S2D) % S2H) % S2M; cout << input_seconds << " seconds = ";
cout << days << " days," << hours << " hours," << minutes << " minutes," << seconds << " seconds."; system("pause"); }

  

《C++ primer plus》第3章练习题的更多相关文章

  1. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  2. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  3. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  4. 《C++ primer plus》第5章练习题

    1.输入两个整数,输出两个整数之间所有整数的和,包括两个整数. #include<iostream> using namespace std; int main() { int num1, ...

  5. C Primer Plus 第3章 数据和C 编程练习

    1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...

  6. C++ Primer 5th 第1章 开始

    *****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...

  7. C++ Primer 笔记 第三章

    C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 ...

  8. C++primer拾遗(第二章:变量和基本类型)

    这是我对c++primer第二章的一个整理总结,算是比较适用于我自己吧,一小部分感觉不用提及的就省略了,只提了一下平时不注意,或者不好记住的内容. 排版太费劲了,直接放了图片格式.从自己的oneNot ...

  9. python第一章练习题

    本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...

随机推荐

  1. laravel+vue+vuetify 前端匹配不到数据记录 No matching records found

    后端数据:使用guzzle获取api数据,(安装扩展包guzzle) use GuzzleHttp\Client; //获取请求远程产品信息需要的参数public function getParams ...

  2. Nodejs模块:fs

    /** * @description fs模块常用api */ // fs所有的文件操作都是异步IO,如果要以同步的方式去调用,都会加一个在原同步api的基础上加Sync // 同步的方式会在最后传入 ...

  3. 让这个Java语言的开源商城系统火起来

    Java是一门非常优秀的面向对象编程语言,功能强大且简单易用,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,凭借其简单性.面向对象.分布式.健壮性.安全性.平台独立与可 ...

  4. 使用DataStax Java驱动程序的最佳实践

    引言 如果您想开始建立自己的基于Cassandra的Java程序,欢迎! 也许您已经参加过我们精彩的DataStax Academy课程或开发者大会,又或者仔细阅读过Cassandra Java驱动的 ...

  5. Rider 2018 激活码

    http://jetbrains.license.laucyun.com (Lower of 2017.3) http://13.112.247.119:8000 (All version, espe ...

  6. C III

    http://cossacksworld.ucoz.co.uk/load/c_iii_files/79 http://cossacksworld.ucoz.co.uk/load/c_iii_files ...

  7. 如何编写高质量的C#代码(一)

    从"整洁代码"谈起 一千个读者,就有一千个哈姆雷特,代码质量也同样如此. 想必每一个对于代码有追求的开发者,对于"高质量"这个词,或多或少都有自己的一丝理解.当 ...

  8. 跟着尚硅谷系统学习Docker-【day01】

    day01-20200710   p1.基础知识   鲸鱼背上有一些集装箱   学习docker得基础知识   1.强制:熟悉Linux命令和相关背景知识 2.建议有maven和git的相关知识   ...

  9. shell小技巧(5)计算当前目录下一级子目录的数量

    方法1: ls -al |grep -E "^d" |grep -v "\.$" |wc -l 详解: -a 列出目录下的所有文件,包含以.开头的隐藏文件: & ...

  10. Java中构建长字符串的四种模式

    回字有四种写法,构建字符串也有四种方式. 用+号最快,第二第三种可读性好,第四种是log4j自有的.下面请见代码: package logbackCfg; import java.text.Messa ...