简单的算术运算和表达式

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

浮点数除法 (Floating Division) W or Z or both are floats
2020/5/19
6/51
整数除法(Integer Division)
Example: 11 / 5 = 2
an integer an integer
the result is
also an integer
Example: 2.5 + 6 – 2 * 2 = ?4.5
2.5 + 6 – 4
8.5 – 4
4.5
2020/5/19
16/51
算术表达式 (Arithmetic Expression)
巧妙使用圆括号改变运算顺序 –从内往外运算
Example:
(9–(3+2))*3=?
2020/5/19
17/51
算术表达式 (Arithmetic Expression)
Example:
( 9 – (–34 + 25) ) * 3 = ?1?2 ( 9 – ( 3 + 2 ) ) * 3 = 12
2020/5/19
18/51
赋值语句 (Assignment Statement)
三种赋值形式:
Simple——简单赋值 Multiple——多重赋值 Shorthand——简写的复合赋值
2020/5/19
19/51
算术混合运算
2020/5/19
20/51
【例3.1】计算并输出一个三位整 数的个位、十位和百位数字之和
关键是如何分离个位、十位和百位数字?
153 % 10 = 3 153 / 100 = 1
153 - 1*100 = 53
53 / 10 = 5
2020/5/19
21/51
【例3.1】计算并输出一个三位整 数的个位、十位和百位数字之和
#include <stdio.h> 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=%d\n", b2, b1, b0, sum); }
算术表达式 (Arithmetic Expression)
优先级(Order of Precedence)
High: * / % Low: + -
不同优先级时的运算顺序:
——从高到低
相同优先级时的运算顺序:
——算术运算符为左结合(从左到右)
2020/5/19
15/51
算术表达式 (Arithmetic Expression)
2020/5/19
10/51
求余(Modulus)
-11 Example: % 5 = -1
an integer
result
-2 5 -11
the result is the remainder of -11/5
-10
remainder
-1
an integer
2020/5/19
11/51
求余(Modulus)
Rule: – Operands must be integers
2020/5/19
9/51
求余(Modulus)
Example: 11 % 5 = 1
an integer
result
2
the result is the
5 11
remainder of 11/5
10
remainder
1
an integer
2020/5/19
22/51
变量的赋值
简单赋值(Simple Assignment):
变量 = 表达式 ;
多重赋值(Multiple Assignment):
变量1 = 变量2 = 表达式 ;
2020/5/19
23/51
3.1.2复合的赋值运算符 (Combined Assignment Operators)
2020/5/19
7/51
实数除法(Floating Division)
Example: 11.0 / 5 = 2.2
a float
an integer
the result is a float
2020/5/19
8/51
求余(Modulus)
It returns the remainder that occurs after performing the division of 2 operands
2020/5/19
INVALID!
an integer
13/51
算术表达式 (Arithmetic Expression)
当算术表达式包含两个或两个以上的算术 运算符时 ◘ 首先要确定运算顺序 ◘ 所有的运算符都有一个优先级( Order of Precedence )
2020/5/19
14/51
2020/5/19
3/51
3.1C运算符和表达式 (Operator and Expression)
何谓运算符和操作数?
Example:
W+Z
操作数 (Operand)
2020/5/19
运算符 (Operator)
操作数 (Operand)
4/51
3.1.1算术运算符和表达式
Arithmetic Operators
Addition (+)
Modulus (%)
Subtraction (-)
Division (/)
பைடு நூலகம்
Multiplication (*)
2020/5/19
5/51
除法(Division)
W / Z Example:
整数除法 (Integer Division) W and Z are integers
11 Example: % -5 = 1
an integer
result
-2 -5 11
the result is the remainder of 11/-5
10
remainder
1
an integer
2020/5/19
12/51
求余(Modulus)
Example:
11.0 % 5 = ?
a float
C语言程序设计
第3章 简单的算术运算 和表达式
本章学习内容
算术运算符 增1和减1运算符 宏常量与 const常量 表达式与赋值中的自动类型转换 强制类型转换运算符 常用的标准数学函数
2020/5/19
2/51
运算符( Operator )
详见附录C
常见的运算符 –算术运算符 –赋值运算符 –强制类型转换 –关系运算符 –逻辑运算符 –增1和减1 –位运算符
相关文档
最新文档