// 第三章.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>//预编译,使用iostream文件在编译前替代这行代码
#include <string>
#include <climits>
using namespace std;//将使用std命名空间中的定义
const int foot2in = 12;//定义一个英寸到英尺转换因子,1英尺=12英寸
const double in2m = 0.0254;//定义一个英寸到米的转换因子,1英寸=0.0254米
const double kg2pd = 2.2;//定义一个千克到磅的转换因子,1千克=2.2磅
const int de2mi2se = 60;//定义度分秒转换因子,1度=60分=60秒
const int se2mi = 60;
const int se2hour = 3600;
const int se2day = 86400;
const double gl2yl = 62.14;//1公里=0.6214英里
const double jl2s = 3.875;//1加仑=3.875升
void Xiti1()//简述:输入一个自己身高单位为英寸,转换为英尺和英寸
{
int high;//定义自己身高
cout << "请输入自己的身高(单位英寸):___\b\b\b";
cin >> high;
int foot = high / foot2in;
int in = high % foot2in;
cout << endl << "身高为:" << foot << "英尺" << in << "英寸\n";
return;
}
void Xiti2()//简述:计算BMI=体重(千克)/身高的平方
{
cout << "请输入你的身高(以英尺和英寸的模式)\n";
cout << "首先输入英尺:\n";
double height_foot = 0;//英尺;
cin >> height_foot;
cout << "然后输入英寸:\n";
double height_inch = 0;//英寸;
cin >> height_inch;
cout << "输入重量(磅为单位):\n";
double weight_pd = 0;//磅;
cin >> weight_pd;
double height = height_foot*foot2in + height_inch;
cout << "你的身高为" << height << "英寸\n";
double height_mi = height*in2m;
cout << "你的身高为" << height_mi << "米\n";
double weight_kg = weight_pd / kg2pd;
cout << "你的体重为" << weight_kg << "千克\n";
cout << "BMI为" << weight_kg/(height_mi*height_mi)<<endl;
return;
}
void Xiti3_1()//简述:度分秒转换
{
cout << "请输入一个纬度(以度、分、秒的模式)\n";
cout << "首先输入度数:\n";
double degrees = 0;//度;
cin >> degrees;
cout << "然后输入分:\n";
double minute = 0;//分;
cin >> minute;
cout << "最后输入秒:\n";
double second = 0;//秒;
cin >> second;
cout << degrees << "度" << minute << "分" << second << "秒" << "也就是" << degrees + minute / de2mi2se + second / de2mi2se / de2mi2se << "度。\n";
return;
}
void Xiti4()//简述:以整数输入秒数,转换成天数小时分钟
{
long long seconds;
cout << "请输入秒数:" << endl;
/*
cout<<"longlong "<<LLONG_MAX<<endl
<<"long "<< LLONG_MAX/ se2day << endl
<< "int "<<INT32_MAX << endl
<< "char "<<CHAR_BIT << endl
<< "float "<<LLONG_MAX<< endl
<< "double "<<sizeof(double) << endl
<< "short " << sizeof(short) << endl;
*/
cin >> seconds;
long long day= seconds/se2day;
int hour=seconds%se2day/se2hour;
int minute=seconds%se2day%se2hour/se2mi;
int second=seconds%60;
cout << seconds << "秒=" << day << "天," << hour << "小时," << minute << "分钟," << second << "秒\n";
return;
}
void Xiti5()//简述:输入两个国家的人口,输出人口的比例
{
long long popu_world,popu_china;
cout << "请输入全球的人口数:\n" << endl;
cin >> popu_world;
cout << "请输入中国的人口数:\n" << endl;
cin >> popu_china;
double bili=((double)popu_china/popu_world)*100.00;//这里要说明,除法的应用如果任意参数是浮点,结果才是浮点,要不就会省略小数。
cout << "中国的人口占世界人口的"<<bili<<"%\n";
return ;
}
void Xiti6()//简述:计算油耗
{
double distance, gallon;
cout << "请输入公里数:\n" << endl;
cin >> distance;
cout << "请输入油量(升):\n" << endl;
cin >> gallon;
double bili = 100*gallon/distance;//这里要说明,除法的应用如果任意参数是浮点,结果才是浮点,要不就会省略小数。
cout << "油耗:100公里" << bili << "升\n";
return ;
}
void Xiti7()//简述:油耗转换,欧洲转美洲
{
double youhao_ou, youhao_mei;
cout << "请输入欧洲标准油耗(100公里):\n" << endl;
cin >> youhao_ou;
youhao_mei = gl2yl*jl2s / youhao_ou;//这里要说明,除法的应用如果任意参数是浮点,结果才是浮点,要不就会省略小数。
cout << "美国油耗:" << youhao_mei << "英里每加仑\n";
return;
}
int main()
{
int XitiNo = 1;//输入习题编号
cout << "第三章编程练习答案:\n";
while (XitiNo>0 && XitiNo <= 7)
{
cout << "请输入习题编号或按(0)退出:\n";
cin >> XitiNo;
switch (XitiNo)
{
case 0:
{
cout << "即将退出程序啦!\n";
}break;
case 1:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti1();
}break;
case 2:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti2();
}break;
case 3:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti3_1();
}break;
case 4:
{
cout << "第" << XitiNo << "题如下:\n";
Xiti4();
}break;
case 5:
{
Xiti5();
}break;
case 6:
{
Xiti6();
}break;
case 7:
{
Xiti7();
}break;
default:
{
cout << "即将退出程序啦\n";
}
break;
}
}
system("pause");//为了程序能够停下看看。
return 0;
}
C++ primer Plus(第六版)中文版 第三章 数据处理 课后编程练习
最新推荐文章于 2024-01-03 16:32:48 发布