C++ Primer Plus 第三章

//2

对于全局变量 const double pound = x,其中x的取值要是小数形式,在编译下分数不能转换成小数,结果出错。

问题:在vs2012编译环境下,浮点数的分数表示。

//4

#include <iostream>

using namespace std;


int main()
{
	long second = 0;
	int minute = 0;
	int hour = 0;
	int day = 0;
	int xsecond = 0;

	cout << "Enter the number of seconds:    \b\b\b";
	cin >> second;

	day = second / (24*60*60);
	hour = (second % (24*60*60)) / (60*60);
	minute = (second % (60*60)) / 60;
	xsecond = second % 60;

	cout << second << " seconds" << " = " << day << " days, " << hour << " hours, " << minute << " minutes, " <<  xsecond << " seconds."<<endl;

	system("pause");
	return 0;
	
}

//5

#include <iostream>

using namespace std;


int main()
{
	long long  world = 0;
	long long US = 0;
	double rate;
	cout << "Enter the world's population:    \b\b\b";
	cin >> world;
	cout << "Enter the  population of the US:    \b\b\b";
	cin >> US;
	rate = double (US)/(world);

	cout << "The population of the US is " << rate*100 << "%of the world population." << endl;

	system("pause");
	return 0;
	
}

注:rate = double (US)/(world); 要加double类型和括号。

//6

#include <iostream>

using namespace std;


int main()
{
	double mile = 0;
	double galoon = 0;
	double per_gal_mile = 0;

	cout << "Enter your miles:   \b\b";
	cin >> mile;
	cout << "Enter the galoon of the oil used:   \b\b";
	cin >> galoon;
	per_gal_mile = double (mile)/(galoon);

	cout << "When the oil used is one galoon,the car drive " << per_gal_mile << " miles." << endl;

	system("pause");
	return 0;
	
}


//7

尴尬题没读懂!!!我是一个假的汽车专业的学生。。

猜你喜欢

转载自blog.csdn.net/Fiona_Deng/article/details/81027237