亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? calc.html

?? C ++ in action
?? HTML
?? 第 1 頁 / 共 2 頁
字號:
<html>
<head>
    <title>Stack-based Calculator</title>
    <meta  name="description" content="Reverse Polish notation calculator in C++">
    <meta name="keywords" content="top-down, design, calculator, stack, member function, private">
    <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>Stack-based calculator</h3>

<p class=topics>
  Top down design, make, private member functions, if statement, do/while loop.
<p>
Our next step will be to design and implement a small but useful program using the techniques that we learned so far. It will be a simple stack based calculator. Even though it is a very small project, we will follow all the steps of the design and implementation process. We'll start with the functional specification, then proceed with the architectural design, and finally implement the program.

<h3>Functional Specification</h3>
<p>
The stack-based calculator has a simple interface. It accepts user input in the form of numbers and operators. Numbers are stored in the LIFO type memory--they are pushed on the stack. Binary operators are applied to the two top level numbers popped from the stack, and the result of the operation is pushed on top of the stack. If there's only one number in the stack, it is substituted for both operands. After every action the contents of the whole stack is displayed.
<p>
Since effectively the calculator implements reverse Polish notation, there is no need for parentheses. For simplicity we will implement only four basic arithmetic operations. A sample session may look like this (user input follows the prompt '&gt;'):
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>> 3
    3
> 2
    3
    2
> +
    5
> +
    10</pre>
</table>
<!-- End Code -->

