C++ Primer Plus 编程练习 第三章

来源:互联网 发布:美国 博士 知乎 编辑:程序博客网 时间:2024/06/05 20:33

1编写一个小程序,要求用户输入自己的身高(单位为英寸),然后将身高转换为英尺。使用const符号常量表示转换因子。

#include <iostream>const int Inch_Per_Foot=12;int main(){using namespace std;double h;cout<<"Please enter your high:______\b\b\b\b\b\b";cin>>h;cout<<h/Inch_Per_Foot;cin.sync();cin.get();return 0;}


2编写一个小程序,要求以几英尺几英寸的方式输入身高,并以磅表示体重,计算其BMI值。

#include <iostream>const double Inch_Per_Foot=12;const double Ko_To_Pan=2.2;const double Inch_To_MI=0.0254;int main(){using namespace std;double inch,foot,weight,m,k;cout<<"Please enter your high(inch):______\b\b\b\b\b\b";cin>>inch;cout<<"Please enter your high(foot):______\b\b\b\b\b\b";cin>>foot;cout<<"Please enter your weight:______\b\b\b\b\b\b";cin>>weight;m=(inch*Inch_Per_Foot+foot)*Inch_To_MI;k=weight/Ko_To_Pan;cout<<"your BMI is "<<k/m/m;cin.sync();cin.get();return 0;}

3编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位表示。
#include <iostream>const double D_To_M=60.0;const double M_To_S=60.0;int main(){using namespace std;double d,m,s,r;cout<<"Enter a latitude in degrees,minutes, and seconds:"<<endl;cout<<"First, enter the degrees:";cin>>d;cout<<"Next, enter the minutes:";cin>>m;cout<<"Finally, enter the seconds:";cin>>s;cout<<d<<" degress, "<<m<<" minutes, "<<s<<" seconds = "<<(d+(m/D_To_M)+(s/M_To_S/D_To_M)) << " degrees";cin.sync();cin.get();return 0;}

4编写一个程序,要求用户以整数秒的形式输入秒数(使用long或long long),然后以小时、分钟和秒的形式输出。

#include <iostream>const long Hour_Per_Day=24;const long Minute_Per_Hour=60;const long Second_Per_Minute=60;int main(){using namespace std;long d,h,m,s,ss,seconds;cout<<"Enter the number of seconds: ";cin>>ss;seconds=ss;s=ss%Second_Per_Minute; // 获得秒钟 ss=ss/Second_Per_Minute;m=ss%Minute_Per_Hour; // 获得分钟 ss=ss/Minute_Per_Hour;h=ss%Hour_Per_Day; // 获得小时 d=ss/Hour_Per_Day; // 获得天数cout<< seconds <<" seconds = "<< d<<" days, "<<h<<" hours, "<<m<<" minutes, "<<s<<" seconds";cin.sync();cin.get();return 0;}

5编写一个程序,要求用户输入全球当前的人口,将这些信息存储在long long变量中,并让程序显示美国的人口占全球人口的百分比

#include <iostream>int main(){using namespace std;long long world,Amer;cout<<"Enter the world's population: ";cin>>world;cout<<"Enter the population of the US: ";cin>>Amer;double rate=(double)Amer/world*100.0;cout << "The population of the US is " << rate<< "% of the world population.";cin.sync();cin.get();return 0;}

6编写一个程序,要求用户输入驱车里程和使用汽油量,然后指出汽车耗油量为一加仑的里程。

#include <iostream>int main(){using namespace std;double inch,gal;cout<<"Please enter the inch: ";cin>>inch;cout<<"Please enter the gallon: ";cin>>gal;cout<<"耗油量为:"<<inch/gal<<" inch/gallon";cin.sync();cin.get();return 0;}


原创粉丝点击