《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.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...
随机推荐
- Python爬虫实战 批量下载高清美女图片
彼岸图网站里有大量的高清图片素材和壁纸,并且可以免费下载,读者也可以根据自己需要爬取其他类型图片,方法是类似的,本文通过python爬虫批量下载网站里的高清美女图片,熟悉python写爬虫的基本方法: ...
- 广州做假证c
广州做假证[电/薇:187ヘ1184ヘ0909同号]办各类证件-办毕业证-办离婚证,办学位证书,办硕士毕业证,办理文凭学历,办资格证,办房产证不. 这是一个简单的取最大值程序,可以用于处理 i32 数 ...
- 【python】超有用的集合类collections,不来了解一下?
在使用Python的过程中,一定是离不开数据结构的, 也就是List-列表,Tuples-元组,Dictionaries-字典. 那实际应用中我们更多的还是要去操作这些结构里的数据.比如,在列表后面添 ...
- 万级K8s集群背后etcd稳定性及性能优化实践
背景与挑战 随着腾讯自研上云及公有云用户的迅速增长,一方面,腾讯云容器服务TKE服务数量和核数大幅增长, 另一方面我们提供的容器服务类型(TKE托管及独立集群.EKS弹性集群.edge边缘计算集群.m ...
- Spring Cloud:Consul基础知识
一.基本概念 Consul是一套开源的分布式服务发现和配置管理系统,由HashiCorp公司用Go开发. 它提供微服务系统中的服务治理.配置中心.控制总线等功能. 服务发现:提供HTTP和DNS两种发 ...
- CodeForces - 1114D-Flood Fill (区间dp)
You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ...
- Roads in the North (树的直径)
Building and maintaining roads among communities in the far North is an expensive business. With thi ...
- Educational Codeforces Round 68 (Rated for Div. 2)-D. 1-2-K Game
output standard output Alice and Bob play a game. There is a paper strip which is divided into n + 1 ...
- java-对象引用和对象本身
示意图 应用场景 1.赋值 只有用新的对象本身直接赋值给对应引用,该对象引用的值会改变,因为对象引用指向的对象本身改变了. 2.方法入参 调用方法和被调用方法. 代码 import com.aliba ...
- 在微信公众号"码海"里学了一招:在update语句里使用case when 以避免多次更新导致的数据异常.
需求:将emp表中工资大于一万的降到9成,工资少于一万的乘以1.2. 难点:如果分成两句update执行,在10000附近的值可能会执行两次. 钥匙:在update语句里采用case when,使更新 ...