1.編寫一個小程序,要求用戶使用一個整數指出自己的身高,然後將身高轉換爲英尺和英寸。該程序使用下劃線字符來指示輸入位置。另外,使用一個const符號常量來表示轉換因子。
#include<iostream>
int main()
{
using namespace std;
const float Rate = 12.0;
int inch;
float feet;
cout << "您的身高爲____英寸\b\b\b\b\b\b\b\b";
cin >> inch;
feet=(float) inch / Rate;
cout << "您的身高爲 " << feet << " 英尺" << endl;
return 0;
}
2.編寫一個小程序,要求以幾英尺幾英寸的方式輸入其身高,並以磅爲單位輸入其體重。(使用3個變量來存儲這些信息。)該程序報告其BMI(Body Mass Index,體重指數)。
#include<iostream>
int main()
{
using namespace std;
const float RateA = 12.0;
const float RateB = 0.0254;
const float RateC = 2.2;
float feet,inch,meter,pound,kg,bmi;
//輸入部分
cout << "請您按英尺、英寸、磅的順序輸入身高體重:" << endl;
cout << "英尺: ";
cin >> feet ;
cout << "英寸: ";
cin >> inch;
cout << "磅: ";
cin >> pound;
//計算部分
inch = inch + feet * RateA;
meter = inch * RateB;
kg = pound * RateC;
bmi = meter * meter / kg;
//顯示部分
cout << "你的BMI指數爲: " << bmi << endl;
return 0;
}
3.編寫一個程序,要求用戶以度、分、秒的方式輸入一個緯度,然後以度爲單位顯示該緯度。
#include<iostream>
int main()
{
using namespace std;
const float Rate = 60.0;
float degree,minute,second,final;
//輸入部分
cout << "Enter a latitude in degrees, minutes, and seconds :" << endl << "First, enter the degrees: ";
cin >> degree;
cout << "Next, enter the minutes of arc: ";
cin >> minute;
cout << "Finally, enter the seconds of arc: ";
cin >> second;
//計算部分
final = (float) degree + minute / Rate + second /Rate / Rate;
//輸出部分
cout << degree << " degrees, " << minute << " minutes, " << second << " second = " << final << " degrees" << endl;
return 0;
}
4.編寫一個程序,要求用戶以整數方式輸入秒數,然後以天、小時、分鐘和秒的方式顯示這段時間。
#include<iostream>
int main()
{
using namespace std;
long second,seconds;
int days,hours,minutes;
cout << "Enter the number of seconds: ";
cin >> second;
seconds = second;
days = seconds / ( 60*60*24 );
seconds = seconds % ( 60*60*24 );
hours = seconds / 3600;
seconds = seconds % 3600;
minutes = seconds / 60;
seconds = seconds % 60;
cout << second << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
return 0;
}
5.編寫一個程序,要求用戶輸入全球當前的人口和美國當前的人口。將這些信息儲存在long long 變量中,並讓程序顯示美國的人口占全球人口的百分比。
#include<iostream>
int main()
{
using namespace std;
long long world,us;
float rate;
cout << "Enter the world's population: ";
cin >> world;
cout << "Enter the population of the US: ";
cin >> us;
rate = (float) us / world * 100;
cout << "The population of the US is " << rate << "% of the world population" << endl;
return 0;
}
6.編寫一個程序,要求用戶輸入驅車裏程(英里)和使用汽油量(加侖),然後指出汽車耗油量爲一加侖的里程。
#include<iostream>
int main()
{
using namespace std;
float distance,gasoline,result;
cout << "Enter the distance: ";
cin >> distance;
cout << "Enter the gasoline consumption: ";
cin >> gasoline;
result = (float) distance / gasoline;
cout << "The car can run " << result << " miles with 1 gallon gasoline." << endl;
return 0;
}
7.編寫一個程序,要求用戶按歐洲風格輸入汽車的耗油量(每100公里消耗的汽油量(升)),然後將其轉換爲美國風格的耗油量(每加侖多少英里)。
#include<iostream>
int main()
{
using namespace std;
float us,eu;
cout << "Please input the fuel consumption according to the European style:" << endl << "Fuel consumption is:____/100km\b\b\b\b\b\b\b\b\b\b";
cin >> eu;
us = (float) 62.4 / eu * 3.875;
cout << "The fuel consumption converted to American style is: " << us << "mpg" << endl;
return 0;
}