1.1 什么是编程语言:如何像计算机专家一样思考(C++版)

类别:VC语言 点击:0 评论:0 推荐:
Chapter 1 The way of the program 第一章 编程方法

The goal of this book is to teach you to think like a computer scientist. I like the way computer scientists think because they combine some of the best features of Mathematics, Engineering, and Natural Science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.

这本书的目的是教你如何像计算机专家一样思考。我很喜欢计算机专家的思考方式,因为他们将数学,工程学和自然科学中一些最好的特征结合在一起。像数学家一样,计算机专家使用形式化语言来表达思想(特别是计算)。像工程师一样,他们设计事物,把部件装配为系统,在候选方案中寻求一种平衡。像科学家一样,他们观察复杂系统的行为,形成假设,并对其进行检验。

The single most important skill for a computer scientist is problem-solving. By that I mean the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills. That's why this chapter is called "The way of the program."

对一个计算机专家而言,最重要的一种技能就是问题的解决。我的意思是明确表述问题,创新的思考解决方案,并清晰而准确的表达解决方案的能力。正像所表述的,学习编程的过程是练习问题解决技能的一个绝好机会。这就是为什么本章称为“编程方法”的原因。

Of course, the other goal of this book is to prepare you for the Computer Science AP Exam. We may not take the most direct approach to that goal, though. For example, there are not many exercises in this book that are similar to the AP questions. On the other hand, if you understand the concepts in this book, along with the details of programming in C++, you will have all the tools you need to do well on the exam.

当然,本书另外的目标是使你备战计算机科学能力考试。然而,我们没有非常直接的给出如何达到这一目标的途径,例如,本书中并没有太多与能力考试问题相近的练习。尽管如此,如果你理解本书的内容,以及C++编程细节,你将拥有在考试中取得优异成绩的所有基础。

1.1         What is a programming language? 1.1 什么是编程语言

The programming language you will be learning is C++, because that is the language the AP exam is based on, as of 1998. Before that, the exam used Pascal. Both C++ and Pascal are high-level languages; other high-level languages you might have heard of are Java, C and FORTRAN.

你将学习的编程语言是C++,由于能力考试从1998年开始(在此之前是Pascal),是基于此语言的。C++和Pascal都是高级语言;其它你可能听说的语言是Java,C,和FORTAN。

As you might infer from the name "high-level language," there are also low-level languages, sometimes referred to as machine language or assembly language. Loosely-speaking, computers can only execute programs written in low-level languages. Thus, programs written in a high-level language have to be translated before they can run. This translation takes some time, which is a small disadvantage of high-level languages.

或许你会根据“高级语言”的名称来推断,也存在低级语言,有时也称为机器语言或者汇编语言。坦率而言,计算机仅能执行用低级语言编写的程序。因此,使用高级语言编写的程序不得不在运行之前进行转换。这种转换需要一些时间,这是高级语言的一个小小的不足。

But the advantages are enormous. First, it is much easier to program in a high-level language; by "easier" I mean that the program takes less time to write, it's shorter and easier to read, and it's more likely to be correct. Secondly, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can only run on one kind of computer, and have to be rewritten to run on another.

但是其优势是巨大的。首先,使用高级语言编程更容易;这里所讲的“容易”,我的意思是编程耗费的时间更短,并且编写的程序更简短易读,更可能正确。第二点,高级语言可便携,意味着这些程序可以经过较小的修改或者无需改动,就可以运行在不同类型的计算机之上。低级语言编写的程序仅仅能运行在某一类计算机上,而必须重写,以便用于其它类型计算机。

Due to these advantages, almost all programs are written in high-level languages. Low-level languages are only used for a few special applications.

由于上述优势,几乎所有的程序都用高级语言编写。低级语言近用于一些特定应用之中。

There are two ways to translate a program; interpreting or compiling. An interpreter is a program that reads a high-level program and does what it says. In effect, it translates the program line-by-line, alternately reading lines and carrying out commands.

有两种程序转换的方法;解释或者编译。解释器是一段程序,其读取高级语言程序,并做程序所编写的操作。在效果上,将程序一行一行的转换,在读取程序行和执行命令之间交替切换。

A compiler is a program that reads a high-level program and translates it all at once, before executing any of the commands. Often you compile the program as a separate step, and then execute the compiled code later. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable.

编译器是一种程序,其读取高级语言程序,然后在执行任一命令前,一次转换所有。你经常将编译程序作为独立的步骤,在以后再执行所编译的代码。在此意义下,高级语言程序被称为源代码,编译后的程序称为目标代码或者可执行程序。

As an example, suppose you write a program in C++. You might use a text editor to write the program (a text editor is a simple word processor). When the program is finished, you might save it in a file named program.cpp, where "program" is an arbitrary name you make up, and the suffix .cpp is a convention that indicates that the file contains C++ source code.

例如,假设你使用C++语言写一个程序,你可能使用文本编辑器来编写(文本编辑器是一种简单的文字处理软件)。当程序编写完毕后,保存为program.cpp,在上文中“program”是你给的任意文件名,后缀.cpp是C++源码程序文件的常用形式。

Then, depending on what your programming environment is like, you might leave the text editor and run the compiler. The compiler would read your source code, translate it, and create a new file named program.o to contain the object code, or program.exe to contain the executable.

然后,根据你的编程环境,离开编辑器,运行编译器。编译器读取程序源代码,并进行转换,创建program.o文件来保存目标文件,或者program.exe来保存可执行代码。

The next step is to run the program, which requires some kind of executor. The role of the executor is to load the program (copy it from disk into memory) and make the computer start executing the program.

下一步工作是运行程序,这需要某种执行器。执行器的作用是加载程序(将程序从磁盘中拷入内存之中),并使计算机开始执行程序。

Although this process may seem complicated, the good news is that in most programming environments (sometimes called development environments), these steps are automated for you. Usually you will only have to write a program and type a single command to compile and run it. On the other hand, it is useful to know what the steps are that are happening in the background, so that if something goes wrong you can figure out what it is.

尽管这个过程可能看起来很复杂,好消息是在大多数编程环境中(有时称为开发环境),这些步骤对你而言是自动的。通常你仅需要编写一个程序,输入一条指令去编译和运行它。但是,知道这些发生在幕后的步骤是很有用的。如果某些东西出错,你能够指出错在何处。

本文地址:http://com.8s8s.com/it/it879.htm