C++ primer plus 课后练习题

第三章: 处理数据

        一、复习题

        1. 为了存储不同大小的整数 可以根据需求选择最合适的类型

        2. a. short x = 80;

            b. unsighed int x = 42110;

            c. long long x = 3000000000;

        3. C++没有提供自动防止超出整形限制的功能

        4. 33L的类型是long,而33的类型是int

        5. 只在某些情况下等价(只有在使用ASCII码的系统上)

        6. 1. 定义一个char类型的变量并将其赋值为88,随后打印该变量

            2. 将88进行强制类型转化,即(char) 88 , 再将其打印输出

        7. 他们都会导致误差 (准确的说,取决于这两个类型的长度)

        8. a. 74

            b. 4

            c. 0

            d. 4.5

            e. 3

        9. a. int res = (int) x1 + (int) x2;

            b. int res = x1 + x2;

        10.a.int

             b.float

             c.char

             d.char32_t

             e.double

        二、编程练习

        1.

#include <iostream>
using namespace std;

int main()
{
	const double cm_to_foot = 30.48;
	const double cm_to_inch = 2.54;


	cout << "请输入自己的身高: ___ (cm)" << endl;

	int height = 0;
	cin >> height;

	cout << "转化为英尺为: " << height / cm_to_foot << endl;
	cout << "转化为英寸为: " << height / cm_to_inch << endl;
	return 0;
}

            2.

#include <iostream>
using namespace std;

int main()
{
	const int inch_to_foot = 12;
	const double m_to_inch = 0.0254;
	const double pound_to_kg = 2.2;

	cout << "请输入自己的身高: (英尺)" << endl;
	int foot = 0;
	cin >> foot;

	cout << "请输入自己的身高: (英寸)" << endl;
	int inch = 0;
	cin >> inch;

	cout << "请输入自己的体重: (磅)" << endl;
	int weight = 0;
	cin >> weight;

	cout << "您的BMI为: " << (weight / 2.2) / ((foot * 12 + inch) * 0.0254 * (foot * 12 + inch) * 0.0254);
	return 0;
}

        3.

#include <iostream>
using namespace std;

int main()
{
	const double degree_to_min = 60.0;
	const double min_to_sec = 60.0;

	cout << "Enter a latitude in degrees,minutes,and seconds" << endl;
	cout << "First, enter the degrees: " << endl;
	int degree = 0;
	cin >> degree;

	cout << "Next, enter the minutes of arc: " << endl;
	int min = 0;
	cin >> min;

	cout << "Finally, enter the seconds of arc: " << endl;
	int sec = 0;
	cin >> sec;

	cout << degree << " degrees, " << min << " minutes, " << sec << " seconds = "
		<< degree + min / degree_to_min + sec / min_to_sec / degree_to_min << " degrees";

	return 0;
}

        4.(注:题目要求使用long类型存储变量second)

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter the number of seconds: " << endl;

	long second = 0;
	cin >> second;

	int days = second / (60 * 60 * 24);
	int hours = second % (60 * 60 * 24) / (60 * 60);
	int minutes = second % (60 * 60) / 60;
	int m_second = second % 60;

	cout << second << " seconds = " << days << " days, " << hours << " hours, "
		<< minutes << " minutes, " << m_second << " seconds" << endl;
	return 0;
}

        5.

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter the world's population: " << endl;
	long long world_population = 0;
	cin >> world_population;

	cout << "Enter the population of the US: " << endl;
	long long US_population = 0;
	cin >> US_population;

	cout << "The population of the US is " << (double)US_population / world_population * 100
		<< "\% of the world population" << endl;
	return 0;
}

        6.

#include <iostream>
using namespace std;

int main()
{
	cout << "请输入您想选择的单位制" << endl;
	cout << "1.英里-加仑" << endl;
	cout << "2.公里-升" << endl;
	int select = 0;
	cin >> select;
	while (true)
	{
		if (select == 1) {
			cout << "请输入你的英里数: " << endl;
			double foot = 0;
			cin >> foot;
			cout << "请输入你的加仑数: " << endl;
			double gallon = 0;
			cin >> gallon;
			cout << "您每英里耗油" << gallon / foot << "加仑" << endl;
			break;
		}
		else if (select == 2) {
			cout << "请输入你的公里数: " << endl;
			double km = 0;
			cin >> km;
			cout << "请输入你的升数: " << endl;
			double l = 0;
			cin >> l;
			cout << "您每公里耗油" << km/l << "升" << endl;
			break;
		}
		cout << "选择有误,请重新输入" << endl;
	}

	return 0;
}

        7.略

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值