第三章练习题(部分)

第一题

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值