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

📄 ch02.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>
<head>
<title>Fundamental Data Types</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<h2>Chapter 2: Fundamental Data Types</h2>
<p align="center"><img src="images/ccc_4.png"></p>
<h2>Chapter Goals</h2>
<ul>
  <li><font size="+1">To understand integer and floating point numbers</font></li>
  <li><font size="+1">To write arithmetic expressions in C++</font></li>
  <li><font size="+1">To appreciate the importance of comments and good code layout</font></li>
  <li><font size="+1">To be able to define and initialize variables and constants</font></li>
  <li><font size="+1">To recognize the limitations of the <tt>int</tt> and <tt>double</tt> 
    types and the overflow and roundoff errors that can result</font></li>
  <li><font size="+1">To be able to change the values of variables through assignment</font></li>
  <li><font size="+1">To use the standard C++ <tt>string</tt> type to define and 
    manipulate character strings</font></li>
  <li><font size="+1">To be able to write simple programs that read numbers and 
    text, process the input, and display the results</font></li>
</ul>
<h2>Number Types</h2>
<ul>
  <li><font size="+1">Integer numbers (<tt>int</tt>) are whole numbers without 
    a fractional part.</font> 
    <ul>
      <li><font size="+1">Includes zero and negative numbers.</font></li>
      <li><font size="+1">Used for storing values that are conceptually whole 
        numbers (e.g. pennies).</font></li>
      <li><font size="+1">Processed faster and require less storage space.</font></li>
    </ul>
  </li>
  <li><font size="+1">Floating-point numbers (<tt>double</tt>) have decimal points.</font></li>
</ul>
<h2>Number Types (coins1.cpp)</h2>
  <iframe src="code/coins1.cpp.html" width="80%" height="80%">Your browser does not support the iframe tag</iframe>
<h2>Number Types (Syntax 2.1: Output Statement)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.1 : Output Statement</font></p>
      <pre><font size="+1">cout &lt;&lt; <i>expression</i><sub>1</sub> &lt;&lt; <i>expressioun</i><sub>2</sub> &lt;&lt; ... &lt;&lt; <i>expression</i><sub>n</sub>;</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">cout &lt;&lt; pennies;
cout &lt;&lt; &quot;Total value = &quot; &lt;&lt; total &lt;&lt; &quot;\n&quot;;</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">Prints the values of one or more expressions.</font></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<h2>Number Types</h2>
<ul>
  <li><font size="+1">Use the asterisk * for multiplication (not dot or cross). 
    </font></li>
  <li><font size="+1">Comments are denoted by <tt>/* comment text */ </tt></font> 
    <ul>
      <li><font size="+1">Ignored by the compiler. </font></li>
      <li><font size="+1">Explains the program to other programmers or yourself. 
        </font></li>
    </ul>
  </li>
  <li><font size="+1">Variable naming rules:</font></li>
  <li><font size="+1">Names must start with a letter. </font>
    <ul>
      <li><font size="+1">Remaining characters are letters or underscore (<tt>_</tt>) 
        (no spaces or special symbols)</font></li>
      <li><font size="+1">You cannot use <i>reserved words.</i></font></li>
      <li><font size="+1">Variable names are <i>case-sensitive</i>.</font></li>
    </ul>
  </li>
</ul>
<h2>Number Types (Syntax 2.2: Variable Definition)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.2 : Variable Definition</font></p>
      <pre><font size="+1"><i>type_name variable_name;
type_name variable_name = initial_value;</i>
</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">int pennies = 8;
double total;</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">Defines a new variable of a particular type, and 
            optionally supply an initial value.</font></td>
        </tr>
      </table>
      <p>&nbsp; 
    </td>
  </tr>
</table>
<h2>Number Types (Syntax 2.3: Comment)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.3 : Comments</font></p>
      <pre><font size="+1"><i>/* comment text */
// comment text</i>
</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">/* total value of coins */
// total value of coins</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">Add a comment to help the human reader understand 
            the program.</font></td>
        </tr>
      </table>
      <p>&nbsp; 
    </td>
  </tr>
