C++PrimerPlus编程练习__第三章

#include "pch.h"


/*
 第三章  处理数据
	**	面向对象编程(OOP)的本质是设计并扩展自己的数据类型
	**	内置的C++类型分类两组:基本类型和复合类型
	**	本章介绍基本类型,即整数和浮点数
 3.1	简单变量
	**	程序必须记录3个基本属性
		**	信息将储存在哪里
		**	要存储什么值
		**	存储何种类型的信息
	3.1.1	变量名
		**	名称中只能使用字母、数字、下划线
		**	名称的第一个字符不能是数字
		**	区分大写字符和小写字符
		**	不能将C++关键字用作名称
		**	C++对名称的长度没有限制
	3.1.2	整形		
	3.1.3	整形short、int、long和long long
		位与字节
			**	1位指1或者0的二进制,计算机内存的基本单元
			**	字节通常指8位内存单元
			**	1KB 等于 1024字节
			**	C++对字节的定义是 至少能够容纳的基本字符集的相邻位组成,取值数目必须大于或等于字符数目
			**	C++字节通常包含8位
			**	Unicode 有些实现可能使用16位或32位的字节
		**	short		16位	2个字节		最大	32767				3万
		**	int			32位	4个字节		最大	2147483647			21亿
		**	long		32位	4个字节		最大	2147483647			21亿
		**	long long	64位	8个字节		最大	9223372036854775807	9百亿亿
		实际上,short 是 short int 的简称,long 是long int 的简称,只不过一般不使用这么长的形式
		头文件climits(老式实现为limits.h)包含了关于整形限制的信息
		C++11 初始化方式
			**	int a = { 1 };
			**	int a{ 1 };
			**	int a = {};//被初始化为0
	3.1.4	无符号类型
		要创建无符号版本的基本整形,只需使用关键字 unsigned
		**	unsigned 本身是 unsigned int 的缩写
	3.1.5	选择整形类型
		**	通常仅当有大型整数数组时,才有必要使用 short
	3.1.6	整形字面值
		**	第一位为1-9				十进制
		**	第一位为0第二位为1-7	八进制
		**	前两位为0x或者0X		十六进制
		控制符oct dec hex 分别用于指示 八进制 十进制 十六进制的整数显示 
		cout << hex 只是修改cout显示整数的方式
	3.1.7	C++如何确定常量的类型
		**	C++提供用于表示类型long的后缀l和L,long long 的后缀ll和LL,无符号型unsigned long 的后缀 ul 或者 UL ,无符号型unsigned long long 的后缀 ull Ull uLL 和 ULL
	3.1.8	char 类型:字符和小整数
		虽然char 常用来处理字符,但也可以用作比short更小的整型
		**	字符0的编码为48
		**	字符A的编码为65 
		**	字符a的编码为97
		**	cout.put()是一个成员函数 用来输出字符
		**	cout << '$'; 打印的是ASCII码,但是Relase 2.0之后,C++将字符常量存储为char类型而不是int型,所以现在可以正确处理字符常量
		**	cout.put('s');打印的是字符$
		**	\a	振铃
		**	\n	换行
		**	\b	退格
		**	\t	水平制表符
		**	\v	垂直制表符
		**	char 在默认情况下既不是没有符号,也不是有符号,是否有符号由C++实现来决定
		**	unsigned char	0-255
		**	signed char		-127-127
		**	8位char 可以表示基本字符集,而wchar_t 可以表示扩展字符集,可通过加上前缀L表示宽字符常量和宽字符串
		**	C++11 新增char16_t 和 char32_t可通过加上前缀 u 和 U 表示宽字符常量和宽字符串
	3.1.9	bool 类型
		**	任何数字值或指针值都可以被隐式转换为bool值非0为true,0为fasle
 3.2	const 限定符
		**	应初始化
 3.3	浮点数
	3.3.1	书写浮点数
	3.3.2	浮点类型
		**	float double 和 long double 
		**	float		32位		指数范围是-37到37
		**	double		64位
		**	long double	80,96,128
		**	可以从cfloat 或float.h中找到系统限制
		**	ostream 的方法 setf():迫使输出使用定点表示法,以便更好的了解精度,防止程序把较大的值转换位E表示法,并使程序显示到小数点后6位
		**	通常cout 会删除结尾的0,cout.setf()会覆盖这种行为
	3.3.3	浮点常量
		**	通常cout 会删除结尾的0,cout.setf()会覆盖这种行为
	3.3.4	浮点数的优缺点
		**	浮点运算速度通常比整数满,且精度降低
 3.4	C++算术运算符
		**	+ - * / %
	3.4.1	运算符优先级和结合性
	3.4.2	除法分支
	3.4.3	求模运算符
	3.4.4	类型转换
	3.4.5	C++11中的 auto 声明
 3.5	总结
 
 
 */



#include <iostream>
#include <climits>

using namespace std;

