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

📄 chapter01.html

📁 think like a computer scientist
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<P>For example, when you hear the sentence, ``The other shoe fell,'' you understand that ``the other shoe'' is the subject and ``fell'' is the verb.  Once you have parsed a sentence, you can figure out what it means, that is, thesemantics of the sentence.  Assuming that you know what a shoe is, and what it means to fall, you will understand the general implication of this sentence.</P><P>Although formal and natural languages have many features in common---tokens,structure, syntax and semantics---there are many differences.</P><DL>  <DT>ambiguity:</DT><DD>Natural languages are full of ambiguity, which people   deal with by using contextual clues and other information.  Formal languages   are designed to be nearly or completely unambiguous, which means that any   statement has exactly one meaning, regardless of context.</DD>  <DT>redundancy:</DT><DD>In order to make up for ambiguity and reduce   misunderstandings, natural languages employ lots of redundancy.  As a result,  they are often verbose.  Formal languages are less redundant and more concise.  </DD>  <DT>literalness:</DT><DD>Natural languages are full of idiom and metaphor.    If I say, ``The other shoe fell,'' there is probably no shoe and nothing   falling.  Formal languages mean exactly what they say.</DD></DL><P>People who grow up speaking a natural language (everyone) often have a hard time adjusting to formal languages.  In some ways the difference between formaland natural language is like the difference between poetry and prose, but more so:</P><DL>  <DT>Poetry:</DT><DD>Words are used for their sounds as well as for their   meaning, and the whole poem together creates an effect or emotional response.  Ambiguity is not only common but often deliberate.</DD>  <DT>Prose:</DT><DD>The literal meaning of words is more important and the   structure contributes more meaning.  Prose is more amenable to analysis than   poetry, but still often ambiguous.</DD>     <DT>Programs:</DT><DD>The meaning of a computer program is unambiguous and   literal, and can be understood entirely by analysis of the tokens and   structure.</DD></DL><P>Here are some suggestions for reading programs (and other formal languages).First, remember that formal languages are much more dense than natural languages, so it takes longer to read them.  Also, the structure is very important, so it is usually not a good idea to read from top to bottom, left toright.  Instead, learn to parse the program in your head, identifying the tokens and interpreting the structure.  Finally, remember that the details matter.  Little things like spelling errors and bad punctuation, which you can get away with in natural languages, can make a big difference in a formal language.</P><BR><BR><H3>1.5 The first program</H3><P>Traditionally the first program people write in a new language is called ``Hello, World.'' because all it does is print the words ``Hello, World.''  In C++, this program looks like this:</P><PRE>#include &lt;iostream.h&gt;// main: generate some simple outputvoid main (){  cout << "Hello, world." << endl;}</PRE><P>Some people judge the quality of a programming language by the simplicity of the ``Hello, World.'' program.  By this standard, C++ does reasonably well.Even so, this simple program contains several features that are hard to explainto beginning programmers.  For now, we will ignore some of them, like the firstline.</P><P>The second line begins with <TT>//</TT>, which indicates that it is a <B>comment</B>.  A comment is a bit of English text that you can put in the middle of a program, usually to explain what the program does.  When the compiler sees a <TT>//</TT>, it ignores everything from there until the end of the line.</P><P>In the third line, you can ignore the word <TT>void</TT> for now, but notice the word <TT>main</TT>.  <TT>main</TT> is a special name that indicates the place in the program where execution begins.  When the program runs, it starts by executing the first statement in <TT>main</TT> and it continues, in order, until it gets to the last statement, and then it quits.</P><P>There is no limit to the number of statements that can be in <TT>main</TT>, but the example contains only one.  It is a basic <B>output</B> statement, meaning that it outputs or displays a message on the screen.</P><P><TT>cout</TT> is a special object provided by the system to allow you to send output to the screen.  The symbol <TT>&lt;&lt;</TT> is an <B>operator</B> that you apply to <TT>cout</TT> and a string, and that causes the string to be displayed.</P><P><TT>endl</TT> is a special symbol that represents the end of a line.  When you send an <TT>endl</TT> to <TT>cout</TT>, it causes the cursor to move to the next line of the display. The next time you output something, the new text appears on the next line.</P><P>Like all statements, the output statement ends with a semi-colon (<TT>;</TT>).</P><P>There are a few other things you should notice about the syntax of this program.  First, C++ uses squiggly-braces ({ and }) to group things together.In this case, the output statement is enclosed in squiggly-braces, indicating that it is <I>inside</I> the definition of <TT>main</TT>.  Also, notice that the statement is indented, which helps to show visually which lines are inside the definition.</P><P>At this point it would be a good idea to sit down in front of a computer andcompile and run this program.  The details of how to do that depend on your programming environment, but from now on in this book I will assume that you know how to do it.</P><P>As I mentioned, the C++ compiler is a real stickler for syntax.  If you makeany errors when you type in the program, chances are that it will not compile successfully.  For example, if you misspell <TT>iostream</TT>, you might get anerror message like the following:</P><PRE>hello.cpp:1: oistream.h: No such file or directory</PRE><P>There is a lot of information on this line, but it is presented in a dense format that is not easy to interpret.  A more friendly compiler might say something like:</P><BLOCKQUOTE>``On line 1 of the source code file named hello.cpp, you tried to include a header file named oistream.h.  I didn't find anything with that name, but I didfind something named iostream.h.  Is that what you meant, by any chance?''</BLOCKQUOTE><P>Unfortunately, few compilers are so accomodating.  The compiler is not really very smart, and in most cases the error message you get will be only a hint about what is wrong.  It will take some time to gain facility at interpreting compiler messages.</P><P>Nevertheless, the compiler can be a useful tool for learning the syntax rules of a language.  Starting with a working program (like hello.cpp), modify it in various ways and see what happens.  If you get an error message, try to remember what the message says and what caused it, so if you see it again in the future you will know what it means.</P><BR><BR><H3>1.6 Glossary</H3><DL>  <DT>problem-solving:</DT><DD>The process of formulating a problem, finding a   solution, and expressing the solution.</DD>  <DT>high-level language:</DT><DD>A programming language like C++ that is   designed to be easy for humans to read and write.</DD>  <DT>low-level language:</DT><DD>A programming language that is designed to be  easy for a computer to execute.  Also called ``machine language'' or   ``assembly language.''</DD>  <DT>portability:</DT><DDT>A property of a program that can run on more than   one kind of computer.</DD>  <DT>formal language:</DT><DD>Any of the languages people have designed for   specific purposes, like representing mathematical ideas or computer programs.  All programming languages are formal languages.</DD>  <DT>natural language:</DT><DD>Any of the languages people speak that have   evolved naturally.</DD>  <DT>interpret:</DT><DD>To execute a program in a high-level language by   translating it one line at a time.</DD>  <DT>compile:</DT><DD>To translate a program in a high-level language into a   low-level language, all at once, in preparation for later execution.</DD>  <DT>source code:</DT><DD>A program in a high-level language, before being   compiled.</DD>  <DT>object code:</DT><DD>The output of the compiler, after translating the   program.</DD>  <DT>executable:</DT><DD>Another name for object code that is ready to be   executed.</DD>  <DT>algorithm:</DT><DD>A general process for solving a category of problems.  </DD>  <DT>bug:</DT><DD>An error in a program.</DD>  <DT>syntax:</DT><DD>The structure of a program.</DD>  <DT>semantics:</DT><DD>The meaning of a program.</DD>  <DT>parse:</DT><DD>To examine a program and analyze the syntactic structure.  </DD>  <DT>syntax error:</DT><DD>An error in a program that makes it impossible to   parse (and therefore impossible to compile).</DD>  <DT>run-time error:</DT><DD>An error in a program that makes it fail at   run-time.</DD>  <DT>logical error:</DT><DD>An error in a program that makes it do something   other than what the programmer intended.</DD>  <DT>debugging:</DT><DD>The process of finding and removing any of the three   kinds of errors.</DD></DL><BR><DIV CLASS=navigation><HR>  <TABLE ALIGN=center WIDTH="100%" CELLPADDING=0 CELLSPACING=2>  <TR>    <TD><A HREF="chapter02.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/chapter02.html">      <IMG WIDTH=32 HEIGHT=32 ALIGN=bottom BORDER=0 ALT="next"       SRC="images/next.gif" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/next.gif"></A>    </TD>    <TD><A HREF="index.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/index.html">      <IMG WIDTH=32 HEIGHT=32 ALIGN=bottom BORDER=0 ALT="up"       SRC="images/up.gif" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/up.gif"></A>    </TD>    <TD><A HREF="index.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/index.html">      <IMG WIDTH=32 HEIGHT=32 ALIGN=bottom BORDER=0 ALT="previous"      SRC="images/previous.gif" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/previous.gif"></A>    </TD>    <TD ALIGN=center BGCOLOR="#99CCFF" WIDTH="100%">      <B CLASS=title>How to Think Like a Computer Scientist: Chapter 1</B>    </TD>    <TD><A HREF="index.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/index.html">      <IMG WIDTH=32 HEIGHT=32 ALIGN=bottom BORDER=0 ALT="contents"      SRC="images/contents.gif" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/contents.gif"></A>    </TD>    <TD>      <IMG WIDTH=32 HEIGHT=32 ALIGN=bottom BORDER=0 ALT=""      SRC="images/blank.gif" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/blank.gif">    </TD>    <TD>      <IMG WIDTH=32 HEIGHT=32 ALIGN=bottom BORDER=0 ALT=""      SRC="images/blank.gif" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/images/blank.gif">    </TD>  </TR>  </TABLE>  <B CLASS=navlabel>Next:</B>  <SPAN CLASS=sectref><A HREF="chapter02.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/chapter02.html">Chapter 2</A></SPAN>  <B CLASS=navlabel>Up:</B>  <SPAN CLASS=sectref><A HREF="index.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/index.html">Index</A></SPAN>  <B CLASS=navlabel>Previous:</B>  <SPAN CLASS=sectref><A HREF="index.html" tppabs="http://rocky.wellesley.edu/downey/ost/thinkCS/c++_html/index.html">Index</A></SPAN>  <HR></DIV></BODY></HTML>

⌨️ 快捷键说明

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