</table>
<h2>Input and Output</h2>
<UL>
  <LI><TT><font size="+1">&gt;&gt;</font></TT><font size="+1"> operator reads 
    console input: </font> 
    <blockquote> 
      <p><font size="+1"><TT>cin &gt;&gt; pennies;</TT> </font></p>
    </blockquote>
  <LI><font size="+1">If user doesn't enter appropriate input, then the stream 
    fails. All subsequent input also fails.</font> 
  <LI><font size="+1">Can read multiple values: </font> 
    <blockquote> 
      <p><font size="+1"><TT>cin &gt;&gt; pennies &gt;&gt; nickels &gt;&gt; dimes 
        &gt;&gt; quarters;</TT> </font></p>
    </blockquote>
  <LI><font size="+1">User enters input separated by spaces or newlines, e.g.<BR>
    </font> 
    <blockquote> 
      <p><font size="+1"><TT>8 0 4 3</TT> </font></p>
    </blockquote>
    <font size="+1"> 
    <P>or 
    </font> </LI>
  <blockquote><font size="+1"> 
    <pre>8
0
4
3</pre>
    </font></blockquote>
  <li><font size="+1">Input is <I>buffered</I>. If you type two inputs on one 
    line, the second one is saved for the next input statement.</font><BR>
  </li>
</UL>
<h2>Input and Output (Syntax 2.4: Input Statement)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.4 : Input Statement</font></p>
      <pre><font size="+1">cin &gt;&gt;<i> variable</i><sub>1</sub> &gt;&gt;<i> variable</i><sub>2</sub> &gt;&gt; ... &gt;&gt; <i>variable</i><sub>n</sub>;</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">cin &gt;&gt; pennies;
cin &gt;&gt; first &gt;&gt; middle &gt;&gt; last;</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">Read the value for one or more variables in from 
            the console.</font></td>
        </tr>
      </table>
      <p>&nbsp; 
    </td>
  </tr>
</table>
<h2>Input and Output (coins3.cpp)</h2>
  <iframe src="code/coins3.cpp.html" width="80%" height="80%">Your browser does not support the iframe tag</iframe>
<h2>Assignment (Syntax 2.5 : Assignment)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.5 : Assignment</font></p>
      <pre><font size="+1"><i>variable</i> = <i>expression</i>;</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">total = pennies * 0.01;</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">Store the value of an expression in a variable.</font></td>
        </tr>
      </table>
      <p>&nbsp; 
    </td>
  </tr>
</table>
<h2>Assignment (Assignment Operator)</h2>
<ul>
  <li><font size="+1"><tt>=</tt> operator assigns the value of the expression 
    on the <i>right</i> to the variable on the <i>left</i>. 
    <blockquote><pre>total = pennies * 0.01;
total = count * 0.05 + total;</pre></blockquote><img src="images/ch2-assignment.png"></font></li>
  <li><font size="+1">Do not confuse the <i>assignment operation</i> with <i>algebraic 
    equality</i>. </font></li>
</ul>
<h2>Assignment (Increment and Decrement)</h2>
<ul>
  <li><font size="+1">Assignments like <tt>month = month + 1;</tt> are very common 
    in programming. </font> 
    <P><img src="images/ch2-increment.png" width="292" height="238">
  </li>
  <li><font size="+1">This assignment <i>increments</i> the variable month.</font></li>
  <li><font size="+1">C++ uses a special shorthand for increment: </font>
    <blockquote> 
      <p><font size="+1"><tt>month++; // add 1 to month</tt></font></p>
    </blockquote>
  </li>
  <LI><font size="+1">C++ also supports <i>decrement</i>: </font> 
    <blockquote> 
      <p><font size="+1"><tt>month--; // subtract 1 from month</tt></font></p>
    </blockquote>
</ul>
<h2>Constants (Syntax 2.7 : Constant Definition)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.7 : Constant Definition</font></p>
      <pre><font size="+1">const<i> type_name constant_name</i> = <i>initial_value</i>;</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">const double LITER_PER_OZ = 0.029586</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">Define a new constant of a particular type and supply 
            its value.</font></td>
        </tr>
      </table>
      <p>&nbsp; 
    </td>
  </tr>
</table>
<h2>Constants (volume2.cpp)</h2>
  
<iframe src="code/volume2.cpp.html" width="80%" height="80%">Your browser does not 
support the iframe tag</iframe> 
<h2>Constants</h2>
<ul>
  <li><font size="+1">Giving symbolic names to constants makes programs easier 
    to read and modify.</font></li>
  <li><font size="+1">Example: Suppose you switch from 2-liter bottle to half 
    gallon bottles:</font> 
    <ul>
      <li><font size="+1">Must <i>find</i> every 2 in program.</font></li>
      <li><font size="+1"><i>Decide</i> if number represents bottle volume, or 
        something else</font></li>
      <li><font size="+1"><i>Replace</i> with 1.893 if appropriate.</font></li>
    </ul>
  </li>
  <li><font size="+1">Constants mean only changing a program at <i>one location</i>.</font></li>
