《C++ primer plus》第3章练习题
注:有的题设条件自己改动了一下,比如英寸英尺改成米和厘米,部分题目自己加了点额外要求。
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章练习题的更多相关文章
- 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 ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- 《C++ primer plus》第5章练习题
1.输入两个整数,输出两个整数之间所有整数的和,包括两个整数. #include<iostream> using namespace std; int main() { int num1, ...
- C Primer Plus 第3章 数据和C 编程练习
1. /* 整数上溢 */ #include <stdio.h> int main(void) { ; unsigned ; /* 无符号整数j像一个汽车里程指示表(形容的太好了,可参考& ...
- C++ Primer 5th 第1章 开始
*****代码在Ubuntu g++ 5.31 / clang++ 3.8(C++11)下编写调试***** 每个C++程序必须有一个main( )函数,main( )函数的返回值也必须是int类型, ...
- C++ Primer 笔记 第三章
C++ Primer 第三章 标准库类型 3.1using声明 例: using namespace atd; using std::cin; 3.2string类型 初始化方式 string s1 ...
- C++primer拾遗(第二章:变量和基本类型)
这是我对c++primer第二章的一个整理总结,算是比较适用于我自己吧,一小部分感觉不用提及的就省略了,只提了一下平时不注意,或者不好记住的内容. 排版太费劲了,直接放了图片格式.从自己的oneNot ...
- python第一章练习题
本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...
随机推荐
- 【转载】pandas常用函数
原文链接:https://www.cnblogs.com/rexyan/p/7975707.html 一.import语句 import pandas as pd import numpy as np ...
- 【学习中】Unity<中级篇> Schedule
章节 内容 签到 Unity3D 实战技术第二版视频教程(中级篇) 1.游戏引擎发展史 2.Unity发展史 3.3D图形学与必要组件 5月19日 4.核心类_GameObject类 5月19日 5. ...
- 【C#】Random类中构造方法、时间种子与随机数序列的关系
Random类 构造函数 1) Random random = new Random(); // 无参数构造函数使用系统时钟生成其种子值 然而,系统时钟取值范围有限,因此在小规模计算中,可能无法使用不 ...
- 把Employees显示在页面上
项目代码下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200429-1.zip 需求:从后台DB取出雇员数据,显示在前台页面上: ...
- 关于Vue-loader的那些事儿
什么是Vue-loader 一个webpack的加载器,负责将vue组件编译成普通的JavaScript模块. 关于webpack的介绍 这里呢?用到webpack,在项目的编译打包的过程中,将复杂的 ...
- C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
本文参考C++改写 https://blog.csdn.net/dpsying/article/details/20139651 (该文章的坐标理解的有误解,会导致功能无效) SendMessage ...
- 15个随机图片API
15个随机图片API 妈妈再也不用担心我网站没图用了呜 请不要重复刷新此页面 ! 找了很久的说,你难道不想收藏一下吗 其中有些 API 速度并不太好,可能会拖慢贵站的速度 我也不能保证这些 API 能 ...
- Python实现加密的ZIP文件解压(密码已知)
博主在上篇博文介绍了<Python实现加密的RAR文件解压(密码已知)>后,又尝试了ZIP文件的解压方法,下面开始分享. 当ZIP文件的压缩密码已知时,可以通过调用zipfile库进行解压 ...
- Charles 抓包(Charles二)
前面安装了Charles,并初步配置了一下:https://blog.csdn.net/qq_38175040/article/details/105411407 今天开始正式抓包了 但过程却不是很顺 ...
- 解压gzip格式文件(包括网页)
先上源码 参数说名: - source :gzip格式流内容. - len: gzip流长度 - dest: 解压后字符流指针 - gzip: 压缩标志,非0时解压gzip格式,否则按照zip解压 说 ...