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

📄 chapter03.html

📁 Thinking in c++ 2nd edition,c++编程思想(第2版)
💻 HTML
📖 第 1 页 / 共 5 页
字号:
execute.</FONT><A NAME="_Toc462979749"></A><A NAME="_Toc472654754"></A><BR></P></DIV>
<A NAME="Heading112"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using and misusing goto</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The
<A NAME="Index490"></A><A NAME="Index491"></A><B>goto</B> keyword is supported
in C++, since it exists in C. Using <B>goto</B> is often dismissed as poor
programming style, and most of the time it is. Anytime you use <B>goto</B>, look
at your code and see if there&#8217;s another way to do it. On rare occasions,
you may discover <B>goto</B> can solve a problem that can&#8217;t be solved
otherwise, but still, consider it carefully. Here&#8217;s an example that might
make a plausible candidate:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:gotoKeyword.cpp</font>
<font color=#009900>// The infamous goto is supported in C++</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>long</font> val = 0;
  <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 1; i &lt; 1000; i++) {
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> j = 1; j &lt; 100; j += 10) {
      val = i * j;
      <font color=#0000ff>if</font>(val &gt; 47000)
        <font color=#0000ff>goto</font> bottom; 
        <font color=#009900>// Break would only go to the outer 'for'</font>
    }
  }
  bottom: <font color=#009900>// A label</font>
  cout &lt;&lt; val &lt;&lt; endl;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The alternative would be to set a Boolean
that is tested in the outer <B>for</B> loop, and then do a <B>break</B> from the
inner for loop. However, if you have several levels of <B>for</B> or
<B>while</B> this could get awkward.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index492"></A><A NAME="_Toc472654755"></A><BR></P></DIV>
<A NAME="Heading113"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Recursion</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Recursion is an interesting and sometimes
useful programming technique whereby you call the function that you&#8217;re in.
Of course, if this is all you do, you&#8217;ll keep calling the function
you&#8217;re in until you run out of memory, so there must be some way to
&#8220;bottom out&#8221; the recursive call. In the following example, this
&#8220;bottoming out&#8221; is accomplished by simply saying that the recursion
will go only until the <B>cat</B> exceeds
&#8216;Z&#8217;:</FONT><A NAME="fnB31" HREF="#fn31">[31]</A><A NAME="Index493"></A><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:CatsInHats.cpp</font>
<font color=#009900>// Simple demonstration of recursion</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>void</font> removeHat(<font color=#0000ff>char</font> cat) {
  <font color=#0000ff>for</font>(<font color=#0000ff>char</font> c = 'A'; c &lt; cat; c++)
    cout &lt;&lt; <font color=#004488>"  "</font>;
  <font color=#0000ff>if</font>(cat &lt;= 'Z') {
    cout &lt;&lt; <font color=#004488>"cat "</font> &lt;&lt; cat &lt;&lt; endl;
    removeHat(cat + 1); <font color=#009900>// Recursive call</font>
  } <font color=#0000ff>else</font>
    cout &lt;&lt; <font color=#004488>"VOOM!!!"</font> &lt;&lt; endl;
}

