Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Hu Junfeng 2015/09/23 第三讲 语言 数据类型 流程控制 函数. 关于选课和免修.

Similar presentations


Presentation on theme: "1 Hu Junfeng 2015/09/23 第三讲 语言 数据类型 流程控制 函数. 关于选课和免修."— Presentation transcript:

1 1 Hu Junfeng 2015/09/23 第三讲 语言 数据类型 流程控制 函数

2 关于选课和免修

3 先回顾一下前一讲的 LZW 基本词汇表 输入信息流 —— 生成词汇表 查表变换 —— 输出信息流

4 LZW 中的词汇集 S= ? 字母表中 Ă 所有字母都是词 词 + 字母 是词 …. 词表大小是固定的 词表内容随输入字符流而定 达到词表上限则词表清零重新开始 有没有可能进一步优化?

5 C 语言概述 类型化的计算 标识符、算符、表达式 表达式到语句流程 功能化函数与输入输出

6 6 C 语言的数据类型 整数、浮点数编码 整数、浮点数运算 字符类型 溢出与精度问题 变量、常量及数组的使用

7 正负数(补码表达)与加减法 补码表达与加减法 加法溢出问题

8 8 8 Integer Data Types (continued)

9 9 9 Data types (integer type) Data type size ( bit ) range of values short int 16 -32K ~ 32K-1 (- 32768 ~ 32767) int32-2G ~ 2G-1 Unsigned short1664K -1 ~ 0 unsigned 324G-1 ~ 0

10 Character Data Type ASCII character set (A-Za-z0-9, etc.) char is a single byte in size also used for small integers

11

12 12 浮点数内部表达 Floating –point encoding Floating-point numbers consist of an ``exponent,'' ``significand'', and ``sign bit'' sign bit 1 Exponent 7significand 23

13 浮点数计算中的精度、截断误差 0.1 ( 十进制 ) = 110011001100110011001101 e -4 = 0.100000001490116119384765625 https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

14 Floating Point Data Types real numbers with a decimal and/or exponent (1.5 or 2.67e-3) stored in floating point format (mantissa, exponent and sign bit) single precision uses float keyword double precision uses double keyword long modifier can be applied to double

15 15 Floating-Point Data Types (cont.) float literal is indicated by appending an f or F long double is created by appending an l or L –9.234 indicates a double literal –9.234f indicates a float literal –9.234L indicates a long double literal

16 16 标识符( Identifiers ) /* A first program in C */ #include int main() { printf(“Programming is fun!\n"); printf("I see!"); return 0; } Identifiers in C consist of three types:  Reserved words  Standard identifiers  Programmer-created identifiers Reserved words Standard routine Tokens: end of statement comments

17 17 Identifiers (continued) Examples of invalid C programmer-created names:  4ab7  calculate total  While Examples of valid user-created names:  _systemBuffer  LJ113_10am C is a case-sensitive language  Total, total represent different identifiers

18 C 常量 ( Constants ) representation of a number, character or group of characters (i.e. a string) a constant has a value and a type integer, floating point, character, string

19 Integer Constants hexadecimal, starts with 0x, digits are 0 – 9, a - f (e.g. 0x5e, 0xFF, 0X1234) octal, starts with 0, digits are 0 - 7 (e.g. 017) decimal, digits are 0 – 9 (e.g. 200, 7, 32767) can have u (unsigned) or l (long) modifier suffix

20 Floating Point Constants use decimal and/or exponential notation, digits are 0 – 9 with e and + or - (e.g. 1.5, 6.22e-2, 3e1, 2e+9) single precision, uses f suffix (e.g. 1.5f) double precision, default case (e.g. 1.5) long double, uses L suffix (e.g. 1.5L)

21 Character Constants enclosed in single quotes (e.g. ‘a’) ASCII encoding alphanumerics (e.g. ‘A’, ‘7’) and special characters (e.g. ‘$’, ‘%’) escape sequences (e.g. ‘\n’, ‘\r’, ‘\\’) or in octal notation (e.g. ‘\377’, ‘\0’)

22 String Constants contiguous group of characters enclosed in double quotes (e.g. “how did I get here?”) null terminated (‘\0’ char is at the end)

23 变量( Variables )、 内置运算与数据对象 have a name, type and value type name; (e.g. int sum;) type name = initial_value; (e.g. int sum = 0;) type name1, name2; (e.g. int sum1, sum2;) Self defined identifiers

