程序设计基础及语言 -...
TRANSCRIPT
程序设计基础及语言— C++大学教程
Dr.杨 明 副教授/博导Tel(O): 025-52091012
QQ: 184093156Office: 计算机楼360室
[email protected]东南大学计算机科学与工程学院
江苏省网络与信息安全重点实验室
计算机网络和信息集成教育部重点实验室
http://dwz.cn/1K8F7C
2
计算机系统结构
计算机科学与技术一级学科
计算机软件与理论
计算机应用技术
图像处理与科学可视化
2017年9月,入选“双一流”建设学科
计算机应用技术 国家重点学科
课前简介
1960 -- 1979 -- 1981 -- 2006
2016年1月28日, 首批新增列“网络空间安全”一级学科博士点2017年9月16日,入选首批“一流网络安全学院建设示范项目高校”
36解算装置及技术 招收硕士生 独立为计算机系 升格为学院
3
计算机硬件课程群
软件基础课程群
科学理论课程群
计算机网络课程群
软件技术课程群
数据库课程群
人工智能课程群
图形图像课程群
课前简介
4
“软件基础”课程群
课程目标
支撑数据结构、算法、软件工程等课程
课前简介
5
C++大学教程
(第八版) (英文版)(美) P.Deitel, H.Deitel电子工业出版社
课前简介
6
Syllabus
Poss
ible
con
tent
s to
co
ver
7
程序设计基础与语言:2学期
本学期学时:80学时(48+32)
平时成绩:作业+上机实验+到课率
期末考试(英文)成绩:• 笔试部分
(代码阅读20分+代码填空20分+编程20分)
• 机考部分(编程40分)
总成绩:30%*平时成绩 +70%*期末考试成绩
课前简介
8
实验安排
第04, 06, 08, 10, 12, 14, 16周周四下午06-09节课 (14:00-17:00)
第13周周四晚上11-14节课 (18:30-21:30)
课前简介
9
课程主要参考书《C++程序设计教程》, 钱能, 清华大学出版社
《C++编程思想》, Bruce Eckel, 机械工业出版社
课件资料下载http://cse.seu.edu.cn/people/yangming2002[ http://dwz.cn/1K8F7C ]
网络资源
http://msdn.microsoft.com/zh-cnhttp://www.csdn.net
课前简介
11
OBJECTIVES Basic hardware(硬件) and software(软件)
concepts. Basic object-technology(对象技术) concepts:
classes(类), objects(对象), attributes(属性), behaviors(行为), encapsulation(封装) and inheritance(继承).
Different types of programming languages. Typical C++ development environment Internet (互联网) and the World Wide Web(万维
网) UML, Unified Modeling Language(统一建模语言)
12
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
13
U.S.DoD ARPANet (1960s) Internet (互联网, 1973)
• 工作更方便
• 信息获取更容易
• 商业模式的改变
• 产品和服务的虚拟化
WWW (万维网, 1991)• HTML (超文本标记语言) • HTTP (超文本传输协议)
Web 2.0 (2004)• Social Networking• 博客、微博等
1.1 Internet, WWW and Computer
Web 2.0是一种新的互联网方式, 通过Web Applications促进网络上人与人间的信息交换和协同合作, 其模式更加以用户为中心. [Wikipedia]
《纽约客》(The New Yorker)
14
1.1 Internet, WWW and Computer2016中国企业500强
1 国家电网公司
2 中国石油天然气集团公司
3 中国石油化工集团公司
4 中国工商银行股份有限公司
5 中国建设银行股份有限公司
6 中国建筑股份有限公司
7 中国农业银行股份有限公司
8 中国银行股份有限公司
9 中国平安保险(集团)股份有限公司
10 中国移动通信集团公司
11 上海汽车集团股份有限公司
12 中国人寿保险(集团)公司
13 中国铁路工程总公司
14 中国铁道建筑总公司
15 国家开发银行股份有限公司
16 东风汽车公司
17 华润(集团)有限公司
18 中国南方电网有限责任公司
19 太平洋建设集团有限公司
20 中国兵器装备集团公司
27 华为技术有限公司
15
算 盘 电子管计算机
1946年,美国物理学家莫奇利任总设计师,研制成功世界上第一台电子管计算机ENIAC(图中左为莫奇利)
晶体管计算机
集成电路计算机
1964年,美国IBM公司研制成功一个采用集成电路的电子 计算机
1.1 Internet, WWW and Computer
《浪潮之巅》人民邮电出版社·吴军
16
1.1 Internet, WWW and Computer
17
冯·诺伊曼: 计算机之父• 二进制
• 存储程序
艾伦·图灵: 计算机科学之父• 图灵机
• 1966, Turing Award(ACM, Association for Computing Machinery), A.M. 图灵奖
• 姚期智, 2000年
冯·诺伊曼
Moore’s Law: 戈登·摩尔, 集成电路上可容纳的晶体管
数目, 约每隔18个月便会增加一倍, 性能也将提升一倍
1.1 Internet, WWW and Computer
18
Computer – programmable machine designed to follow instructions
Operating System (OS) - system software that manages computer hardware and software resources and provides common services for computer programs
Program – instructions tell computer to do something
Programmer – person who writes instructions (program) to make computer perform a task
1.1 Internet, WWW and Computer
19
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
20
1.2 机内信息表示
1.2.1 数制的基本概念
1.2.2 常用的数制
1.2.3 不同数制间的转换
1.2.4 二进制编码
1.2.5 计算机中信息的存储
21
1.2.1 数制的基本概念
基数:数制中数字的个数
数位:每个数字在数中的位置
数位值(权):每个数位对应的单位值
22
1.2.2 常用的数制(1)
十进制数:
组成:由0 - 9 十个数字组成
基数: 10 逢十进一
表示方法:(15)10多项式表示:
(115.5)10 =1X102+ 1X101+5X100+5X10-1
23
二进制数:
组成:由0、1两个数字组成
基数: 2 逢二进一
表示方法:(101)2多项式表示:
(111.1)2 =1X22+ 1X21+1X20 +1X2-1
1.2.2 常用的数制(2)
24
1.2.3 不同数制间的转换(1)
二进制数转换为十进制数
方法:先将被转换的数用多项式表示法表示,再求出对应的和
举例:(101.1)2
= 1x22 + 0x21 + 1x20 + 1x2-1
= 4 + 0 + 1 + 0.5= 5.5
25
1.2.3 不同数制间的转换(2)
十进制整数转为二进制数
方法:求余法,即“除以2,取余数,将所
得余数倒读”
举例:13除以2 余数 余数排列
……1……0……1
1326232120
……1所以:13 =(1101)2
26
1.2.4 二进制编码
七位二进制数组成 — ASCII码 (America Standard Code for Information Interchange)
如:A的ASCII码表示为 01000001(P770.附录B)
27
位(bit)
字节(Byte)基本存储单元
1.2.5 计算机中信息的存储(1)
28
1.2.5 计算机中信息的存储(2)
位(bit):计算机存储信息的最小单位, 指二进制数中的一个数位, 其值为“0”或“1”.
字节(Byte):8个二进制位称一个字节, 即一个存储单元.
存储容量:存储器中包含存储单元的数量.
29
KB(千字节):1KB=210B=1024BMB(兆字节):1MB=1024KBGB(千兆字节):1GB=1024MB
-----存储容量的换算
1.2.5 计算机中信息的存储(3)
30
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
31
1.3 Machine, Assembly and High-level Language
Three types of programming languages• Machine Language(机器语言)
• Assembly Language(汇编语言)
• High-Level Language(高级语言)
32
1.3 Machine, Assembly and High-level Language
Machine languages(机器语言)
Strings of numbers giving machine specific instructions
machine dependent Object Code(目标代码)
Example:+1300042774+1400593419+1200274027
33
1.3 Machine, Assembly and High-level Language
Assembly languages(汇编语言)
English-like abbreviations representing elementary computer operations
Translated via Assemblers 汇编器
Example:LOAD BASEPAYADD OVERPAYSTORE GROSSPAY
34
1.3 Machine, Assembly and High-level Language
High-level languages(高级语言)
Codes similar to everyday English, Use mathematical notations
Translated via Compilers 编译器,△Interpreter解释器
Example:grossPay = basePay + overPay
35
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
36
1.4 C,C++,Java and other High-level languages
1.4.1 History of C and C++1.4.2 C++ Standard Library1.4.3 History of Java1.4.4 FORTRAN,COBOL,Pascal and Ada1.4.5 Basic,Visual Basic,Visual C++, C#
and .NET
37
BCPL
B
1967, Martin Richards, OS & Compiler
1970, Ken Thompson, early Versionof Unix, BELL
C1972, Dennis Ritchie, Unix,BELL
ANSI C1990, ANSI & ISO, standardized、hardware-independent,Updated in 1999
C++Early 1980s, Bjarne Stroustrup,object-oriented programming
1.4.1 History of C and C++
C++98, ISO/IEC 14882:1998
38
BCPL
B
1967, Martin Richards, OS & Compiler
1970, Ken Thompson, early Versionof Unix, BELL
C1972, Dennis Ritchie, Unix,BELL
ANSI C1990, ANSI & ISO, standardized、hardware-independent,Updated in 1999
C++Early 1980s, Bjarne Stroustrup,object-oriented programming
1.4.1 History of C and C++
C++98, ISO/IEC 14882:1998
39
1.4.2 C++ Standard Library
C++ programs consist of pieces called Functions(函数) & Classes(类)• A programmer can all create his own
Advantage: the programmer knows exactly how it worksDisadvantage: time consuming
• Otherwise he can use• C++ Standard Library (C++标准库)• C Standard Library
40
Conclusions• 学习
• C++ Language itself• Functions and Classes in C++ Standard Library
• 项目开发
• 自己的函数和类
• 合作者开发的函数和类
• 标准库
1.4.2 C++ Standard Library
41
1.4.3 History of Java Sun Microsystems
• A C++ based Language – OAK, 1991• Java, 1995 formally announced
Java is used to • Intelligent consumer electronic devices• WWW - Create Web pages with dynamic and
interactive content (Applet)• Develop large-scale enterprise applications• Enhance the functionality of Web servers• Provide applications for consumer devices (such
as Cell phones, PDA Smart Phone: Android)
42
1.4.4 Other Programming Languages
Objective-C• 一种通用、高级、面向对象的编程语言, 扩展了标准的
ANSI C编程语言
• main programming language used by Apple for the OS X and iOS operating systems, and their respective application programming interfaces (APIs) prior to the introduction of Swift
Pascal• designed for teaching structured programming• DELPHI (Object Pascal, MIS,管理信息系统)
FORTRAN, COBOL, Ada, BASIC
43
1.4.4 Other Programming Languages
Python• 解释型、面向对象、动态数据类型
• CMU的编程基础和MIT的计算机科学及编程导论使用Python语言讲授
• 适合科研人员处理实验数据、制作图表, 甚至开发科学计算应用程序
• 在Google内部的很多项目, 例如Google App Engine使用C++编写性能要求高的部分, 然后用Python或Java/Go调用相应的模块
44
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
45
1.5.1 Why Object?1.5.2 What is Object? 1.5.3 Object-oriented Design (OOD) 1.5.4 Object-oriented Analysis and
Design (OOAD)
1.5 Object Technology
46
1.5.1 Why Object ? (1)
Procedure Oriented(面向过程)• FORTRAN, COBOL, Pascal, Basic and C• 将复杂的过程按功能分层分解, 以解决问题
Structured Programming(结构化编程, 1970s)• 描述任何实体的操作序列只需要三种基本控制结构
Sequence (顺序结构)Selection (选择结构)*3Repetition (循环结构)*3
消除以往编程过程中无限制使用转移语句的情况:goto
CH4, 5
47
1.5.1 Why Object ? (2)
用户需求
主要问题
• 软件重用性差
• 软件可维护性差
• 开发出的软件不能很好地满足用户需要
系统总体功能
功能1 功能2 功能n……
过程1 过程2 过程n……
48
1.5.1 Why Object ? (2)
用户需求
主要问题
• 软件重用性差
• 软件可维护性差
• 开发出的软件不能很好地满足用户需要
系统总体功能
功能1 功能2 功能n……
过程1 过程2 过程n……
49
电 器
交通工具
Humans
Attributes ? 属性
Behaviors ?行为
Life
1.5.1 Why Object ? (3)
Humans learn about existing objects by studying their Attributes and observing their behaviors.
50
1.5.1 Why Object ? (4)
Conclusion: Structured Programming + Object oriented Programming (面向对象编程) • Their internal structure is often built using
structured-programming techniques. • Also, the logic of manipulating objects is
occasionally expressed with structured programming.
51
1.5.2 What is Object?(1)
Object oriented (面向对象)• Reusable software components (可重用的软件组件)• model items in the real world (现实事物的建模)• Any noun can be represented as an object• Object = Attribute + Behavior
Class(类): 对同一类Objects的共同描述和抽象
• C++中的一种用户定义的数据类型
• Attribute: member data• Behavior: method / Member Functions
52
1.5.2 What is Object?(2)
Encapsulates (封装) attributes(属性) and operations (操作, behaviors) into objects
Inheritance(继承) : 人/猩猩 - 生物
Information hiding• Communicating with one another across
well-defined interfaces using Message• Without knowing how it is implemented• 钟、手表、手机等
53
1.5.2 What is Object?(3) Procedure oriented vs Object oriented
• focused on actions (verbs) rather than on things or objects (nouns)
• break down a programming task into• variables, data structures, and subroutines• Objects, encapsulating its own data and methods
procedural programming uses procedures to operate on data structures
object-oriented programming bundles the two together so an "object" operates on its "own" data structure
54
1.5.2 What is Object?(4)
1. 开始游戏
2. 黑子先走
3. 绘制画面
4. 判断输赢
5. 轮到白子
6. 绘制画面
7. 判断输赢
8. 返回步骤29. 输出最后结果
1. Player: 黑白双方, 这两方的行为是一模一样的(走子、悔棋)
2. Chessboard: 棋盘系统, 负责绘制画面
3. Rule: 规则系统, 负责判定诸如犯规、输赢等.
面向过程 面向对象
悔棋?!
55
1.5.3 Object-oriented design
Typical steps• determine the objects required to
implement that system• determine the attributes the objects will
have• determine the behaviors these objects will
exhibit• specify how the objects interact with one
another to meet the system requirements
56
1.5.4 Object-oriented Analysis and Design
Analyzing project's requirements and Developing a design that satisfies them from an object-oriented point of view, it is called object-oriented analysis and design
小型项目
• Pseudocode(伪代码): an informal text-based means of expressing program logic (Ch4)
大型项目• UML
57
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
58
GNU
1.6 Typical C++ Development Environment
Borland C++ Builder
Microsoft Visual C++Microsoft Visual C++ .NET
GNU C++ on LinuxGNU C++ on the Cygwin
(vi / emacs, gcc / g++)
59
.
.
.
.
.
.
1.6 Typical C++ Development Environment
1. Edit
2. Preprocess宏、文件包含、条件编译
3. Compile编译错误(语法等)
4. Link
5. Load
6. ExecuteRuntime Error(运行时错误)Fatal致命错误Logic逻辑错误
60
.
.
.
.
.
.
1.6 Typical C++ Development Environment
1. Edit
2. Preprocess宏、文件包含、条件编译
3. Compile编译错误(语法等)
4. Link
5. Load
6. ExecuteRuntime Error(运行时错误)Fatal致命错误Logic逻辑错误
61
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
62
1.7 Test-Driving a C++ ApplicationOpen the Command Prompt window
• 开始-所有程序-附件-命令提示符
• WIN+R - cmd Locating the completed application Running the GuessNumber application Entering your first guess Entering another guess Entering additional guesses Playing the game again or exiting the
application Close the Command Prompt window
63
Fig. 1.1 | Opening a Command Prompt window and changing the directory.
64
Fig. 1.2 | Running the GuessNumber application.
65
Fig. 1.3 | Entering your first guess.
66
Fig. 1.4 | Entering a second guess and receiving feedback.
67
Fig. 1.5 | Entering additional guesses and guessing the correct number.
68
Fig. 1.6 | Playing the game again.
69
输入数据的特点?(500、250、125、62、31、46、38、42)
为何每次执行该程序让猜的第一个数都是42?
Fig. 1.7 | Exiting the game.
70
Topics 1.1 Internet, WWW and Computer 1.2 Information representation(机内信息表示) 1.3 Machine languages, Assembly
Languages and High-level Languages 1.4 C, C++, Java and other High-level
languages 1.5 Object Technology 1.6 Typical C++ Development Environment 1.7 Test-Driving a C++ Application 1.8 UML
71
1.8 UML
UML (Unified Modeling Language, 统一建模语言). 1997年, OMG组织(Object Management Group对象管理组织)发布.
目标: 为开发团队提供标准通用的图形化设计语言来开发和构建计算机应用. 通过使用UML, 开发人员能够阅读和交流系统架构和设计规划.
72
Name
Attributes
Operations
Relations
UML - Class diagram (类图)
73
Name
Attributes
Operations
Relations
Class diagrams show the classes of the system, their interrelationships(including inheritance, aggregation, and association), and the operations and attributesof the classes.
UML - Class diagram (类图)
74
Start
Fork
Merge Joint
End
Branch
描述系统的工作流程
UML - Action Diagram(活动图)
75
Summary
计算机科学的基本概念
• What is Computer?• Computer Organization• 数制
熟悉面向对象的一些基本概念:类、对象、属性、行为和继承
熟悉不同类型的编程语言
• 机器语言
• 汇编语言
• 高级语言
初步了解UML