C++ Primer Plus 第三章之编程练习题
2020-02-02 17:17
816 查看
概要
这一章的编程练习题主要考察不同数据类型的运用(在什么时候用什么样的数据类型比较好)以及类型转换(定义数据类型跟实际计算的时候所需的类型不一样)
3.1英尺和英寸表示身高
#include<iostream> using namespace std; int main(){ const float changeFactor = 12.0; //输入身高 int n_tall; float f_tall; cout<<"请输入你的身高_____\b\b\b\b\b"; cin>>n_tall; cout<<"你的身高为 "<<n_tall<<"英寸"<<endl; f_tall = float(n_tall/changeFactor); cout<<"用英尺表示,你的身高表示"<<f_tall<<"英尺"<<endl; return 0; }
注意:不要在\b\b\b\b后面加\n,不要转行,不然用\b回退的意义就没有了。
3.2求BMI指数
#include<iostream> using namespace std; int main() { //转换因子 const int Inch = 12; const float Meters = 0.0254; const float Pounds = 2.2; float tallInch;//英寸 int tallFeet;//英尺 float meter;//米 float weight;//体重 cout<<"英尺:"; cin>>tallFeet; cout<<"\n英寸:"; cin>>tallInch; cout<<"\n体重(磅):"; cin>>weight; //将身高转换为英寸 tallInch = tallFeet*Inch+tallInch; //将身高转换为米 meter = tallInch*Meters; //将体重转换为千克 weight = weight/Pounds; //计算BMI指数 float bmi = weight /(meter*meter); cout<<"你的BMI指数为:"<<bmi<<endl; return 0; }
3.3以度为单位显示该维度
#include<iostream> using namespace std; int main(){ const int Six = 60; int degrees,minutes,seconds; cin>>degrees>>minutes>>seconds; cout<<"测试float(minutes/Six):" <<float(minutes/Six)<<endl; //float f_degrees = (float)degrees+float(minutes/Six)+float(seconds/(Six*Six)); //这种是错的,因为最开始是用int进行计算,这样子计算会舍掉 ,这样子就算计算完再转换为float也没有什么用了 float f_degrees = (float)degrees+float(minutes)/float(Six)+float(seconds)/float(Six*Six); cout<<"以度为单位输出:"<<f_degrees; return 0; }
注意:注意强制转换的时间点。因为最开始是用int进行计算,这样子计算会舍掉 ,这样子就算计算完再转换为float也没有什么用了 。所以应该是进行相除的两个是被强制转换为float类型了。
3.4以天、时、分钟和秒显示时间
#include<iostream> using namespace std; int main(){ const int DayHour = 24; const int HourMinu = 60; const int MinuSeco = 60; long long seconds; cin>>seconds; cout<<"输入的秒数为:"<<seconds<<endl; cout<<seconds/(DayHour*HourMinu*MinuSeco)<<"天,"<<seconds/(HourMinu*MinuSeco)%DayHour<<"小时,"<<seconds/MinuSeco%HourMinu<<"分钟,"<<seconds%MinuSeco<<"秒\n"; //用求模就是为了求出不能跟他们为一个整体的剩余的,划分到小时分钟和秒数里去 return 0; }
注意:求模相当于是不能被整出而剩下的零散的,比如求还有多少小时的,能凑够24小时的都归为一天里去了,剩下的小时是不能凑满24小时,即不能凑满一天的,但又满足小时的数,所以可以seconds/(HourMinu*MinuSeco)%DayHour,类似于求出所有的小时然后%24小时,这些小时里没有把分钟和秒数的包含进来。在秒数里seconds%MinuSeco的%MinuSeco是把没有凑够60秒的模出来。
3.5求国家人口比例
#include<iostream> using namespace std; int main(){ long long china; long long global; cin>>china>>global; cout<<float(china)/float(global)*100<<"%"<<endl; return 0; }
注意:需要转换为float类型,然后化为百分比需要*100
3.6指出每100公里的耗油量(升)
#include<iostream> using namespace std; int main(){ double distance; double oilCost; cin>>distance; cin>>oilCost; cout<<distance/oilCost<<endl; cout<<float(distance)/float(oilCost); return 0; }
3.7不同风格的耗油量的计算
#include<iostream> using namespace std; int main(){ float oilCost; cin>>oilCost; cout<<62.14/(oilCost/3.875); return 0; }
总结合并
- 注意:不要在\b\b\b\b后面加\n,不要转行,不然用\b回退的意义就没有了。
- 注意:注意强制转换的时间点。因为最开始是用int进行计算,这样子计算会舍掉 ,这样子就算计算完再转换为float也没有什么用了 。所以应该是进行相除的两个是被强制转换为float类型了。
- 注意:求模相当于是不能被整出而剩下的零散的,比如求还有多少小时的,能凑够24小时的都归为一天里去了,剩下的小时是不能凑满24小时,即不能凑满一天的,但又满足小时的数,所以可以seconds/(HourMinu*MinuSeco)%DayHour,类似于求出所有的小时然后%24小时,这些小时里没有把分钟和秒数的包含进来。在秒数里seconds%MinuSeco的%MinuSeco是把没有凑够60秒的模出来。
- 注意:需要转换为float类型,然后化为百分比需要*100
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- C++ primer plus 第六版编程练习题参考代码(第三章)
- 【第三章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus (第6版) 中文版 第三章 处理数据 编程练习答案
- c++ primer plus(第6版)中文版 第三章编程练习答案
- 【第十三章】C++ Primer plus 的编程练习题
- C++ Primer Plus第三章课后编程答案
- C++ Primer Plus 第三章练习题
- C++ Primer Plus第六版 第三章 编程练习答案
- 【第十四章】C++ Primer plus 的编程练习题
- 【第九章】C++ Primer plus 的编程练习题
- c++ primer plus 第六版 第三章课后编程练习答案
- 【第十五章】C++ Primer plus 的编程练习题
- 【第十七章】C++ Primer plus 的编程练习题
- C++ Primer Plus(第六版) 第三章 编程练习
- 【第十八章】C++ Primer plus 的编程练习题
- 【第四章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus第五版 第三章 编程练习答案
- C++ primer plus 第六版编程练习题参考代码(第二章)
- C++ Primer Plus(第五版)第五章编程练习题6(关于string难点哦)
- 《C++ Primer Plus 第六版》编程练习参考答案(第三章)