写在开始

计算机编程语言:

Remember that a program is just a sequence of instructions telling a computer what to do. Obviously, we need to provide those instructions in a language that a computer can understand. It would be nice if we could just tell a computer what to do using our native language, like they do in science fiction movies. (“Computer,how long will it take to reach planet Alphalpha at maximum warp?”) Unfortunately, despite the continuing efforts of many top-flight computer scientists (including your author), designing a computer to understand human language is still an unsolved problem.

记住,程序就是一段告诉计算机要做什么的指令序列(有顺序的集合)。很明显,我们需要用语言精确描述的指令集给计算机,它们才能够理解或者明白。当然最理想,就是像科幻电影中计算机一样,直接能够听懂我们的人类语言(计算机,告诉我们如果全速飞行,去Alphalpha还需要多长时间?)。不幸的是,很多顶尖聪明的计算机科学家付出巨大的努力,但让计算机理解人类的语言还是遥遥无期的。

Even if computers could understand us, human languages are not very well suited for describing complex algorithms. Natural language is fraught with ambiguity and imprecision. For example, if I say: “I saw the man in the park with the telescope,” did I have the telescope, or did the man? And who was in the park? We understand each other most of the time only because all humans share a vast store of common knowledge and experience. Even then, miscommunication is commonplace.

即使计算机能够弄懂人类的语言,我们的语言也不适合来描述解释一些复杂的算法。人类自然语言的缺陷是多义性与精确度不高。打个比方,如果我说“I saw the man in the park with the telescope." 是我本人手里拿着望远镜,还是我看到的人?到底谁在公园里?生活在地球上的人类能够相互沟通的原因是,我们共享在海量的知识与经验。但是误解也是家常便饭,不然地球上就没有争端与战争了。

Computer scientists have gotten around this problem by designing notations for expressing computations in an exact, and unambiguous way. These special notations are called programming languages. Every structure in a programming language has a precise form (its syntax) and a precise meaning (its semantics). A programming language is something like a code for writing down the instructions that a computer will follow. In fact, programmers often refer to their programs as computer code, and the process of writing an algorithm in a programming language is called coding.

计算机科学家设计一套记号系统,通过精确无歧义的方法来描述计算过程。这套记号系统叫做编程语言。每一种编程语言由它的精确的形式(语法),精确的含义(语义)。编程语言是编写指令的代码,计算机能够执行的。事实上,程序员通常用编写的程序来特指计算机代码,用编程语言编写算法的过程叫编程。

Python is one example of a programming language. It is the language that we will use throughout this book. You may have heard of some other languages, such as C++, Java, Perl, Scheme, or BASIC. Although these languages differ in many details, they all share the property of having well-defined, unambiguoussyntax and semantics.

Python就是一门编程语言。本书使用Python为例进行展开的。大家可能听说过其它编程语言,如C++,Java,Perl, Scheme等。虽然这些语言在细节上有差异,有也很多的相似点,如定义优良,语法,语义无歧义。

All of the languages mentioned above are examples of high-level computer languages. Although they are precise, they are designed to be used and understood by humans. Strictly speaking, computer hardware can only understand very low-level language known as machine language.

上面提及的语言均为高级计算机语言。它们是精确的,能够被人们所理解并使用。严格说来,计算机只能理解并执行低级语言,我们称其为机器语言。

Suppose we want the computer to add two numbers. The instructions that the CPU actually carries out might be something like this.

举例说来,我们让计算机把两个数累加,CPU执行的指令如下:

load the number from memory location 2001 into the CPU

load the number from memory location 2002 into the CPU

Add the two numbers in the CPU

store the result into location 2003

(存储器2001地址处的数转入到Cpu, 存储器2002地址处的数转入到CPU, 在CPU中把这两数累加,结果保存到存储器2003地址处)

This seems like a lot of work to add two numbers, doesn’t it? Actually, it’s even more complicated than this because the instructions and numbers are represented in binary notation (as sequences of 0s and 1s).

看起来,把两个数相加挺费事的!实际上,把指令与数用二进制序列的方式表*征,可能情况更复杂。

In a high-level language like Python, the addition of two numbers can be expressed more naturally: c = a + b. That’s a lot easier for us to understand, but we need some way to translate the high-level language into the machine language that the computer can execute. There are two ways to do this: a high-level language can either be compiled or interpreted.