int main_3_0()
{
	cout << "hello world" << endl;
	short a = SHRT_MAX;
	int b = INT_MAX;
	long c = LONG_MAX;
	long long d = LLONG_MAX;
	cout << "short:" << sizeof(a) << endl;
	cout << "short:" << sizeof(short) << endl;
	cout << "short:" << a << endl;
	cout << "int:" << sizeof(b) << endl;
	cout << "int:" << sizeof(int) << endl;
	cout << "int:" << b << endl;
	cout << "long:" << sizeof(c) << endl;
	cout << "long:" << sizeof(long) << endl;
	cout << "long:" << c << endl;
	cout << "long long:" << sizeof(d) << endl;
	cout << "long long:" << sizeof(long long) << endl;
	cout << "long long:" << d << endl;
	a += 100;
	cout << "short:" << a << endl;
	int f = 42;
	int g = 0X2a;
	cout << "f:" << f <<endl;
	cout << "g:" << g <<endl;
	cout << hex;
	cout << "f:" << f << endl;
	cout << "g:" << g << endl;
	cout << oct;
	cout << "f:" << f << endl; 
	cout << "g:" << g << endl;
	cout << dec;
	cout << "a" << endl;
 	cout << "\n";
 	cout << "\tdef" << endl;
	cout << "abc____\b\b\b\b" ;
 	int h;
	cin >> h;
	cout << h << "\a"<<endl;
	return 0;
}

//1、编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸,该程序使用下划线字符来指示输入位置,另外使用一个 const 符号常量来表示转换因子
int main_3_1()
{
	const int factor = 12;
	int inch = 0;
	cout << "Enter the number of height:___\b\b\b";
	cin >> inch;
	cout << "Your height is: " << inch / 12 << " foot " << inch % 12 << " inch" <<endl;
	return 0;
}

//2、编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重,(使用三个变量来存储这些信息。)该程序报告给BMI(Body Mass Index,体重指数)。为了
//	计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将英寸为单位的身高转换为以米为单位的身高(1英寸 = 0.0254 米)。然后以磅为单位的体重转换为以千克为单
//	位的体重(1千克 = 2.2磅)。最后,计算相应的BMI--体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。

int main_3_2()
{
	int foot, inch, weight;
	cout << "Enter the foot: ";
	cin >> foot;
	cout << "Enter the inch: ";
	cin >> inch;
	cout << "Enter the weight: ";
	cin >> weight;
	const int a = 12;
	const float b = 0.0254f;
	const float c = 2.2f;
	cout << "Your height is : " << (foot * a + inch) * b << " Meter" <<endl;
	cout << "Your body weight is : " << weight * c << endl;
	cout << "Your BMI is :" << (foot * a + inch) * b / (weight * c) << endl;
	return 0;
}

//3、编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。
//下面是该程序运行时的情况:
//Enter a latitude in degrees,minutes,and seconds:
//First,enter the degrees:37 
//Next,enter the minutes of arc:51
//Finally,enter the seconds of arc:19
//37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

int main_3_3()
{
	const int a = 60;
	const int b = 60;
	float du, fen, miao;
	cout << "Enter a latitude in degrees,minutes,and seconds:" << endl;
	cout << "First,enter the degrees: ";
	cin >> du;
	cout << "Next,enter the minutes of arc:";
	cin >> fen;
	cout << "Finally,enter the seconds of arc:";
	cin >> miao;
	cout << du << " degrees, " << fen << " minutes, " << miao << " seconds = " << du + fen / 60 + miao / 60 / 60 << " degrees";
	return 0;
}

//4、编写一个程序,要求用户以整数的方式输入秒数(使用long或 long long 变量存储),然后天、小时,分钟和秒的方式显示这段时间,使用符号常量来表示每天
//有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
//Enter the number of seconds:31600000
//31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

int main_3_4()
{
	int long  miao,fen,shi,tian;
	cout << "Enter the number of seconds: ";
	cin >> miao;
	tian = miao / (60 * 60 * 24);
	shi = miao % (60 * 60 * 24) / (60 * 60);
	fen = miao % (60 * 60 * 24) % (60 * 60) / 60;
	cout << miao << " seconds  = " << tian << " days, " << shi << " hours, " << fen << " minutes, " << miao - tian * 60 * 60 * 24 - shi * 60 * 60 - fen * 60 << " seconds" << endl;
	return 0;
}

//5、编写一个程序,要求用户输入全球当前人口和美国当前人口(或其他国家的人口),将这些信息存储在long long 变量中,并让程序显示美国(或其他国家)的人口占全
//球人口的百分比,该程序的输出应与下面类似:
//Enter the world's population:6898758899
//Enter the population of the US:310783781
//The population of the US is 4.50492% of the world population.

int main_3_5()
{
	long long worldpop, USpop;
	cout << "Enter the world's population:";
	cin >> worldpop;
	cout << "Enter the population of the US:";
	cin >> USpop;
	cout << "The population of the US is " << double((double(USpop) / worldpop) * 100) << "% of the world population." << endl;
	return 0;
}

//6、编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程,如果愿意,也可以让程序要求用户以公里为单
//位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果---即每100公里的耗油量(升)。

int main_3_6()
{
	int mile,gallon;
	cout << "Enter the mile:";
	cin >> mile;
	cout << "Enter the gallon:";
	cin >> gallon;
	cout << "1 gallon is mile: " << mile * 100 / float(gallon) << endl;
	return 0;
}

//7、编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量---每加仑多少英里。注意,除
//了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km

int main_3_7()
{
	float liang;
	cout << "Enter the liang:";
	cin >> liang;
	cout << "liang is: " << 62.14f / (liang / 3.875f) << "gallon/mile" << endl;
	return 0;
}

  

posted @ 2019-07-27 15:57  小喜蛋7  阅读(347)  评论(0编辑  收藏  举报