⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter01.html

📁 think like a computer scientist
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD>  <TITLE>Chapter 1</TITLE>  <LINK REL="STYLESHEET" HREF="downey.css" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/downey.css"></HEAD><BODY><H2>Chapter 1</H2><H1>The way of the program</H1><P>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 bestfeatures 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.</P><P>The single most important skill for a computer scientist is <B>problem-solving</B>.  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 topractice problem-solving skills.  That's why this chapter is called ``The way of the program.''</P><P>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 conceptsin this book, along with the details of programming in C++, you will have all the tools you need to do well on the exam.</P><BR><BR><H3>1.1 What is a programming language?</H3><P>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 <B>high-level languages</B>; other high-level languages you might have heard of are Java, C and FORTRAN.</P><P>As you might infer from the name ``high-level language,'' there are also <B>low-level languages</B>, 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.</P><P>But the advantages are enormous.  First, it is <I>much</I> 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 <B>portable</B>, 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.</P><P>Due to these advantages, almost all programs are written in high-level languages.  Low-level languages are only used for a few special applications.</P><P>There are two ways to translate a program; <B>interpreting</B> or <B>compiling</B>.  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.</P><P CLASS=1><IMG SRC="images/interpret.jpg" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/interpret.jpg" ALT="Interpret Image"></P><P>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 <B>source code</B>, and the translated program is called the <B>object code</B> or the <B>executable</B>.</P><P>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 <TT>program.cpp</TT>, where ``program'' is an arbitrary name you make up, and the suffix <TT>.cpp</TT> is a convention that indicates that the file contains C++ sourcecode.</P> <P>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 <TT>program.o</TT> to contain the object code, or <TT>program.exe</TT> to contain the executable.</P> <P CLASS=1><IMG SRC="images/compile.jpg" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/compile.jpg" ALT="Compile Image"></P><P>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.</P><P>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 andtype 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.</P><BR><BR><H3>1.2 What is a program?</H3><P>A program is a sequence of instructions that specifies how to perform a computation.  The computation might be something mathematical, like solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, like searching and replacing text in a document or (strangely enough) compiling a program.</P><P>The instructions (or commands, or statements) look different in different programming languages, but there are a few basic functions that appear in just about every language:</P><DL>  <DT>input:</DT><DD>Get data from the keyboard, or a file, or some other   device.</DD>  <DT>output:</DT><DD>Display data on the screen or send data to a file or   other device.</DD>  <DT>math:</DT><DD>Perform basic mathematical operations like addition and  multiplication.</DD>  <DT>testing:</DT><DD>Check for certain conditions and execute the appropriate  sequence of statements.</DD>  <DT>repetition:</DT><DD>Perform some action repeatedly, usually with some   variation.</DD></DL>  <P>Believe it or not, that's pretty much all there is to it.  Every program you've ever used, no matter how complicated, is made up of functions that look more or less like these.  Thus, one way to describe programming is the process of breaking a large, complex task up into smaller and smaller subtasks until eventually the subtasks are simple enough to be performed with one of these simple functions.</P><BR><BR><H3>1.3 What is debugging?</H3><P>Programming is a complex process, and since it is done by human beings, it often leads to errors.  For whimsical reasons, programming errors are called <B>bugs</B> and the process of tracking them down and correcting them is called<B>debugging</B>.</P><P>There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly.</P><H4>1.3.1 Compile-time errors</H4><P>The compiler can only translate a program if the program is syntactically correct; otherwise, the compilation fails and you will not be able to run your program.  <B>Syntax</B> refers to the structure of your program and the rules about that structure.</P><P>For example, in English, a sentence must begin with a capital letter and end with a period.  this sentence contains a syntax error.  So does this one</P><P>For most readers, a few syntax errors are not a significant problem, which is why we can read the poetry of e e cummings without spewing error messages.</P><P>Compilers are not so forgiving.  If there is a single syntax error anywhere in your program, the compiler will print an error message and quit, and you will not be able to run your program.</P><P>To make matters worse, there are more syntax rules in C++ than there are in English, and the error messages you get from the compiler are often not very helpful.  During the first few weeks of your programming career, you will probably spend a lot of time tracking down syntax errors.  As you gain experience, though, you will make fewer errors and find them faster.</P><H4>1.3.2 Run-time errors</H4><P>The second type of error is a run-time error, so-called because the error does not appear until you run the program.  For the simple sorts of programs we will be writing for the next few weeks, run-time errors are rare, so it might be a little while before you encounter one.</P><H4>1.3.3 Logic errors and semantics</H4><P>The third type of error is the <B>logical</B> or <B>semantic</B> error.  If there is a logical error in your program, it will compile and run successfully,in the sense that the computer will not generate any error messages, but it will not do the right thing.  It will do something else.  Specifically, it willdo what you told it to do.</P><P>The problem is that the program you wrote is not the program you wanted to write.  The meaning of the program (its semantics) is wrong.  Identifying logical errors can be tricky, since it requires you to work backwards by looking at the output of the program and trying to figure out what it is doing.</P><H4>1.3.4 Experimental debugging</H4><P>One of the most important skills you should acquire from working with this book is debugging.  Although it can be frustrating, debugging is one of the most intellectually rich, challenging, and interesting parts of programming.</P><P>In some ways debugging is like detective work.  You are confronted with clues and you have to infer the processes and events that lead to the results you see.</P><P>Debugging is also like an experimental science.  Once you have an idea what is going wrong, you modify your program and try again.  If your hypothesis was correct, then you can predict the result of the modification, and you take a step closer to a working program.  If your hypothesis was wrong, you have to come up with a new one.  As Sherlock Holmes pointed out, ``When you have eliminated the impossible, whatever remains, however improbable, must be the truth.'' (from A. Conan Doyle's <I>The Sign of Four</I>).</P><P>For some people, programming and debugging are the same thing.  That is, programming is the process of gradually debugging a program until it does what you want.  The idea is that you should always start with a working program thatdoes <I>something</I>, and make small modifications, debugging them as you go, so that you always have a working program.</P> <P>For example, Linux is an operating system that contains thousands of lines of code, but it started out as a simple program Linus Torvalds used to explore the Intel 80386 chip.  According to Larry Greenfield, ``One of Linus's earlier projects was a program that would switch between printing AAAA and BBBB.  This later evolved to Linux'' (from <I>The Linux Users' Guide</I> Beta Version 1).</P><P>In later chapters I will make more suggestions about debugging and other programming practices.</P><BR><BR><H3>1.4 Formal and natural languages</H3><P><B>Natural languages</B> are the languages that people speak, like English, Spanish, and French.  They were not designed by people (although people try to impose some order on them); they evolved naturally.</P><P><B>Formal languages</B> are languages that are designed by people for specific applications.  For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols.  Chemists use a formal language to represent the chemical structure of molecules.  And most importantly:</P><BLOCKQUOTE>Programming languages are formal languages that have beendesigned to express computations.</BLOCKQUOTE><P>As I mentioned before, formal languages tend to have strict rules about syntax.  For example, <TT>3+3=6</TT> is a syntactically correct mathematical statement, but <TT>3=+6$</TT> is not.  Also, <TT>H<SUB>2</SUB>O</TT> is a syntactically correct chemical name, but <TT><SUB>2</SUB>Zz</TT> is not.</P><P>Syntax rules come in two flavors, pertaining to tokens and structure.  Tokens are the basic elements of the language, like words and numbers and chemical elements.  One of the problems with <TT>3=+6$</TT> is that <TT>$</TT> is not a legal token in mathematics (at least as far as I know).Similarly, <TT><SUB>2</SUB>Zz</TT> is not legal because there is no element with the abbreviation <TT>Zz</TT>.</P><P>The second type of syntax error pertains to the structure of a statement; that is, the way the tokens are arranged.  The statement <TT>3=+6</TT> is structurally illegal, because you can't have a plus sign immediately after an equals sign.  Similarly, molecular formulas have to have subscripts after the element name, not before.</P><P>When you read a sentence in English or a statement in a formal language, you have to figure out what the structure of the sentence is (although in a natural language you do this unconsciously).  This process is called <B>parsing</B>.</P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -