栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ Primer plus(第六版)中文版 第三章练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ Primer plus(第六版)中文版 第三章练习

一、复习题

1、整数有很多,如果将无限大的整数看作很大,则不可能用有限的计算机内存来表示所有的整数。因此,语言只能表示所有整数的一个子集。C++提供好几种整型,这样便能够根据程序的具体要求选择最合适的整型。

2、 a) short Value=80;

b) unsigned int Value=42110;

c) long long Value=3000000000; 或 unsigned long Value=3000000000;(建议使用这个)

3、C++本身并不能限制某个类型是否超出了它类型的范围,但是可以用 limits 头文件来看某种类型具体的范围。

4、33L和33这两个都是数值型常量。
  33L强制为长整数long型,33系统会识别为int整型。
  在数值上,两者相等,只是存储上,33根据编译器不同可能为2字节也可能为4字节,33L则一定为4字节。

5、这两条语句并不真正等价,虽然对于某些系统来说,它们是等效的。最重要的是,只有在使用ASCII码上的系统上,第一条语句是将ASCII编码值设置为字母A,而第二条语句还可用于使用其他编码的系统。其次,65是一个int常量,而'A'是一个char常量。

6、

char c = 88;
cout << c << endl;         // char type prints as character
cout.put(char{ 88 });       // put() prints char as character
cout << char{88} << endl;  // new-style type cast value to char
cout << (char)88 << endl;  // old-style type cast value to char

7.这种情况也还是得视系统而定,也就是说取决于两种类型的长度。如果 long 为 4 个字节,则没有损失,因为占 4 个字节的 long 的最大值为 2147483647,即有十位数,而 double 提供了至少十三位有效数字,这种情况下没有舍入误差。但是 long long 类型提供了 19 位数字,这超过了 double 的有效位数,故这种情况下有舍入误差。

8.a) 74, b) 4, c) 0, d) 4.5, e) 3。

9、

#include 
using namespace std;

int main()
{
	double x1, x2;
	int y;
	cin >> x1 >> x2;
	y = (int)( x1 + x2 );//或者使用y=int(x1+x2),但是编译器会报错
	cout << y << endl;
	return 0;
}

10

a.int

b.float

c.char

d.char32_t//auto crat = U'/U00002155' 字符常量中字符过多(编写时提示)

U再字符串前面代表 宽字符字符在串 即UNICODE 字符串。UTF-8/UTF-16 ...
等等
类型 char32_t 的宽字符文本,例如 U'a'

e.double

  •  后面的2.5如果后面不加f的话,就是个double类型的值
  •  /的两个操作数一个是float一个是double,float向double方向做类型提升(Type promotion)
  •  两个double的结果还是double,所以auto 的类型推断就认为fract是个double了
二、编程练习

1、

//一英尺等于12英寸,一英寸等于0.083英尺
#include
using namespace std;

int main()
{
	 constexpr auto  F = 0.083;
	double high;
	
	cout << "请输入自己的身高" << endl;
	cin >> high;
	cout << "您的身高转化为英尺为" << endl;
	cout << F * high << endl;
	system("pause");
	return 0;
}

2、

#include
using namespace std;

int main()
{       
	constexpr auto yczhm = 0.0254;
	constexpr auto yczhyc = 12;
	constexpr auto qgzhb = 0.0254;
	    int yhigh, chigh ,weight ;
		double zhigh, kweight,IBM;

		cout << "请以几英尺几英寸的方式输入你的身高" << endl;
	    cout << "你的身高有几英尺" << endl;
		cin >> yhigh;
		cout << "你的身高有几英寸" << endl;
		cin >> chigh;
		cout << "你的体重是多少磅" << endl;
		cin >> weight;
		zhigh = yczhm *(yczhyc * yhigh + chigh);
		kweight = weight * 1 / qgzhb;
		IBM = kweight / zhigh / zhigh;
		cout << "你的体重指数为:"<< IBM << endl;
		return 0;
}

3.

#include
using namespace std;

int main()
{
	auto degrees = 0, minutes = 0, secends = 0;
	float final=0;
	constexpr int zh = 60;

	cout << "Enter a latitude in degree ,minutes,and seconds:" << endl;
	cout << "First,enter the degrees: " << endl;
	cin >> degrees;
	cout << "Next,enter the minutes of arc: " << endl;
	cin >> minutes;
	cout << "Finally,enter the secends of arc: " << endl;
	cin >> secends;
	final = degrees + minutes / zh + secends / zh / zh;
	cout << degrees << " degrees, " << minutes << " minutes, " << secends << " secends =  " << final << " degrees " << endl;
	return 0;
}

5.

#include
#include
using namespace std;

int main()
{
 
	long long worldpopulation, USpopulation ;
	cout << "Enter the world's population : " << endl;
	cin >> worldpopulation;
	cout << "Enter the populartion of the US: " << endl;
	cin >> USpopulation;
	cout << "The population of the US is " <<  100 *( USpopulation*1.)/ (worldpopulation*1.) << "%" << endl;

	return 0;

}

6.

#include
#include
using namespace std;

int main()
{
 
	int Facter, mileage1=0, quantity1=0, mileage2=0, quantity2=0;
	cout << "选择输入驱车里程和使用汽油量,选择以英里为单位输入距离键入:0,选择以公里为单位输入距离键入:1" << endl;
	cin >> Facter;
	if (Facter == 1)
	{   

		cout << "请输入驱车里程(英里): " << endl;
		cin >> mileage1;
		cout << "请输入使用汽油量(加仑): " << endl;
		cin >> quantity1;
		cout << "您的车耗油量为一加仑的里程: " << mileage1 / quantity1 << endl;
	}
	else
	{
		cout << "请输入驱车里程(公里): " << endl;
		cin >> mileage2;
		cout << "请输入使用汽油量(升): " << endl;
		cin >> quantity2;
		cout << "您的车每一百公里的耗油量为: " <<100* mileage2 / quantity2 << endl;
	}
	return 0;
}

7.

#include
using namespace std;


int main()
{
	double x = 0;//输入的汽油量
	constexpr double y1 = 62.14, y2 = 3.875;
	cout << "按照欧洲风格输入汽车的耗油量(每一百公里消耗的汽油量)" << endl;
	cin >> x;
	cout <<  x/y1/y2;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号