第三章 处理数据
#include <iostream>
using namespace std;
const int To_inch = 12 ;
int main ( )
{
int inch;
cout << "Please enter your height (inch): ___\b\b\b" ;
cin >> inch;
int feet = inch / To_inch;
inch % = To_inch;
cout << "Height: " << feet << " feet " << inch << " inch" << endl;
return 0 ;
}
#include <iostream>
using namespace std;
const double InchToMeter = 0.0254 ;
const double PoundsToKg = 1 / 2.2 ;
int main ( )
{
double feet, inch, height, weight;
cout << "Height: ___ feet ___ inches" << endl;
cin >> feet;
cin >> inch;
cout << "Weight: ___ pounds" << endl;
cin >> weight;
inch + = feet* 12 ;
height = inch * InchToMeter;
weight * = PoundsToKg;
cout << "BMI = " << weight / ( height* height) << endl;
return 0 ;
}
#include <iostream>
#include <cstdio>
using namespace std;
int main ( )
{
double result;
int deg, minutes, sec;
cout << "Enter a latitude in degrees, minutes and seconds:" << endl;
cout << "First, enter the degrees: __\b\b" ;
cin >> deg;
cout << "Next, enter the minutes of arc: __\b\b" ;
cin >> minutes;
cout << "Finally, enter the seconds of arc: __\b\b" ;
cin >> sec;
result = deg* 3600 + minutes* 60 + sec;
result / = 3600 ;
printf ( "%d degrees, %d minutes, %d seconds = %.4lf degrees\n" , deg, minutes, sec, result) ;
return 0 ;
}
#include <iostream>
#include <cstdio>
using namespace std;
const int HrsPerDay = 24 ;
const int MinsPerHr = 60 ;
const int SecsPerMin = 60 ;
int main ( )
{
long long secs, seconds, days;
int hrs, mints;
cout << "Enter the number of seconds: " ;
cin >> secs;
seconds = secs;
days = secs/ ( SecsPerMin * MinsPerHr * HrsPerDay) ;
secs % = SecsPerMin * MinsPerHr * HrsPerDay;
hrs = secs/ ( SecsPerMin * MinsPerHr) ;
secs % = SecsPerMin * MinsPerHr;
mints = secs / SecsPerMin;
secs % = SecsPerMin;
printf ( "%lld seconds = %lld days, %d hours, %d minutes, %lld secs" , seconds, days, hrs, mints, secs) ;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
long long popult, pop_US;
cout << "The world's population: " ;
cin >> popult;
cout << "The population of US: " ;
cin >> pop_US;
cout << pop_US* 100 / ( double ) popult << "%" << endl;
return 0 ;
}
#include <iostream>
using namespace std;
int main ( )
{
double miles, gallon, km, liter;
cout << "Enter the distance (miles): " ;
cin >> miles;
cout << "Enter the gasoline usage (gallon): " ;
cin >> gallon;
cout << miles / gallon << " mile(s)/gallon" << endl;
cout << "Enter the distance (kilometers): " ;
cin >> km;
cout << "Enter the gasoline usage (liters): " ;
cin >> liter;
cout << liter* 100 / km << " liter(s)/100km" << endl;
return 0 ;
}
#include <iostream>
using namespace std;
const double KMtoMiles = 62.14 ;
const double GaltoLiter = 3.875 ;
int main ( )
{
double usage_EU;
cout << "Enter in the European Style: " ;
cin >> usage_EU;
cout << 1 / ( usage_EU / ( GaltoLiter* KMtoMiles) ) << "mpg" << endl;
}