- 这退格好使欸
#include <iostream>
int main()
{
using namespace std;
cout << "Enter your height in inches (enter an integer):___\b\b\b";//英寸
int inches;
const int inch_per_feet = 12;//1英尺=12英寸
cin >> inches;
cout << inches << " inches is " << inches / inch_per_feet << " feet and " << inches % inch_per_feet << " inches.\n";
return 0;
}
Enter your height in inches (enter an integer):98_
98 inches is 8 feet and 2 inches.
#include <iostream>
int main()
{
using namespace std;
const int inch_per_feet = 12;
const float meter_per_inch = 0.0254;
const float pound_per_kg = 2.2;
cout << "Enter your height in feet and inches, first enter the number of feet:\n";
int height_feet, height_inch, height_inches;
float weight_pound, weight_kg, height_meter;
cin >> height_feet;
cout << "Then the number of inches:\n";
cin >> height_inch;
height_inches = height_feet * inch_per_feet + height_inch;
height_meter = height_inches * meter_per_inch;
cout << "Your height is " << height_inches << " inches.\n";
cout << "Now enter your weight in pounds:\n";
cin >> weight_pound;
weight_kg = weight_pound / pound_per_kg;
cout << "Your BMI (Body Mass Index) is " << weight_kg / (height_meter * height_meter) << endl;
return 0;
}
Enter your height in feet and inches, first enter the number of feet:
5
Then the number of inches:
2
Your height is 62 inches.
Now enter your weight in pounds:
127
Your BMI (Body Mass Index) is 23.2772
#include <iostream>
int main()
{
using namespace std;
cout << "Enter a latitude in degrees, minutes, and seconds:\n";
float degrees, minutes, seconds;
const int min_per_deg = 60;
const int sec_per_min = 60;
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;
float deg;
deg = degrees + (seconds / sec_per_min + minutes) / min_per_deg;
cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << deg <<
" degress.\n";
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, 19 seconds = 37.8553 degress.
#include <iostream>
int main()
{
using namespace std;
long long secs;
int day, hour, minute, second, day_, hour_;
const int hours_per_day = 24;
const int minutes_per_hour = 60;
const int seconds_per_minute = 60;
cout << "Enter the number of seconds:";
cin >> secs;
day = secs / (hours_per_day * minutes_per_hour * seconds_per_minute);
day_ = secs % (hours_per_day * minutes_per_hour * seconds_per_minute);
hour = day_ / (minutes_per_hour * seconds_per_minute);
hour_ = day_ % (minutes_per_hour * seconds_per_minute);
minute = hour_ / seconds_per_minute;
second = hour_ % seconds_per_minute;
cout << secs << " seconds = " << day << " days, " << hour
<< " hours, " << minute << " minutes, " << second << " seconds\n";
return 0;
}
Enter the number of seconds:31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
5 错误示范(类型转换错误,还没写乘以100)
#include <iostream>
int main()
{
using namespace std;
long long global, american;
cout << "Enter the world's population:";
cin >> global;
cout << "Enter the population of the US:";
cin >> american;
cout << "The population of the US is " << float(american / global) << "% of the world population.\n";
return 0;
}
Enter the world's population:6898758899
Enter the population of the US:310783781
The population of the US is 0% of the world population.
正确版本,只改return前面最后一句
cout << "The population of the US is " << float(american) / float(global) * 100 << "% of the world population.\n";
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.