本题要求输入转换为不同的格式,实质上考查的是求模和求商。
这里要求使用下滑线指示输入位置,需要使用'\b'使光标向前退,身高以英尺为单位应该是三个数,因此输入三个下划线三个'\b',程序如下:
#include<iostream> int main() { using namespace std; const int inch_per_foot =12; int height; cout << "Enter your height in inch:___\b\b\b"; cin >> height; int foot = height / inch_per_foot; int inch = height % inch_per_foot; cout << "Your height is " << foot <<" feet, " <<inch <<" inches."<<endl; cin.get(); cin.get(); return 0; }
运行结果如下:
本题考查单位的转换,与第一题类似,身高以几英尺几英寸的方式输入,转换成m需要两步,程序如下:
#include<iostream> int main() { using namespace std; const int inch_per_foot = 12; const double meters_per_inch = 0.0254; const double pounds_per_kg = 2.2; int feet,inches; cout << "Enter your height in feet and inches: "<< endl; cout << "First, enter feet: __\b\b"; cin >> feet; cout << "Second, enter inches: __\b\b"; cin >> inches; cout << "Enter you weight in pound:___\b\b\b"; double pounds; cin >> pounds; int height_inch = feet * inch_per_foot + inches; double height_m = height_inch * meters_per_inch; double weight = pounds / pounds_per_kg; double BMI = weight / (height_m * height_m); cout << "Your BMI is " << BMI <<endl; cin.get(); cin.get(); return 0; }
运行结果如下:
Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19seconds = 37.8553 degrees
本题与前两题类似,按照题目要求编写程序即可,程序如下:
#include<iostream> int main() { using namespace std; int degrees, minutes,seconds; cout << "Enter a latitude in degrees, minutes, and seconds: " << endl; cout << "First, enter the degrees: "; cin >> degrees; cout << "Next, enter the minutes of arc: "; cin >> minutes; cout << "Finally, enter the seconds of arc: "; cin >> seconds; double latitude; latitude = degrees + minutes / 60.0 + seconds / 3600.0; cout << degrees << " degrees, " << minutes << " minutes, "; cout << seconds << " seconds = " << latitude << " degrees" << endl; cin.get(); cin.get(); return 0; }
运行结果如下:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
本题要求用户输入一个秒数,然后转换为天数+小时数+分钟数+秒数,相当于把之前的英尺+英寸转换为英寸的题目反过来计算,程序如下:
#include<iostream> int main() { using namespace std; long long seconds; cout << "Enter the number of seconds: "; cin >> seconds; int second, minute, hour, days; second = seconds % 60; long minutes = seconds / 60; minute = minutes % 60; long hours = minutes / 60; hour = hours % 24; days = hours / 24; cout << seconds <<" seconds = " << days <<" days, " << hour; cout << " hours, " << minute <<" minutes, " << second << "seconds" <<endl; cin.get(); cin.get(); return 0; }
运行结果如下:
Enter the world's population: 6898758899
Enter the population of the US: 310783781
The population of the US is 4.50492% of the world population.
本题要求用户两个人口数目值,计算一个占比,使用百分比显示,程序如下:
#include<iostream> int main() { using namespace std; long long pp_us, pp_world; cout << "Enter the worlds's population: "; cin >> pp_world; cout << "Enter the population of US: "; cin >> pp_us; double percentage = double(pp_us) / double(pp_world) * 100; cout.precision(6); cout << "The population of US is " << percentage; cout << "% of the world population." <<endl; cin.get(); cin.get(); return 0; }
运行结果如下:
本题与前面的计算BMI很类似,程序如下:
//ex6.cpp - - mpg convert to l/100km #include<iostream> int main() { using namespace std; double m,g; // mlie ,garlon cout << "Enter the mile: "; cin >> m; cout << "Enter the garlon: "; cin >> g; cout << "The mile per garlon is " << m / g << " mpg." << endl; double l = g * 3.875 / (m / 62.14); cout << "The fuel consumption is " << l << "L/100Km."; cin.get(); cin.get(); return 0; }
运行结果如下:
本题不算难,是第六题的相反过程,程序如下:
//ex6.cpp - - l/100km convert to mpg #include<iostream> int main() { using namespace std; const double m_per_100km = 62.14; const double l_per_g = 3.875; double l; // mlie ,garlon cout << "Enter the fuel comsuption per 100 km: "; cin >> l; double m = m_per_100km / (l / l_per_g); cout << l <<" L/100km = " << m << " mpg" <<endl; cin.get(); cin.get(); return 0; }
运行结果如下: