c++ primer plus (第六版) 2、3章答案
自己在平板上手敲的代码(部分是在VS上敲的看起来会清楚一点),已经运行过了
//3.4 秒数转换//
#include<iostream>
using namespace std;
const int DAY_TO_HOUR = 24;
const int HOUR_TO_MINUTE = 60;
const int MINUTE_TO_SECOND = 60;
int main()
{
long long seconds;
int days, hours, minutes;
cout << "Enter the number of seconds:";
cin >> seconds;
cout << seconds << " seconds = ";
days = seconds / (DAY_TO_HOUR * HOUR_TO_MINUTE * MINUTE_TO_SECOND);
seconds = seconds % (DAY_TO_HOUR * HOUR_TO_MINUTE * MINUTE_TO_SECOND);
hours = seconds / (HOUR_TO_MINUTE * MINUTE_TO_SECOND);
seconds = seconds % (DAY_TO_HOUR* MINUTE_TO_SECOND);
minutes = seconds / MINUTE_TO_SECOND;
seconds = seconds % MINUTE_TO_SECOND;
cout << days << "days, " << hours << " hours," << minutes << " minutes, " << seconds << " seconds." << endl;
return 0;
}
//3.5 美国人口比例//
#include<iostream>
using namespace std;
int main()
{
long long global_amount , american_amount;
double population_percent;
cout << "Enter the world's population: ";
cin >> global_amount;
cout << "Enter the population of US: ";
cin >> american_amount;
population_percent = 100 * (double)american_amount / (double)global_amount;
cout << "The population of the US is " << population_percent << "% of the world population.";
return 0;
}
//3.6 汽油损耗单位换算//
#include<iostream>
using namespace std;
int main()
{
float distance_in_mile, distance_in_km;
float fuel_in_gallon, fuel_in_litre;
float fuel_consume;
cout << "Enter the distance in miles: ";
cin >> distance_in_mile;
cout << "Enter the fuel consume in gallon: ";
cin >> fuel_in_gallon;
fuel_consume = distance_in_mile / fuel_in_gallon;
cout << "The fuel consume is " << fuel_consume << " mpg(mile/gallon)." << endl;
cout << "Enter the distance in kilometer: ";
cin >> distance_in_km;
cout << "Enter the fuel consume in litre: ";
cin >> fuel_in_litre;
cout << "The fuel consume is " << fuel_consume << "L/100KM." << endl;
return 0;
}
//3.7 耗油量单位转换//
#include<iostream>
using namespace std;
const float GALLON_TO_LITRE = 3.875;
const float HKM_TO_MILE = 62.14;
int main()
{
float fuel_consume_eur, fuel_consume_us;
cout << "Enter the fuel consume in europe(1/100km): ";
cin >> fuel_consume_eur;
fuel_consume_us = HKM_TO_MILE / (fuel_consume_eur / GALLON_TO_LITRE);
cout << "The fuel consume is " << fuel_consume_eur << "L/100KM." << endl;
cout << "The fuel consume is " << fuel_consume_us << " mpg(mile/gallon)." << endl;
return 0;
}
🆗敲完了,csdn1上的代码片看起来还挺好看的。冲冲冲!!!2