c语言算术表达式程序设计,c语言程序设计课件简单的算术运算和表达式.pptx

c语言程序设计课件简单的算术运算和表达式

本章学习内容? 算术运算符? 增1和减1运算符? 宏常量与 const常量? 表达式与赋值中的自动类型转换? 强制类型转换运算符? 常用的标准数学函数运算符( Operator )详见附录C常见的运算符算术运算符赋值运算符类型强转关系运算符逻辑运算符增1和减1位运算符Example:W + Z操作数(Operand)操作数(Operand)运算符(Operator)3.1C运算符和表达式(Operator and Expression)何谓运算符和操作数?Addition (+)Modulus (%)Subtraction (-)Division (/)Multiplication (*)3.1.1算术运算符和表达式Arithmetic OperatorsExample:W / Z整数除法(Integer Division)W and Z are integers浮点数除法(Floating Division)W or Z or both are floats除法(Division)an integer? the result is also an integeran integer整数除法(Integer Division)Example:11 / 5 = 2a floatthe result is a floatan integer实数除法(Floating Division)Example:11.0 / 5 = 2.2注意!求余(Modulus)It returns the remainder that occurs after performing the division of 2 operandsRule: 操作数必须是整数Operands must be integersthe result is the remainder of 11/5an integerresultremainderan integer求余(Modulus)Example:11 % 5 =he result is the remainder of -11/5an integerresultremainderan integer求余(Modulus)Example:-11 % 5 = -1-25-11-10-1the result is the remainder of 11/-5an integerresultremainderan integer求余(Modulus)Example:11 % -5 = 1-2-51110 1a floatINVALID! 注意!an integer求余(Modulus)Example:11.0 % 5 = ?算术表达式(Arithmetic Expression)当算术表达式包含两个或两个以上的算术运算符时 首先要确定运算顺序所有的运算符都有一个优先级( Order of Precedence ) 算术表达式(Arithmetic Expression) 优先级(Order of Precedence) High:* / %Low: + -不同优先级时的运算顺序:——从高到低相同优先级时的运算顺序:——算术运算符为左结合(从左到右)算术表达式(Arithmetic Expression)Example:2.5 + 6 – 2 * 2 = ?4.52.5 + 6 –48.5 – 44.5算术表达式(Arithmetic Expression)巧妙使用圆括号改变运算顺序从内往外运算Example:( 9 – ( 3 + 2 ) ) * 3 = ?算术表达式(Arithmetic Expression)Example: ( 9 – ( 3 + 2 ) ) * 3 = ?? – 5124? ( 9 – ( 3 + 2 ) ) * 3 = 12赋值语句(Assignment Statement)三种赋值形式:Simple——简单赋值Multiple——多重赋值Shorthand——简写的复合赋值算术混合运算 【例3.1】计算并输出一个三位整数的个位、十位和百位数字之和 关键是如何分离个位、十位、百位数字?153 % 10 = 3153 / 100 = 1153 - 1*100 = 53 53 / 10 = 5【例3.1】计算并输出一个三位整数的个位、十位和百位数字之和 变量的赋值简单赋值(Simple Assignment):变量= 表达式 ;多重赋值(Multiple Assignment): 变量1= 变量2= 表达式 ;3.1.2复合的赋值运算符(Combined Assignment Operators)Syntax: 变量x =变量x 运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值