《计算机文化、程序设计.docx》由会员分享,可在线阅读,更多相关《计算机文化、程序设计.docx(263页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、计算概论-计算机文化、程序设计Introduction to Computing: Computer Culture, and Programming闫宏飞陈卿编著by Hongfei Yan and Chong Chen2010/9/23内容简介本书主要是汇编各书和参考资料而成,比较系统地介绍了计算机文化,和程序设计。通过这两部分有机的结合(前者占1/3,后者占2/3),即理论与实践结合,使学生理解和掌握有关计算机和信息技术的基本概念和基本原理,对计算机学科有全局性的认识;学会使用计算机进行信息处理,熟练掌握C+语言编程技术,为后续相关课程的学习打好基础。本书层次分明,由浅入深,具有学习和实
2、用双重意义。本书可作为高等院校各专业一、二年级学生的教学参考书和技术资料,对广大从事计算机相关研究和应用开发的科技人员也有很大的参考价值。*刖 R计算概论是普通高校面向理工科低年级学生开设的计算机基础教育课。课程前1/3部分为计算机文化,后2/3部分为程序设计。任教此课两年来,发现没有合适的教材,因此根据授课经验,汇编各书和参考资料,编成此书。编者2009年1月于北大燕园hITb第1章引论.11.1 计科学21.2 摩尔定律31.3 Scope of Problems5计i化9第2章统102.1 Computer Introduction102.1.1 TURING MODEL112.1.2
3、VON NEUMANN MODEL162.1.3 Computer components182.1.4 History192.1.5 Practice set242.2 计射L系统漫游252.2.1 Information is Bits + Context272.2.2 Programs Are Translated by Other Programs into Different Forms292.2.3 It Pays to Understand How Compilation SystemsWork312.2.4 Processors Read and Interpret Instru
4、ctions Stored in Memory322.2.5 Caches Matter382.2.6 Storage Devices Form a Hierarchy392.2.7 The Operating System Manages the Hardware402.2.8 Systems Communicate With Other Systems Using Networks472.2.9 The Next Step49第3章和数的zj523.1 数据的表示523.2 数的表示66第4章程序设计语言和开发环境704.1 Wtig 言70) I *+*,+*+*+*+d3*.8)下篇出
5、设计86第5章875.1 Getting Started875.2 Fundamental Types915.3 Arithmetic Operator1005.4 Control Structures123第6章M和结构1536.1数组1531.1.1 Initializing arrays!未定义书签。1.1.2 Accessing the values of an array!未定义书签。1.1.3 Multidimensional arrays!未定义书签。6.2结构1646.2.1 Data structures错误!未定义书签。6.2.2 Pointers to structure
6、s!未定义书签。6.2.3 Nesting structures!未定义书签。Quiz: Structures199第7章C+标准库2057.1 C Language library2067.2 Input/Output Stream library2077.3 String library2087.4 STL: Standard Template Library208第8章函数和递归错误!未定义书签。8.1 Functions with no type. The use of void错误!未定义书签。8.2 Arguments passed by value and by referenc
7、e!未定义书签。8.3 Default alues in mrameters!未定义书签。8.4 Overloaded functions!未定义书签。8.5 INLINE FUNCTIONS未定义书签。8.6 RECURSIVITY未定义书签。8.7 Declaring functions未定义书签。in9.1.1 Reference operator (&)2129.1.2 Dereference operator (*)2149.1.3 Declaring variables of pointer types2159.1.4 Pointers and arrays2189.1.5 Poi
8、nter initialization2209.1.6 Pointer arithmetics2219.1.7 Pointers to pointers2239.1.8 void pointers2249.1.9 Null pointer2259.1.10 Pointers to functions2269.2 DYNAMIC Momory2279.2.1 Operators new and new2279.2.2 Operators delete and deletc2299.2.3 Dynamic memory inANSI-C231第10章 VARIABLES: A DEEPER LOO
9、K23210J Memory organiz ation23210.2 VARIABLE SCOPE23410.3 Understanding pointers 235第11章算法23811.1 The Role of Algorfthms in Computing23911.1.1 Algorithms23911.1.2 Algorithms as a technology24411.2 算法的概念24711.3 算法的三种基本结构24711.4 算法的表示24811.5 介绍几种基本算法24811.6 迭代与递归248第12章龄设计24912.1 简单计算题24912.2 模拟24912.
10、3 可模型化的问题250Introduction (Beginner)251Elementary254Intermediate255Upper-Intermediate257Advanced258参考文献262第1章引论计算机文化这个词的出现到被广泛认可的时间并无确切的考证,但基本上是在20世纪80年代后期。计算机开始是一种装置,进而到一门学科,再发展成为一种“文化”,它对人类的影响力之大的确令人惊叹。计算机文化是指能够理解计算机是什么,以及它如何被作为资源使用的。简单地说,计算机文化不但是知道如何使用计算机,更重要的是知道什么时候使用计算机。在当今世界,几乎所有专业都与计算机息息相关。但是,
11、只有某些特定职业和学科才会深入研究计算机本身的制造、编程和使用技术。用来诠释计算机学科内不同研究领域的各个学术名词的涵义不断发生变化,同时新学科也层出不穷。五个主要的计算机学科(disipline of computing)包括1计算机工程学(Computer Engineering),是电子工程的一个分支,主要研究计算机软硬件和二者间的彼此联系。计算机科学(ComputerScience),是对计算机进行学术研究的传统称谓。主要研究计算技术和执行特定任务的高效算法。该门学科为我们解决确定一个问题在计算机领域内是否可解,如可解其效率圳何,以及如何作成更加高效率的程序。时至今日,在计算机科学内已
12、经派生了许多分支,每一个分支都针对不同类别的问题进行深入研究。 软件工程学(Software Engineering),着重于研究开发高质量软件系统的方法学和实践方式,并试图压缩并预测开发成本及开发周期。 信息系统(Infbrmation Systems),研究计算机在一个广泛的有组织环境(商业为主)中的计算机应用。 信息技术(Infbrmation Technology),指计算机相关的管理和维护。计算概论课程关注的是计算机学科。较大规模的致力于计算机科学的组织有:美国计算机协会(Association of Computing Machinery,简称ACM);美国电气电子工程师协会(In
13、stitute of Electrical and Electronics Engineers.简称为 IEEE)。1 Computing Curricula 2005: The Overview Report, http:www.acm.org/education/curric vols/CC2005-March06Final.pdf1.1 计算机科学计算机科学是一门包含各种各样与计算和信息处理相关主题的系统学科,从抽象的算法分析、形式化语法等等,到更具体的主题如编程语言、程序设计、软件和硬件等。作为一门学科,它与数学、计算机程序设计、软件工程和计算机工程有显著的不同,却通常被混清,尽管这些
14、学科之间存在不同程度的交叉和覆盖。2计算机科学研究的课题是: 计算机程序能做什么和不能做什么(可计算性); 如何使程序更高效的执行特定任务(算法和复杂性理论); 程序如何存取不同类型的数据(数据结构和数据库); 程序如何显得更具有智能(人工智能); 人类如何与程序沟通(人机互动和人机界面)。计算机科学的大部分研究是基于“冯诺依曼计算机”和“图灵机”的,它们是绝大多数实际机器的计算模型。作为此模型的开山鼻祖,邱奇-图灵论题(Church-Turing Thesis)表明,尽管在计算的时间,空间效率上可能有所差异,现有的各种计算设备在计算的能力上是等同的。尽管这个理论通常被认为是计算机科学的基础,
15、可是科学家也研究其它种类的机器,如在实际层面上的并行计算机和在理论层面上概率计算机、oracle计算机和量子计算机。在这个意义上来讲,计算机只是一种计算的工具:著名的计算机科学家Dijkstra有一句名言“计算机科学之关注于计算机并不甚于天文学之关注于望远镜。计算机科学根植于电子工程、数学和语言学,是科学、工程和艺术的结晶。它在20世纪最后的三十年间兴起成为一门独立的学科,并发展出自己的方法与术语。早期,虽然英国的剑桥大学和其他大学已经开始教授计算机科学课程,但它只被视为数学或工程学的一个分支,并非独立的学科。剑桥大学声称有世界上第一个传授计算的资格。世界上第一个计算机科学系是由美国的普渡大学
16、在1962年设立,第一个计算机学院于1980年由美国的东北大学设立。现在,多数大学都把计算机科学系列为独立的部门,一部分将它与工程系、应用数学系或其他学科联合。计算机科学领域的最高荣誉是ACM设立的图灵奖,被誉为是计算机科学的诺贝尔奖。它的获得者都是本领域最为出色的科学家和先驱。华人中首获图灵奖的是姚期智博士。他于2000年以其时计算理论做出的诸多根本性的、意义重大的”贡献而获得这一崇高荣誉。 http:zh.wikipedia.org/wiki/计算机科学1.2 摩尔定律http:en.w汰ipedia.org/wiki/Moore%27s LawMoores law describes a
17、 long-term trend in the history of computing hardware. Since the invention of the integrated circuit in 1958, the number of transistors that can be placed inexpensively on an integrated circuit has increased exponentially, doubling approximately every two years.The trend was first observed by Intel
18、co-fbunder Gordon E. Moore in a 1965 paper.lt has continued for almost half of a century and is not expected to stop for another decade at least and perhaps much os-0一scnuall2,000,000,0001,000,000,000100,000,00010,000,0001,000,000100,00010,0002,300TiAwfla GT200 RV770 AtomDual Cora nanium 1 .POWCR6G8
19、0.、itanaum 2 (Mth 9MB cache jBCurve shows Moores Law: transistor count doubling every two years19711980199020002008Date of introduction图 CPU Transistor Counts 1971-2008& Moores Law, Growth of transistor countsfor Intel processors (dots) and Moores Law (logarithmic vertical scale)Almost every measure
20、 of the capabilities of digital electronic devices is linked to Moores law: processing speed, memory capacity, even the number and size of pixels in digital cameras.All of these are improving at (roughly) exponential rates as well.This has dramatically increased the usefulness of digital electronics
21、 in nearly every segment of the world economy. Moores law describes this driving force of technological and social change in the late 20th and early 21st centuries.计算机第一定律摩尔定律Moore定律。归纳起来,主要有以下三种“版本”: 集成电路芯片上所集成的电路的数目,每隔18个月就翻一番。 微处理器的性能每隔18个月提高一倍,而价格下降一倍。 用一个美元所能买到的电脑性能,每隔18个月翻两番。图1-2 Computer Spee
22、dupMoores Law: MThe density of transistors on a chip doubles every 18 months, far the same cost半导体集成电路的密度或容量每18个月翻一番Moores Law is still valid. His law has nothing to do with the speed of the procceso匚 It has to do with the number of transitotrs which is still doubleing every couple of years. Case in
23、 point there is now multiple cores in the same space instead of one core.戈登摩尔(Gordon Moore), CPU生产商Intel公司的创始人之1965年提出“摩尔定律”,1968年创办Intel公司。摩尔1929年出生在美国加州的旧金山。曾获得加州大学伯克利分校的化学学士学位,并且在加州理工大学(CIT)获得物理和化学两个博士学位。50年代中期他和集成电路的发明者罗伯特诺伊斯(Robert Noyce)-起,在威廉肖克利半导体公司工作。后来,诺伊斯和摩尔等8人集体辞职创办了半导体工业史上有名的仙童半导体公司(Fai
24、rchild Semiconductor)。仙童成为现在的Intel和AMD之父。1968年,摩尔和诺伊斯一起退出仙童公司,创办了 Intel。Intel初期致力于开发当时计算机工业尚未开发的数据存储领域,后来,Intel进行战略转移,专攻微型计算机的心脏”部件-CPU。1.3 Scope of ProblemsWhat can you do with 1 computer?What can you do with 100 computers?What can you do with an entire data center?httD:en.wikiDedia.org/wiki/Distri
25、buted comDuting#ProiectsProjects:A variety of distributed computing projects have grown up in recent years. Many are run on a volunteer basis, and involve users donating their unused computational power to work on interesting computational problems. Examples of such projects include the Stanford Uni
26、versity Chemistry Department Foldinghome project, which is focused on simulations of protein folding to find disease cures and to understand biophysical systems; World Community Grid, an effort to create the world*s largest public computing grid to tackle scientific research projects that benefit hu
27、manity, run and funded by IBM; SETIhome, which is focused on analyzing radio-telescope data to find evidence of intelligent signals from space, hosted by the Space Sciences Laboratory at the University of California, Berkeley (the Berkeley Open Infrastructure for Network Computing (BOINC), was origi
28、nally developed to support this project); LHChome, which is used to help design and tune the Large Hadron Collider, hosted by CERN in Geneva; and , which is focused on finding optimal Golomb rulers and breaking various cryptographic ciphers.http: fblding.stanfbrd.edu/English/MainhtlD: zh.wikiDcdia.o
29、rg/wiki/Foldinghomchttp: www.stanford.edu/grouD/DandegrouD/images/FAHMay2008.Dnghttp :Foldinghome是如何工作的呢?Foldinghome是一个研究研究蛋白质折叠,误折,聚合及山此引起的相关疾病的分布式计算工程。使用联网式的计算方式和大量的分布式计算能力来模拟蛋白质折叠的过程,并指引我们近期对由折叠引起的疾病的一系列研究。图13 Foldinghome图1-4 Shrek Dreamworks Animation, rendering multiple frames of high-qualityan
30、imationHappy Feet Kingdom Feature Productions; Lord of the Rings New Line Cinema 图1-5 Simulating several hundred or thousand charactersIndexing the web (Google)Google ()是一个搜索引擎,由两个斯坦福大学博士生Larry Page 与 Sergey Brin 于1998年9月发明,Google Inc.于1999年创立。Google 网页搜索技术是来源于信息检索技术。Google的“网页快照”功能,能从Google服务器里直接取出
31、缓存的网页。Simulating an Internet-sized network for networking experiments (PlanetLab)http: www.plancklab.org/PlanetLab is a global research network that supports the development of new network services. Since the beginning of 2003, more than 1,000 researchers at top academic institutions and industrial
32、research labs have used PlanetLab to develop new technologies for distributed storage, network mapping, peer-to-peer systems, distributed hash tables, and query processing. PlanetLab currently consists of 1128 nodes at 511 sites.Speeding up content delivery (Akamai)美国Akamai是国际上最大的CDN服务商,它巨大的网络分发能力在峰
33、值时可达到15Gbps Akamai公司是为数不多的旨在消除Internet瓶颈和提高下载速度的几家新公司之一,是一个致力于网络交通提速的“内容发布”公司,是波士顿高技术区最卓越的新兴企业之一。Akamai公司向全球企业提供发送互联网内容,汇流媒体和应用程序的服务(目前,该公司为15个国家的企业管理着8000多台服务器)。1998年,丹尼尔.L和麻省理工学院的一些研究人员起创立了这家公司,他在麻省理工学院的硕上论文构成了 Akamai公司最初的“自由流”(Freeflow)技术的核心。根据美国航空公司的消息,丹尼尔.L31岁,在2001年9月11 H撞击纽约世界贸易中心的被劫持飞机上遇难。上篇
34、计算机文化上篇的主要目的是向读者介绍有关计算机和信息技术的基本概念和基本原理,使读者能够对计算机学科有全局性的认识。第2章计算机系统2.1 Computer Introduction本节大部分内容取自下面这本书的第一章。等号线之间内容是我加的。Foundations of Computer Science,2e,by Behrouz Forouzan and Firouz Mosharraf.Ccngagc Learning Business Press, December 5,2007http: www.cengage.co.uk/forouzan/http: 844807002/reQsi
35、3Jdr bb productThe phrase computer science has a very broad meaning today. However, in this book, we define the phrase as “issues related to the computer0. This introductory chapter first tries to find out what a computer is, and then investigates other issues directly related to computers. We look
36、first at the Turing model as a mathematical and philosophical definition of computation. We then show how todayGive & short history of computefs. io-s computers arc based on the von Neumann model. The chapter ends with a brief history of this culture-changing device.the computer.ObjectivesAfter stud
37、ying this chapter, the students should be able to: Define the Turing model of a compute匚 Define the von Neumann model of a computer. Describe the three components of a computer: hardware, data, and software. List topics related to computef hardware. List topics related to data. List topics related t
38、o software.2.1.1 TURING MODELThe idea of a universal computational device was first described by Alan Turing in 1937. He proposed that all computation could be performed by a special kind of machine, now called a Turing machine. Although Turing presented a mathematical description of such a machine,
39、 he was more interested in the philosophical definiton of computation than in building the actual machine. He based the model on the actions that people perfonn when involved in computation. He abstracted these actions into a model for a computational machine that has really changed the world.Percep
40、tual knowledge (感性认识)计算机组成部分http: 101/2008/video/comDuter components.flvIntroduction to Computer Hardwarehttp: hardwarc.flvInstallhttp:net.D 101/2008/video/flvplayer setuD.exe, if yourcomputer can not show videos.图2-1 Mother board (主板:集成多个部件、适配器,提供它们之间的互联)主板(Main Board)又名主机板、系统板、母板,是PC机的核心部件。PC机的主板包
41、括CPU、芯片组(Chipset)、高速缓存(Cache)、ROM_BIOS芯片、CMOS 芯片、内存RAM、总线通道、软硬磁盘接口、串行和并行接口、USB接口、扩展槽(Slots),直流电源插座、可充电电池以及各种条线。图中从上到下,左到右:内存条,磁盘、光驱等的数据线接口; CPU风扇(一般F面是散热器,和CPU):棕色AGP槽:只能接显卡;白色PCI槽:能接显卡、网卡、声卡等。图2-2 CPU =运算器+控制器图2-3 Alan Turing, founder of computer science, and artificial intelligencehttp: 图灵是举世罕见的天才
42、数学家和计算机科学家,仅仅在世42年。他的英年早逝,像他横溢的才华样,令世界吃惊与难以置信。生命虽然短暂,但那传奇的人生,丰富多彩的创造力和智慧而深邃的思想,使他犹如一颗耀眼的明星,持续地照耀着人间后世在科学的浩瀚太空里探索未来的人们。自上个世纪60年代以来,计算机技术飞速发展,信息产业逐渐成为影响人类社会的最重要的工业之一。支持技术与工业发展的理论基础是计算机科学。众所周知,“诺贝尔奖”是世界上最负盛名的奖项,但仅用于奖励那些在物理、化学、文学、医学、经济学与促进世界和平等方面做出开拓性重大贡献的人士。“图灵奖”则是计算机科学领域的最高奖项,有“计算机界诺贝尔奖”之称。设立这个大奖,既是为了
43、促进计算机科学的进一步发展,也是为了纪念一位天才数学家、计算机科学的奠基人艾兰图灵。http: zh.wikipedia.org/wiki/图灵图灵被视为计算机科学之父。1931年进入剑桥大学国王学院,毕业后到美国普林斯顿大学攻读博士学位,二战爆发后回到剑桥,后曾协助军方破解德国的著名密码系统Enigma,帮助盟军取得了二战的胜利。图灵对于人工智能的发展有诸多贡献,例如图灵曾写过一篇名为机器会思考吗?(Can Machine Think?)的论文,其中提出了一种用于判定机器是否具有智能的试验方法,即图灵试验。至今,每年都有试验的比赛。此外,图灵提出的著名的图灵机模型为现代计算机的逻辑工作方式奠
44、定了基础。http: turing.tlvA short video describing the life and unfortunate death of Alan Turing.http:zh.wikipedia.org/wiki/姚期智姚期智,美籍华人,计算机科学家,2000年图灵奖得主,是目前唯一一位获得此奖项的华人及亚洲人。目前是清华大学理论计算机科学研究中心教授。因为对计算理论,包括伪随机数生成,密码学与通信复杂度的诸多贡献,美国计算机协会(ACM)决定把该年度的图灵奖授予他。Data processorsInputOutputComputerFigure 1.1 A signl
45、e purpose computing machine Before discussing the Turing model, let us define a computer as a data processor. Using this definition, a computer acts a black box that accepts input data, processes the data, and created output data (Figure 1.1). Although this model can define the functionality of a computer today, it is too general. In this model, a pocket calculator is also a computer (which it is, in a literal sense).Another problem with this model is that it does not specify the type of processing, or whether
限制150内