在Python中,两数相加,可以写成这样的代码:c=a+b。这个我们容易理解,但问题是我们得把这代码翻译成机器语言,计算机才能执行。一般有两种方法完成这个事情:编绎和解释。

A compiler is a complex computer program that takes another program written in a high-level language and translates it into an equivalent program in the machine language of some computer.

编绎器是一个复杂的计算机程序,它的作用是把高级语言转化(翻译)成计算机能够理解的机器语言。

a block diagram of the compiling process. The high-level program is called source code, and the resulting machine code is a program that the computer can directly execute. The dashed line in the diagram represents the execution of the machine code.

下面图中会告诉你编绎过程,高级语言编写的程序叫源代码。结果机器代码是计算机能够直接执行的程序。图中的虚线部分表示转化机器代码的过程。



An interpreter is a program that simulates a computer that understands a high-level language. Rather than translating the source program into a machine language equivalent, the interpreter analyzes and executes the source code instruction by instruction as necessary.

解释器也是一段程序,使得计算机能够理解高级语言.。 它不是把源代码翻译成相应的机器语言,它是通过解释器,边读取指令边分析并执行源代码。

The difference between interpreting and compiling is that compiling is a one-shot translation; once a program is compiled, it may be run over and over again without further need for the compiler or the source code. In the interpreted case, the interpreter and the source are needed every time the program runs. Compiled programs tend to be faster, since the translation is done once and for all, but interpreted languages lend themselves to a more flexible programming environment as programs can be developed and run interactively.

编绎与解释的差别是,编绎是一次性翻译,编绎工作完成,目标代码就可以反复执行,不需要编绎器与源代码文件了。而翻译型,解释器与源代码,程序每次执行时都需要的。编绎程序速度相对快,因为编绎工作是一劳永逸,但解释性语言,在一个更灵活的编程环境,使得程序可以开发与运行交互性的进行。

The translation process highlights another advantage that high-level languages have over machine language: portability. The machine language of a computer is created by the designers of the particular CPU.

翻译过程的一个更重要的优势是,高级语言可以在不同的计算机上运行,便捷性。机器语言的构造与CPU的设计有关。



Each kind of computer has its own machine language. A program for a Pentium CPU won’t run on a Macintosh that sports a PowerPC. On the other hand, a program written in a high-level language can be run on many different kinds of computers as long as there is a suitable compiler or interpreter (which is just another program). For example, if I design a new computer, I can also program a Python interpreter for it, and then any program written in Python can be run on my new computer。

不同种类的计算机有不同的机器语言。Intel奔腾系列程序不能在苹果Macintosh上运行,它支持PowerPC。另一方面,高级语言可以在不同种类的计算机运行,只要它有相应的编绎器或者解释器。比如我设计一台新的计算机,我可以做一个Python的解释器,那用Python写的代码就可以在这台新电脑上运行了。

关于c++语言

c++语言的应用:

    1. 游戏开发
    1. 办公软件
    1. 图形处理软件
    1. 网站开发
    1. 操作系统
    1. 关系型数据库

愉快的入门(我们采用与c语言对照的方式入门):

首先我们来分析一个最简单的程序:

//c语言版本
#include<stdio.h>
void main()
{
printf("Hello World!");
while(1);
}
//c++版本
#include<iostream>
using namespace std;
void main()
{
cout<<"Hello World!";
cout<<endl;
while(1);
}

上述两种代码,第一种是c语言,第二种是c++.

下面我解读这两种代码:

c语言版本:

#include<stdio.h>

指示编译器在对程序进行预处理,将stdio.h这文件中的代码嵌入到当前程序,可以这么理解就是,前辈写了一些基础程序,这个语句就是将某些已经编写好的程序代码集合起来我们可以直接用,当然include语句还可以将自己写的文件嵌入进来.

void main()

这是C语言中非常重要的一部分——函数模块里面的核心部分,这语句就是main函数的标头,是程序的主函数,是程序的入口函数

*{} *

这个大括号是成对出现的,一个程序中大括号必须的相对应好的,他的作用就是标识这里面的话是一个整体,都属于左大括号外连接最近的那句话,所以在程序编程的时候必须注意大括号的对应关系。

printf("Hello World!");