<h3>Design</h3>
<p>
The obvious top-level object is the <i>calculator</i> itself. It stores numbers in its memory and performs operations on them. Since we would like to be able to display the contents of the calculator's stack after every action, we need access to a stack <i>sequencer</i>. Finally, in order to completely decouple input/output operations from the calculator, we should have an <i>input</i> object that obtains input from the user and does some pre-processing  (tokenization) to distinguish between numbers and operators.
<p>We'll use the standard <var>cout</var> object for output.
<p>
The minimum lifetimes of these three objects are the following: 
<ul>
<li>The calculator has to be alive during the whole session since it has long term memory.
<li>The scope of the input object is associated with a single input operation.
<li>The scope of the sequencer is associated with every stack display operation that follows a successful calculator action.
</ul>
<p>
The input object obtains a string from the standard input. It can distinguish between numbers and operators, returning different tokens depending on the input. If the token is 'number' the string is converted to an <var>int</var> and the input object remembers its value.
<p>
The calculator object accepts the input object, from which it can get pre-digested data, and executes the command. If the data is a number, it stores it in a stack; if it is an operator, it performs the operation. The results can be viewed by creating a stack sequencer--the calculator may simply give access to its stack for the purpose of iteration. 
<p>
Notice that we have only three types of objects interacting at the top level, plus one object, the stack, that we can treat as a black box (we don't call any of its methods, we just pass access to it from one component to another). This is not just a side effect of the simplicity of our project--we should always strive to have only a small number of top level objects. 
<p>
Once the top level is established, we can start our top-down descent. In our design we may go one level deeper into the <i>calculator</i> object. We already know that it has a stack. The stack object will thus be embedded in it. We will re-use the stack that we used in the previous paragraph.

<h3>Stubbed Implementation</h3>
<p>
The top-down design will be followed by the top-down implementation. Based on our architectural specification we start to write the <var>main</var> procedure.

<tr>
<td class=margin valign=top>
<a href="source/stubs.zip"><img src="../../images/brace.gif" width=16 height=16 border=1 alt="Download!"><br>source</a>
<td>


<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>void <span class=method>main</span> ()
{
    Calculator TheCalculator;
    bool status;
    do
    {
        // Prompt for input
        cout &lt;&lt; "&gt; ";
        Input input;
        status = TheCalculator.Execute (input);
        if (status)
        {
            for (StackSeq seq (TheCalculator.GetStack ());
                !seq.AtEnd ();
                seq.Advance () )
            {
                cout &lt;&lt; "    " &lt;&lt; seq.GetNum () &lt;&lt; endl;
            }
        }
    } while (status);
}</pre>
</table>
<!-- End Code -->

<p>
We have introduced some new constructs here, the <var>do</var>/<var>while</var> loop and the <var>if</var> statement. The execution of the body of the <i>do</i>/<i>while</i> loop is repeated as long as the condition in the <i>while</i> clause remains true. Notice that, unlike in the case of the <i>for</i> loop, the body of the <i>do</i>/<i>while</i> loop is always executed at least once. As should be obvious by now, the body of the loop forms a separate local scope (even if it is a single statement and the braces are omitted).
<p>
The body of the <i>if</i> statement is entered only if the condition in the <i>if</i> clause is true (different from zero). Otherwise it is skipped altogether. And again, the body of the <i>if</i> statement forms a local scope even if it is only one statement, in which case the braces may be omitted.
<p>
Notice also that the variable <var>status</var> is defined without being initialized. We try to avoid such situations in C++. Here I took the liberty of not initializing it, since it is always initialized inside the body of the <i>do</i>/<i>while</i> loop (and we know that it is executed at least once). I couldn't define the variable <var>status</var> inside the scope of the loop, because it is tested in the <i>while</i> clause which belongs to the outer scope. The <i>while</i> clause is evaluated during each iteration, after the body of the loop is executed.
<p>
Next, following the top-down approach, we'll write stub implementations for all classes. The stack is passed from the calculator to the sequencer and we don't need to know anything about it (other than that it's an object of class <var>IStack</var>). Hence the trivial implementation:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
	<td class=codeTable>
<pre>class IStack {};</pre>
</table>
<!-- End Code -->

<p>
The sequencer has all the methods stubbed out. I have added to it a dummy variable, <var>_done</var>, to simulate the finiteness of the stack. <var>GetNum</var> returns the arbitrarily chosen number 13.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>class <span class=method>StackSeq</span>
{
public:
    StackSeq (IStack const & stack) : _stack (stack), _done (false)
    {
        cout &lt;&lt; "Stack sequencer created\n";
    }
    bool AtEnd () const { return _done; }
    void Advance () { _done = true; }
    int GetNum () const { return 13; }
private:
    IStack const &  _stack;
    bool            _done;
};</pre>
</table>
<!-- End Code -->

<p>
At this level of detail, the class <var>Input</var> exposes only its constructor:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>class <span class=method>Input</span>
{
public:
    Input ()
    {
        cout &lt;&lt; "Input created\n";
    }
};</pre>
</table>
<!-- End Code -->

<p>
The calculator, again, has a dummy variable whose purpose is to break out of the loop in main after just one iteration.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>class <span class=method>Calculator</span>
{
public:
    Calculator () : _done (false)
    {
        cout &lt;&lt; "Calculator created\n";
    }
    bool Execute (Input& input)
    {
        cout &lt;&lt; "Calculator::Execute\n";
        return !_done;
    }
    IStack const & GetStack () const
    {
        _done = true;
        return _stack;
    }
private:
    IStack  _stack;
    bool    _done;
};</pre>
</table>
<!-- End Code -->

<p>
The method <var>GetStack</var> returns a <var>const</var> reference to <var>IStack</var>. In other words it makes a read-only alias for the calculator's private object <var>_stack</var> and makes it available to the caller. The user may use this alias to access <var>_stack</var>, but only through its <var>const</var> methods, or, if it is an <var>IStack</var>'s friend, by reading the values of <var>_top</var> and those stored in the array <var>_arr</var>. This is exactly what the sequencer needs. Notice also that the statement <var>return _stack</var> is interpreted by the compiler to return a <i>reference</i> to <var>_stack</var>. This is because <var>GetStack</var> was declared as returning a reference. If <var>GetStack</var> were declared as
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>IStack const GetStack () const;</pre>
</table>
<!-- End Code -->

the compiler would return a read-only <i>copy</i> of the stack. Copying the stack is somehow more expensive than providing a reference to it. We'll come back to this problem later, when we talk about value classes.
<p>
With all the dummies in place, we can compile and execute the test program. Its output shows that everything works as expected.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>Calculator created
> Input created
Calculator::Execute
Stack sequencer created
    13
> Input created
Calculator::Execute</pre>
</table>
<!-- End Code -->

<h3>Implementation</h3>
<h4>Calculator: Implementation</h4>
<p>
Now is the time to start the top to bottom descent. The first candidate for implementation is the <var>Calculator</var> itself. When implementing the calculator we'll find out what we need from the <var>Input</var> object. Let's start with the <var>Execute</var> method. First, it should retrieve the token from the input. We may expect the following tokens: the number token, any of the arithmetic operator tokens, or the <i>error</i> token. For each type we do a different thing. For the <i>number</i> token we retrieve the value of the number from the input and push it on the stack. For the <i>operator</i>, we pop two numbers (or one if there aren't two), pass them to the <var>Calculate</var> method and push the result.

<tr>
<td class=margin valign=top>

<br>
<a href="source/calc.zip">
<img src="../../images/brace.gif" width=16 height=16 border=1 alt="Download!"><br>source</a>
<td>


<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>bool <span class=method>Calculator::Execute</span> (Input const & input)
{
    int token = input.Token ();
    bool status = false; // assume failure

    if (token == tokError)
    {
        cout &lt;&lt; "Unknown token\n";
    }
    else if (token == tokNumber)
    {
        if (_stack.IsFull ())
        {
            cout &lt;&lt; "Stack is full\n";
        }
        else
        {
            _stack.Push (input.Number ());
            status = true; // success
        }
    }
    else
    {
        assert (token == '+' || token == '-' 
              || token == '*' || token == '/');

        if (_stack.IsEmpty ())
        {
            cout &lt;&lt; "Stack is empty\n";
        }
        else
        {        

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲欧美视频一区| 欧美一区二区精品久久911| ...av二区三区久久精品| 成人三级伦理片| 国产精品乱人伦| 色欲综合视频天天天| 亚洲一区二区三区四区不卡| 欧美三级电影网| 久久精品国产在热久久| 久久久精品国产免费观看同学| 国产电影一区二区三区| 国产精品丝袜91| 日本电影亚洲天堂一区| 婷婷中文字幕综合| 精品sm捆绑视频| av电影在线观看不卡 | 精品女同一区二区| 国产99久久精品| 一区二区三区在线观看国产| 在线电影一区二区三区| 国产一区二区电影| 亚洲欧洲av另类| 在线播放/欧美激情| 国产一区二区精品久久99| 亚洲欧洲精品一区二区精品久久久 | 色又黄又爽网站www久久| 亚洲成人av在线电影| 精品裸体舞一区二区三区| 大美女一区二区三区| 一区二区三区小说| 精品久久人人做人人爰| 99久久夜色精品国产网站| 午夜精品国产更新| 久久夜色精品一区| 色婷婷亚洲一区二区三区| 蜜桃视频在线观看一区| 国产精品嫩草影院av蜜臀| 欧美日韩成人在线一区| 国产精品一区二区你懂的| 亚洲一区二区三区美女| 久久亚洲一级片| 91成人在线观看喷潮| 国内精品免费在线观看| 伊人一区二区三区| 2022国产精品视频| 在线亚洲人成电影网站色www| 蜜桃视频在线观看一区| 亚洲精品视频在线观看免费| 精品精品国产高清a毛片牛牛 | 成人黄色a**站在线观看| 午夜精品久久久| 亚洲同性gay激情无套| 日韩欧美国产一区二区三区| 91麻豆免费看片| 国产一区二区三区免费看| 亚洲尤物视频在线| 欧美国产97人人爽人人喊| 91精品国产综合久久久久久久| 成人精品鲁一区一区二区| 免费看日韩精品| 亚洲欧美色图小说| 久久久久久久久久久电影| 欧美日韩大陆在线| 91丨国产丨九色丨pron| 国产一区二区导航在线播放| 日韩va亚洲va欧美va久久| 亚洲乱码中文字幕| 国产亚洲一区二区三区在线观看 | 在线看日本不卡| 国产成人亚洲精品青草天美| 日本aⅴ精品一区二区三区| 亚洲欧美激情小说另类| 国产视频视频一区| 日韩网站在线看片你懂的| 欧洲国内综合视频| 972aa.com艺术欧美| 狠狠色丁香久久婷婷综| 日韩国产精品久久久| 亚洲自拍都市欧美小说| 最新日韩av在线| 亚洲国产精品成人久久综合一区| 欧美一级黄色大片| 欧美喷潮久久久xxxxx| 色网综合在线观看| 99久久99久久免费精品蜜臀| 国产成人免费av在线| 狠狠狠色丁香婷婷综合激情| 青青青伊人色综合久久| 午夜视频一区二区| 亚洲一区二区三区自拍| 18成人在线观看| 欧美极品美女视频| 久久精品人人做人人爽97| 久久人人爽人人爽| 26uuu精品一区二区| 日韩你懂的在线观看| 欧美一级久久久| 欧美成人综合网站| 日韩欧美国产综合| 精品久久一区二区三区| 亚洲精品在线电影| 精品久久国产字幕高潮| 精品国产制服丝袜高跟| 欧美xxxxx牲另类人与| 日韩免费一区二区三区在线播放| 欧美欧美午夜aⅴ在线观看| 欧美精选一区二区| 欧美乱妇20p| 91精品国产综合久久香蕉麻豆| 欧美久久一二区| 91麻豆精品91久久久久久清纯| 欧美高清激情brazzers| 91麻豆精品国产自产在线| 欧美高清视频在线高清观看mv色露露十八 | 日本怡春院一区二区| 天天做天天摸天天爽国产一区| 亚洲成a人v欧美综合天堂下载| 亚洲地区一二三色| 石原莉奈一区二区三区在线观看| 亚洲电影欧美电影有声小说| 日日欢夜夜爽一区| 秋霞影院一区二区| 经典三级视频一区| 国产伦精品一区二区三区免费迷 | 69久久99精品久久久久婷婷 | 亚洲欧美自拍偷拍色图| 成人免费在线观看入口| 一区二区三区在线视频免费 | 五月综合激情日本mⅴ| 日本亚洲免费观看| 青青草国产精品97视觉盛宴| 久久精品国产一区二区三| 国产成人鲁色资源国产91色综| 不卡在线观看av| 日本韩国精品一区二区在线观看| 欧美性色综合网| 欧美一二三区精品| 久久久蜜臀国产一区二区| 国产精品沙发午睡系列990531| 中文字幕欧美一区| 亚洲国产成人高清精品| 美日韩黄色大片| 国产成a人亚洲精| 日本乱码高清不卡字幕| 欧美妇女性影城| 久久青草欧美一区二区三区| 国产精品污www在线观看| 亚洲精品一二三| 蜜臀av在线播放一区二区三区 | 日本久久电影网| 777午夜精品免费视频| 久久久精品欧美丰满| 亚洲日本中文字幕区| 日韩一区欧美二区| 国产综合色在线视频区| 色综合天天狠狠| 欧美成人福利视频| 日韩毛片在线免费观看| 日本中文字幕一区二区视频 | 一区二区三区国产| 麻豆国产精品视频| 99精品在线观看视频| 91精品国产综合久久久久久久久久 | 精品国精品国产| 亚洲精品视频在线观看网站| 麻豆国产91在线播放| 91美女蜜桃在线| 精品国内片67194| 一区二区三区免费网站| 久久99热这里只有精品| 91网站最新地址| 日韩精品中文字幕一区| 亚洲色图欧洲色图婷婷| 久久99精品久久久久久| 91麻豆精东视频| 26uuu欧美| 五月婷婷综合激情| 成人激情开心网| 欧美一区二视频| 久久午夜国产精品| 欧美大片一区二区三区| 中文字幕一区二区三区色视频| 婷婷综合另类小说色区| 成人精品国产免费网站| 日韩一区二区三区在线| 亚洲手机成人高清视频| 麻豆精品视频在线观看视频| 色老综合老女人久久久| 26uuu成人网一区二区三区| 午夜精品免费在线| 99久久99久久精品免费看蜜桃| 日韩精品一区二区三区swag| 一区二区三区欧美视频| 懂色av中文字幕一区二区三区| 欧美一区二区性放荡片| 亚洲精品国产一区二区精华液| 国产一区二区免费视频| 91精品久久久久久蜜臀| 亚洲黄色性网站| 成人高清视频在线|