C语言算数运算

上传人:dja****22 文档编号:242970899 上传时间:2024-09-13 格式:PPT 页数:49 大小:783KB
返回 下载 相关 举报
C语言算数运算_第1页
第1页 / 共49页
C语言算数运算_第2页
第2页 / 共49页
C语言算数运算_第3页
第3页 / 共49页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,2024/9/13,49,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,2024/9/13,15,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,2024/9/13,45,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,*,*,/51,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,HIT-C Programming,本章学习内容,算术运算符,增,11,运算符,宏常量与,const,表达式与赋值中的自动类型转换,强制类型转换运算符,常用的标准数学函数,运算符(,Operator,),详见附录,C,常见的运算符,算术运算符,赋值运算符,强制类型转换,关系运算符,逻辑运算符,增和减,位运算符,3.1C,运算符和表达式(,Operator and Expression,),Example:,W + Z,操作数,(Operand),运算符,(Operator),操作数,(Operand),何谓运算符和操作数,?,3.1.1,算术运算符和表达式,Addition (+),Subtraction (-),Multiplication (*),Division (/),Modulus (%),Arithmetic Operators,除法,(,Division,),Example:,W / Z,浮点数除法,(Floating Division),W,or,Z,or both are floats,整数除法,(Integer Division),W,and,Z,are integers,Example:,an integer,an integer,the result is also an integer,整数除法(Integer Division),11 / 5 = 2,Example:,实数除法,(,Floating Division,),11.0 / 5 = 2.2,a float,an integer,the result is a float,求余,(,Modulus,),It returns the,remainder,that occurs after performing the division of 2 operands,Rule:,Operands must be,integers,注意!,Example:,11 % 5 = 1,an integer,an integer,the result is the remainder of 11/5,11,5,2,10,1,remainder,result,求余(Modulus),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 = 1,an integer,an integer,11,-5,-2,10,1,remainder,result,the result is the remainder of 11/-5,求余(Modulus),Example:,11.0 % 5 = ?,a float,an integer,INVALID!,求余(Modulus),注意!,当算术表达式包含两个或两个以上的算术运算符时,首先要确定运算顺序,所有的运算符都有一个优先级,(,Order of Precedence,),算术表达式,(,Arithmetic Expression,),优先级(,Order of Precedence,),High:,* / %,Low:,+ -,不同,优先级时的运算顺序:,从高到低,相同,优先级时的运算顺序:,算术运算符为左结合(从左到右),算术表达式(,Arithmetic Expression,),Example:,?,4,8.5,2.5 + 6 , 4,4.5,2.5 + 6 2 * 2 =,算术表达式(,Arithmetic Expression,),4.5,巧妙使用,圆括号,改变运算顺序,从内往外运算,Example:,( 9 ( 3 + 2 ) ) * 3 = ?,算术表达式(,Arithmetic Expression,),Example:,( 9 ( 3 + 2 ) ) * 3 = ?, 5,4,12,( 9 ( 3 + 2 ) ) * 3 = 12,算术表达式(,Arithmetic Expression,),赋值语句(,Assignment Statement,),三种赋值形式,:,Simple,简单赋值,Multiple,多重赋值,Shorthand,简写的复合赋值,算术混合运算,【,例,3.1】,计算并输出一个三位整数的个位、十位和百位数字之和,关键是如何分离个位、十位和百位数字?,15,3,% 10,=,3,1,53 / 100 =,1,1,5,3,1*100 =,5,3,5,3 / 10 =,5,【,例,3.1】,计算并输出一个三位整数的个位、十位和百位数字之和,#include ,main(),int x = 153, b0, b1, b2, sum;,b2 = x / 100;,b1 = (x - b2 * 100) / 10;,b0 = x % 10;,sum = b2 + b1 + b0;,printf(b2=%d, b1=%d, b0=%d, sum=%dn, b2, b1, b0, sum);,变量的赋值,简单赋值,(,Simple Assignment,),:,变量,=,表达式,;,多重赋值,(,Multiple Assignment,),:,变量,1,=,变量,2,=,表达式,;,Syntax:,变量,x,=,变量,x,运算符,op,表达式,;,变量,x,运算符,op,=,表达式,;,3.1.2,复合的赋值运算符,(,Combined Assignment Operators,),这种形式看起来更直观,且执行效率一般也更高一些,Example:,num,=,num,+ 5;,num,15,15 + 5,20,20,Example:,num,+=,5;,similar to,num,= num +,5,shorthand assignment operator,3.1.2,复合的赋值运算符,(,Combined Assignment Operators,),已知,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,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,),3.1.2,复合的赋值运算符,(,Combined Assignment Operators,),3.1.3,增,1,和减,1,运算符,(,Increment and Decrement,),n+,,,n,-,,,+n,,,-n,+,让参与运算的变量加,1,,,-,让参与运算的变量减,1,作为,后缀,(postfix),运算符时,先取,n,的值,然后加,/,减,1,m = n,+,;,m = n;,n,+,;,3.1.3,增,1,和减,1,运算符,(,Increment and Decrement,),n+,,,n,-,,,+n,,,-n,+,让参与运算的变量加,1,,,-,让参与运算的变量减,1,作为,后缀,(,postfix,),运算符时,先取,n,的值,然后加,/,减,1,作为,前缀,(prefix),运算符时,先加,/,减,1,,然后取,n,的值,m =,+,n;,n,+,;,m = n;,Example:,j =,+,i - 2,i,5,similar to,i = i + 1;,j = i 2;,j,?,4,6,前缀(,Prefix,),增,1,和减,1,运算符,Example:,j = i,+,- 2,i,5,similar to,j = i 2;,i = i + 1;,后缀(,Postfix,),增,1,和减,1,运算符,j,?,3,6,int a=3;,printf(%d, -a+);,a,3,similar to,printf(%d, -a);,a = a + 1;,后缀(,Postfix,),增,1,和减,1,运算符,4,良好的程序设计风格提倡:,在一行语句中,一个变量只能出现一次增,1,或者减,1,运算,过多的增,1,和减,1,运算混合,不仅可读性差,而且因为编译器实现的方法不同,导致不同编译器产生不同的运行结果,3.1.3,增,1,和减,1,运算符,(Increment and Decrement),【例3.2】计算圆的周长和面积,circumference = 33.300854,area = 88.247263,#include ,main(),double r = 5.3;,printf(circumference = %fn, 2*3.14159*r);,printf(area = %fn, 3.14159*r*r);,#include ,main(),double r;,printf(Input r:);,scanf(%lf, ,printf(circumference = %fn, 2*3.14159*r);,printf(area = %fn, 3.14159*r*r);,【例3.3】计算圆的周长和面积,Input r: 5.3,circumference = 33.300854,area = 88.247263,在程序中直接使用的常数称为幻数,(Magic Number),使用幻数存在的问题?,直接使用常数会有什么影响?,程序的可读性变差,容易发生书写错误,当常数需要改变时,要修改所有引用它的代码,工作量大,还可能有遗漏,解决方案:,避免使用幻数,把幻数定义为常量(宏常量、,const,常量,),3.2宏常量与宏替换,#define,标识符,字符串,宏常量(,Macro Constant,),也称符号常量,一般采用全大写字母表示,宏定义不是语句,而是一种编译预处理命令,#include ,main(),double r;,printf(Input r:);,scanf(%lf, ,printf(circumference = %fn, 2*PI*r);,printf(area = %fn, PI*r*r);,【例3.4】计算圆的周长和面积,宏替换,#,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 ,main(),const double PI = 3.14159;,double r;,printf(Input r:);,scanf(%lf, ,printf(circumference = %fn, 2*PI*r);,printf(area = %fn, PI*r*r);,const,常量与宏常量相比的优点是什么?,const,常量有,数据类型,某些集成化调试工具可以对,const,常量进行调试,3.3 const常量,【,例,3.5】,表达式与赋值中的自动类型转换,相同类型数据的运算结果,还是该类型,不同类型数据的运算结果,是两种类型中取值范围大的那种,long,double,double,float,long,int,short,char,double,float,long,unsigned,int,char,,,short,低,高,表达式与赋值中的自动类型转换,取值范围,小,的类型,赋值给,取值范围,大,的类型是,安全,的,反之是不安全的,若,大,类型的值在,小,类型能容纳的范围之内,则平安无事。但是,浮点数转为整数,,会丢失小数部分,而非四舍五入,反之转换后的结果必然是错误的,具体结果与机器和实现方式有关,避免如此使用,好的编译器会发出警告,注意!,表达式与赋值中的自动类型转换,【,例,3.6】,下面程序演示了赋值中的类型转换,Example:,int x = 10;,float y;,y = (float)x;,(float)10,10.000000,x,10,y,?,10.000000,3.4,自动类型转换与强制类型转换运算符,强转,(,Casting,)可以,消除从大到小的警告,通过下面方式把表达式的值转为任意类型,(,类型,),表达式,不改变,x,Example:,int total, number;,float average;,average = total / number;,15 / 2,7,total,15,number,2,average,?,7.000000,两个整数运算的结果,还是整数,不是浮点数,3.4,自动类型转换与强制类型转换运算符,Example:,int total, number;,float average;,average =,(float),total / number;,15.000000 / 2,7.500000,total,15,number,2,average,?,7.500000,3.4,自动类型转换与强制类型转换运算符,【例3.7】演示强制类型转换运算符的使用,m/2 = 2,(float)(m/2) = 2.000000,(float)m/2 = 2.500000,m = 5,3.5 常用的标准数学函数,【例3.8】计算三角形面积,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),注意!,【例3.8】计算三角形面积,Input a,b,c:3,4,5,area = 6.000000,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 大学资料


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

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


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