编程题:
1.编写一个小程序,要求用户使用一个整数指出自己的身高(单
位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来
指示输入位置。另外,使用一个const符号常量来表示转换因子。
#include<iostream>
using namespace std;
int main() {
//program1
int inches,feet;
const int inch_per_foot = 12;
cout << "please enter your high in inches :____\b\b\b\b ";
cin >> inches;
feet = inches / inch_per_foot;
inches = inches % inch_per_foot;
cout << "your high is " << feet << " feet ," << inches << " inch" << endl;
return 0;
}
2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并
以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI—体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#include<iostream>
using namespace std;
int main() {
//program2
float foot, inch, pound,BMI;
float wt_kilogram, ht_meter;
const double inch_per_foot = 12;
const double Meter_per_inch = 0.0254;
const double pound_per_kilogram = 2.2;
cout << "please enter your height in feet and inches:__ __\b\b\b\b\b";
cin >> foot >> inch;
cout << "please enter your weight in pound:___\b\b\b";
cin >> pound;
wt_kilogram = pound / pound_per_kilogram;
ht_meter = (foot * inch_per_foot + inch) * Meter_per_inch;
BMI = wt_kilogram / (ht_meter * ht_meter);
cout << "your wt_kilogram: " << wt_kilogram << " ht_meter: " << ht_meter << endl;
cout << "your BMI : " << BMI << endl;
return 0;
}
3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度,
然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量
的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。
下面是该程序运行时的情况:
#include<iostream>
using namespace std;
int main() {
//program3
const double Minute_per_Degree = 60.0;
const double Second_per_Minute = 60.0;
int degrees, minutes, seconds;
double Degree;
cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: ";
cin >> degrees;
cout << "Second,enter the minutes of arc:";
cin >> minutes;
cout << "Finally, enter the seconds of arc:";
cin >> seconds;
Degree = degrees + (minutes + (seconds / Second_per_Minute)) / Minute_per_Degree;
cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << Degree << " degrees";
return 0;
}
4.编写一个程序,要求用户以整数方式输入秒数(使用long或long
long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使
用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多
少秒。该程序的输出应与下面类似:
#include<iostream>
using namespace std;
int main() {
//program4
const int hours_per_day = 24;
const int minutes_per_hour = 60;
const int seconds_per_minute = 60;
long long Seconds;
int days, hours, minutes, seconds;
cout << "Enter the number of seconds:";
cin >> Seconds;
seconds = Seconds % seconds_per_minute;
minutes = (Seconds / seconds_per_minute) % minutes_per_hour;
hours = ((Seconds / seconds_per_minute) / minutes_per_hour) % hours_per_day;
days = ((Seconds / seconds_per_minute) / minutes_per_hour) / hours_per_day;
cout << Seconds << "seconds = " << days << " days, " << hours << " hours, "
<< minutes << " minutes, " << seconds << "seconds";
return 0;
}
5.编写一个程序,要求用户输入全球当前的人口和美国当前的人
口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
#include<iostream>
using namespace std;
int main() {
//program5
long long population_of_world, population_of_US, population_of_China;
cout << "Enter the world's population: ";
cin >> population_of_world;
cout << "Enter the population of US: ";
cin >> population_of_US;
cout << "Enter the population of China: ";
cin >> population_of_China;
cout << "The population of the US is " << 100 * double(population_of_US) / double(population_of_world) << "% of the world population."<<endl;
cout << "The population of the China is " << 100 * double(population_of_China) / double(population_of_world) << "% of the world population."<<endl;
return 0;
}
6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量
(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后
指出欧洲风格的结果—即每100公里的耗油量(升)。
#include <iostream>
using namespace std;
int main() {
// 用于存储输入的里程和汽油量
double distanceMiles, volumeGallons;
// 提示用户输入里程和汽油量(英里和加仑)
cout << "Please enter the distance traveled (miles): ";
cin >> distanceMiles;
cout << "Please enter the volume of gasoline used (gallons): ";
cin >> volumeGallons;
// 计算每加仑的里程
double mileagePerGallon = distanceMiles / volumeGallons;
// 输出每加仑的里程
cout << "Your car's mileage per gallon is: " << mileagePerGallon << " miles/gallon" << endl;
// 询问用户是否想要使用公制单位输入
char choice;
cout << "Do you want to input in metric units? (y/n): ";
cin >> choice;
// 如果用户选择使用公制单位输入
if (choice == 'y' || choice == 'Y') {
// 提示用户输入里程和汽油量(公里和升)
double distanceKm, volumeLiters;
cout << "Please enter the distance traveled (kilometers): ";
cin >> distanceKm;
cout << "Please enter the volume of gasoline used (liters): ";
cin >> volumeLiters;
// 计算每100公里的耗油量(升)
double consumptionPer100Km = (volumeLiters / distanceKm) * 100;
// 输出每100公里的耗油量(升)
cout << "Your car's fuel consumption per 100 kilometers is: " << consumptionPer100Km << " liters/100km" << endl;
}
return 0;
}
7.编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100
公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量—每加
仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃
料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等
于3.875升。因此,19mpg大约合12.4l/100km,l27mpg大约合
8.71/100km。
#include <iostream>
using namespace std;
int main() {
// 用于存储用户输入的耗油量(每100公里的升数)
double consumptionPer100Km;
// 提示用户输入耗油量(每100公里的升数)
cout << "Please enter the fuel consumption per 100 kilometers (liters): ";
cin >> consumptionPer100Km;
// 欧洲风格的每100公里耗油量(升)转换为美国风格的每加仑里程(英里)
const double kmPer100Miles = 62.14; // 100公里等于62.14英里
const double litersPerGallon = 3.875; // 1加仑等于3.875升
double milesPerGallon = kmPer100Miles / (consumptionPer100Km / litersPerGallon);
// 输出转换后的结果
cout << "In US style, your car's fuel consumption is approximately " << milesPerGallon << " miles per gallon." << endl;
return 0;
}