1 / 55

An Introduction to Computer Programming

A tutor on coding , Sep 9 , 20 11. An Introduction to Computer Programming. 童 伟 华 E-mail: tongwh@ustc.edu.cn. 中国科学技术大学 数学系 http://math.ustc.edu.cn/. 内容提纲. 编程概要 编程模型 编程语言 编程环境 软件库介绍 编程进阶. 编程概要. 什么是编程?

monita
Télécharger la présentation

An Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A tutor on coding, Sep 9, 2011 An Introduction to Computer Programming 童 伟 华 E-mail: tongwh@ustc.edu.cn 中国科学技术大学 数学系 http://math.ustc.edu.cn/

  2. 内容提纲 • 编程概要 • 编程模型 • 编程语言 • 编程环境 • 软件库介绍 • 编程进阶 A tutor on coding , Sep9, 2011

  3. 编程概要 • 什么是编程? • From Wikipedia : Computer programming (often shortened to programming or coding) is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language. The code may be a modification of an existing source or something completely new. The purpose of programming is to create a program that exhibits a certain desired behaviour (customization). The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic. • 编程:编写,测试,调试,维护 • 核心:应用领域知识+算法+数据结构 A tutor on coding , Sep9, 2011

  4. 编程概要 • 历史简述 • 编程的概念出现:The concept of devices that operate following a pre-defined set of instructions traces back to Greek Mythology, notably Hephaestus and his mechanical servants. The Antikythera mechanism was a calculator utilizing gears of various sizes and configuration to determine its operation. • 可编程机械出现:1206年,伊斯兰数学家Al-Jazari's programmable Automata • 现代计算机逻辑模型的发明:1936年,Alan Turing在伦敦数学进展中发表题为“On Computable Numbers, with an Application to the Entscheidungsproblem”,提出通用图灵机的概念(计算机的逻辑模型),1944年,Von Neumann在美国Los Alamos National Laboratory 的 Manhattan Project中提出该体系结构,后被ENIAC project 使用 A tutor on coding , Sep9, 2011

  5. 编程概要 • 图灵机(Turing machine) • Logical Computing Machine :an infinite memory capacity obtained in the form of an infinite tape marked out into squares on each of which a symbol could be printed. At any moment there is one symbol in the machine; it is called the scanned symbol. The machine can alter the scanned symbol and its behavior is in part determined by that symbol, but the symbols on the tape elsewhere do not affect the behavior of the machine. However, the tape can be moved back and forth through the machine, this being one of the elementary operations of the machine. Any symbol on the tape may therefore eventually have an innings A tutor on coding , Sep9, 2011

  6. 编程概要 • 图灵机 A tutor on coding , Sep9, 2011

  7. 编程概要 • 图灵机 • 真实的计算机:图灵机的实现(the random access stored program machine) A tutor on coding , Sep9, 2011

  8. 编程概要 • Von Neumann结构 • is a design model for a stored-program digital computer that uses a processing unit and a single separate storage structure to hold both instructions and data. It is named after the mathematician and early computer scientistJohn von Neumann. Such computers implement a universal Turing machine and have a sequential architecture. A tutor on coding , Sep9, 2011

  9. 编程概要 • Personal computer的组成(包括IBM-PC, Notebook or Laptop, Tablet PC, Pocket PC等) • CPU:中央处理器 • Motherboard:主板 • Main memory:内存 • Hard disk:硬盘 • Video card:视频卡 • 辅助设备:网卡、声卡等(现在一般集成在主板上) • 输入设备:键盘,鼠标等 • 输出设备:显示器,音响等 A tutor on coding , Sep9, 2011

  10. 编程概要 • GPU编程 • Graphics processing unit: is a specialized processor that offloads 3D graphics rendering from the microprocessor • Modern GPUs are very efficient at manipulating computer graphics, and their highly parallel structure makes them more effective than general-purpose CPUs for a range of complex algorithms • 特点: • Stream processing(SIMD orMIMD,单指令多数据 或多指令多数据),适合并行计算模型 • 具有强大的浮点运算能力,尤其适合数值并行计算 • 具有独立的内存资源+高速的总线 • 编程语言:CUDA (“Compute Unified Device Architecture”), NVidia公司 • 行业标准:OpenCL(GPU通用计算的统一 标准) A tutor on coding , Sep9, 2011

  11. 编程模型 • Kernighan and Ritchie: C Programming language • ASCII code ( text characters) A tutor on coding , Sep9, 2011

  12. 编程模型 • 代码编译 • 为什么需要了解编译系统 • 代码优化:例如switch语句是否总是比if-then-else有效?函数调用的代价是多高?While循环是否总比do循环有效?等等问题,涉及编译器,机器语言 • 理解链接错误信息:link-time errors,例如静态库与动态库的区别?库的次序对链接的影响? • 避免安全漏洞:例如缓冲区溢出,堆、栈的管理,参数传递及返回等 A tutor on coding , Sep9, 2011

  13. 编程模型 • 程序的执行 A tutor on coding , Sep9, 2011

  14. 编程模型 • 程序的执行 A tutor on coding , Sep9, 2011

  15. 编程模型 • 程序的执行 A tutor on coding , Sep9, 2011

  16. 编程模型 • CPU的Caches • L1 caches: as fast as the register file • L2 caches: 5 times longer than L1 caches, but faster than 5-10 times than accessing the main memory A tutor on coding , Sep9, 2011

  17. 编程模型 • 内存的层次结构 A tutor on coding , Sep9, 2011

  18. 编程模型 • 操作系统:管理硬件 A tutor on coding , Sep9, 2011

  19. 编程模型 • Process: A process is the operating system’s abstraction for a running program • Context switching: The operating system performs this interleaving with a mechanism known as context switching • 多进程操作系统:Windows XP/Vista等,Unix/Lunix, OS2等 A tutor on coding , Sep9, 2011

  20. 编程模型 • Thread: a process can actually consist of multiple execution units, called threads, each running in the context of the process and sharing the same code and global data • Virtual Memory:Virtual memory is an abstraction that provides each process with the illusion that it has exclusive use of the main memory. Each process has the same uniform view of memory, which is known as its virtual address space • Program code and data • Heap • Shared libraries • Stack • Kernel virtual memory A tutor on coding , Sep9, 2011

  21. 编程模型 • 虚拟内存空间 A tutor on coding , Sep9, 2011

  22. 编程模型 • 网络编程 • 网络模型:ISO七层模型,TCP/IP四层模型等 • 网络协议:例如TCP/IP协议 • 编程模型:例如Server/Client, P2P等 • 套接口:TCP/IP Sockets, Windows Sockets 等 • 分布式计算 • 并行程序设计 • MPI • OpenMP A tutor on coding , Sep9, 2011

  23. 编程模型 • 信息的表示 • 输出的结果是什么? A tutor on coding , Sep9, 2011

  24. 编程模型 • 整数在计算机中的表示 • Char : 1 Bytes • Short int: 2 Bytes • Int: 4 Bytes • Long int: 4 Bytes • 多字节数据的顺序 • 0x01234567 A tutor on coding , Sep9, 2011

  25. 编程模型 • 如何处理负号? • Two’s Complement Encodings A tutor on coding , Sep9, 2011

  26. 编程模型 • 实数在计算机中的表示 • Float: 4 Bytes • Double: 4 Bytes • 国际标准:IEEE Standard 754 • 非精确表示 • 舍入误差(不满足结合律) A tutor on coding , Sep9, 2011

  27. 编程语言 • 什么是编程语言? • From Wikipedia : A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication. • 编程语言分类 • 目前大概有2500多种编程语言 • 按演化情况(第一代、第二代…第五代) • 按实际用途(系统编程语言、网络编程语言、数据库语言等等) • 按语言层次(机器语言,汇编语言,高级语言 ) • 按执行情况(编译型、解释型、虚拟机) A tutor on coding , Sep9, 2011

  28. 编程语言 • TIOBE世界编程语言排行榜(2009年8月) A tutor on coding , Sep9, 2011

  29. 编程语言 • 系统编程语言 • C • C++ • 汇编语言 • 网络编程语言 • Java • C# • XML/AJAX /PHP/PERL/Javascript/ • Ruby • Python • 数据库编程语言 • SQL(国际标准) • 人工智能编程语言 • Lisp • Prolog A tutor on coding , Sep9, 2011

  30. 编程语言 • 脚本编程语言 • 通常为解释执行,有专门的解释器 • Unix/Linux shell: bsh/csh等 • 其他如PHP/PERL/RUBY/PYTHON等( 通用动态语言 ) • 文本处理编程语言 • Tex/Latex等 • 科学计算编程语言 • Fortran • Matlab • 符号计算编程语言 • Maple • Mathematica A tutor on coding , Sep9, 2011

  31. 编程语言 • 计算机图形学、辅助几何设计、几何建模等编程 • 推荐语言:C/C++(原因:效率+通用性+可移值性+软件库)或者Java • 推荐平台:windows操作系统(原因:广泛性+图形硬件的支持)或者Xwindows(Unix/Linux系统的图形操作界面,缺点:图形硬件的支持不够) • 图形接口:OpenGL或DirectX 3D • 数值线性代数软件库:CLAPACK,LASPACK,TACUS等 • 计算几何软件库:CGAL,Wild Magic,OpenMesh, Open Geometry等 • 通用数值算法库:Numerical Recipes in C/C++ • 可视化软件库:VTK,Tecplot等 • 真实感渲染软件:PovRay,BMRT,PBRT等 A tutor on coding , Sep9, 2011

  32. 编程语言 • 重点学习C/C++ • C语言推荐书籍 • 《The C Programming language》,Brian W.Kernighan,Dennis M.Ritchie ,机械工业出版社(有中译本) • 《C程序设计》,谭浩强,清华大学出版社 • C++语言推荐书籍 • 《The C++ programming language》,Bjarne Stroustrup,高等教育出版社 (有中译本) • 《Thinking in C++》,Bruce Eckel,Chuck Alison,机械工业出版社(有中译本) • 《C++ Primmer》,Stanley B. Lippman,Josee Lajoie, Barbara E. Moo,人民邮电出版社(有中译本) A tutor on coding , Sep9, 2011

  33. 编程语言 • C++语言推荐书籍 • 《The Design and Evolution of C++》, Bjarne Stroustrup,机械工业出版社(有中译本) • 《Effective C++ 》, Scott Meyers,电子工业出版社 (有中译本) • 《More Effective C++ 》, Scott Meyers,电子工业出版社 (有中译本) • 《Inside the C++ Object Model》,Stanley B. Lippman,中国电力出版社(有中译本) • STL编程推荐书籍 • 《The C++ Standard Library: A Tutorial and Reference 》, Nicolai M. Josuttis,华中科技大学出版社(有中译本) • 《Effective STL》, Scott Meyers, 机械工业出版社(有中译本) • 《STL 源码剖析》,侯捷 ,华中科技大学出版社 • 《The C++ Standard Template Library 》,P.J. Plauger, Alexander Stepanov, Meng Lee , David R. Musser, Pearson Eduction Press • 《Generic Programming and the STL: Using and Extending the C++ Standard Template Library》,Matthew H. Austern,中国电力出版社 (有中译本) A tutor on coding , Sep9, 2011

  34. 编程语言 • Java语言推荐书籍 • 《Core Java, Volume I--Fundamentals 》,Cay S. Horstmann, Gary Cornell,机械工业出版社 (有中译本) • 《Core Java, Volume II – Advanced Features 》,Cay S. Horstmann, Gary Cornell,机械工业出版社 (有中译本) • 《Thinking in Java》,Bruce Eckel,机械工业出版社(有中译本) • Java图形推荐书籍 • 《Computer Graphics Using Java 2D and 3D 》,Y. Daniel Liang, Hong Zhang,机械工业出版社(有中译本) • 《Foundations of 3D Graphics Programming: Using JOGL and Java3D》, Jim X.Chen, Chunyang Chen, Springer A tutor on coding , Sep9, 2011

  35. 编程环境 • 硬件部分 • CPU指令:Intel 和 AMD的X86指令集(CISC指令集),IBM Power系列的Power 指令集(RISC指令集)等 • 系统架构:X86架构,IA和IA-64架构,Power架构等 • 软件部分 • 编译器 • 链接器 • 调试器 • 系统调用API • 基础类库:windows下的MFC,Linux下的QT等 • 集成开发环境:Microsoft的Visual Studio系列,Linux下的kdevelop 或GTK+GONME+glade+vim等 A tutor on coding , Sep9, 2011

  36. 编程环境 • Microsoft的Visual Studio系列 • Visual Studio 6.0:经典版本 • Visual Studio 2003 • Visual Studio 2005 • Visual Studio 2008:最新版本 • Visual Studio • Workspace:工程项目的容器 • Project:包括源代码、资源文件、编译器选项、子工程等 • Project类型:譬如Win32 Console Application ,Win32 Application ,Win32 Static Library ,Win32 Dynamic Link Library 等(对于不同工程类型,visual studio生成不同的文件,配置缺省的编译选项) A tutor on coding , Sep9, 2011

  37. 编程环境 • OpenGL示例 • 创建工程文件 • 编辑源代码 • 设置编译选项 • 编译 • 调试 • 运行 A tutor on coding , Sep9, 2011

  38. 软件库介绍 • 图形编程接口 • 通用平台的三维图形编程接口:OpenGL,适用于Microsoft Windows系列操作系统,Unix/Linux操作系统。优点:硬件加速,跨平台,开放的工业标准。最新版本:OpenGL 4.2+OpenGL Shading Language 4.1 • Microsoft Windows平台:DirectGraphics (DirectDraw + Direct3D),优点:硬件加速,功能更新快。最新版本:DirectX 11.0,包括HLSL (High Level Shading Language) 5.0,主流版本:DirectX 9.0 A tutor on coding , Sep9, 2011

  39. 软件库介绍 • OpenGL编程书籍推荐: • 《OpenGL Programming Guide: The Official Guide to Learning OpenGL Version 4.1 》(8th),OpenGL Architecture Review Board ,Dave Shreiner,Mason Woo ,Jackie Neider ,Tom Davis ,Addison-Wesley Professional Press(经典书籍,俗称红宝书,学习OpenGL必读) • 《OpenGL Shading Language》(3rd),Randi J. Rost,Bill Licea-Kane Dan Ginsburg , John M. Kessenich ,Barthold Lichtenbelt ,Hugh Malan , Mike Weiblen, Addison-Wesley Professional Press(经典书籍,俗称橙宝书) • 《OpenGL SuperBible: Comprehensive Tutorial and Reference 》(5th),Richard S. Wright ,Benjamin Lipchak,Nicholas Haemel, Addison-Wesley Professional Press • http://www.opengl.org/内有非常不错的资源 A tutor on coding , Sep9, 2011

  40. 软件库介绍 • DirectX编程书籍推荐: • 《Inside Directx (Microsoft Programming Series) 》,Bradley Bargen,Terence Peter Donnelly,Microsoft Press • 《Inside Direct3D》,Peter J. Kovach, Microsoft Press • 《DirectX9 Programmable Graphics Pipeline》,Kris Gray,Microsoft Press • 《DirectX 3D Graphics Programming Bible》,Julio Sanchez,Maria P. Canton,IDG Books Worldwide Inc Press A tutor on coding , Sep9, 2011

  41. 软件库介绍 • 数值线性代数软件库 • 书籍推荐:《Matrix Computations》, Gene H. Golub, Charles F. van Loan,The Johns Kopkins University Press • 一般线性方组求解:CLAPACK • 稀疏线性方程组求解:LASPACK,TACUS等 • 注意:一定要根据所求解线性方程组的特点,选取合适的求解方法! A tutor on coding , Sep9, 2011

  42. 软件库介绍 • 计算几何软件库 • 需要熟悉计算几何的基本算法、数据结构 • 推荐书籍: • 《Computational Geometry》(3rd), Mark de Berg, Marc van Kreveld, Mark Overmars, Otfried Schwarzkopf, Springer Press. • 《Computational Geometric in C》(2ed), Joseph O’Rourke, Cambridge University Press. • CGAL:包含计算几何中各种基本算法及数据结构的通用库。优点:2D和3D都有,功能全面,采用泛型编程,通用性较好,开源代码,功能更新快。缺点:库很大,初学者不易掌握 • Wild Magic:三维实时游戏引擎,包括图形编程中各种基本算法及数据结构的通用库。优点:功能全面,引擎系统,开源代码 • 《Geometric Tools for Computer Graphics》,Philip J. Schneider,David Eberly,Elsevier Science Inc Press. • OpenMesh:多边形网格库,主要针对网格表示、存储、操作等。优点:采用泛型编程,针对网格表示特点优化,性能较高 • Open Geometry:建立在OpenGL库基础上的几何造型高级库 A tutor on coding , Sep9, 2011

  43. 软件库介绍 • 通用数值算法库 • 数值积分、随机数生成、快数傅立叶变换、最优化算法等 • 推荐书籍:《Numerical Recipes: The Art of Scientific Computing》(3rd),William H. Press, Saul A. Teukolsky,William T. Vetterling, Brian P. Flannery • 可视化软件库 • 体绘制、标量场/向量场可视化、网格处理、图像处理等 • 推荐书籍:《The Visualization Toolkit An Object-Oriented Approach To 3D Graphics》(4th), Will Schroeder, Ken Martin, Bill Lorensen • 数据可视化软件:Tecplot • 真实感渲染软件 • 光线跟踪+辐射度+折射/反射+过程纹理+photo mapping等 • 推荐软件:PovRay,BMRT,PBRT等 A tutor on coding , Sep9, 2011

  44. 编程进阶 • 如何提高编程水平 • 勤加练习,学会思考 • 要看一些有一定深度的书籍(切勿看垃圾书籍!) • 提高编程水平的突破点 • 数据结构与算法 • 设计模式 • 编程环境 • 编程规范 • 代码调试 • 代码阅读 • 软件工程 A tutor on coding , Sep9, 2011

  45. 编程进阶 • 数据结构与算法 • 掌握基本数据结构,譬如堆、栈、队列、树、Hash表等;基本算法,譬如排序、查找等 • 推荐书籍: • 《The Art of Computer Programming 》 (Volumes 1-3),Donald E.Knuth,Pearson Education Inc Press • 《Introduction to Algorithms》(3rd) ,Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein,The MIT Press A tutor on coding , Sep9, 2011

  46. 编程进阶 • 设计模式 • 在生活和工作当中,不断地重复一些事物和事情的方法,可以看作是一种设计模式 • Christopher Alexander : "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" • 如何设计可复用的面向对象软件?必须要找到相关的对象,以适当的粒度将它们归类,再定义类的接口和继承层次,建立对象之间的基本关系。你的设计应该对手头的问题有针对性,同时对将来的问题和需求也要有足够的通用性。每一个设计模式系统的命名、解释和评价了面向对象系统中一个重要的和重复出现的设计。设计模式使人们可以更加简单方便的复用成功的设计和体系结构。 • 每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样你就可以一次又一次的使用该解决方案而不做重复性的劳动。 A tutor on coding , Sep9, 2011

  47. 编程进阶 • 基本要素 • The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two. • The problem describes when to apply the pattern • The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations • The consequences are the results and trade-offs of applying the pattern • 模式分类 • 按照模式的目的性准则,模式可以分为创建型模式、结构型模式和行为型模式。创建型模式与对象的创建有关;结构型模式处理类和对象的组合;行为型模式对类和对象怎样交互和怎样分配职责进行描述 • 按照模式的范围准则,模式可以分为类模式和对象模式。类模式处理类和子类的关系,这些关系通过继承建立,是静态的,在编译时就确定下来。对象模式是处理对象之间的关系,这些关系在运行时刻是可以变化的,更具动态性 A tutor on coding , Sep9, 2011

  48. 编程进阶 • 设计模式与框架 • 面向对象软件系统一般划分为三大类:应用程序、工具箱和框架(Framework) • 应用程序:指开发工具如Visual Studio 2008、Java等 • 工具箱:指开发程序时使用的工具,如服务器控件、控件、组件等 • 框架:是一个可复用的设计构件,它规定了应用的体系结构,阐明了整个设计、协作构件之间的依赖关系、责任分配和控制流程,表现为一组抽象类以及其实例之间协作的方法,它为构件复用提供了上下文(Context)关系。简单说就是使用别人搭好的舞台,你来做表演。而且,框架一般是成熟的,不断升级的软件 • 软件开发中的重用 • 软件开发中三种级别的重用:内部重用,即在同一应用中能公共使用的抽象块;代码重用,即将通用模块组合成库或工具集,以便在多个应用和领域都能使用;应用框架的重用,即为专用领域提供通用的或现成的基础结构,以获得最高级别的重用性 • 构件通常是代码重用,而设计模式是设计重用,框架则介于两者之间,部分代码重用,部分设计重用,有时分析也可重用 A tutor on coding , Sep9, 2011

  49. 编程进阶 • 设计模式推荐书籍 • 《Design Patterns:Elements of Reuseable Object-Oriented Software》,Erich Gamma,Richard Helm,Ralph Johnson,John Vlissides,Addison-Wesley Professional (机械工业出版社,双语版) • 《Head First Design Patterns》,Elisabeth Freeman, Eric Freeman, Bert Bates, Kathy Sierra,O‘Reilly Press(东南大学出版社,影印版,中文版) • 《大话设计模式 》,程杰,清华大学出版社 A tutor on coding , Sep9, 2011

  50. 编程进阶 • 编程规范 • 编程风格:函数名、变量名的选取,代码缩进,表达式和语句的书写,代码注释(包括使用一些辅助工具,譬如Doxygen)等等 • 代码质量:可靠性、安全性、时间效率、空间效率、可移植性、可维护性等 • 编程语言:如何合理、有效的使用编程语言?软件开发者需要掌握一些窍门 A tutor on coding , Sep9, 2011

More Related