﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-cloned</title><link>http://www.cppblog.com/cloned/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 17 Apr 2026 03:21:37 GMT</lastBuildDate><pubDate>Fri, 17 Apr 2026 03:21:37 GMT</pubDate><ttl>60</ttl><item><title>一道笔试题——输入二叉树的前序和中序遍历序列，输出后序遍历序列</title><link>http://www.cppblog.com/cloned/archive/2010/12/02/135289.html</link><dc:creator>Cloned</dc:creator><author>Cloned</author><pubDate>Thu, 02 Dec 2010 10:49:00 GMT</pubDate><guid>http://www.cppblog.com/cloned/archive/2010/12/02/135289.html</guid><wfw:comment>http://www.cppblog.com/cloned/comments/135289.html</wfw:comment><comments>http://www.cppblog.com/cloned/archive/2010/12/02/135289.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cloned/comments/commentRss/135289.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cloned/services/trackbacks/135289.html</trackback:ping><description><![CDATA[


<div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">算法：</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">1 &nbsp;如果输入序列长度小于等于0，结束</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">2 &nbsp;输入的前序序列的第一个节点是后序遍历的最后一个节点，将这个节点保存到output变量中</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">3 &nbsp;把输入的两个序列拆成4部分，output节点的左子树的前序和中序序列，output节点右子树的前序和中序序列</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">4 &nbsp;用3中的结果先左后右递归调用该算法</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">5 &nbsp;输出output</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><br></div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; ">代码如下</div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><span style="font-family: monospace; font-size: 13px; white-space: pre; "><span style="color: rgb(0, 128, 128); "> 1</span> <span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> LastTree::ShowLast(</span><span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">po, </span><span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">mo, </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> n)
</span><span style="color: rgb(0, 128, 128); "> 2</span> <span style="color: rgb(0, 0, 0); ">{
</span><span style="color: rgb(0, 128, 128); "> 3</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">递归结束条件</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); "> 4</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(n </span><span style="color: rgb(0, 0, 0); ">&lt;=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">)
</span><span style="color: rgb(0, 128, 128); "> 5</span> <span style="color: rgb(0, 0, 0); ">    {
</span><span style="color: rgb(0, 128, 128); "> 6</span> <span style="color: rgb(0, 0, 0); ">        </span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); "> 7</span> <span style="color: rgb(0, 0, 0); ">    }
</span><span style="color: rgb(0, 128, 128); "> 8</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); "> 9</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">输出值</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">10</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> output </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">po;
</span><span style="color: rgb(0, 128, 128); ">11</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">12</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">output节点的左子树中序序列</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">13</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> i </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">14</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">lmct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> (</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">)malloc(n</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 255); ">sizeof</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">));
</span><span style="color: rgb(0, 128, 128); ">15</span> <span style="color: rgb(0, 0, 0); ">    memset(lmct, </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">, n);
</span><span style="color: rgb(0, 128, 128); ">16</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(mo </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> i) </span><span style="color: rgb(0, 0, 0); ">!=</span><span style="color: rgb(0, 0, 0); "> output)
</span><span style="color: rgb(0, 128, 128); ">17</span> <span style="color: rgb(0, 0, 0); ">        </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(lmct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> i) </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(mo </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">);
</span><span style="color: rgb(0, 128, 128); ">18</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> lnum </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> i;
</span><span style="color: rgb(0, 128, 128); ">19</span> <span style="color: rgb(0, 0, 0); ">    i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">20</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">21</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">output节点的右子树中序序列</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">22</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> j </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">23</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">rmct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> (</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">)malloc(n</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 255); ">sizeof</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">));
</span><span style="color: rgb(0, 128, 128); ">24</span> <span style="color: rgb(0, 0, 0); ">    memset(rmct, </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">, n);
</span><span style="color: rgb(0, 128, 128); ">25</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(mo </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> i) </span><span style="color: rgb(0, 0, 0); ">!=</span><span style="color: rgb(0, 0, 0); "> NULL)
</span><span style="color: rgb(0, 128, 128); ">26</span> <span style="color: rgb(0, 0, 0); ">    {
</span><span style="color: rgb(0, 128, 128); ">27</span> <span style="color: rgb(0, 0, 0); ">        </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(rmct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> j</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">) </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(mo </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> i</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">);
</span><span style="color: rgb(0, 128, 128); ">28</span> <span style="color: rgb(0, 0, 0); ">    }
</span><span style="color: rgb(0, 128, 128); ">29</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">30</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">output节点的左子树前序序列</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">31</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">lpct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> (</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">)malloc(n</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 255); ">sizeof</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">));
</span><span style="color: rgb(0, 128, 128); ">32</span> <span style="color: rgb(0, 0, 0); ">    memset(lpct, </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">, n);
</span><span style="color: rgb(0, 128, 128); ">33</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> k </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">34</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">35</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> a </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">; </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(po </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> a); a</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)
</span><span style="color: rgb(0, 128, 128); ">36</span> <span style="color: rgb(0, 0, 0); ">    {
</span><span style="color: rgb(0, 128, 128); ">37</span> <span style="color: rgb(0, 0, 0); ">        </span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> b </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">; </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(lmct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> b); b</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)
</span><span style="color: rgb(0, 128, 128); ">38</span> <span style="color: rgb(0, 0, 0); ">        {
</span><span style="color: rgb(0, 128, 128); ">39</span> <span style="color: rgb(0, 0, 0); ">            </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(po </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> a) </span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(lmct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> b))
</span><span style="color: rgb(0, 128, 128); ">40</span> <span style="color: rgb(0, 0, 0); ">            {
</span><span style="color: rgb(0, 128, 128); ">41</span> <span style="color: rgb(0, 0, 0); ">                </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(lpct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> k</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">) </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(po </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> a);
</span><span style="color: rgb(0, 128, 128); ">42</span> <span style="color: rgb(0, 0, 0); ">                </span><span style="color: rgb(0, 0, 255); ">break</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">43</span> <span style="color: rgb(0, 0, 0); ">            }
</span><span style="color: rgb(0, 128, 128); ">44</span> <span style="color: rgb(0, 0, 0); ">            
</span><span style="color: rgb(0, 128, 128); ">45</span> <span style="color: rgb(0, 0, 0); ">        }
</span><span style="color: rgb(0, 128, 128); ">46</span> <span style="color: rgb(0, 0, 0); ">    }
</span><span style="color: rgb(0, 128, 128); ">47</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">48</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">output节点的右子树前序序列</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">49</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">rpct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> (</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">)malloc(n</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 255); ">sizeof</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">));
</span><span style="color: rgb(0, 128, 128); ">50</span> <span style="color: rgb(0, 0, 0); ">    memset(rpct, </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">, n);
</span><span style="color: rgb(0, 128, 128); ">51</span> <span style="color: rgb(0, 0, 0); ">    k </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">52</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">53</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> a </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">; </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(po </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> a); a</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)
</span><span style="color: rgb(0, 128, 128); ">54</span> <span style="color: rgb(0, 0, 0); ">    {
</span><span style="color: rgb(0, 128, 128); ">55</span> <span style="color: rgb(0, 0, 0); ">        </span><span style="color: rgb(0, 0, 255); ">for</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> b </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">; </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(rmct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> b); b</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">)
</span><span style="color: rgb(0, 128, 128); ">56</span> <span style="color: rgb(0, 0, 0); ">        {
</span><span style="color: rgb(0, 128, 128); ">57</span> <span style="color: rgb(0, 0, 0); ">            </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(po </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> a) </span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(rmct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> b))
</span><span style="color: rgb(0, 128, 128); ">58</span> <span style="color: rgb(0, 0, 0); ">            {
</span><span style="color: rgb(0, 128, 128); ">59</span> <span style="color: rgb(0, 0, 0); ">                </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(rpct </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> k</span><span style="color: rgb(0, 0, 0); ">++</span><span style="color: rgb(0, 0, 0); ">) </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">(po </span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); "> a);
</span><span style="color: rgb(0, 128, 128); ">60</span> <span style="color: rgb(0, 0, 0); ">                </span><span style="color: rgb(0, 0, 255); ">break</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">61</span> <span style="color: rgb(0, 0, 0); ">            }
</span><span style="color: rgb(0, 128, 128); ">62</span> <span style="color: rgb(0, 0, 0); ">            
</span><span style="color: rgb(0, 128, 128); ">63</span> <span style="color: rgb(0, 0, 0); ">        }
</span><span style="color: rgb(0, 128, 128); ">64</span> <span style="color: rgb(0, 0, 0); ">    }
</span><span style="color: rgb(0, 128, 128); ">65</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">66</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">先左后右递归</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">67</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    ShowLast(lpct, lmct, lnum);
</span><span style="color: rgb(0, 128, 128); ">68</span> <span style="color: rgb(0, 0, 0); ">    ShowLast(rpct, rmct, n </span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); "> lnum </span><span style="color: rgb(0, 0, 0); ">-</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">);
</span><span style="color: rgb(0, 128, 128); ">69</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">70</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">输出当前节点</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); ">71</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">    std::cout</span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); ">output;
</span><span style="color: rgb(0, 128, 128); ">72</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">73</span> <span style="color: rgb(0, 0, 0); ">    free(lmct);
</span><span style="color: rgb(0, 128, 128); ">74</span> <span style="color: rgb(0, 0, 0); ">    free(rmct);
</span><span style="color: rgb(0, 128, 128); ">75</span> <span style="color: rgb(0, 0, 0); ">    free(lpct);
</span><span style="color: rgb(0, 128, 128); ">76</span> <span style="color: rgb(0, 0, 0); ">    free(rpct);
</span><span style="color: rgb(0, 128, 128); ">77</span> <span style="color: rgb(0, 0, 0); ">    lpct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> NULL;
</span><span style="color: rgb(0, 128, 128); ">78</span> <span style="color: rgb(0, 0, 0); ">    rpct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> NULL;
</span><span style="color: rgb(0, 128, 128); ">79</span> <span style="color: rgb(0, 0, 0); ">    lmct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> NULL;
</span><span style="color: rgb(0, 128, 128); ">80</span> <span style="color: rgb(0, 0, 0); ">    rmct </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> NULL;
</span><span style="color: rgb(0, 128, 128); ">81</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); ">; 
</span><span style="color: rgb(0, 128, 128); ">82</span> <span style="color: rgb(0, 0, 0); ">}</span></span></div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><span style="font-family: monospace; font-size: 13px; white-space: pre; "><span style="color: rgb(0, 0, 0); "><br></span></span></div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><font face="monospace" size="3"><span style="font-size: 13px; white-space: pre;">测试用例：</span></font></div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><font face="monospace" size="3"><span style="font-size: 13px; white-space: pre;"><br></span></font></div><div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal verdana; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><font face="monospace" size="3"><span style="font-size: 13px; white-space: pre;"><table id="Table1" class="tb" cellspacing="0" cellpadding="3" border="0" style="font-size: 13px; font-family: Simsun; white-space: normal; "><tbody><tr><td><pre><div><span style="color: rgb(0, 128, 128); "> 1</span> <span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> main()
</span><span style="color: rgb(0, 128, 128); "> 2</span> <span style="color: rgb(0, 0, 0); ">{
</span><span style="color: rgb(0, 128, 128); "> 3</span> <span style="color: rgb(0, 0, 0); ">    LastTree lt;
</span><span style="color: rgb(0, 128, 128); "> 4</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">po </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">ABCDEFG</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); "> 5</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">mo </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">CBDAEGF</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); "> 6</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); "> 7</span> <span style="color: rgb(0, 0, 0); ">    lt.ShowLast(po, mo, </span><span style="color: rgb(0, 0, 0); ">7</span><span style="color: rgb(0, 0, 0); ">);
</span><span style="color: rgb(0, 128, 128); "> 8</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); "> 9</span> <span style="color: rgb(0, 0, 0); ">    _getch();
</span><span style="color: rgb(0, 128, 128); ">10</span> <span style="color: rgb(0, 0, 0); ">}</span></div></pre></td></tr></tbody></table>输出结果为：CDBGFEA</span></font></div>
<img src ="http://www.cppblog.com/cloned/aggbug/135289.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cloned/" target="_blank">Cloned</a> 2010-12-02 18:49 <a href="http://www.cppblog.com/cloned/archive/2010/12/02/135289.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MFC中自定义类访问主对话框控件</title><link>http://www.cppblog.com/cloned/archive/2010/08/17/123729.html</link><dc:creator>Cloned</dc:creator><author>Cloned</author><pubDate>Tue, 17 Aug 2010 06:52:00 GMT</pubDate><guid>http://www.cppblog.com/cloned/archive/2010/08/17/123729.html</guid><wfw:comment>http://www.cppblog.com/cloned/comments/123729.html</wfw:comment><comments>http://www.cppblog.com/cloned/archive/2010/08/17/123729.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cloned/comments/commentRss/123729.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cloned/services/trackbacks/123729.html</trackback:ping><description><![CDATA[<p>在使用mfc的时候经常遇到自定义类访问主对话框控件的问题，例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件、控制对话框的进度条等。<br><br>要在自定义的类中访问主对话框空间，须获得主对话的指针。主对话框的对象在app类的InitInstance()方法中声明。查看app类的cpp文件，在InitInstance()方法中有如下语句：<br><br>&nbsp;&nbsp;&nbsp; CAppDlg dlg;<br>&nbsp;&nbsp;&nbsp; m_pMainWnd = &amp;dlg;<br><br>主对话框指针被传给m_pMainWnd 成员，按f12查看其定义，发现m_pMainWnd 是在afxwin.h中声明的成员：<br><br>&nbsp;&nbsp;&nbsp; CWnd* m_pMainWnd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // main window (usually same AfxGetApp()-&gt;m_pMainWnd)<br><br>由于MFC中的自定义类会自动包含stdafx.h头文件，且stdafx.h包含afxwin.h，因此根据注释，在自定义类中可以直接使用<br><br>&nbsp;&nbsp;&nbsp; AfxGetApp()-&gt;m_pMainWnd<br><br>来获取主对话框的指针。之后可通过<br><br>&nbsp;&nbsp;&nbsp; AfxGetApp()-&gt;m_pMainWnd-&gt;GetDlgItem(&nbsp; )；<br><br>来获得要访问的主对话框控件的指针，以对其进行访问。<br></p>
<img src ="http://www.cppblog.com/cloned/aggbug/123729.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cloned/" target="_blank">Cloned</a> 2010-08-17 14:52 <a href="http://www.cppblog.com/cloned/archive/2010/08/17/123729.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>