📄 solutions2.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">
<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 > 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 () > 0)
std::cout << stack.Pop ();
std::cout << 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 < _putIdx);
++_getIdx;
return _arr [_getIdx - 1];
}
void Queue::Put (double x)
{
assert (_putIdx < maxPuts);
_arr [_putIdx] = x;
| ++_putIdx;
}
|
int main ()
{
Queue queue;
queue.Put (0.1);
queue.Put (0.2);
std::cout << "Getting: " << queue.Get () << ", "
<< queue.Get () << std::endl;
queue.Put (0.3);
std::cout << "Getting more: " << queue.Get () << 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 < maxCells; ++i)
_isSet [i] = false;
}
void DblArray::Set (int i, double val)
{
assert (i < maxCells);
assert (!IsSet (i));
_arr [i] = val;
_isSet [i] = true;
}
double DblArray::Get (int i) const
{
assert (i < maxCells);
assert (_isSet [i]);
return _arr [i];
}
bool DblArray::IsSet (int i) const
{
assert (i < maxCells);
return _isSet [i];
}
int main ()
{
DblArray arr;
arr.Set (2, 0.2);
arr.Set (4, 0.4);
std::cout << "IsSet (0) returns " << arr.IsSet (0) << std::endl;
std::cout << "IsSet (2) returns " << arr.IsSet (2) << std::endl;
std::cout << "Get (2) returns " << arr.Get (2) << std::endl;
std::cout << "Get (4) returns " << arr.Get (4) << 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 + -