C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算

上传人:E**** 文档编号:89349668 上传时间:2019-05-23 格式:PPT 页数:61 大小:1.92MB
返回 下载 相关 举报
C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算_第1页
第1页 / 共61页
C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算_第2页
第2页 / 共61页
C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算_第3页
第3页 / 共61页
C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算_第4页
第4页 / 共61页
C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算_第5页
第5页 / 共61页
点击查看更多>>
资源描述

《C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算》由会员分享,可在线阅读,更多相关《C++教程教学课件 PPT 作者 郑莉 李宁 02_简单数据及运算(61页珍藏版)》请在金锄头文库上搜索。

1、第二章 简单数据及运算,清华大学 郑 莉,学习目标,掌握常量与变量的概念及其定义和初始化方法; 掌握C+的基本数据类型; 掌握基本类型数据的运算实现方法。,2,目录,2.1 程序中的数据 2.2 基本数据类型 2.2.1 整数类型 2.2.2 浮点数类型 2.2.3 字符类型 2.2.4 字符串类型 2.2.5 布尔类型,3,目录(续),2.3 简单运算 2.3.1 算术运算 2.3.2 赋值运算 2.3.3 逗号运算 2.3.4 关系运算 2.3.5 逻辑运算 2.3.6 sizeof运算 2.3.7 位运算,4,目录(续),2.4 语句 2.4.1 声明语句 2.4.2 表达式语句 2.4

2、.3 复合语句,5,2.1 程序中的数据,常量 在源程序中直接写明的数据,其值在整个程序运行期间不可改变,这样的数据称为常量。 变量 在运行过程中从计算机的外部设备(例如键盘、硬盘)读取的,这些数据的值在程序运行过程中允许改变,这样的数据称为变量。,6,例2-1:读入并显示整数,#include using namespace std; int main() int radius; coutradius; coutradius; cout“Now the radius is changed to:“radiusn cout“PI is still:“3.14n; return 0; ,7,2.

3、1 程序中的数据,运行结果: Please enter the radius! 2 The radius is:2 PI is:3.14159 Please enter a different radius! 3 Now the radius is changed to:3 PI is still:3.14159,2.1 程序中的数据,例2-1(续),例2-2:为常量命名,#include using namespace std; int main() const double pi(3.14159); int radius; coutradius; coutradius; cout“Now

4、the radius is changed to:“radiusn; cout“PI is still:“pin; return 0; ,9,2.1 程序中的数据,运行结果: Please enter the radius! 2 The radius is:2 PI is:3.14159 Please enter a different radius! 3 Now the radius is changed to:3 PI is still:3.14159,2.1 程序中的数据,例2-2(续),例2-3:变量的初始化,#include using namespace std; int main

5、() const double pi(3.14159); int radius(0); coutradius; cout“Now the radius is changed to:“radiusn; cout“PI is still:”pin; return 0; ,11,2.1 程序中的数据,运行结果: The initial radius is:0 PI is:3.14159 Please enter a different radius! 3 Now the radius is changed to:3 PI is still:3.14159,2.1 程序中的数据,例2-3(续),2.2

6、.1 整数类型,基本的整数类型 Int 按符号分 符号的(signed)和无符号的(unsigned) 按照数据范围分 短整数(short)和长整数(long) char类型,13,2.2 基本数据类型,2.2.1 整数类型(续),十进制整数 若干个09的数字,但数字部分不能以0开头,正数前边的正号可以省略。 八进制整数 八进制整常量的数字部分要以数字0开头,一般形式为:0若干个07的数字 十六进制整数 十六进制整常量的数字部分要以0x开头,一般形式为:0x若干个09的数字及AF的字母(大小写均可),14,2.2 基本数据类型,例2-4:整数变量的定义与输出,#include using na

7、mespace std; int main() short int i; unsigned short int j; j = 50000; i = j; cout “Short int is:”iendl; cout “Short unsigned int is: ”jendl; return 0; ,15,2.2 基本数据类型2.2.1整数类型,运行结果: Short int is: -15536 Unsigned short int is: 50000,例2-4(续),2.2 基本数据类型2.2.1整数类型,例2-5:不同类型整数的最值,#include #include #include