</ul>
<h2>Arithmetic (Basic Operations)</h2>
<ul>
  <li><font size="+1">All four basic arithmetic operations are supported by C++:</font>
    <ul>
      <li><font size="+1">Addition and subtraction: <tt>+ -</tt></font></li>
      <li><font size="+1">Multiplication and division: <tt>* /</tt></font></li>
    </ul>
  </li>
  <li><font size="+1">Multiplication and division <i>bind more strongly</i> than 
    addition and subtraction.</font></li>
  <li><font size="+1">Use parentheses to indicate the order in which the subexpressions 
    should be evaluated.</font> 
    <blockquote> 
      <table border="0" cellpadding="4">
        <tr> 
          <td><img src="images/math1.png"></td>
          <td> 
            <div align="center"><tt><font size="+1">a + b / 2</font></tt></div>
          </td>
        </tr>
        <tr> 
          <td><img src="images/math2.png"></td>
          <td> 
            <div align="center"><tt><font size="+1">( a + b ) / 2</font></tt></div>
          </td>
        </tr>
      </table>
    </blockquote>
  </li>
</ul>
<h2>Arithmetic (Integer Division)</h2>
<ul>
  <li><font size="+1">In division, if at least one of the numbers is a floating-point 
    number, the result is a floating point number: </font> 
    <pre><font size="+1">7.0 / 4.0   /* 1.75 */
7 / 4.0     /* 1.75 */
7.0 / 4     /* 1.75 */</font></pre>
  </li>
  <li><font size="+1">If <i>both</i> numbers are integers, then the result is 
    an integer: </font> 
    <pre><font size="+1">7 / 4      /* 1 */</font></pre>
  </li>
  <li><font size="+1">To get the remainder of division between two integers, use 
    the modulus operator <tt>%</tt>:</font> 
    <pre><font size="+1">7 % 4      /* 3 remainder */</font></pre>
  </li>
</ul>
<h2>Arithmetic (coins5.cpp)</h2>
  
<iframe src="code/coins5.cpp.html" width="80%" height="80%">Your browser does not 
support the iframe tag</iframe> 
<h2>Arithmetic (Syntax 2.8 : Function Call)</h2>
<table border="1" cellpadding="4" bgcolor="#00FFFF">
  <tr> 
    <td bgcolor="#FFFFFF"> 
      <p><font size="+1" color="#00FFFF">Syntax 2.8 : Function Call</font></p>
      <pre><font size="+1"><i>function_name</i>(<i>expression</i><sub>1</sub>,<i>expressioun</i><sub>2</sub>, ..., <i>expression</i><sub>n</sub>);</font></pre>
      <table border="0" cellpadding="4">
        <tr> 
          <td valign="top"><font size="+1" color="#00FFFF">Example:</font><font size="+1"> 
            </font></td>
          <td> 
            <pre><font size="+1">sqrt(x)
pow(z + y, n)</font></pre>
          </td>
        </tr>
        <tr> 
          <td><font size="+1" color="#00FFFF">Purpose:</font><font size="+1"> 
            </font></td>
          <td><font size="+1">The result of calling a function and supplying the 
            values for the function parameters.</font></td>
        </tr>
      </table>
      <p>&nbsp; 
    </td>
  </tr>
</table>
<h2>Arithmetic (cmath Library Functions)</h2>
<blockquote>
  <table border="1" cellpadding="4" width="509" bgcolor="black">
    <tr bgcolor="#00FFFF"> 
    <td width="145"> 
      <div align="center"><b><font size="+1">Function</font></b></div>
    </td>
    <td width="336"> 
      <div align="center"><b><font size="+1">Description</font></b></div>
    </td>
  </tr>
    <tr bgcolor="White"> 
      <td height="30" width="145"> 
        <div align="center"><tt><font size="+1">sqrt(x)</font></tt></div>
    </td>
      <td height="30" width="336"> 
        <div align="center"><font size="+1">square root of <i>x</i></font></div>
    </td>
  </tr>
    <tr bgcolor="White"> 
      <td width="145"> 
        <div align="center"><tt><font size="+1">pow(x, y)</font></tt></div>
    </td>
      <td width="336"> 
        <div align="center"><i><font size="+1">x<sup>y</sup></font></i></div>
    </td>
  </tr>
    <tr bgcolor="White"> 
      <td width="145"> 
        <div align="center"><tt><font size="+1">sin(x)</font></tt></div>
    </td>
      <td width="336"> 
        <div align="center"><font size="+1">sine of <i>x</i> (in radians)</font></div>
    </td>
  </tr>
    <tr bgcolor="White"> 
      <td width="145"> 

⌨️ 快捷键说明

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