1、编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用给一个const符号常量来表示转换因子。
const关键字类似于C语言中的#define的定义。不同的是。#define是在开头进行宏定义。const这是在程序中进行定义。
<span style="font-size:14px;"><span style="font-size:14px;">#include <iostream>
using namespace std;
int main() {
const int factors = 12;
int in;
int ft;
cout << "Please Enter you Heigth : (in)";
cin >> in;
ft = in / factors;
cout <<"This Height is "<< ft << " (ft)"<<endl;
return 0;
}</span>
</span>
2、编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用三个变量来存储这些信息。)改程序报告为BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸作为单位的身高转换为以米为单位的身高(1英寸=0.0254米)然后,将以磅为单位的的体重转换为以千克为单位的体重(1千克=2.2磅)。最后计算相应的BMI---体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
<span style="font-size:14px;"><span style="font-size:14px;">#include <iostream>
using namespace std;
int main() {
const int factors = 12;
const float factors1 = 0.0254;
const float factors2 = 2.2;
float in;
float ft;
float wei,wei1;
float m;
float BMI;
cout << "Please Enter you Heigth : (ft)(in)";
cin >> ft;
cin >> in;
m = in * factors1;
// cout <<m<<endl;
in = ft * factors;
// cout << in<<endl;
m += in * factors1;
// cout <<m<<endl;
cout << "Please Enter you Weigth :";
cin >>wei;
wei1 = wei / factors2;
BMI = wei1 / m;
cout << "The BMI is : "<< BMI <<endl;
return 0;
}</span></span>
3、编写一个程序,要求用户以度、分、秒的方式输入一个维度,然后以度为单位显示该维度。1度为60分,1分等于60秒。轻易符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量储存它。下面是该程序运行时的情况。
Enter a latitude in degrees,minutes,and seconds:
First, enter the degree : 37
Next, enter the minutes of arc :51
Finally, enter the seconds of arc : 19
37 degrees,51 minutes,19 seconds = 37.8553 degrees.
<span style="font-size:14px;"><span style="font-size:14px;">#include <iostream>
#include <math.h>
void locate();
using namespace std;
int main() {
locate();
return 0;
}
void locate() {
const int factor = 60;
int Degree,Minute,Second;
float total;
cout <<"Enter a latitude in degrees,minutes,and seconds"<<endl;
cout <<"First, enter the degree : ";
cin >> Degree;
cout <<"Next, enter the minutes of arc : ";
cin >> Minute;
cout <<"Finally, enter the seconds of arc : ";
cin >> Second;
total = (float)Degree;
total += (float)Minute / factor;
total += (float)Second / pow(factor,2);
cout << Degree <<" degrees, " << Minute <<" minutes, "<< Second<<" seconds = "<< total <<" degrees."<<endl;
}</span></span>
4、编写一个程序,要求用户以正数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少个小时,每小时有多少分钟以及每分钟有多少秒。该程序的输入应与下面类似:
Enter the number of seconds : 31600000
31600000 seconds = 365 days,17 hours, 46 minutes, 40 seconds
<span style="font-size:14px;"><span style="font-size:14px;">#include <iostream>
void Days();
using namespace std;
int main() {
Days();
return 0;
}
void Days() {
const int factor = 60;
const int factor1 = 24;
int Day,Hour,Minute;
long long Second;
cout <<"Enter the number of seconds : ";
cin >> Second;
Minute = (int)Second / factor;
Second -= Minute *factor;
Hour = Minute / factor;
Minute -= Hour * factor;
Day = Hour / factor1;
Hour -= Day * factor1;
cout << Second <<" seconds = "<< Day <<" days "<< Hour <<" hours "<< Minute <<" minutes "<< Second <<"seconds."<<endl;
}
</span></span>
5、编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
Enter the world's population: 6898758899
Enter the population of the US:310783781
The population of theUS is 4.50492% of the world population.
<span style="font-size:14px;">#include <iostream>
void Population();
using namespace std;
int main() {
Population();
return 0;
}
void Population() {
const int factor = 100;
long long ToPeople,ApPeople;
float Percent;
cout <<"Enter the world's population: ";
cin >> ToPeople;
cout <<"Enter the population of the US: ";
cin >> ApPeople;
Percent = (float)ApPeople/(float)ToPeople;
cout <<"The population of the US is "<< Percent*factor<<"% of the world population."<<endl;
}</span>
6、编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果----即每100公里的耗油量(升)
<span style="font-size:14px;">#include <iostream>
void Compution();
using namespace std;
int main() {
Compution();
return 0;
}
void Compution(){
float Dis,Oil,Dis1,Oil1;
cout <<"Please Enter the Distance : ";
cin >> Dis;
cout <<"Please Enter the Used Oil : ";
cin >> Oil;
Dis1 = Dis/Oil;
Oil1 = Oil/Dis;
cout <<"1 us gal can go "<< Dis1 <<" kilometers"<<endl;
cout << "1 kilometer can use "<< Oil1 <<" us gal"<<endl;
}</span>
上述代码使用的单位绝比不对。2333因为我逗比所以就酱紫写啦。反正思路肯定是这样。两个除法就搞定辣。炒鸡简单哦。
7、编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后酱其转换为美国风格的耗油量---每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,27mpg大约合8.71/100km
<span style="font-size:14px;">#include <iostream>
void Compution();
using namespace std;
int main() {
Compution();
return 0;
}
void Compution(){
float Dis,Oil,Oil1;
int Dis1;
const float factor = 3.875;
cout <<"Please Enter the per 100 kilometers used the Oil :(L) ";
cin >> Oil;
Oil1 = Oil / factor;
cout <<"it means "<< Oil1 <<" Us gal"<<endl;
Dis = 62.14;
Dis1 = Dis / Oil1;
cout << "America way is "<< Dis1 <<" mpg"<<endl;
}
</span>
第二组8.71的数据验证不通过。不知道是书上127写错了还是我的整个程序有问题。唔。总之我就酱紫写了。误导你了的话我的错。不行你咬我吧。好嘛