C++ Primer Plus编程练习

C++ Primer Plus编程练习

Chapter 3

1、将身高(单位为英寸) 转化成几英尺几英寸的格式

#include <iostream>

const int inch_feet = 12;

int main()
{
	using namespace std;
	int height;
	cout << "Please enter your height in inches:___\b\b\b";
	cin >> height;
	int feet = height / inch_feet;
	int inch = height % inch_feet;
	cout << "Your height is "<<feet<<" feet,and "<<inch<<" inches\n";
	return 0;
}

2、计算体重指数BMI的值

#include <iostream>

const int inch_feet = 12;
const double feet_meter = 0.0254;
const double kilo_pound = 2.2;

int main()
{
	using namespace std;
	int inch;                                    //英寸  
	int foot;                                    //英尺
	double pound;                                //磅
	cout << "Please enter your height(feet and inches)" << endl;
	cout << "Please enter feet of your height:___\b\b\b";
	cin >> foot;
	cout << "Please enter inches of your height:___\b\b\b";
	cin >> inch;
	cout << "Please enter your weight(pound):___\b\b\b";
	cin >> pound;
	double inches = foot * inch_feet + inch;     //总英寸(inch)
	double meteres = inches * feet_meter;        //身高(米)
	double kilo = pound / kilo_pound;            //体重(kg)
	double BMI = kilo / (meteres *meteres);
	cout << "Your BMI:" << BMI << endl;
	return 0;
}

3、将“度-分-秒”格式的纬度转换成“度”格式的纬度

#include <iostream>

const double factor = 60;
using namespace std;

int main()
{
	double degrees;                //度  
	double minutes;                //分
	double seconds;                //秒
	cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
	cout << "First, enter the degrees: __\b\b";
	cin >> degrees;
	cout << "Next, enter the minutes of arc: __\b\b";
	cin >> minutes;
	cout << "Finally, enter the seconds of arc: __\b\b";
	cin >> seconds;
	double result = degrees + minutes / factor + seconds / (factor * factor);
	cout <<degrees<< " degrees,"<<minutes<<" minutes,"<<seconds<<" seconds = " << result <<" degrees"<< endl;
	return 0;
}

4、将“秒”格式的时间转换成“天-时-分-秒”格式的时间

#include <iostream>

const int d_h = 24;
const int h_m = 60;
const int m_s = 60;
using namespace std;

int main()
{
	long long total_seconds = 0;
	int days ;
	int hours ;
	int minutes ;
	int seconds ;

	cout << "Enter the total number of seconds: ";
	cin >> total_seconds;

	days = total_seconds / (d_h * h_m * m_s);
	hours = ((total_seconds % (d_h * h_m * m_s)) / (h_m * m_s));
	minutes = ((total_seconds % (h_m * m_s)) / m_s);
	seconds = (total_seconds % m_s);
	cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
	return 0;
}
发布了46 篇原创文章 · 获赞 19 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_40520596/article/details/102058429