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

📄 solutions2.html

📁 C ++ in action
💻 HTML
字号:
<html>
<head>
    <title>Exercise Solutions</title>
    <link rel="stylesheet" href="../../rs.css">
</head>

<body background="../../images/margin.gif" bgcolor="#FFFFDC">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">
    &nbsp;
    <td>

<h3>Exercise Solutions</h3>

<ol>
<li>Top
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>int IStack::Top () const
{
    assert (_top &gt; 0);
    return _arr [_top - 1];
}

int IStack::Count () const
{    return _top;
}
</pre>
</td></tr></table><!-- End Code -->

<li>This example calls Pop on an empty stack, in violation of the contract.
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>int main ()
{
    IStack stack;
    stack.Pop (); 
}</pre>
</td></tr></table><!-- End Code -->


<li> This is the interface of the stack of characters:
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>class CharStack
{
public:
    CharStack () :_top (0) {}
    void Push (char c);
    char Pop ();
    char Top () const;
    int Count () const;
private:
    char _arr [maxStack];
    int  _top;
};</pre>
</td></tr></table><!-- End Code -->

This is how you reverse a string using a stack of characters:
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>int main ()
{
    CharStack stack;
    char str [] = "esreveR";
    for (int i = 0; str [i] != '\0'; ++i)
        stack.Push (str [i]);
    while (stack.Count () &gt; 0)
        std::cout &lt;&lt; stack.Pop ();
    std::cout &lt;&lt; std::endl;
}</pre>
</td></tr></table><!-- End Code -->


<li>Queue
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>#include <iostream>
#include <cassert>

const int maxPuts = 8;

class Queue
{
public:
    Queue ();
    double Get ();
    void Put (double x);
private:
    double    _arr [maxPuts];
    int        _putIdx;
    int        _getIdx;
};

Queue::Queue ()
: _putIdx (0),
_getIdx (0)
{}

double Queue::Get ()
{
    assert (_getIdx &lt; _putIdx);
    ++_getIdx;
    return _arr [_getIdx - 1];
}

void Queue::Put (double x)
{
    assert (_putIdx &lt; maxPuts);
    _arr [_putIdx] = x;
|    ++_putIdx;
}
|
int main ()
{
    Queue queue;
    queue.Put (0.1);
    queue.Put (0.2);
    std::cout &lt;&lt; "Getting: " &lt;&lt; queue.Get () &lt;&lt; ", " 
        &lt;&lt; queue.Get () &lt;&lt; std::endl;
    queue.Put (0.3);
    std::cout &lt;&lt; "Getting more: " &lt;&lt; queue.Get () &lt;&lt; std::endl; 
}
</pre>
</td></tr></table><!-- End Code -->

<li>Array of double
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>#include <iostream>
#include <cassert>

const int maxCells = 16;

class DblArray
{
public:
    DblArray ();
    void Set (int i, double val);
    double Get (int i) const;
    bool IsSet (int i) const;
private:
    double    _arr [maxCells];
    bool    _isSet [maxCells];
};

DblArray::DblArray ()
{
    for (int i = 0; i &lt; maxCells; ++i)
        _isSet [i] = false;
}

void DblArray::Set (int i, double val)
{
    assert (i &lt; maxCells);
    assert (!IsSet (i));
    _arr [i] = val;
    _isSet [i] = true;
}

double DblArray::Get (int i) const
{
    assert (i &lt; maxCells);
    assert (_isSet [i]);
    return _arr [i];
}

bool DblArray::IsSet (int i) const
{
    assert (i &lt; maxCells);
    return _isSet [i];
}

int main ()
{
    DblArray arr;
    arr.Set (2, 0.2);
    arr.Set (4, 0.4);
    std::cout &lt;&lt; "IsSet (0) returns " &lt;&lt; arr.IsSet (0) &lt;&lt; std::endl;
    std::cout &lt;&lt; "IsSet (2) returns " &lt;&lt; arr.IsSet (2) &lt;&lt; std::endl;
    std::cout &lt;&lt; "Get (2) returns " &lt;&lt; arr.Get (2) &lt;&lt; std::endl;
    std::cout &lt;&lt; "Get (4) returns " &lt;&lt; arr.Get (4) &lt;&lt; std::endl;
}</pre>
</td></tr></table><!-- End Code -->


</ol>
</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

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