8、 using namespace std; int main() cout ”Min of short is: ”SHRT_MINendl; cout ”Max of short is: ”SHRT_MAXendl; cout ”Min of int is: ”INT_MINendl; cout ”Max of int is: ”INT_MAXendl; cout ”Min of long is: ”LONG_MINendl; cout ”Max of long is: ”LONG_MAXendl; cout ”Max of unsigned short is: ”USHRT_MAXendl;

9、 cout ”Max of unsigned int is: ”UINT_MAXendl; cout ”Max of unsigned long is: ”ULONG_MAXendl; return 0; ,17,2.2 基本数据类型2.2.1整数类型,运行结果: Min of short is: -32768 Max of short is: 32767 Min of int is: -2147483648 Max of int is: 2147483647 Min of long is: -2147483648 Max of long is: 2147483647 Max of unsig

10、ned short is: 65535 Max of unsigned int is: 4294967295 Max of unsigned long is: 4294967295,例2-5(续),2.2 基本数据类型2.2.1整数类型,例2-6:整型常量的应用,#include #include using namespace std; int main() int isample, osample, hsample; unsigned long ulsample; cinisampleoctosamplehexhsample; coutisample;octosample; hexhsam

11、pleendl; isample=123; osample=0173; hsample=0x7B; ulsample=4294967295UL;,19,2.2 基本数据类型2.2.1整数类型,例2-6(续),coutdecisample;oct isample ;hex isample endl; coutdec osample; octosample ;hex osample endl; cout dechsample ;oct hsample; hexhsampleendl; coutdec ulsample ;octulsample; hexulsampleendl; return 0;

12、 ,20,2.2 基本数据类型2.2.1整数类型,运行结果: 123 0173 0x7B 123;173;7b 123;173;7b 123;173;7b 123;173;7b 4294967295;37777777777;ffffffff,例2-6(续),2.2 基本数据类型2.2.1整数类型,2.2.2 浮点数类型,单精度 float 双精度 double 扩展精度 long double,22,2.2 基本数据类型,2.2.2 浮点数类型(续),一般形式 例如,12.5,-12.5等 指数形式 double例如,0.345E+2表示0.345102,-34.4E-3表示-34.410-3

13、,其中,字母E可以大写或小写。当以指数形式表示一个实数时,整数部分和小数部分可以省略其一,但不能都省略。例如:.123E-1,12.E 2,1.E-3都是正确的,但不能写成E-3这种形式。需要注意的是,在浮点文字量的中间不能出现空格。,23,2.2 基本数据类型,例2-7:不同类型浮点数的应用,#include #include using namespace std; const float PI_FLOAT = 3.1415926f; const double PI_DOUBLE = 3.1415926; const long double PI_LDOUBLE = 3.1415926;

14、int main() float nRadiusFloat = 5.5f, nAreaFloat; double nRadiusDouble = 5.5, nAreaDouble; long double nRadiusLDouble = 5.5, nAreaLDouble; nAreaFloat = PI_FLOAT* nRadiusFloat* nRadiusFloat; nAreaDouble = PI_DOUBLE* nRadiusDouble* nRadiusDouble;,24,2.2 基本数据类型2.2.2浮点数类型,例2-7(续),nAreaLDouble = PI_DOUBL

15、E* nRadiusDouble* nRadiusDouble; cout “nAreaFloat = ” nAreaFloat ” , sizeof(nAreaFloat) = ” sizeof(nAreaFloat) endl; cout “nAreaDouble = ” nAreaDouble ” , sizeof(nAreaDouble) = ” sizeof(nAreaDouble)endl; cout “nAreaLDouble = ” nAreaLDouble ” , sizeof(nAreaLDouble) = ” sizeof(nAreaLDouble)endl; retur

16、n 0; ,25,2.2 基本数据类型2.2.2浮点数类型,运行结果: nAreaFloat = 95.0332 , sizeof(nAreaFloat) = 4 nAreaDouble = 95.0332 , sizeof(nAreaDouble) = 8 nAreaLDouble = 95.0332 , sizeof(nAreaLDouble) = 8,例2-7(续),2.2 基本数据类型2.2.2浮点数类型,2.2.3 字符类型,字符变量 关键字 char 字符常量 单引号括起,例如:a,D,$等 转义字符 不可显示字符,无法通过键盘输入字符,例如响铃、换行、制表符、回车等,27,2.2 基本数据类型,2.2.3 字符类型(续),转义字符 换行符(new line)n 水平制表键(horizontal tab)t 垂直制表键(vertical tab)v 退

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号