第2章数据类型运算符和表达式课件

上传人:仙*** 文档编号:244171362 上传时间:2024-10-03 格式:PPT 页数:85 大小:1,004.67KB
返回 下载 相关 举报
第2章数据类型运算符和表达式课件_第1页
第1页 / 共85页
第2章数据类型运算符和表达式课件_第2页
第2页 / 共85页
第2章数据类型运算符和表达式课件_第3页
第3页 / 共85页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,/98,HIT-C Programming,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,2024/10/3,85,/86,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/98,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/98,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/98,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/98,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/98,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/98,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,第,2,章 数据类型、运算符与表达式,第2章 数据类型、运算符与表达式,C,程序的框架是什么样的?,如何编写程序实现,计算两个整数,x=2,y=3,的和,然后显示器输出两个数相加的结果?,如何编写程序实现从键盘输入两个整数,然后显示器输出两个数相加的结果?,思考,C程序的框架是什么样的?思考,一个简单的,C,程序例子,#,include,/*,函数功能,:,计算两个整数相加之和,入口参数,:,整型数据,a,和,b,返回值: 整型数,a,和,b,之和,*,/,int,Add,(,int,a,int,b),return,(a + b);,/*,主函数*,/,void,main,(),int,x, y, sum = 0;,printf,(Input two integers:);,scanf,(%d%d, /*,输入两个整型数,x,和,y*/,sum =,Add,(x, y); /*,调用函数,Add,计算,x,和,y,相加之和*,/,printf,(sum = %dn, sum); /*,输出,x,和,y,相加之和*,/,并列的两个函数,其中主函数是,程序的入口,程序注释,编译预处理命令,一个简单的C程序例子 #include 并,C,程序常见符号分类,关键字(,Keyword,),又称保留字(,C,Reserved Word,),A word that has special meaning in C,标识符(,Identifier,),C Standard Identifier,(,系统预定义标识符),A word having special meaning but may be redefined (but is not recommended!),用户自定义标识符,变量,函数名,,C程序常见符号分类 关键字(Keyword),C,程序常见符号分类,运算符(,Operator,),34,种,详见附录,2,分隔符(,Separator,),空格、回车,/,换行、逗号等,其它符号,“,”,和“,”,标识函数体或语句块,“,/*”,和“*,/”,程序注释的定界符,常量(,Constant,),C程序常见符号分类 运算符(Operator),常量和变量,C,语言程序处理的数据形式,常量(,constant,),在程序中不能改变其值的量,变量(,variable,),其值在程序执行过程中是可以改变的,常量和变量C语言程序处理的数据形式,常量,(,Constant,),在程序中不能改变其值的量,Consists of:,整型,(e.g.,0 67 -2,123,L,123,u,0,22,0x,12,),缺省为,int,实型,(e.g.,2.3 1.2e-5 2.73,F,2.73,L,),缺省为,double,字符型,(e.g.,z 3 $ n,),用,开头的字符为转义字符,代表,1,个字符,字符串,(e.g.,UKM 1 5a,),枚举型,长整型,无符号整型,八进制,十六进制,单精度实型,长双精度实型,十进制小数,指数形式,十进制,常量(Constant)长整型无符号整型八进制十六进制单精度,字符常量,转义字符,一些特殊字符(无法从键盘输入或者另有它用)用转义字符表示,字符常量转义字符,字符常量,字符常数就是一个普通整数,也可参与各种数学运算,每个字符具有一个,0255,之间的数值,可从,ASCII,表,查出,注意:,5,和整数,5,的区别,5,的,ASCII,码值是,53,字符的数学运算在密码学内,用得比较多,字符常量字符常数就是一个普通整数,也可参与各种数学运算,例:小写字母转换为大写字母,#,include,main,(),char,ch = b;,printf,(%c, %dn, ch, ch);,ch = b -,32,;,printf,(%c, %dn, ch, ch);,#,include,main,(),char,ch = b;,printf,(%c, %dn, ch, ch);,ch = b -,(a - A),;,printf,(%c, %dn, ch, ch);,b, 98,B, 66,b, 98,B, 66,相当于,97-65,例:小写字母转换为大写字母 #include stdio.,字符串常量,用双引号括住的由,0,个或多个字符组成的字符序列,I am a string,表示空字符串,除注释外,是唯一可以出现中文的地方,C,语言内部用,0,表示字符串的结束,x,和,x,是不同的,里定义了一系列专门的字符串处理函数,转义字符也可在字符串中使用,字符串,tNameAddressn,的长度?,15,字符串常量用双引号括住的由0个或多个字符组成的字符序列,变量,(,Variable,),A name associated with a memory cell whose value can change,其值在程序执行过程中是可以改变的,Properties of a variable,Name,变量名,Type,变量类型,Value,变量的值,Address,变量的存储单元,地址,变量(Variable )A name associated,Needs to be declared:,类型关键字 变量名,;,变量声明,(Variable Declaration),*,使用变量的基本原则,变量必须先定义,后使用,声明的顺序无关紧要,一条声明语句可声明若干个同类型的变量,Needs to be declared:变量声明(Vari,变量声明,(,Variable Declaration,),*,声明变量是初始化变量的最好时机,不被初始化的变量,其值为随机数,结果会是什么?,变量声明(Variable Declaration)*声明变,Algorithm,变量,表达式,Syntax,变量,=,表达式,;,Rules,:,类型一致,Expressions type must be the same as variables type,int x; int y;,x = 12;y = 5.75;,变量赋值,(,Variable Assignment,),Algorithm变量赋值(Variable Assignm,Example:,int number1, number2;,number1 = 25;,number2 = 23;,number1 = number2;,number1,?,number2,?,25,23,23,变量赋值,(,Variable Assignment,),*,Example:number1?number2?252323,Example:,Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.,Input:,quantity,and,pricePerkg,Output:,price,Process:,price,=,quantity,*,pricePerkg,变量赋值,(,Variable Assignment,),Example:变量赋值(Variable Assignme,Example:,int quantity;,float pricePerkg, price;,quantity = 5;,pricePerkg = 4.50;,price = quantity * pricePerkg;,How does this program work?,变量赋值,(,Variable Assignment,),Example:How does this program,Example:,int quantity;,float pricePerkg, price;,quantity = 2;,pricePerkg = 4.50;,price = quantity * pricePerkg;,quantity,?,pricePerkg,?,price,?,4.50,9.00,2,变量赋值,(,Variable Assignment,),Example:quantity? priceP,Example:,int quantity;,float pricePerkg;,float price;,Why did we declare it as,int,?,Why did we declare them as,float,?,变量类型,(Variable Type),Example:Why did we declare it,数据类型(,Data Type,),为什么要区分类型?,变量的类型决定了,数据的存储形式,合法的取值范围,占用内存空间大小,可参与的运算种类,数据类型(Data Type)为什么要区分类型?,数据类型(,Data Type,),数据类型,基本类型,构造类型,指针类型,空类型,整型,实型(浮点型),字符型,枚举类型,数组类型,结构体类型,共用体类型,单精度实型,双精度实型,基本整型,长整型,短整型,无符号整型,数据类型(Data Type)数据类型基本类型构造类型指针类,基本数据类型,*,int,整数,在目前绝大多数机器上占,4,个字节,TC2.0,,,2,个字节,float,单精度浮点数,,4,个字节,double,双精度浮点数,,8,个字节,char,字符,,1,个字节,表示,256,个,ASCII,字符,或,0255,的整数,基本数据类型*int,数据类型修饰符,short,short,int,,简写为,short,,,短整数,,2,个字节,long,long,int,,简写为,long,,,长整数,,4,个字节,long,double,,长双精度(高精度)浮点数,,10,个字节,unsigned,用来修饰,char,、,int,、,short,和,long,无符号,整数(正整数和,0,),数据类型修饰符short,不同类型取值范围不同,C,语言直接提供的任何类型都有取值范围。,不同类型取值范围不同C语言直接提供的任何类型都有取值范围。,不同类型取值范围不同,Most significant bit (MSB) is sign(,最高位为符号位,),不同类型取值范围不同Most significant bit,如何衡量变量所占空间大小?,bit,,,中文叫法:,位,Byte,,,中文叫法:,字节,Kilobyte(KB),,,中文叫法:,K,Megabyte(MB),,,中文叫法:,兆,Gigabyte(GB),,,中文叫法:,G,Terabyte(TB),,,中文叫法:,T,1 TB,=,1,024 GB,1 GB,=,1,024 MB,1 MB,=,1,024 KB,1 KB,=,1,024 B,1 B,=,8 b,如何衡量变量所占空间大小?1 TB = 1,024 GB1,一个位有多大?,只能是“,0”,或者“,1”,,二进制,一个字节有多大?,可以表示数字,0255,之间的整数,如何衡量变量所占空间大小?,一个位有多大?如何衡量变量所占空间大小?,不同类型占用的内存字节数不同,同种类型在不同的平台其占字节数不尽相同,如,int,在,16,位、,32,位和,64,位系统上分别占,2,、,4,和,8,个字节,不要对变量所占的内存空间字节数想当然,用,sizeof,获得变量或者数据类型的长度,现象与危害:,在平台间移植时会出现问题,导致数据丢失或者溢出,注意!,不同类型占用的内存字节数不同同种类型在不同的平台其占字节数不,sizeof,到底是什么?,*,C,语言的关键字,并非函数,计算,类型,占用的字节数,两种语法形式,sizeof(,类型,),结果为,类型,占用的字节数,sizeof(,表达式,),结果为,表达式值所属类型,占用的字节数,一般都使用,sizeof,(,变量名,),sizeof到底是什么? *C语言的关键字,并非函数,例在,TC,和,VC,、,CB,下的运行结果,#,include,void main,(),printf,(Data type Number of bytesn);,printf,(- -n);,printf,(char %dn,sizeof,(char);,printf,(int %dn,sizeof,(int);,printf,(short int %dn,sizeof,(short);,printf,(long int %dn,sizeof,(long);,printf,(float %dn,sizeof,(float);,printf,(double %dn,sizeof,(double);,例在TC和VC、CB下的运行结果#include a = 123, type(a), a = abc, type(a),变量是一个对象的别名、指针或引用,赋值是重新绑定,int a;,a = 123; ,a = abc;,变量是数据的内存地址名,,赋值是改写,Python,C,动态和静态数据类型 a = 123变量是一个对象的别名,Syntax:,变量,1,=,变量,2,=,表达式,;,多重赋值,Multiple Assignment,Dont forget the semicolon !,右结合:从右向左赋值,变量,1 =,(,变量,2 =,表达式,),;,Syntax:多重赋值Multiple Assignmen,Example:,int a, b;,float x, y;,. . .,a = b = 0;,x = y = 100.0;,b,?,a,?,x,?,y,?,0,0,100.0,100.0,多重赋值,Multiple Assignment,Example:b?a? x? y?00100.01,运算符(,Operator,),34,种,详见附录,常见的运算符,算术运算符,赋值运算符,增和减,类型强转,关系运算符,逻辑运算符,.,运算符( Operator )34种,详见附录,C,运算符和操作数(,Operator and Operand,),Example:,W + Z,操作数,(Operand),运算符,(Operator),操作数,(Operand),What are operator and operand?,C运算符和操作数(Operator and Operand,算术运算符(,Arithmetic Operators,),Addition (+),Subtraction (-),Multiplication (*),Division (/),Modulus (%),算术运算符(Arithmetic Operators)Add,除法,(,Division,),Example:,W / Z,浮点数除法,(Floating Division),W,or,Z,or both are floats,整数除法,(Integer Division),W,and,Z,are integers,除法(Division)Example:W / Z浮点数除法,Example:,an integer,an integer,the result is also an integer,整数除法(,Integer Division,),11 / 5 = 2,Python,/ float division,/ integer division,对,Python 2.x,版本 当,a/b,同为整数时,,a/b,等价于,a/b,来处理以避免出错,Example:an integeran integer,Example:,实数除法,(,Floating Division,),11.0 / 5 = 2.2,a float,an integer,the result is a float,Example:实数除法(Floating Division,What will be the answer if an integer is divided by 0?,Something to ponder ,What will be the answer if an,求余,(,Modulus,),*,It returns the,remainder,that occurs after performing the division of 2 operands,Rule:,操作数必须是整数,Operands must be,integers,注意!,求余(Modulus)*It returns the rem,Example:,-11 % 5 = -1,an integer,an integer,-11,5,-2,-10,-1,remainder,result,the result is the remainder of -11/5,求余(,Modulus,),Example:-11 % 5 = -1an integer,Example:,11 % -5 = 1,an integer,an integer,11,-5,-2,10,1,remainder,result,the result is the remainder of 11/-5,求余(,Modulus,),Example:11 % -5 = 1an integera,Example:,11.0 % 5 = ?,a float,an integer,INVALID!,求余(,Modulus,),*,注意!,Example:11.0 % 5 = ?a floatan,An expression may contain 2 or more arithmetic operators,Main issue:,运算顺序,Order of Precedence,优先级,算术表达式,(,Arithmetic Expression,),An expression may contain 2 or,优先级(,Order of Precedence,),High:,* / %,Low:,+ -,All operators have a precedence level.,不同,优先级时的运算顺序:,High precedence level operators are evaluated before lower ones.,相同,优先级时的运算顺序:,Operators of the same precedence level are evaluated from left to right,(算术运算符为左结合),算术表达式 (,Arithmetic Expression,),优先级(Order of Precedence)算术表达式,Example:,?,4,8.5,2.5 + 6 , 4,4.5,2.5 + 6 2 * 2 =,算术表达式(,Arithmetic Expression,),4.5,Example:?48.52.5 + 6 44,巧妙使用,圆括号,改变运算顺序,All expressions in parentheses must be evaluated prior to values outside brackets,Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first,Example:,( 9 ( 3 + 2 ) ) * 3 = ?,算术表达式(,Arithmetic Expression,),巧妙使用圆括号改变运算顺序Example:( 9 ( 3,【,例,】,计算并输出一个三位整数的个位、十位和百位数字之和,Example:,15,3,% 10,=,3,1,53 / 100 =,1,1,5,3 - 1*100 =,5,3,5,3 / 10 =,5,【例】计算并输出一个三位整数的个位、十位和百位数字之和 Ex,【,例,】,计算并输出一个三位整数的个位、十位和百位数字之和,【例】计算并输出一个三位整数的个位、十位和百位数字之和,赋值语句(,Assignment Statement,),There are 3 types of assignment:,Simple,简单赋值,Multiple,多重赋值,Shorthand,简写的复合赋值,赋值语句(Assignment Statement)The,变量的赋值,简单赋值,(,Simple Assignment,),:,变量,=,表达式,;,多重赋值,(,Multiple Assignment,),:,变量,1,=,变量,2,=,表达式,;,变量的赋值简单赋值(Simple Assignment):多,Syntax:,变量,x,=,变量,x,运算符,op,表达式,;,变量,x,运算符,op,=,表达式,;,复合的赋值运算符,(,Combined Assignment Operators,),这种形式看起来更直观,且执行效率一般也更高一些,Syntax: 复合的赋值运算符(Combined,Example:,num,=,num,+ 5;,num,15,15 + 5,20,20,Example:,num,+=,5;,similar to,num,= num +,5,shorthand assignment operator,复合的赋值运算符,(,Combined Assignment Operators,),Example:num1515 + 52020Examp,Operation,Examples of expression,Description,+=,num += 5;,num = num + 5;,-=,num -= 5;,num = num 5;,*,=,num *= 5;,num = num * 5;,/=,num /= 5;,num = num / 5;,%=,num %= 5;,num = num % 5;,简写的复合赋值(,Shorthand Assignment,),复合的赋值运算符,(,Combined Assignment Operators,),OperationExamples of expressio,赋值运算符的优先级和结合性,赋值运算符优先级低于算术、关系和逻辑运算符,赋值表达式规定为完成运算后左操作数的值,赋值运算符结合性为右结合。,赋值运算符的优先级和结合性赋值运算符优先级低于算术、关系和,已知,int a = 3;,执行,a += a -= a * a,后,变量,a,的值?,a += a -= a * a,a += a -= 9,a += -6,a = -12,3,-6,-12,执行,a += a -= a *= a,后,变量,a,的值?,a += a -= a *= a,a += a -= 9,a += 0,a = 0,3,9,0,0,已知 int a = 3;3-6-12执行 a += a -,增一和减一运算符,(,Increment and Decrement,),*,n+,,,n,-,,,+n,,,-n,+,让参与运算的变量加,1,,,-,让参与运算的变量减,1,作为,后缀,(postfix),运算符时,先取,n,的值,然后加,/,减,1,m = n,+,;,m = n;,n,+,;,增一和减一运算符(Increment and Decrem,增一和减一运算符,(,Increment and Decrement,),n+,,,n,-,,,+n,,,-n,+,让参与运算的变量加,1,,,-,让参与运算的变量减,1,作为,后缀,(,postfix,),运算符时,先取,n,的值,然后加,/,减,1,作为,前缀,(prefix),运算符时,先加,/,减,1,,然后取,n,的值,m =,+,n;,n,+,;,m = n;,增一和减一运算符(Increment and Decrem,Example:,j =,+,i - 2,i,5,similar to,i = i + 1;,j = i 2;,j,?,4,6,前缀(,Prefix,),增一和减一运算符,Example:i5similar to j?46前,Example:,j = i,+,- 2,i,5,similar to,j = i 2;,i = i + 1;,后缀(,Postfix,),增一和减一运算符,j,?,3,6,Example:i5similar to 后缀(Pos,int a=3;,printf(%d, -a+);,a,3,similar to,printf(%d, -a);,a = a + 1;,后缀(,Postfix,),增一和减一运算符,4,int a=3;a3similar to 后缀(Pos,良好的程序设计风格提倡:,在一行语句中,一个变量只能出现一次增,1,或者减,1,运算,过多的增,1,和减,1,运算混合,不仅可读性差,而且因为编译器实现的方法不同,导致不同编译器产生不同的运行结果,增一和减一运算符,(Increment and Decrement),良好的程序设计风格提倡:增一和减一运算符(Incremen,【,例,】,计算圆的周长和面积,circumference = 33.300854,area = 88.247263,【例】计算圆的周长和面积 circumference = 3,如何从键盘读取半径值?,【,例,】,计算圆的周长和面积,Input r: 5.3,circumference = 33.300854,area = 88.247263,在程序中直接使用的常数称为幻数,(Magic Number),如何从键盘读取半径值? 【例】计算圆的周长和面积 Input,使用幻数存在的问题?,假如直接使用常数,会有什么影响?,程序的可读性变差,容易发生书写错误,当常数需要改变时,要修改所有使用它的代码,工作量大,还可能有遗漏,解决方案:,避免使用幻数,把幻数定义为常量(宏常量、,const,常量,.,),使用幻数存在的问题?假如直接使用常数,会有什么影响?,宏常量与宏替换,#define,标识符,字符串,宏常量(,Macro constant,),也称符号常量,一般采用全大写字母表示,宏定义不是语句,而是一种编译预处理命令,宏常量与宏替换 #define 标识符 字符,【,例,】,计算圆的周长和面积,宏替换,【例】计算圆的周长和面积 宏替换,#,include,#,define,PI 3.14159,;,#,define,R 5.3,;,main,(),printf,(area = %fn, PI * R * R);,printf,(circumference = %fn, 2 * PI * R);,相当于执行,#,include,main,(),printf,(area = %fn, 3.14159,;,*5.3,;,*5.3,;,);,printf,(circumference = %fn, 2*3.14159,;,*,5.3,;,);,语法错误,计算圆的周长和面积,#include 相当于执行语法错误计算,const,常量与宏常量相比的优点是什么?,const,常量有,数据类型,某些集成化调试工具可以对,const,常量进行调试,const,常量,【,例,】,const常量与宏常量相比的优点是什么?const常量【例】,表达式与赋值中的自动类型转换,相同类型数据的运算结果,还是该类型,不同类型数据的运算结果,是两种类型中取值范围大的那种,long,double,double,float,long,int,short,char,double,float,long,unsigned,int,char,,,short,低,高,表达式与赋值中的自动类型转换相同类型数据的运算结果,还是该类,表达式与赋值中的自动类型转换,取值范围,小,的类型,赋值给,取值范围,大,的类型是,安全,的,反之是不安全的,若,大,类型的值在,小,类型能容纳的范围之内,则平安无事,但是,,浮点数转为整数,,会丢失小数部分,非四舍五入,反之转换后的结果必然是错误的,具体结果与机器和实现方式有关,避免如此使用,好的编译器会发出警告,注意!,表达式与赋值中的自动类型转换取值范围小的类型赋值给取值范围大,表达式与赋值中的自动类型转换,【,例,】,下面程序演示了赋值中的类型转换,表达式与赋值中的自动类型转换【例】下面程序演示了赋值中的类型,Example:,int x = 10;,float y;,y = (float)x;,(float)10,10.000000,x,10,y,?,10.000000,类型强转(,Casting,),消除从大到小的警告,x = (,int,)y;,通过下面方式把表达式的值转为任意类型,(,类型,),表达式,不改变,x,Example: (float)10 10.000000x1,Example:,int total, number;,float average;,average = total / number;,15 / 2,7,total,15,number,2,average,?,7.000000,类型强转(,Casting,),两个整数运算的结果,还是整数,不是浮点数,Example: 15 / 2 7total15number,Example:,int total, number;,float average;,average =,(float),total / number;,15.000000 / 2,7.500000,total,15,number,2,average,?,7.500000,类型强转(,Casting,),Example: 15.000000 / 2 7.50000,【,例,】,演示类型强转运算符,m/2 = 2,(float)(m/2) = 2.000000,(float)m/2 = 2.500000,m = 5,【例】演示类型强转运算符m/2 = 2,常用的标准数学函数,常用的标准数学函数,【,例,】,计算三角形面积,area = sqrt(s * (s - a) * (s - b) * (s - c),area = sqrt(s(s-a)(s-b)(s-c),s = 0.5 * (a + b + c),s = 1.0/2 * (a + b + c),s = (a + b + c) / 2.0,s = (float)(a + b + c) / 2,s = 1/2 * (a + b + c),s = (float)(a + b + c) / 2),注意!,【例】计算三角形面积 area = sqrt(s * (s,【,例,】,计算三角形面积,Input a,b,c:3,4,5,area = 6.000000,【例】计算三角形面积 Input a,b,c:3,4,5,本章知识点,标识符的命名规则,数据类型,变量(声明、赋值、初始化),常量,运算符及其优先级和结合性,算术,赋值,类型强转,关系,逻辑,增一,/,减一,位运算,sizeof,本章知识点标识符的命名规则,Questions and answers?,Questions and answers?,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!