printf就是stdio.h头文件的窗口输出语句,输出的是双引号内部的东西,以字符串的形式.

while(1)

在vs编译的时候常常发现窗口闪退,这是很正常的,因为程序已经完成了,那在没有任何需要用户完成的操作的情况下,窗口就会关闭,那么我们使用while循环使程序进入死循环,窗口不会关闭,那么同时我们可以使用getchar(),或者system("pause"),这些我们到后续课程将会讲解

c++版本

#include

指示编译器在对程序进行预处理,将iostream这文件中的代码嵌入到当前程序,可以这么理解就是,前辈写了一些基础程序,这个语句就是将某些已经编写好的程序代码集合起来我们可以直接用.

using namespace std;

是针对命名空间的指令,可以默认和上一句属于组合,搭配使用

void main()

这是C++语言中非常重要的一部分——函数模块里面的核心部分,这语句就是main函数的标头,标志着下面的语句内容是main函数的,作为重点,下面的附注中会重点讲解分析对C++语言的基本运行原则与main函数的关系。

{}

这个大括号是成对出现的,一个程序中大括号必须的相对应好的,他的作用就是标识这里面的话是一个整体,都属于左大括号外连接最近的那句话,所以在程序编程的时候必须注意大括号的对应关系。

cout<<"Hello World!";

cout就是iostream头文件中的窗口输出语句,<<就是将后面的字符串传递给cout,再由cout操作进行输出,在后续学习中我们会发现cout后面会连接多个<<,起到的作用也就是连续输出多值。

cout << endl;

当然四五两句可以实现合并,变成cout << "Hello World" << endl;作用是一样的,endl语句的作用就是换行。

while(1)

在vs编译的时候常常发现窗口闪退,这是很正常的,因为程序已经完成了,那在没有任何需要用户完成的操作的情况下,窗口就会关闭,那么我们使用while循环使程序进入死循环,窗口不会关闭,那么同时我们可以使用getchar(),或者system("pause"),这些我们到后续课程将会讲解

对语句进行详细描述之后,接下来,我们对一些细节进行分析:

1.c++和c语言,java语言一样,在每一个语句结束都要加一个分号( ; ) ,为了标识,语句已经结束,其中的标点符号,都要使用英文的,不能使用中文的标点符号,这是很多初学者容易犯的错误.

2.简单的c++程序的运行:简单的C++程序中cpp文件的基本运行相对简单,程序会先寻找到main函数,然后从main函数开始一句一句的执行,当main函数调用某个其他函数的时候,程序会去寻找该函数,并将所需参数传递进去,当该函数调用完毕后,程序会返回main函数从刚刚那个语句继续向下读取,最后当程序完全读完main函数后,程序将会退出。

最新文章

  1. 最新深度技术GHOST XP系统旗舰增强版 V2016年
  2. 算术表达式解析(第二版) C++11版
  3. win7 下加载MSCOMCTL.OCX
  4. JavaScript Ajax之美~
  5. Python快速建站系列-Part.Two-结构化和布局
  6. android opengl
  7. 一口气学会Linq
  8. nodejs 文件上传
  9. git plumbing 更加底层命令解析-深入理解GIT
  10. codeforces 613B B. Skills(枚举+二分+贪心)
  11. Git教程之工作区和暂存区(5)
  12. 2015第16周六学习java建议
  13. leetcode String to Integer (atoi) python
  14. 深入理解Java虚拟机-----------虚拟机类加载机制
  15. Django----模板层
  16. 03_Linux文件和目录
  17. volatile分析
  18. Intel 处理器架构演进 转
  19. nginx: worker process is shutting down
  20. anaconda的安装教程和使用方法

热门文章

  1. oracle 常用语句2
  2. 面试 11-01.ES6:模块化的使用和编译环境
  3. 线段树入门详解,洛谷P3372 【模板】线段树 1
  4. Vue 修改成功之后我做了什么
  5. Liunx运维(五)-信息显示与搜索文件命令
  6. mybatis-plus逻辑删除
  7. 【Azure Service Bus】 Service Bus如何确保消息发送成功,发送端是否有Ack机制 
  8. (三)文件的链接(ln)
  9. Kotlin 简单使用手册
  10. eclipse再见,android studio 新手入门教程(二)项目的导入