C++ Primer Plus(第6版) 第3章编程练习
3.1
//3.1#include<iostream>
int main()
{using namespace std;const int in_per_ft=12;cout<<"Enter your height(in inch):__\b\b ";int height; cin>>height;int ft=height/in_per_ft;//ft英尺int in=height%in_per_ft;//in英寸cout<<"Your height is "<<ft<<" feet "<<in<<" inch.\n";cin.get();cin.get();return 0;
}
3.2
//3.2#include<iostream>
#include<cmath>
int main()
{using namespace std;const int in_per_ft=12;//1英尺为12英寸 const double m_per_in=0.0254;//1英寸=0.0254米 const float pound_per_kg=2.2;//1千克=2.2磅 int ft,in;float pound;cout<<"Enter your height(in feet): ___\b\b\b";cin>>ft;cout<<"Enter your height(in inch): ___\b\b\b";cin>>in;cout<<"Enter your weight(in pound): ___\b\b\b";cin>>pound;int height=ft*in_per_ft+in;//计算以英寸为单位的身高 cout<<"Your height is "<<height<<" inch.\n";double height1=height*m_per_in;//计算以米为单位的身高cout<<"Your height is "<<height1<<" meters.\n";float kg=pound/pound_per_kg;//计算以千克为单位的体重cout<<"Your weight is "<<kg<<" kg.\n";float BMI=kg/pow(height1,2);cout<<"BMI= "<<BMI<<endl;cin.get();cin.get();return 0;}
3.3
#include<iostream>
int main()
{using namespace std;const float DToM=60.0;const float MToS=60.0;int d,m,s;//d,m,s分别表示度,分,秒 cout<<"Enter a latitude in degrees, minutes, and seconds:\n";cout<<"First, enter the degrees: ";cin>>d;cout<<"Next, enter the minutes of arc: ";cin>>m;cout<<"Finally, enter the seconds of arc: ";cin>>s;float n=float (d+m/DToM+s/DToM/MToS);//n表示以度为单位表示 cout<<d<<" degrees, "<<m<<" minutes, "<<s<<" seconds = "<<n<<" degrees\n";cin.get();cin.get();return 0;}
3.4
//3.4#include<iostream>
int main()
{using namespace std;const int h_per_day=24;const int m_per_h=60;const int s_per_m=60;long second;cout<<"Enter the number of seconds: ";cin>>second;int s=second%s_per_m;int m=second/s_per_m%m_per_h;int h=second/s_per_m/m_per_h%h_per_day;int day=second/s_per_m/m_per_h/h_per_day;cout<<second<<" seconds = "<<day<<" days, "<<h<<" hours, "<<m<<" minutes, "<<s<<" seconds\n";return 0;cin.get();cin.get();
}
3.5
//3.5#include<iostream>
int main()
{using namespace std;cout<<"Enter the world\'s population: ";long long US_population, world_population;cin>>world_population;cout<<"Enter the population of the US: ";cin>>US_population;double percentage=(double)US_population/world_population*100;cout<<"The population of the US is "<<percentage<<"% of the world population.\n";return 0;cin.get();cin.get();
}
3.6
//3.6 汽车耗油量,一加仑的里程 or 每100公里的耗油量(L/100Km) #include<iostream>
int main()
{using namespace std;cout<<"Enter the miles of the distance you have driven: ";double m_distance;//驱车里程(英里) cin>>m_distance;cout<<"Enter the gallons of gasoline you have used: ";double m_gasoline;//汽油量(加仑)cin>>m_gasoline;cout<<"Your car can run "<<m_distance/m_gasoline<<" miles per gallon.\n";cout<<"Computing by European style:\n";cout<<"Enter the kilometers of the distance you have driven: ";double k_distance;//驱车里程(公里)cin>>k_distance;cout<<"Enter the petrol in liters: ";double k_gasoline;//汽油量(升)cin>>k_gasoline;cout<<k_gasoline/k_distance*100<<" L/100Km.\n";return 0;cin.get();cin.get();}
3.7
//3.7 将欧式耗油量(每100公里消耗的汽油量升)转换为美式耗油量(每加仑多少英里)#include<iostream>
int main()
{using namespace std;cout<<"Enter the automobile gasoline consumption figure in European style(liters per 100 kilometers): ";double Euro_style;cin>>Euro_style;cout<<"Converts to U.S. style(miles per gallon): "<<62.14*3.875/Euro_style<<" mpg.\n";return 0;cin.get();cin.get();}