啊楼哈 开始第二章
1.编写一个程序,要求用户使用一个整数指出自己的身高(英寸表示),将身高转换为英尺和英寸,使用下划线来指示输入位置,使用const表示转换因子。
#include <iostream>
using namespace std;
int main()
{
int high1,high2,high3;
const int standard = 12;
cout<<"请输入您的身高,单位为英寸:";
cin>>high1;
high2 = high1 / standard;
high3 = high1 % standard;
cout<<"您的身高为:"<<high2<<" 英尺 "<<high3<<" 英寸"<<endl;
return 0;
}
运行结果:
2.编写一个程序,要求按照几英尺几英寸的方式输入身高,以磅为单位输入体重(使用三个变量),报告BMI,计算方式书中给出。
#include <iostream>
#include <math.h>
using namespace std;
void displayBMI();
double displayshifthigh();
int high1,high2,weight;
int main()
{
cout<<"请输入你的身高(英尺 英寸):";
cin>>high1;
cin>>high2;
cout<<endl;
cout<<"请输入你的体重(磅):";
cin>>weight;
displayshifthigh();
displayBMI();
}
double displayshifthigh()
{
return high1 * 12 * 0.0254 + high2 * 0.0254; //没有设置转换因子..
}
void displayBMI()
{
cout<<"你的身高是:"<<high1<<"英尺"<<high2<<"英寸"<<endl;
cout<<"所以你的身高是:"<<displayshifthigh()<<"米"<<endl;
cout<<"你的体重(千克)是:"<< double(weight / 2.2) <<endl;
cout<<"你的BMI指数是:"<<double(weight / 2.2) / pow(displayshifthigh(),2) <<endl;
}
运行结果:
3.编写一个程序,用户可以以度,分,秒的方式输入一个纬度。然后以度为单位显示该纬度。
#include <iostream>
#include <math.h>
using namespace std;
double result();
double degree,minute,second;
int main()
{
cout<<"Enter a latitude in degrees,minutes,and seconds:"<<endl;
cout<<"First, enter the degrees:";
cin>>degree;
cout<<"Next, enter the minutes of arc:";
cin>>minute;
cout<<"Finally, enter the second of arc:";
cin>>second;
cout<<degree<<" degrees, "<<minute<<" minutes, "<<second<<" seconds= "<< result()<<" degrees"<<endl;
return 0;
}
double result()
{
return degree + double(minute/60) + double(second/3600);
}
运行结果:
4.编写一个程序,用户按照整数的方式输入秒数,以天、时、分钟、秒来显示这段时间,并且运用到相应的符号常量
#include <iostream>
#include <math.h>
using namespace std;
const int hour_of_day = 24;
const int minute_of_hour = 60;
const int Second_of_minute = 60;
int main()
{
int s;
cout<<"Enter the number of seconds: ";
cin>>s;
int second = s % Second_of_minute;
int all_minute = s / Second_of_minute;
int minute =all_minute % minute_of_hour;
int all_hour = all_minute / minute_of_hour;
int hour = all_hour % hour_of_day;
int day = all_hour / hour_of_day;
cout<<s<<" seconds = "<<day<<" days, "<<hour<<" hours, "<<minute<<" minutes, "<<second<<" seconds"<<endl;
}
运行结果:
5.编写完一个程序,要求用户输入全球当前的人口和美国当前的人口,信息被储存在longlong变量中,输出如书中所示
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long world_num,US_num;
cout<<"Enter the world's population:";
cin>>world_num;
cout<<"Enter the population of the US:";
cin>>US_num;
cout<<"The population of the US is "<<double(US_num)/ double(world_num)*100<<"% of the world population."<<endl;
return 0;
}
运行结果:
6. 编写一个程序,要求用户输入驱车里程(里程)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。同时也可使用另外一种欧洲风格的结果
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"请用户选择两种计算方式"<<"(输入1为正常计算,其他数字为欧洲式计算):";
cin>>x;
if(x==1)
{
double mile,gas;
cout<<"请输入您的驱车里程(英里):";
cin>>mile;
cout<<"请输入您使用的汽油量(加仑):";
cin>>gas;
cout<<"耗油量为一加仑的里程为:"<<double(mile)/double(gas)<<endl;
}
else
{
double kilo,L;
cout<<"请输入您行驶的距离(公里):";
cin>>kilo;
cout<<"请输入您使用的汽油量(升):";
cin>>L;
cout<<"每100公里的耗油量(升):"<<double(kilo)/double(L)<<endl;
}
return 0;
}
实验结果:
7.编写一个程序,按照书中所示实现所需功能
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter fuel consumption per 100 km(UN):";
double UN;
cin >> UN;
cout <<" Gallons per mile(US) : "<< 1 / UN*62.14*3.875;
return 0;
}
实验结果:
C++…简直是望断高楼…