Browse Source

上传文件至 '数据类型1'

Creamo 3 years ago
parent
commit
4a228592b3

+ 30 - 0
数据类型1/1、整形.cpp

@@ -0,0 +1,30 @@
+#include <iostream>
+
+//数据类型存在的意义:
+//给变量分配合适的内存空间
+
+int main1()
+{
+	//整形
+	//1、短整型
+	short num1 = 10;
+
+	//2、整形
+	int num2 = 10;
+
+	//3、长整型
+	long num3 = 10;
+
+	//4、长长整形
+	long long num4 = 10;
+	
+	std::cout << "num1 =" << num1 << std::endl;
+	std::cout << "num2 =" << num2 << std::endl;
+	std::cout << "num3 =" << num3 << std::endl;
+	std::cout << "num4 =" << num4 << std::endl;
+	
+
+	system("pause");
+
+	return 0;
+}

+ 27 - 0
数据类型1/2、sizeof.cpp

@@ -0,0 +1,27 @@
+#include <iostream>
+
+int main2()
+{
+	//整形 :short  2bytes  int  4bytes   long   4bytes   long long  8bytes  
+	//可以利用sizeof求出数据类型占用内存大小
+	//语法: sizeof(数据类型 / 变量)
+
+	short num1 = 10;
+	std::cout << "short占用的内存空间为:" << sizeof(short) << std::endl;
+
+	int unm2 = 10;
+	std::cout << "int占用的内存空间为:" << sizeof(int) << std::endl;
+
+	long num3 = 10;
+	std::cout << "long占用的内存空间为:" << sizeof(long) << std::endl;
+
+	long long num4 = 10;
+	std::cout << "long占用的内存空间为:" << sizeof(long) << std::endl;
+
+	//整形大小比较
+	//short < int <= long <= long long 
+
+	system("pause");
+
+	return 0;
+}

+ 32 - 0
数据类型1/3、实型(浮点型).cpp

@@ -0,0 +1,32 @@
+#include <iostream>
+
+int main3()
+{
+
+	//1、单精度     float
+	//2、双精度		double
+	//默认情况下  输出一个小数,会显示出6位有效数字
+
+	float f1 = 3.14f;
+	std::cout << "f1 = " << f1 << std::endl;
+
+	double d1 = 3.14;
+	std::cout << "d1 = " << d1 << std::endl;
+
+	//统计 float 和 double 占用的内存空间
+	std::cout << "float占用的内存空间: " << sizeof(float) << std::endl;
+
+	std::cout << "double占用的内存空间: " << sizeof(double) << std::endl;
+
+	//科学计数法
+	float f2 = 3e2;   // 3*10^2
+	std::cout << "f2 = " << f2 << std::endl;
+	float f3 = 3e-2;   // 3*0.1^-2
+	std::cout << "f3 = " << f3 << std::endl;
+
+
+	system("pause");
+
+	return 0;
+
+}

+ 25 - 0
数据类型1/4、字符型.cpp

@@ -0,0 +1,25 @@
+# include <iostream>
+
+int main4()
+{
+
+	//1、字符型变量的创建方式
+	char ch = 'a';
+	std::cout << ch << std::endl;
+	
+	//2、字符型变量所占内存大小
+	std::cout << "char字符型变量所占内存空间" << sizeof(char) << std::endl;
+
+	//3、字符型变量常见错误
+	//char ch2 = "b";                    //创建字符型变量的时候要用单引号
+	//char ch2 = 'abcdefg'               //创建字符型变量的时候单引号内只能有一个字符
+
+	//4、字符型变量对应ASCII编码
+	//a - 97
+	//A - 65
+	std::cout << (int)ch << std::endl;
+
+	system("pause");
+
+	return 0;
+}

+ 23 - 0
数据类型1/5、转义字符.cpp

@@ -0,0 +1,23 @@
+#include <iostream>
+
+int main5()
+{
+	//转义字符
+
+	//换行符       \n
+	std::cout << "hello world\n";
+
+	//反斜杠       \\
+	
+	std::cout << "\\" <<std::endl;
+
+	//水平制表符   \t   可以整齐输出多行内容
+	std::cout << "aaa\thello world" << std::endl;
+	std::cout << "aa\thello world" << std::endl;
+	std::cout << "aaaaaa\thello world" << std::endl;
+
+
+	system("pause");
+
+	return 0;
+}