当前位置:文档之家› C语言程序设计第三章课件

C语言程序设计第三章课件


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