Title:
Home
|
edited by
Youngrok Pak
at
6 years, 7 months ago.
<h1>
Project Rosalynde for KAIST student</h1>
<p>
</p>
<p class="p1">
<img src="/~attachments/173" /></p>
<p class="p1">
</p>
<p class="p1">
</p>
<p class="p1">
</p>
<p class="p1">
/***************************</p>
<p class="p1">
* Programming Methodology *</p>
<p class="p1">
* Practice Session #9 *</p>
<p class="p1">
* Polymorphism *</p>
<p class="p1">
* Skeleton Code *</p>
<p class="p1">
***************************/</p>
<p class="p2">
</p>
<p class="p3">
<span class="s1">#include </span><iostream></p>
<p class="p2">
</p>
<p class="p4">
using<span class="s2"> </span>namespace<span class="s2"> </span><span class="s3">std</span><span class="s2">;</span></p>
<p class="p2">
</p>
<p class="p5">
#define MAX_SIZE <span class="s4">10</span></p>
<p class="p5">
#define MIN_SIZE <span class="s4">0</span></p>
<p class="p5">
#define ERROR -<span class="s4">1</span></p>
<p class="p2">
</p>
<p class="p2">
</p>
<p class="p4">
class<span class="s2"> ADT</span></p>
<p class="p6">
{</p>
<p class="p4">
public<span class="s2">:</span></p>
<p class="p6">
ADT(){</p>
<p class="p6">
<span class="s5">items</span>=<span class="s6">new</span> <span class="s6">int</span>[<span class="s1">MAX_SIZE</span>];</p>
<p class="p6">
}</p>
<p class="p6">
~ADT(){</p>
<p class="p6">
<span class="s6">delete</span>[] <span class="s5">items</span>;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">virtual</span> <span class="s6">void</span> push(<span class="s6">int</span>)=<span class="s4">0</span>;</p>
<p class="p6">
<span class="s6">virtual</span> <span class="s6">int</span> pop()=<span class="s4">0</span>;</p>
<p class="p6">
<span class="s6">virtual</span> <span class="s6">void</span> print()=<span class="s4">0</span>;</p>
<p class="p1">
<span class="s2"> </span>/* ±∏«ˆ«œººø‰ */</p>
<p class="p4">
protected<span class="s2">:</span></p>
<p class="p6">
<span class="s6">int</span> *items;</p>
<p class="p6">
};</p>
<p class="p2">
</p>
<p class="p4">
class<span class="s2"> Stack : </span>public<span class="s2"> </span><span class="s5">ADT</span></p>
<p class="p6">
{</p>
<p class="p4">
public<span class="s2">:</span></p>
<p class="p6">
Stack()</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s5">current</span>=<span class="s4">0</span>;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">void</span> push(<span class="s6">int</span> x)</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">current</span>==<span class="s1">MAX_SIZE</span>) <span class="s6">return</span>;</p>
<p class="p6">
<span class="s5">items</span>[<span class="s5">current</span>++]=x;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">int</span> pop(){</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">current</span>==<span class="s1">MIN_SIZE</span>) <span class="s6">return</span> <span class="s1">ERROR</span>;</p>
<p class="p6">
<span class="s6">int</span> pop=<span class="s5">items</span>[--<span class="s5">current</span>];</p>
<p class="p6">
<span class="s6">return</span> pop;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">void</span> print(){</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s7">"Stack: "</span>;</p>
<p class="p6">
<span class="s6">for</span>(<span class="s6">int</span> i=<span class="s5">current</span>-<span class="s4">1</span>;i>=<span class="s4">0</span>;i--) <span class="s3">cout</span> << <span class="s5">items</span>[i] << <span class="s7">" "</span>;</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s3">endl</span>;</p>
<p class="p6">
}</p>
<p class="p1">
<span class="s2"> </span>/* ±∏«ˆ«œººø‰ */</p>
<p class="p4">
private<span class="s2">:</span></p>
<p class="p6">
<span class="s6">int</span> current;</p>
<p class="p6">
};</p>
<p class="p2">
</p>
<p class="p4">
class<span class="s2"> Queue : </span>public<span class="s2"> </span><span class="s5">ADT</span></p>
<p class="p6">
{</p>
<p class="p4">
public<span class="s2">:</span></p>
<p class="p6">
Queue(){</p>
<p class="p6">
<span class="s5">front</span>=<span class="s5">rear</span>=<span class="s4">0</span>;</p>
<p class="p6">
<span class="s5">full</span>=<span class="s6">false</span>;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">void</span> push(<span class="s6">int</span> x)</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">full</span>==<span class="s6">true</span>) <span class="s6">return</span>;</p>
<p class="p6">
<span class="s5">items</span>[<span class="s5">rear</span>]=x;</p>
<p class="p6">
<span class="s5">rear</span>=(<span class="s5">rear</span>+<span class="s4">1</span>)%<span class="s1">MAX_SIZE</span>;</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">rear</span>==<span class="s5">front</span>) <span class="s5">full</span>=<span class="s6">true</span>;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">int</span> pop()</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">full</span>==<span class="s6">false</span> && <span class="s5">front</span>==<span class="s5">rear</span>) <span class="s6">return</span> <span class="s1">ERROR</span>;</p>
<p class="p6">
<span class="s6">int</span> pop=<span class="s5">items</span>[<span class="s5">front</span>];</p>
<p class="p6">
<span class="s5">front</span>=(<span class="s5">front</span>+<span class="s4">1</span>)%<span class="s1">MAX_SIZE</span>;</p>
<p class="p6">
<span class="s5">full</span>=<span class="s6">false</span>;</p>
<p class="p6">
<span class="s6">return</span> pop;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">void</span> print()</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">full</span>==<span class="s6">false</span> && <span class="s5">rear</span>==<span class="s5">front</span>) <span class="s6">return</span>;</p>
<p class="p6">
<span class="s6">if</span>(<span class="s5">rear</span><=<span class="s5">front</span>)</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s7">"Queue: "</span>;</p>
<p class="p6">
<span class="s6">for</span>(<span class="s6">int</span> i=<span class="s5">front</span>;i<<span class="s5">rear</span>+<span class="s1">MAX_SIZE</span>;i++)</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s5">items</span>[i%<span class="s1">MAX_SIZE</span>] << <span class="s7">" "</span>;</p>
<p class="p6">
}</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">else</span> {</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s7">"Queue: "</span>;</p>
<p class="p6">
<span class="s6">for</span>(<span class="s6">int</span> i=<span class="s5">front</span>; i<<span class="s5">rear</span>; i++)</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s5">items</span>[i] << <span class="s7">" "</span>;</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s3">cout</span> << <span class="s3">endl</span>;</p>
<p class="p6">
};</p>
<p class="p4">
private<span class="s2">:</span></p>
<p class="p6">
<span class="s6">int</span> front,rear;</p>
<p class="p6">
<span class="s6">bool</span> full;</p>
<p class="p1">
<span class="s2"> </span>/* ±∏«ˆ«œººø‰ */</p>
<p class="p6">
};</p>
<p class="p2">
</p>
<p class="p1">
/* main «‘ºˆ∏¶ πŸ≤Ÿ¡ˆ ∏ª∞Õ! */</p>
<p class="p6">
<span class="s6">int</span> main()</p>
<p class="p6">
{</p>
<p class="p6">
<span class="s5">ADT</span> *adt[<span class="s4">2</span>];</p>
<p class="p6">
adt[<span class="s4">0</span>] = <span class="s6">new</span> <span class="s5">Queue</span>;</p>
<p class="p6">
adt[<span class="s4">1</span>] = <span class="s6">new</span> <span class="s5">Stack</span>;</p>
<p class="p2">
</p>
<p class="p6">
<span class="s6">int</span> input[] = {<span class="s4">3</span>,<span class="s4">5</span>,<span class="s4">4</span>,<span class="s4">8</span>,<span class="s4">6</span>,<span class="s4">1</span>,<span class="s4">3</span>,<span class="s4">9</span>,<span class="s4">7</span>,<span class="s4">2</span>,</p>
<p class="p6">
<span class="s4">3</span>,<span class="s4">2</span>,<span class="s4">8</span>,<span class="s4">5</span>,<span class="s4">2</span>,<span class="s4">7</span>,<span class="s4">6</span>,<span class="s4">4</span>,<span class="s4">1</span>,<span class="s4">3</span>};</p>
<p class="p2">
</p>
<p class="p1">
<span class="s2"> </span>/******* overflow/underflow test **********/</p>
<p class="p6">
<span class="s6">for</span> (<span class="s6">int</span> i = <span class="s4">0</span>; i < <span class="s4">11</span>; i++)</p>
<p class="p6">
{</p>
<p class="p6">
adt[<span class="s4">0</span>]-><span class="s5">push</span>(input[i]);</p>
<p class="p6">
adt[<span class="s4">1</span>]-><span class="s5">push</span>(input[i]);</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">for</span> (<span class="s6">int</span> i = <span class="s4">0</span>; i < <span class="s4">11</span>; i++)</p>
<p class="p6">
{</p>
<p class="p6">
adt[<span class="s4">0</span>]-><span class="s5">pop</span>();</p>
<p class="p6">
adt[<span class="s4">1</span>]-><span class="s5">pop</span>();</p>
<p class="p6">
}</p>
<p class="p1">
<span class="s2"> </span>/******* overflow/underflow test end ******/</p>
<p class="p2">
</p>
<p class="p6">
<span class="s6">for</span> (<span class="s6">int</span> i = <span class="s4">0</span>; i < <span class="s4">10</span>; i++)</p>
<p class="p6">
{</p>
<p class="p6">
adt[<span class="s4">0</span>]-><span class="s5">push</span>(input[i]);</p>
<p class="p6">
adt[<span class="s4">1</span>]-><span class="s5">push</span>(input[i]);</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">for</span> (<span class="s6">int</span> i = <span class="s4">0</span>; i < <span class="s4">5</span>; i++)</p>
<p class="p6">
{</p>
<p class="p6">
adt[<span class="s4">0</span>]-><span class="s5">pop</span>();</p>
<p class="p6">
adt[<span class="s4">1</span>]-><span class="s5">pop</span>();</p>
<p class="p6">
}</p>
<p class="p6">
<span class="s6">for</span> (<span class="s6">int</span> i = <span class="s4">10</span>; i < <span class="s4">20</span>; i++)</p>
<p class="p6">
{</p>
<p class="p6">
adt[<span class="s4">0</span>]-><span class="s5">push</span>(input[i]);</p>
<p class="p6">
adt[<span class="s4">1</span>]-><span class="s5">push</span>(input[i]);</p>
<p class="p6">
}</p>
<p class="p2">
</p>
<p class="p6">
adt[<span class="s4">0</span>]-><span class="s5">print</span>();</p>
<p class="p6">
adt[<span class="s4">1</span>]-><span class="s5">print</span>();</p>
<p class="p2">
</p>
<p class="p4">
return<span class="s2"> </span><span class="s4">0</span><span class="s2">;</span></p>
<p class="p2">
</p>
<p class="p6">
}</p>