<font color=#0000ff>int</font> main() {
  removeHat('A');
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>removeHat(&#160;)</B>, you can see
that as long as <B>cat</B> is less than &#8216;Z&#8217;,
<B>removeHat(&#160;)</B> will be called from <I>within</I>
<B>removeHat(&#160;)</B>, thus effecting the recursion. Each time
<B>removeHat(&#160;)</B> is called, its argument is one greater than the current
<B>cat</B> so the argument keeps increasing.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Recursion is often used when evaluating
some sort of arbitrarily complex problem, since you aren&#8217;t restricted to a
particular &#8220;size&#8221; for the solution &#8211; the function can just
keep recursing until it&#8217;s reached the end of the problem.</FONT><BR></P></DIV>
<A NAME="Heading114"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
<A NAME="_Toc462979750"></A><A NAME="_Toc472654756"></A>Introduction to
operators<A NAME="Index494"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can think of operators as a special
type of function (you&#8217;ll learn that C++ operator overloading treats
operators precisely that way). An operator takes one or more arguments and
produces a new value. The arguments are in a different form than ordinary
function calls, but the effect is the same.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">From your previous programming
experience, you should be reasonably comfortable with the operators that have
been used so far. The concepts of addition (<B>+</B>), subtraction and unary
minus (<B>-</B>), multiplication (<B>*</B>), division (<B>/</B>), and
assignment(<B>=</B>) all have essentially the same meaning in any programming
language. The full set of operators is enumerated later in this
chapter.</FONT><A NAME="_Toc462979751"></A><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index495"></A><A NAME="_Toc472654757"></A><BR></P></DIV>
<A NAME="Heading115"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Precedence<BR><A NAME="Index496"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Operator precedence defines the order in
which an expression evaluates when several different operators are present. C
and C++ have specific rules to determine the order of evaluation. The easiest to
remember is that multiplication and division happen before addition and
subtraction. After that, if an expression isn&#8217;t transparent to you it
probably won&#8217;t be for anyone reading the code, so you should use
parentheses to make the order of evaluation explicit. For
example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>A = X + Y - 2/2 + Z;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">has a very different meaning from the
same statement with a particular grouping of parentheses:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>A = X + (Y - 2)/(2 + Z);</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">(Try evaluating the result with X = 1, Y
= 2, and Z =
3.)</FONT><A NAME="_Toc462979752"></A><A NAME="_Toc472654758"></A><BR></P></DIV>
<A NAME="Heading116"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Auto increment <A NAME="Index497"></A>and decrement<A NAME="Index498"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C, and therefore C++, is full of
shortcuts. Shortcuts can make code much easier to type, and sometimes much
harder to read. Perhaps the C language designers thought it would be easier to
understand a tricky piece of code if your eyes didn&#8217;t have to scan as
large an area of print.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the nicer shortcuts is the
auto-increment<A NAME="Index499"></A> and auto-decrement<A NAME="Index500"></A>
operators. You often use these to change loop variables, which control the
number of times a loop executes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index501"></A><A NAME="Index502"></A><A NAME="Index503"></A><A NAME="Index504"></A><FONT FACE="Georgia">The
auto-decrement operator is &#8216;<B>--</B>&#8217; and means &#8220;decrease by
one unit.&#8221; The auto-increment operator is &#8216;<B>++</B>&#8217; and
means &#8220;increase by one unit.&#8221; If <B>A</B> is an <B>int</B>, for
example, the expression <B>++A</B> is equivalent to (<B>A = A + 1</B>).
Auto-increment and auto-decrement operators produce the value of the variable as
a result. If the operator appears before the variable, (i.e., <B>++A</B>), the
operation is first performed and the resulting value is produced. If the
operator appears after the variable (i.e. <B>A++</B>), the current value is
produced, and then the operation is performed. For example:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:AutoIncrement.cpp</font>
<font color=#009900>// Shows use of auto-increment</font>
<font color=#009900>// and auto-decrement operators.</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>int</font> main() {
  <font color=#0000ff>int</font> i = 0;
  <font color=#0000ff>int</font> j = 0;
  cout &lt;&lt; ++i &lt;&lt; endl; <font color=#009900>// Pre-increment</font>
  cout &lt;&lt; j++ &lt;&lt; endl; <font color=#009900>// Post-increment</font>
  cout &lt;&lt; --i &lt;&lt; endl; <font color=#009900>// Pre-decrement</font>
  cout &lt;&lt; j-- &lt;&lt; endl; <font color=#009900>// Post decrement</font>
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you&#8217;ve been wondering about the
name &#8220;<A NAME="Index505"></A>C++,&#8221; now you understand. It implies
&#8220;one step beyond
C.&#8221;</FONT><A NAME="_Toc462979753"></A><A NAME="_Toc472654759"></A><BR></P></DIV>
<A NAME="Heading117"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Introduction to data<A NAME="Index506"></A> types</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><I>Data types </I>define the way you use
storage (memory) in the programs you write. By specifying a data type, you tell
the compiler how to create a particular piece of storage, and also how to
manipulate that storage.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Data types can be
built-in<A NAME="Index507"></A> or abstract<A NAME="Index508"></A>. A built-in
data type<A NAME="Index509"></A> is one that the compiler intrinsically
understands, one that is wired directly into the compiler. The types of built-in
data are almost identical in C and C++. In contrast, a user-defined data
type<A NAME="Index510"></A><A NAME="Index511"></A> is one that you or another
programmer create as a class. These are commonly referred to as abstract data
types<A NAME="Index512"></A>. The compiler knows how to handle built-in types
when it starts up; it &#8220;learns&#8221; how to handle abstract data types by
reading header files<A NAME="Index513"></A> containing class declarations
(you&#8217;ll learn about this in later
chapters).</FONT><A NAME="_Toc462979754"></A><A NAME="_Toc472654760"></A><BR></P></DIV>
<A NAME="Heading118"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Basic built-in
types<A NAME="Index514"></A><A NAME="Index515"></A><BR><A NAME="Index516"></A><A NAME="Index517"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Standard C specification for built-in
types (which C++ inherits) doesn&#8217;t say how many bits each of the built-in
types must contain. Instead, it stipulates the minimum and maximum values that
the built-in type must be able to hold. When a machine is based on binary, this
maximum value can be directly translated into a minimum number of bits necessary
to hold that value. However, if a machine uses, for example, binary-coded
decimal (BCD) to represent numbers, then the amount of space in the machine
required to hold the maximum numbers for each data type will be different. The
minimum and maximum values that can be stored in the various data types are
defined in the system header files <B>limits.h<A NAME="Index518"></A> </B>and
<B>float.h<A NAME="Index519"></A></B> (in C++ you will generally <B>#include
&lt;climits&gt;</B> and <B>&lt;cfloat&gt;</B> instead).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C and C++ have four basic built-in data
types, described here for binary-based machines. A
<B>char<A NAME="Index520"></A><A NAME="Index521"></A></B> is for character
storage and uses a minimum of 8 bits (one byte) of storage, although it may be
larger. An <B>int<A NAME="Index522"></A><A NAME="Index523"></A></B> stores an
integral number and uses a minimum of two bytes of storage. The
<B>float<A NAME="Index524"></A><A NAME="Index525"></A></B> and
<B>double<A NAME="Index526"></A></B> types store floating-point
numbers<A NAME="Index527"></A>, usually in IEEE floating-point
format<A NAME="Index528"></A>. <B>float</B> is for single-precision floating
point<A NAME="Index529"></A> and <B>double</B> is for double-precision floating
point<A NAME="Index530"></A>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As mentioned previously, you can define
variables anywhere in a scope, and you can define and initialize
them<A NAME="Index531"></A><A NAME="Index532"></A> at the same time.
Here&#8217;s how to define variables using the four basic data
types:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Basic.cpp</font>
<font color=#009900>// Defining the four basic data</font>
<font color=#009900>// types in C and C++</font>

<font color=#0000ff>int</font> main() {
  <font color=#009900>// Definition without initialization:</font>
  <font color=#0000ff>char</font> protein;
  <font color=#0000ff>int</font> carbohydrates;
  <font color=#0000ff>float</font> fiber;
  <font color=#0000ff>double</font> fat;

⌨️ 快捷键说明

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