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

📄 11main.html

📁 C ++ in action
💻 HTML
字号:
<html>
<head>
	<title>Main</title>
    <meta  name="description" content="Incremental improvement in main">
    <meta name="keywords" content="main, parser, arithmetic">
	<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>Main</h3>

<p>The main function drives the whole thing. Its overall structure hasn抰 changed much since our last iteration--except for the addition of the function table, the symbol table and the store.


<!-- Code --><table width="100%" cellspacing=10><tr>	<td class=codetable>

<pre>const int maxBuf = 100;
const int maxSymbols = 40;

int main ()
{
    // Notice all these local objects.
    // A clear sign that there should be
    // a top level object, say, the Calculator.
    // Back to the drawing board!

    char buf [maxBuf];
    Status status;
    SymbolTable symTab (maxSymbols);
    FunctionTable funTab (symTab, funArr);
    Store store (maxSymbols, symTab);

    do
    {
        cout &lt;&lt; "&gt; ";  // prompt
        cin.getline (buf, maxBuf);
        Scanner scanner (buf);
        Parser  parser (scanner, store, funTab, symTab);
        status = parser.Eval ();
    } while (status != stQuit);
}

</pre></table><!-- End Code --><p>Notice that for every line of input we create a new scanner and a new parser. We keep however the same symbol table, function table and the store. This is important because we want the values assigned to variables to be remembered as long as the program is active. The parser抯 destructor is called after the evaluation of every line. This call plays an important role of freeing the parse tree.

<p>There is a comment at the top of <var>main</var>, which hints at ways of improving the structure of the program. There are five local variables/objects defined at the top of <var>main</var>. Moreover, they depend on each other: the symbol table has to be initialized before the function table and before the store. The parser抯 constructor takes references to three of them. As a rule of thumb, whenever you see too many local variables, you should think hard how to combine at least some of them into a separate object. In our case, it's pretty obvious that this object sould be called <var>Calculator</var>. It should combine <var>SymbolTable</var>, <var>FunctionTable</var> and <var>Store</var> as its embeddings.

<p>We抣l come back to this program in the next part of the book to see how it can be made into a professional, &quot;industrial strength&quot; program. 

<br><a href="12aggreg.html">Next.</a>

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

⌨️ 快捷键说明

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