天天看点

第三章练习题(部分)第五题

第一题

#include<iostream>
using namespace std;
const int trans = 12;

int main() {
    int height;
    cout << "Please enter the height (in inches):___\b\b\b ";
    cin >> height;
    cout << "Your height is " << height / trans << " inches " <<
        height % trans << " feets." << endl;
    cin.get();
    return 0;
}
           

第二题

#include<iostream>
using namespace std;
const int Feet_inches = 12;
const float Inches_meter = 0.0254;
const float Kg_pound = 2.2;

int main() {
    float feets, inches, pounds;
    cout << "Please enter your height(feets and inches):";
    cin >> feets >> inches;
    cout << "Please enter your weight(pounds): ";
    cin >> pounds;

    float total_inches, meter, kg, BMI;
    total_inches = feets * Feet_inches + inches;
    meter = total_inches * Inches_meter;
    kg = pounds * Kg_pound;
    BMI = kg / (meter * meter);
    cout << "Your BMI is " << BMI << endl;
    system("pause");
    return 0;
}
           

第三题

#include <iostream>
int main()
{
	using namespace std;
	const int degree2min=60;
	const int min2sec = 60;
	float degrees,mins,secs;
	cout << "Enter a latitude in degrees, minutes, and seconds:\n" ;
	cout << "First, enter the degrees:" ;
	cin >> degrees; 
	cout << "Next, enter the minutes of arc:" ;
	cin >> mins; 
	cout << "Finally, enter the seconds of acr:" ;
	cin >> secs; 
	cin.get();
	float value = degrees + mins/degree2min+secs/(degree2min*min2sec);
	cout<< degrees << " degrees, "<< mins <<" minutes, "<< secs << " seconds = " << value <<" degrees"<<endl;
	cin.get();
	return 0;
}
           

int型的a和b即使使用float(a/b)进行强制转换也是先按int型计算后转为浮点型。

第四题

可以使用%求余,对余数部分再进行除法运算

#include <iostream>
int main()
{
	using namespace std;
	const int hourPerDay = 24;
	const int minsPerHour = 60;
	const int secsPerMins = 60; 
	long secs;
	cout << "Enter the number of seconds:";
	cin >> secs;
	cin.get();
	int Days = secs/hourPerDay/secsPerMins/minsPerHour;
	int Hours =secs/secsPerMins/minsPerHour-Days*hourPerDay;
	int Mins = secs/secsPerMins-(Days*hourPerDay+Hours)*minsPerHour;
	int Secs = secs - ((Days*hourPerDay+Hours)*minsPerHour+Mins)*secsPerMins;
	cout<< secs << " seconds = "<< Days <<" days, "<< Hours << " hours, " << Mins << " minutes, " << Secs << " seconds"<<endl;
	cin.get();
	return 0;
}
           

第五题

#include <iostream>
int main()
{
	using namespace std;
	long long US,Total;
	cout << "ENter the world's population:";
	cin >> Total;
	cout << "Enter the population of the US:";
	cin >> US;
	cin.get();
	cout << "The population of the US is "<< double(US)/double(Total)*100 <<"% of the world population."<<endl;
	cin.get();
	return 0;
}
           

继续阅读