C++ Primer Plus第六版编程练习---第3章 处理数据
1.
#include <iostream>
const int CONVER_FACTOR = 12;
int main(int argc, char* argv[])
{
int height = 0;
std::cout << "Pleas enter your height with inch_ ";
std::cin >> height;
if(0 > height)
{
std::cout << "Pleas enter your height with inch_ ";
std::cin >> height;
}
std::cout << "You are " << height/CONVER_FACTOR << " foots and " << height%CONVER_FACTOR << " inches." << std::endl;
return 0;
}
2.
#include <iostream>
const int FOOT_TO_INCH = 12;
const double INCH_TO_METRE = 0.0254;
const double KILOGRAM_TO_POUND = 2.2;
int main(int argc, char* argv[])
{
double weight = 0;
std::cout << "Pleas enter your weight with pound: ";
std::cin >> weight;
double heightFoot = 0;
double heightInch = 0;
std::cout << "Pleas enter your height with foot: ";
std::cin >> heightFoot;
std::cout << "Pleas enter your height with inch: ";
std::cin >> heightInch;
if(0 > weight || 0 > heightFoot || 0 > heightInch)
{
std::cout << "Pleas enter your weight with pound: ";
std::cin >> weight;
std::cout << "Pleas enter your height with foot: ";
std::cin >> heightFoot;
std::cout << "Pleas enter your height with inch: ";
std::cin >> heightInch;
}
heightInch = heightInch + (heightFoot*FOOT_TO_INCH);
std::cout << "You are " << heightInch << " inches and " << weight << " pounds." << std::endl;
double weightKilogram = weight/KILOGRAM_TO_POUND;
double heightMetre = heightInch*INCH_TO_METRE;
double bodyMassIndex = weightKilogram/pow(heightMetre,2);
std::cout << "You BMI is: " << bodyMassIndex << std::endl;
return 0;
}
3、
#include <iostream>
const int DEGREE_TO_MINUTE = 60;
const int MINUTE_TO_SECOND = 60;
int main(int argc, char* argv[])
{
double degrees = 0;
double minutes = 0;
double seconds = 0;
std::cout << "Enter a latitude in degrees, minutes, and seconds: " << std::endl;
std::cout << "First, enter the degrees: ";
std::cin >> degrees;
std::cout << "Next, enter the minutes of arc: ";
std::cin >> minutes;
std::cout << "Finally, enter the seconds of arc: ";
std::cin >> seconds;
std::cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds= "
<< (degrees + minutes/DEGREE_TO_MINUTE + seconds/(MINUTE_TO_SECOND*DEGREE_TO_MINUTE)) << " degrees" << std::endl;
return 0;
}
4、
#include <iostream>
const int DAY_TO_HOUR = 24;
const int HOUR_TO_MINUTE = 60;
const int MINUTE_TO_SECOND = 60;
int main(int argc, char* argv[])
{
long long secondsTime = 0;
std::cout << "Enter the number of seconds: ";
std::cin >> secondsTime;
long yers = secondsTime/(DAY_TO_HOUR*HOUR_TO_MINUTE*MINUTE_TO_SECOND);
long hours = (secondsTime%(DAY_TO_HOUR*HOUR_TO_MINUTE*MINUTE_TO_SECOND))/(HOUR_TO_MINUTE*MINUTE_TO_SECOND);
long minutes = (secondsTime%(HOUR_TO_MINUTE*MINUTE_TO_SECOND))/MINUTE_TO_SECOND;
long seconds = secondsTime%MINUTE_TO_SECOND;
std::cout << secondsTime << " seconds= " << yers << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds"<< std::endl;
return 0;
}
5、
#include <iostream>
int main(int argc, char* argv[])
{
long long worldPopulation = 0;
long long chinesPopulation = 0;
std::cout << "Enter the world's population: ";
std::cin >> worldPopulation;
std::cout << "Enter the population of the China: ";
std::cin >> chinesPopulation;
std::cout << "The population of the China is "<< chinesPopulation*100/worldPopulation << "% of the world population."<<std::endl;
return 0;
}
6、
#include <iostream>
int main(int argc, char* argv[])
{
long long mileage = 0;
long long oilConsum = 0;
std::cout << "Enter the mileage(mile): ";
std::cin >> mileage;
std::cout << "Enter the oil consumption(gallon): ";
std::cin >> oilConsum;
std::cout << "One gallon can run "<< (double)mileage/oilConsum << " miles" <<std::endl;
return 0;
}
7、
#include <iostream>
int main(int argc, char* argv[])
{
double oilConsumAtEurope = 0;
double oilConsumAtUS = 0;
std::cout << "Enter the oilConsumAtEurope(liters of oil for 100 kilometre): ";
std::cin >> oilConsumAtEurope;
oilConsumAtUS = 62.14/(oilConsumAtEurope*3.875);
std::cout << "It is "<< oilConsumAtUS << " mpg" <<std::endl;
return 0;
}
浙公网安备 33010602011771号