双语版C++程序设计(Learn C++ through English and Chinese)(txt+pdf+epub+mobi电子书下载)


发布时间:2020-09-09 12:15:36

点击下载

作者:(爱) Paul Kelly(保罗 凯利)

出版社:电子工业出版社

格式: AZW3, DOCX, EPUB, MOBI, PDF, TXT

双语版C++程序设计(Learn C++ through English and Chinese)

双语版C++程序设计(Learn C++ through English and Chinese)试读:

前言

针对国内大学逐渐与国际接轨的发展趋势,英语教学和双语教学逐渐被人们重视起来。一方面,在国家教育部的大力支持下,许多课程建设成了国家教育部双语教学示范课程,但是这些课程大多采用英文原版教材,而面向双语教学的双语版教材在国内实属罕见。另一方面,以“国际化、工业化”为办学理念,注重国际化、工业化人才培养的软件示范学院的部分课程还邀请了一些外籍教师进行全英语授课,但是由于目前国内学生的英语水平参差不齐,导致全英语授课的教学效果不是非常理想。本书正是在此背景和需求下应运而生的。

本书的第一作者是爱尔兰都柏林工业大学(DIT)的高级讲师Paul Kelly。Kelly老师长期从事程序设计类课程的教学工作,在程序设计类课程教学方面教学实践经验丰富,在国外已先后出版多本程序设计语言类书籍。自哈尔滨工业大学软件示范学院成立以来,一直作为外聘教师在哈尔滨工业大学软件示范学院从事程序设计方面的教学工作,对中国学生比较了解,针对其在教学中发现的问题,即初学者面临着既不熟悉专业术语和基本概念、又不熟悉英文专用词汇的双重困难,提出了出版英汉对照混排式双语版教材的思路,帮助学生在克服语言障碍的同时,能够更快更好地熟悉和掌握计算机程序设计方面的基础知识,为国内的双语教学提供了一种最佳的解决方案。

本书内容共分14章,由浅入深、全面介绍C++程序设计方法,既适合于以C++作为入门语言的读者,也适合于学习过其他程序设计语言后想再学习C++的读者。本书的特点如下:

1.使用非常实用和贴近生活的例子以及图示来通俗易懂地讲解难于理解的概念,介绍面向对象程序设计方法的同时,尤其强调读者的亲自参与意识。

2.采用案例驱动和循序渐进方式,从一个应用实例出发,先利用现有知识编写出一个较为简单的程序,然后在此基础上,不断扩充,在扩充的过程中引入一个新的概念和知识点,逐渐编写出一个较大的程序,每个例程都有详细的讲解。有的章以一个例子为中心贯穿始终讲解,后面章节的例子还会重用已有的部分程序代码,前、后章节之间既有内容上的联系,也有例程上的联系。

3.重点内容和段落给出了中文注解。

4.每章后面都有一节介绍初学者编程时易犯的错误,以帮助初学者在程序设计中避免这些错误。

5.每章后面都有快速语法参考,总结本章内容,便于读者快速查询相关内容。

6.每章后面都有精心设计的、有趣的习题,便于读者测试和强化对相关内容的理解。

