Download presentation
Presentation is loading. Please wait.
Published byIsabel Jackson Modified over 8 years ago
1
1 Primitive Data Types and Operations C/C++ Language Programming Wanxiang Che
2
2 Introducing Programming with an Example Computing the Area of a Circle Programs? = Data Structures + Algorithms –Niklaus Wirth (Turing Award, 1984)
3
3 Data Structures + Algorithms #include int main() { radius; area; // Step 1: Read in radius // Step 2: Compute area // Step 3: Display the area }
4
4 Trace a Program Execution #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; } no value radius allocate memory for radius
5
5 Trace a Program Execution no value radius memory #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; } no value area allocate memory for area
6
6 Trace a Program Execution 20 radius no value area assign 20 to radius #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; }
7
7 Trace a Program Execution 20 radius memory 1256.636 area compute area and assign it to variable area #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; }
8
8 Trace a Program Execution 20 radius memory 1256.636 area print a message to the console #include int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area std::cout << "The area is "; std::cout << area << std::endl; }
9
9 Variables ( 变量 ) // Compute the first area radius = 20; area = radius * radius * 3.14159; std::cout << area; // Compute the second area radius = 30; area = radius * radius * 3.14159; std::cout << area;
10
数据类型( Data Type ) 10 C/C++DATATYPESC/C++DATATYPES 基本类型 basic types 构造类型 structures 指针类型 pointer 空类型 void 数值类型 字符类型 char 枚举类型 enum 整 型 integer 浮点型 floating point 单精度型 float 双精度型 double 短整型 short 长整型 long 整型 int 数组 array 结构体 structure 共用体 union
11
Why distinguish Data Types? Data Types indicate –data representations, 小数, 整数, 科学计数, 符号 … –size of storage in memory –legal numeric area( 合法的 取值范围 ) 不可思议 10 60 –operations to participate ( 可参与的运算 ) 11
12
12 b , B , KB , MB , GB , TB bit :位 Byte :字节 Kilobyte(KB) : K Megabyte(MB) :兆 Gigabyte(GB) : G Terabyte(TB) : T 1 TB == 1,024 GB 1 GB == 1,024 MB 1 MB == 1,024 KB 1 KB == 1,024 B 1 B == 8 b
13
b , B , KB , MB , GB , TB 世界上有 10 种人, 1 种人懂二进制, 1 种人 不懂二进制 一个位有多大? – 只能是 “0” 或者 “1” ,这叫二进制 二进制诠释了计算机的哲学 – 复杂事物都是由若干种简单事物构成 13
14
b , B , KB , MB , GB , TB 一个字节有多大? – 可以表示数字 0~255 – 保存一个字符(英文字母、数字、符号), ASCII 编码 – 两个字节保存一个汉字 GB2312 , 6763 字 GB13000.1 , 20902 字 GB18030 , 27533 字 BIG5 , 13000 字 – 两个字节保存一个宽字符, UNICODE 编码 65536 个字符 14
15
Basic Types int an integer, typically reflecting the natural size of integers on the host machine Usually be stored in 4 bytes of memory in most of computers float single-precision floating point number 单精度浮点数 usually in 4 bytes double double-precision floating point number 双精度浮点数 usually in 8 bytes char a single byte, capable of holding one character in the local character set store 256 ASCII characters or integer of 0~255 15
16
Qualifiers of Basic Type 数据类型修饰符 short 2 bytes short int long 4 bytes long int long double 10 bytes specifies extended-precision floating point 长双精度 ( 高精度 ) 浮点 signed and unsigned May be applied to char or any integer unsigned specifies the number to be positive or zero signed specifies the number to be negative, positive or zero; the word signed can be omitted in default declaration 16 The word int can be omitted in such declarations
17
17 Storage of basic data types Unlike Java, size is machine-dependent!
18
每种类型究竟多长? 答曰: –char <= short <= int <= long <= long long( 有的编译器不支持 ) – 不固定长度,取决于平台(硬件、 OS 和编译器) sizeof operator –Find the size of a type –std::cout << sizeof(int) << " " << sizeof(long) << " " << sizeof(double); 18
19
19 Assignment Statements x = 1; y = x; x = 2; // What’s the value of y? x = y = z; // How to understand?
20
20 Reading Input from Keyboard Using the std::cin object to read input from the keyboard. –std::cin >> radius; –Give some prompts
21
21 Overflow ( 溢出 ) The range of short is -32768~32767 Assign a short with larger number ? Try …
22
22 Overflow Problems A machine with Windows 95/98 can only run 49.7 days continuously Why? –Windows 自启动时刻起,有一个计数器,记录系统 已经运行了多少毫秒。 – 这个计数器个 unsigned long –unsigned long 的最大值: 4294967295 – 一天有 24*60*60*1000 = 86400000 毫秒 –4294967295 / 86400000 = 49.71026961805…… – 当 49.7 天的时候,此计数器会溢出,引起死机
23
23 Numeric Operators
24
24 Remainder Operator 5 % 2 yields 1 (the remainder of the division) Very useful –Judging even or odd number –Suppose today is Saturday. What day is in 10 days? Tuesday:
25
25 Arithmetic Expressions _____ is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
26
26 Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using the formula:
27
27 Type Casting Implicit casting double d = 3; (type widening) Explicit casting // type narrowing // Fraction part is truncated int i = (int)3.9;
28
Shorthand Assignment Operators 28 OperatorExampleEquivalent +=i += 8i = i + 8 -=f -= 8.0f = f - 8.0 *=i *= 8i = i * 8 /=i /= 8i = i / 8 %=i %= 8i = i % 8
29
29 Increment and Decrement Operators
30
30 Increment and Decrement Operators, cont. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times –k=(i++)+(++i)+(i++);
31
Notes of Variables 变量使用前必须声明 int main() { int j = i + 1; cout << "j is " << j; return 0; } 没有被赋值的变量其 值不定 int main() { int i; int j = i + 1; cout << "j is " << j; return 0; } 31
32
Character Data Type Character Constant –'a' , 'A' , '5' , '%' , '$'…… – 单引号内只能有一个字符,除非用 “ \ ” 开头 就是一个普通整数,也可以参与各种数学运算 – 每个字符对应唯一一个 0~255 之间的数值,可从 ASCII 表 (American Standard Code for Information Interchange) 查出 ASCII 表 – 注意: '5' 和 5 的区别, A 和 'A' 的区别 风马牛,不相及 – 字符可以参与运算了! 'B' - 'A' , '2' - '0’ 32
33
33 Escape Sequences for Special Characters ( 转义字符 )
34
34 Appendix B: ASCII Character Set char is a small int
35
35 char operation Convert lowercase letter into uppercase main() { char ch; ch = 'b' - 32; // ch = 'b' - ('a' - 'A') ; cout << ch << endl; }
36
36 Program Errors Syntax Errors –No type define, identifier name, … Runtime Errors –Zero Division Logic Errors –Finding difficultly
37
Debug Bug – Errors – In 1947, “first actual case of bug being found” Debug ( 调试 ) – Find and fix bugs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.