24 24 data type variableName1 [, variableName2]; int i, j = 90; short si; char c1 = 'a'; float balance, profit, loss; Variable declaration : Const of char Const of integer initialization

25 25 Declaration Statements (cont.)

26 表达式 Expression in C 23 * (67 – 9) x = 23 * (67 – 9); expression statement R valueL value Assignment

27 C Expressions constants, variables, functions calls and combinations of these (almost everything!) evaluate to a type and value examples: x, x + y, (x + y * z), sum = x + y

28 C Statements an expression followed by a semicolon ( ; ) { } – compound statement or block; the only statement not requiring a terminating ; ( ) required around control expression of a conditional or iterative statement (e.g. if statement)

29 C Operators assignment (=) and arithmetic operators (+ - * / %) precedence, which operation is done first associatively, how operations group when there is equal precedence use parentheses ( ) for more control of the grouping

30 Assignment Operator assignment = (a = 2) the value of the “object” on the left side is replaced by the value of the expression on the right side an “object” is a manipulatable region of storage (i.e. you are allowed to update its value) the left side must be an “lvalue”, an expression which evaluates to an object (e.g. a variable) has low precedence and associates right-to-left

31 Arithmetic Operators : Binary addition + (a + b) subtraction - (a - b) multiplication * (a * b) division / (a / b) modulus % (a % b) * / % have higher precedence than + - and all associate left-to-right

32 Arithmetic Operators : Unary plus + (+ a) equivalent to (0 + a) minus - (- a) equivalent to (0 – a) higher precedence than binary arithmetic operators associate right-to-left

33 Precedence of Operators defines which operators take effect first consult the operator precedence table a * b + c is equivalent to (a * b) + c instead of a * (b + c)

34 Associativity of Operators defines how operations group when the operators have equal precedence consult the operator precedence table

35 The Ambiguity of operands if different data types, the smaller type is usually converted to the bigger type before the operations are performed f1 = 12 / 100 vs f1 = 12 / 100.0

36 The printf() function formatted output to the console printf(control string, variable argument list) the control string can be simple text or can be embedded with conversion specifications (these begin with a % and end with a conversion character) the variable argument list is a comma separated list of expressions, and each argument must correspond to one conversion specification in the control string

37 printf() arg1arg2 … conversion Contral string: “%d;%f; %c” arg3 5;12.000000;A

38 38 Displaying Numerical Values(cont.) Invoking or calling the printf() function display as an integer Escape sequence

39 39 Displaying Numerical Values (cont.) printf(“conversion control sequence” , argument list)

40 printf() Conversion Characters d or i – integer in decimal o – integer in octal x – integer in hexadecimal e, f, g – floating point c – single character s – character string

41 The scanf() function formatted input from the console scanf(control string, variable argument address list) the control string can be simple text or can be embedded with conversion specifications (these begin with a % and end with a conversion character) the variable argument list is a comma separated list of argument address, and each argument address must correspond to one conversion specification in the control string

42 scanf() arg1arg2 … conversion Contral string: “%d;%f; %c” arg3 5 12.000000 A

43 scanf() function

44 scanf() function (cont.)

45

46 46 Mixed-mode expression (automatic type conversion)

47 运算中的自动类型转换

48 截断误差 Truncation error

49 第一次作业: 练习一: POJ (24 号前提交 ) http://bailian.openjudge.cn/practice/1316/ http://bailian.openjudge.cn/practice/4042/ 通过 3-4 题以上对 C 熟悉的同学可以跳过前面练习一: 练习二:输入一个字符流,要求生成一个词典,词典条目 < 32k 。在原字 符流中每个字符占一个字节,如果用词典 ID 替换其中对应的条目,每个 ID 要占 2 个字节。要求使用词典编码后原字符流的长度尽可能的小。本次作 业可以只提交算法思路及压缩比的理论计算。也可以提交源程序压缩包( 用学号命名)文件。数据文件稍晚会在课程网站有下载( 27 号前提交)。 ftp://162.105.80.206 用户名: student 口令: eecs2015


Download ppt "1 Hu Junfeng 2015/09/23 第三讲 语言 数据类型 流程控制 函数. 关于选课和免修."

Similar presentations


Ads by Google