7.有相关的教学网站(华信教育资源网,网址http://www.hxedu.com.cn),方便读者下载示例的源代码、部分习题解答及教学课件等资料。

本书由在计算机程序设计方面有着丰富教学和实践经验的中外作者合作编写。本书是在国内首次出版中英文对照混排式双语版教材,适合低年级的学生对照阅读,既方便初学者熟悉相关概念和内容,也便于母语不是英语的读者熟悉英文的专业词汇,尤其适用于作为双语教学示范课程的教材。

Paul Kelly是一位治学非常严谨的教师,本书的第二作者苏小红在与他合著过程中,经常为一个细节内容的编写进行交流与讨论,书稿完成后又进行了多次校对工作。本着对所有读者负责的精神,我们真诚地欢迎读者对教材提出宝贵意见,可以通过发送电子邮件或在网站(http://book.sunner.cn)上留言等多种方式与我们交流讨论。作者E-mail地址为Paul.Kelly@comp.dit.ie,sxh@hit.edu.cn。

苏小红

哈尔滨工业大学计算机科学与技术学院

2010年5月Chapter One Introduction第1章 绪论1.1 What is a computer program?(什么是计算机程序?)

Computers are involved in a wide variety of tasks that we do in our everyday lives.Some of these tasks such as using a word processor or checking e-mail obviously use a computer.Less direct examples occur when we use an ATM at a bank,pay at a supermarket checkout or use a phone.

A computer performs all of these tasks by following a predefined set of instructions.This set of instructions is a called a computer program.A computer program to a computer is like a recipe to a chef;it specifies the steps needed to perform a certain task.But unfortunately,unlike a recipe,you can’t give your instructions to a computer in a language such as English or Chinese.For instructions to be‘intelligible’to a computer,they need to be expressed in a language‘understood’by the computer.The only language‘understood’by a computer is its own machine language,which consists of a series of binary ones and zeroes.

Machine language is very difficult to use directly and so instructions to a computer are given in a special language called a programming language.The programming language is neither English nor machine language,but is somewhere in between.In fact,as you will see,it is more like English than machine language.

Machine languages are known as low-level languages and programming languages are known as high-level languages.

Writing instructions in a high level language is much easier than writing them in low-level machine language,but is still not as easy as writing them in English or Chinese.

现在我们日常生活中的很多工作都要用到计算机。

计算机完成所有这些工作都是通过执行预定义的指令集来实现的。这个指令集就称为“计算机程序”。

计算机程序和计算机之间,就像食谱和厨师之间的关系一样;计算机程序指定了完成某一任务需要的步骤。

但是遗憾的是,不同于菜谱,你不能用英文或者中文这样的自然语言向计算机发送指令。对于计算机来说,指令必须是“易理解”的,必须表示成一种计算机能“理解”的语言。计算机能“理解”的唯一语言就是机器语言,机器语言由一系列二进制的0和1组成。

由于机器语言很难直接使用,所以计算机指令被表示成一种特殊的语言,这种特殊的语言称为“程序设计语言”。程序设计语言既不是英语,也不是机器语言,而是一种介于它们两者之间的语言。实际上,正如下面即将看到的那样,较之机器语言,程序设计语言更像英语。

机器语言被认为是一种低级语言,而程序设计语言则被认为是一种高级语言。

For the computer to carry out the program instructions written in a high level language,they have to be translated from the high level language to the machine language of the computer.A compiler does this translation.

为了让计算机执行由高级语言编写的程序指令,必须把这些指令从高级语言形式翻译成计算机的机器语言形式。编译器用于完成这种翻译。1.2 Developing a computer program(开发计算机程序)

The first step in developing a computer program is to define and understand the problem to be solved.If you cannot understand the problem then you will certainly not be able to tell a computer how to solve the problem.This is called the analysis phase and basically answers the question“what is to be done?”,ignoring for the time being the question of how the problem is to be solved.

Once it is known what has to be done,the question“how is it to be done?”arises.This is called the design phase and it is in this phase that a solution to the problem is developed.The design phase may reveal problems not previously considered in the analysis phase.So rather than being independent phases,the design and analysis phases are closely related and interact with each other.

The analysis and design phases are important to developing a successful solution to a problem.Neglect at either of these phases will result in a‘solution’that will not solve the original problem and may even contribute to making it worse.

Analysis and design are subjects in their own right and are not covered in this book.This book concentrates on the next phase:writing,compiling and testing C++programs.

开发计算机程序的第一步就是定义和理解要解决的问题。

如果不理解要解决的问题是什么,那么就无法告诉计算机如何去解决这个问题。这个阶段称为分析阶段。分析阶段主要是回答“做什么”的问题,而不考虑如何去做的问题。

一旦知道了要做什么,那么“如何去做”的问题又产生了。这称为设计阶段,在这个阶段要找到一种解决问题的方法。在设计阶段也可能遇到一些在分析阶段没有考虑到的问题。所以,分析和设计不是两个独立的阶段,而是两个密切相关、相互影响的阶段。分析和设计对找到一种好的解决问题的方案来说是十分重要的。忽视这两个阶段中的任何一个都会导致最终的解决方案无法解决最初的问题,甚至还可能产生更坏的结果。

There are many different compilers on the market,with updated versions being released regularly.Up to date instructions for writing,compiling and running C++programs with some of the popular compilers can be found on the web site for this book at http://www.hxedu.com.cn.

分析和设计属于不同范畴的问题,不在本书的讨论范围之内。本书重点介绍其后面的阶段:C++程序的编写、编译和测试。尽管编译器之间存在差异,但可以给出开发一个C++程序的一般步骤。1.2.1 Program development cycle

Despite the differences between compilers,the following is a general description of the steps involved in the development of a C++program.

Step 1:Design the program.

A computer system consists of one or more programs.Each program has to be individually designed to implement the overall solution developed at the analysis and design phases.第一步:设计程序。

计算机系统由一个或者多个程序组成。每个程序必须单独设计,以实现在分析阶段和设计阶段提出的整体解决方案。

After each program is designed,it is important to check its logic before starting to write the program.

Step 2:Write the program.

Firstly,the C++program instructions are typed into a file using a text editor.The file containing the C++statements is called the source file and is usually stored on disk.The program instructions are also called the source code or the program code.

Step 3:Compile the program.

Next the C++program is passed through a compiler,which translates the C++program instructions to machine instructions.The compiler reads the source file,translates the C++statements into machine or object code,and stores the object code in an object file.

Some errors are likely to occur in this step.An error in the source code is indicated by the compiler and is referred to as a compile-time error.The simplest kind of compile-time error is a syntax error.This type of error is relatively easy to correct,as the compiler will indicate where in the source code the error has occurred.Typical syntax errors involve missing punctuation and misspellings.All compile-time errors must be corrected before the compilation process can be completed.

The compiler may sometimes issue a warning message during compilation.A warning message is not as serious as a syntax error and will not prevent the program from being compiled.However,warnings are the compiler’s way of drawing attention to what it‘thinks’may be a problem and should be investigated.

Step 4:Link the program.

The final step before running the program is to link the program using the linker.Linking involves combining the object file of the program with other object files from the C++run-time library to form an executable file.

在程序设计好之后、编写程序之前,检查一下程序的逻辑是很重要的。第二步:编写程序。

使用文本编辑器把C++程序指令输入到一个文件中。这个包含C++语句的文件称为源文件,通常存储在磁盘上。程序指令也称为源代码或程序代码。第三步:编译程序。

编译器把C++程序指令转换成机器指令。编译器读入源文件,把C++语句翻译成机器代码或目标代码,然后把目标代码存入目标文件中。

在编译阶段很可能出现一些错误。编译器指出的源代码中的错误属于编译时错误。最简单的编译时错误是语法错误。这类错误相对容易纠正,因为编译器会指出这类错误在源代码中的位置。典型的语法错误有遗漏标点和拼写错误。在编译过程完成之前必须纠正所有的编译错误。

编译器也可能在编译阶段发出一些警告信息。警告信息不像语法错误那样严重,不会妨碍程序的编译。不过,它表示编译器认为可能有问题,希望引起你的注意并检查一下程序。第四步:链接程序。

在运行程序之前的最后一步就是使用链接器来链接程序。

所谓链接就是把程序的目标文件和C++运行时库文件结合起来形成一个可执行文件。

Step 5:Test the program.

When the program is run you may find that it is not working as expected.The fact that a program does not have any compile-time errors does not guarantee that the program will work as required.For example,the programmer may mistakenly have given an instruction to divide a number by zero.This type of error is called a run-time error and causes the program to stop before it has completed its task.

The program may complete its task but produce incorrect results or display them at the wrong position on the screen.

These kinds of errors are known as logic errors or more commonly as bugs.Bugs are much harder to find and fix than compile-time errors.Some bugs appear only under certain conditions,for example when a program is run with a particular set of data.

The process of locating and correcting program errors is called debugging.

Step 6:Debug the program.

Once a bug has been identified,the next step is to find where in the source code the problem lies.Many compilers have tools that can be used to help locate bugs.第五步:测试程序。

程序在运行时有可能没有像预期的那样执行。一个程序没有编译时错误并不能保证这个程序就能按照要求运行。举个例子,编程人员可能错误地在一条指令中把0作为除数。这种错误称为运行时错误,它会导致程序在完成它的任务之前中止。

虽然程序执行完毕,但是结果却是错误的,或者结果显示的屏幕位置是错误的。这类错误称为逻辑错误或者俗称为“bugs(缺陷)”。缺陷比编译时错误更难发现和修正。某些bug仅在特定的条件下才出现,例如仅在特定的数据集上运行程序时才出现。定位和修正程序错误的过程,称为调试。第六步:调试程序。

bug一旦被确认,紧接着就要定位其在源代码中的位置。很多编译器都提供了可用于帮助定位缺陷的工具。

Correcting bugs involves going back to step 2,but hopefully not any further.Going back to step 1 or even back to the analysis and design phases would be like asking an architect to redesign parts of a house while it is being built!In general,try to catch errors as early as possible.

改正一个缺陷后要返回第二步,但是希望不要返回到更前面去。如果返回到第一步甚至是分析和设计阶段,就好像要求建筑师重新设计一座正在建造中的房子一样。一般情况下,应该尽可能早地发现错误。1.3 Learning C++(学习C++)

You’ll learn more from designing,writing,running,and correcting programs than you ever will by simply reading a book.A successful approach to learning to program in C++depends on large amounts of practice.

To help you practise,there are exercises at the end of each chapter.Do as many of the exercises as possible and get some feedback on your solutions from people who know C++.There are solutions to selected exercises at the web site for this book.1.4 Web site for this book(本书的网站)

The web site for this book is at http://www.hxedu.com.cn.The source code for all the example programs used in this book as well as answers to selected exercises are available here.In addition,details of various compilers are also available at the web site.1.5 Brief history of C++(C++简史)

C++is a direct descendent of the C programming language,which was originally developed in 1972 at Bell Laboratories,New Jersey,USA.C evolved from a language called B,which in turn evolved from a language called BCPL(Basic Combined Programming Language).C++was developed by Bjarne Strostrup at AT&T Bell Laboratories from 1979 to 1983.The initial version of the language was called“C with Classes”and was used internally in AT&T in 1983.Later that year,the name was changed to“C++”.The first commercial version of C++was marketed in 1985,and since then,C++has evolved to the latest ANSI/ISO(American National Standards Institute/International Organization for Standardization)C++standard.

C++语言是由C语言直接发展而来的,1972年诞生于美国新泽西州的贝尔实验室。C语言是从一种称为B的语言演化而来的,这个B语言反过来又是从BCPL语言演化而来的。

C++是Bjarne Strostrup于1979年至1983年在AT&T贝尔实验室工作期间开发的。该语言的最初版本称为“有类的C”,并且于1983年在AT&T内部使用。从那以后,该语言被改名为C++。第一个商业版本的C++在1985年上市。从那以后,C++逐渐发展为最新的ANSI/ISO标准。1.6 ANSI/ISO C++standard(ANSI/ISO C++标准)

The example programs used in this book conform to the ANSI/ISO C++standard.Not all compilers conform to this standard,so some compilers may not correctly compile the example programs.Some of the example programs may have to be modified for use with these other compilers.See the web site for details.Chapter TwoBeginning to Program in C++第2章 C++编程入门

As in other programming languages,data can be of two types:constants and variables.

与其他编程语言相同,C++中的数据可分为两种:常量和变量。2.1 Constants(常量)

As the name suggests,a constant does not change its value in a program.Some examples of constants are shown in Table 2.1 below.Table 2.1 Some examples of constants

顾名思义,常量的值在程序中是不能改变的。

integer:整型。

floating-point:浮点型(也称实型)。

character:字符型。

string:字符串型。2.2 Variables(变量)

Unlike a constant,a variable can vary its values in a program,and a variable must be defined before it can be used.A variable is defined by giving it a data type and a name.Program Example P2A

与常量不同,变量的值在程序中是可以改变的。变量在使用之前必须先定义。定义变量就是指定它的类型和名字。

C++programs start with the line

main()

This marks the point where a C++program starts to execute and must appear once only in a C++program.

The program statements are contained within the braces{and}on lines 2 and 9.Each statement ends with a semicolon.The spaces before the semicolon and on each side of the equals sign are not essential and are used here only to improve the readability of the program.

Lines 3,4 and 5 of this program define three variables:v1,v2,and v3.A variable can be given any name,called an identifier in C++,provided it is within the following rules:

●An identifier can only be constructed using letters,numerals or underscores(_).

●An identifier must start with a letter or an underscore.

●An identifier cannot be a C++keyword.A keyword is a word that has a special meaning.(See appendix A for a list of keywords.)

●An identifier can contain any number of characters,but only the first thirty-one characters are significant to the C++compiler.

Lines 3 to 5 of program P2A define v1 as an integer variable,v2 as a floating-point variable,and v3 as a character variable.Note that identifiers are case-sensitive,i.e.the variable V1 is different from the variable v1.

A variable is like a box in the memory of the computer.The box has a name and contains a value.Each box has a name given to it by you,the programmer.The box keeps its value until it is changed by replacing it with some other value.

记号main()指定了C++程序执行的起点,它在程序中只能出现一次。

程序语句用一对花括号“{}”括起来。每条语句都以分号(;)结尾。分号之前及等号两边的空格不是必需的,增加空格只是为了增强程序的可读性。变量名是C++的一种标识符,可以给变量起任何名字,只要遵守如下的标识符命名规则即可:

●标识符只能由字母、数字和下划线(_)组成。

●标识符必须以字母或下划线开头。

●标识符不能是C++关键字。关键字是指在程序中具有特殊意义的单词(见附录A中C++关键字的列表)。

●标识符可以包含任意多个字符,但是C++编译器只识别前31个字符。

变量就像计算机内存中的一个盒子。这个盒子有一个名字和值,程序员可以为每个盒子命名。盒子中的数据直到放入新的数据后才会改变。

Lines 6 to 8 of the program assign values to the variables.The value assigned to each variable is stored in the computer’s memory.2.3 Simple output to the screen(简单的屏幕输出)

Now that values are assigned to the variables,how are the values displayed on the screen?This can be done with cout(pronounced see-out),as demonstrated in the next program.

既然数值被赋值给了变量,那么如何将这些值显示在屏幕上呢?可以用cout(读做c-out)来输出变量的值,如下面程序所示。Program Example P2B

When you compile and run this program you will get the following on your screen:

Line 1 is an example of a preprocessor directive and is pronounced“hash include io stream”.This line,along with line 2,will be in every program that involves output to the screen or input from the keyboard.The#include preprocessor directive causes the file in the angle brackets,called a header file,to be made available to the program.The header file iostream contains some C++statements to make it easy to perform input and output from a C++program.(Preprocessor directives are covered in Appendix G.)

C++refers to your computer as a console.To display data to the screen,C++sends the data to cout(console output).cout is called an output stream object.

In C++,letters or numbers enclosed in double quotation marks is called a string of characters or a character string.

第1行是一个编译预处理命令。这一行和下一行将会出现在每一个需要向屏幕输出数据或者从键盘输入数据的程序中。

位于尖括号内的文件称为头文件,编译预处理命令#include可使头文件在程序中生效。头文件iostream中包含一些帮助程序轻松实现数据输入/输出的C++语句(编译预处理命令将在附录G中详细介绍)。

C++语言将计算机看做控制台。要想在屏幕上显示数据,只要将数据送到称为标准输出流对象的cout即可。

试读结束[说明:试读内容隐藏了图片]

下载完整电子书


相关推荐

最新文章


© 2020 txtepub下载