﻿<?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++博客-&lt;H1&gt;&lt;font color=orange&gt;JonsenElizee&lt;/font&gt;&lt;/H1&gt;</title><link>http://www.cppblog.com/JonsenElizee/</link><description>Software Developing Blog
&lt;BR&gt;
&lt;BR&gt;
"An idea is fragile . It can be killed by a scornful smile or a yawn .It can be mound down by irony and scared to death by a cold look." 
&lt;BR&gt; 
"Most cultures throughout human history have not liked creative individuals .They ignore them or kill them.It is a very efficient way of stopping creativity."  
&lt;BR&gt; 
&lt;BR&gt;
------Advertising boss Charles Browe and Howard Gardner ,professor at Harvard </description><language>zh-cn</language><lastBuildDate>Wed, 08 Apr 2026 23:27:35 GMT</lastBuildDate><pubDate>Wed, 08 Apr 2026 23:27:35 GMT</pubDate><ttl>60</ttl><item><title>Is Natural Number N Powered </title><link>http://www.cppblog.com/JonsenElizee/archive/2010/12/07/135665.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Tue, 07 Dec 2010 02:50:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/12/07/135665.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/135665.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/12/07/135665.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/135665.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/135665.html</trackback:ping><description><![CDATA[make judge on a natural number: if it's equals to pow(x, 2) and x is a natural number too.<br><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;isPowered2(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;low,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;hig,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;dvd)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;mid&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(low</span><span style="color: #000000;">+</span><span style="color: #000000;">hig)</span><span style="color: #000000;">/</span><span style="color: #000000;">dvd;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pwr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mid</span><span style="color: #000000;">*</span><span style="color: #000000;">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pwr&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;n)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(low&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;hig)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pwr&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;n)&nbsp;{&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;target&nbsp;number&nbsp;is&nbsp;in&nbsp;[low,&nbsp;mid-1]</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;isPowered2(n,&nbsp;low,&nbsp;mid</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;dvd);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pwr&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;n)&nbsp;{&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;target&nbsp;number&nbsp;is&nbsp;in&nbsp;[mid+1,&nbsp;hig]</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;isPowered2(n,&nbsp;mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;hig,&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;use&nbsp;binary&nbsp;searching&nbsp;here.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br></span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;isPowered(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">100</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">25</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">16</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">25</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">36</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">49</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">64</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">81</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">||</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">100</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;<span style="color: red;">isPowered2(n,&nbsp;</span></span><span style="color: red;">1,&nbsp;n-1,&nbsp;10);&nbsp;//&nbsp;why&nbsp;use&nbsp;10&nbsp;here&nbsp;?</span><span style="color: #008000;"><br></span><span style="color: #000000;">}</span></div>
<br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/135665.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-12-07 10:50 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/12/07/135665.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IPC waitpid</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/12/02/135274.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 02 Dec 2010 07:28:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/12/02/135274.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/135274.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/12/02/135274.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/135274.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/135274.html</trackback:ping><description><![CDATA[<br style="font-family: 新宋体;">
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; font-family: 新宋体;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">sys</span><span style="color: #000000;">/</span><span style="color: #000000;">types.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">sys</span><span style="color: #000000;">/</span><span style="color: #000000;">wait.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br>pid_t&nbsp;waitpid(pid_t&nbsp;pid,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;status,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;options);<br><br>pid&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;wait&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;any&nbsp;process&nbsp;with&nbsp;group&nbsp;id&nbsp;value&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">pid;<br>pid&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;same&nbsp;</span><span style="color: #0000ff;">as</span><span style="color: #000000;">&nbsp;wait();<br>pid&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;wait&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;any&nbsp;subprocess&nbsp;of&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">&nbsp;process;<br>pid&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;wait&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;process&nbsp;with&nbsp;it</span><span style="color: #000000;">'</span><span style="color: #000000;">s&nbsp;id&nbsp;=&nbsp;pid;</span><span style="color: #000000;"><br></span><span style="color: #000000;"><br>options&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;don</span><span style="color: #000000;">'</span><span style="color: #000000;">t&nbsp;use&nbsp;options;</span><span style="color: #000000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;WNOHANG&nbsp;&nbsp;:&nbsp;just&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;no&nbsp;subprocess&nbsp;finished.<br>&nbsp;&nbsp;&nbsp;&nbsp;WUNTRACED:&nbsp;just&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;subprocess&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;hanged&nbsp;up&nbsp;now.<br><br>status&nbsp;the&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;status&nbsp;of&nbsp;subprocess&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;stored&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;value.<br>&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;make&nbsp;sure&nbsp;what&nbsp;happed,&nbsp;we&nbsp;can&nbsp;use&nbsp;following&nbsp;macro:<br>&nbsp;&nbsp;&nbsp;&nbsp;WIFEXITED(status)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果子进程正常结束则为非0值。<br>&nbsp;&nbsp;&nbsp;&nbsp;WEXITSTATUS(status)&nbsp;&nbsp;&nbsp;&nbsp;取得子进程exit()返回的结束代码,一般会先用WIFEXITED来判断是否正常结束才能使用此宏。<br>&nbsp;&nbsp;&nbsp;&nbsp;WIFSIGNALED(status)&nbsp;&nbsp;&nbsp;&nbsp;如果子进程是因为信号而结束则此宏值为真<br>&nbsp;&nbsp;&nbsp;&nbsp;WTERMSIG(status)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED来判断后才使用此宏。<br>&nbsp;&nbsp;&nbsp;&nbsp;WIFSTOPPED(status)&nbsp;&nbsp;&nbsp;&nbsp; 如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED时才会有此情况。<br>&nbsp;&nbsp;&nbsp;&nbsp;WSTOPSIG(status)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED来判断后才使用此宏。<br><br></span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;pid&nbsp;the&nbsp;subprocess&nbsp;id,&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;error,&nbsp;and&nbsp;errno&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">.</span></div>
<br style="font-family: 新宋体;"><br style="font-family: 新宋体;"><br style="font-family: 新宋体;"><img src ="http://www.cppblog.com/JonsenElizee/aggbug/135274.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-12-02 15:28 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/12/02/135274.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>B+Tree</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135145.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 01 Dec 2010 03:48:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135145.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/135145.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135145.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/135145.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/135145.html</trackback:ping><description><![CDATA[<h1 id="firstHeading" class="firstHeading">B+ tree</h1>
<div id="siteSub">From Wikipedia, the free encyclopedia</div>
<div id="jump-to-nav">
Jump to: <a href="http://en.wikipedia.org/wiki/B%2B_tree#mw-head">navigation</a>,
<a href="http://en.wikipedia.org/wiki/B%2B_tree#p-search">search</a>
</div>
<div class="thumb tright">
<div class="thumbinner" style="width: 402px;"><a href="http://en.wikipedia.org/wiki/File:Bplustree.png" class="image"><img alt="" src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Bplustree.png/400px-Bplustree.png" class="thumbimage" width="400" height="184"></a>
<div class="thumbcaption">
<div class="magnify"><a href="http://en.wikipedia.org/wiki/File:Bplustree.png" class="internal" title="Enlarge"><img src="http://bits.wikimedia.org/skins-1.5/common/images/magnify-clip.png" alt="" width="15" height="11"></a></div>
A simple B+ tree example linking the keys 1&#8211;7 to data values d<sub>1</sub>-d<sub>7</sub>. The linked list (red) allows rapid in-order traversal.</div>
</div>
</div>
<p>In <a href="http://en.wikipedia.org/wiki/Computer_science" title="Computer science">computer science</a>, a <strong>B+ tree</strong> or <strong>B plus tree</strong> is a type of <a href="http://en.wikipedia.org/wiki/Tree_data_structure" title="Tree data structure" class="mw-redirect">tree</a>
which represents sorted data in a way that allows for efficient
insertion, retrieval and removal of records, each of which is identified
by a <em>key</em>. It is a dynamic, multilevel index, with maximum and
minimum bounds on the number of keys in each index segment (usually
called a "<a href="http://en.wikipedia.org/wiki/Block" title="Block">block</a>" or "<a href="http://en.wikipedia.org/wiki/Vertex_%28graph_theory%29" title="Vertex (graph theory)">node</a>"). In a B+ tree, in contrast to a <a href="http://en.wikipedia.org/wiki/B-tree" title="B-tree">B-tree</a>, all records are stored at the <a href="http://en.wikipedia.org/wiki/Leaf_node" title="Leaf node">leaf</a> level of the tree; only keys are stored in interior nodes.</p>
<p>The primary value of a B+ tree is in storing data for efficient retrieval in a <a href="http://en.wikipedia.org/wiki/Block_%28data_storage%29" title="Block (data storage)">block-oriented</a> storage context—in particular, <a href="http://en.wikipedia.org/wiki/Filesystems" title="Filesystems" class="mw-redirect">filesystems</a>. This is primarily because unlike <a href="http://en.wikipedia.org/wiki/Binary_search_tree" title="Binary search tree">binary search trees</a>,
B+ trees have very high fanout (typically on the order of 100 or more),
which reduces the number of I/O operations required to find an element
in the tree.</p>
<p><a href="http://en.wikipedia.org/wiki/NTFS" title="NTFS">NTFS</a>, <a href="http://en.wikipedia.org/wiki/ReiserFS" title="ReiserFS">ReiserFS</a>, <a href="http://en.wikipedia.org/wiki/Novell_Storage_Services" title="Novell Storage Services">NSS</a>, <a href="http://en.wikipedia.org/wiki/XFS" title="XFS">XFS</a>, and <a href="http://en.wikipedia.org/wiki/JFS_%28file_system%29" title="JFS (file system)">JFS</a> filesystems all use this type of tree for metadata indexing. <a href="http://en.wikipedia.org/wiki/Relational_database_management_system" title="Relational database management system">Relational database management systems</a> such as <a href="http://en.wikipedia.org/wiki/IBM_DB2" title="IBM DB2">IBM DB2</a><sup id="cite_ref-DMS_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-DMS-0">[1]</a></sup>, <a href="http://en.wikipedia.org/wiki/Informix" title="Informix" class="mw-redirect">Informix</a><sup id="cite_ref-DMS_0-1" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-DMS-0">[1]</a></sup>, <a href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server" title="Microsoft SQL Server">Microsoft SQL Server</a><sup id="cite_ref-DMS_0-2" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-DMS-0">[1]</a></sup>, <a href="http://en.wikipedia.org/wiki/Oracle_Database" title="Oracle Database">Oracle 8</a><sup id="cite_ref-DMS_0-3" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-DMS-0">[1]</a></sup>, <a href="http://en.wikipedia.org/wiki/Adaptive_Server_Enterprise" title="Adaptive Server Enterprise">Sybase ASE</a><sup id="cite_ref-DMS_0-4" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-DMS-0">[1]</a></sup>, <a href="http://en.wikipedia.org/wiki/PostgreSQL" title="PostgreSQL">PostgreSQL</a><sup id="cite_ref-PostgreSQL_1-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-PostgreSQL-1">[2]</a></sup>, <a href="http://en.wikipedia.org/wiki/Firebird_%28database_server%29" title="Firebird (database server)">Firebird</a>, <a href="http://en.wikipedia.org/wiki/MySQL" title="MySQL">MySQL</a><sup id="cite_ref-CCA_2-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-CCA-2">[3]</a></sup> and <a href="http://en.wikipedia.org/wiki/SQLite" title="SQLite">SQLite</a><sup id="cite_ref-SQLite_3-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-SQLite-3">[4]</a></sup> support this type of tree for table indices. Key-value database management systems such as <a href="http://en.wikipedia.org/wiki/CouchDB" title="CouchDB">CouchDB</a><sup id="cite_ref-CouchDB_4-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-CouchDB-4">[5]</a></sup>, <a href="http://en.wikipedia.org/wiki/Tokyo_Cabinet" title="Tokyo cabinet" class="mw-redirect">Tokyo Cabinet</a><sup id="cite_ref-TC_5-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-TC-5">[6]</a></sup> and <a href="http://en.wikipedia.org/w/index.php?title=Tokyo_Tyrant&amp;action=edit&amp;redlink=1" class="new" title="Tokyo Tyrant (page does not exist)">Tokyo Tyrant</a> support this type of tree for data access. InfinityDB<sup id="cite_ref-InfinityDB_6-0" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-InfinityDB-6">[7]</a></sup> is a concurrent BTree.</p>
<table id="toc" class="toc">
    <tbody>
        <tr>
            <td>
            <div id="toctitle">
            <h2>Contents</h2>
            [<a href="http://en.wikipedia.org/wiki/B%2B_tree#" class="internal" id="togglelink">hide</a>]</div>
            <ul>
                <li class="toclevel-1 tocsection-1"><a href="http://en.wikipedia.org/wiki/B%2B_tree#Details">1 Details</a>
                <ul>
                    <li class="toclevel-2 tocsection-2"><a href="http://en.wikipedia.org/wiki/B%2B_tree#Search">1.1 Search</a></li>
                    <li class="toclevel-2 tocsection-3"><a href="http://en.wikipedia.org/wiki/B%2B_tree#Insertion">1.2 Insertion</a></li>
                </ul>
                </li>
                <li class="toclevel-1 tocsection-4"><a href="http://en.wikipedia.org/wiki/B%2B_tree#Characteristics">2 Characteristics</a></li>
                <li class="toclevel-1 tocsection-5"><a href="http://en.wikipedia.org/wiki/B%2B_tree#Implementation">3 Implementation</a></li>
                <li class="toclevel-1 tocsection-6"><a href="http://en.wikipedia.org/wiki/B%2B_tree#History">4 History</a></li>
                <li class="toclevel-1 tocsection-7"><a href="http://en.wikipedia.org/wiki/B%2B_tree#See_also">5 See also</a></li>
                <li class="toclevel-1 tocsection-8"><a href="http://en.wikipedia.org/wiki/B%2B_tree#References">6 References</a></li>
                <li class="toclevel-1 tocsection-9"><a href="http://en.wikipedia.org/wiki/B%2B_tree#External_links">7 External links</a></li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=1" title="Edit section: Details">edit</a>] Details</h2>
<p>The order, or branching factor <em>b</em> of a B+ tree measures the
capacity of nodes (i.e. the number of children nodes) for internal nodes
in the tree. The actual number of children for a node, referred to here
as <em>m</em>, is constrained for internal nodes so that <img class="tex" alt=" \lceil b/2 \rceil \le m \le b" src="http://upload.wikimedia.org/math/5/8/7/5877c5b445196d30968574bcc701980f.png">. The root is an exception: it is allowed to have as few as two children. For example, if the order of a B+ tree is 7, each <a href="http://en.wikipedia.org/wiki/Internal_node" title="Internal node" class="mw-redirect">internal node</a>
(except for the root) may have between 4 and 7 children; the root may
have between 2 and 7. Leaf nodes have no children , but are constrained
so that the number of keys must be at least <img class="tex" alt=" \lceil (b-1)/2 \rceil " src="http://upload.wikimedia.org/math/b/2/6/b266a9ee01e91c84cc83317c02df1cc7.png"> and at most <em>b</em> &#8722; 1.
In the situation where a B+ tree is nearly empty, it only contains one
node, which is a leaf node. (The root is also the single leaf, in this
case.) This node is permitted to have as little as one key if necessary.</p>
<h3>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=2" title="Edit section: Search">edit</a>] Search</h3>
<p>The algorithm to perform a search for a record r follows pointers to
the correct child of each node until a leaf is reached. Then, the leaf
is scanned until the correct record is found (or until failure).</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="gml source-gml" style="font-family: monospace;">
<pre class="de1">  function search(record r)<br>    u := root<br>    while (u is not a leaf) do<br>      choose the correct pointer in the node<br>      move to the first node following the pointer<br>      u := current node<br>    scan u for r<br></pre>
</div>
</div>
<p>This pseudocode assumes that no repetition is allowed.</p>
<h3>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=3" title="Edit section: Insertion">edit</a>] Insertion</h3>
<p>Perform a search to determine what bucket the new record should go in.</p>
<ul>
    <li>if the bucket is not full, add the record.</li>
    <li>otherwise, split the bucket.
    <ul>
        <li>allocate new leaf and move half the bucket's elements to the new bucket</li>
        <li>insert the new leaf's smallest key and address into the parent.</li>
        <li>if the parent is full, split it also
        <ul>
            <li>add the middle key to the parent node</li>
        </ul>
        </li>
        <li>repeat until a parent is found that need not split</li>
    </ul>
    </li>
    <li>if the root splits, create a new root which has one key and two pointers.</li>
</ul>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=4" title="Edit section: Characteristics">edit</a>] Characteristics</h2>
<p>For a <em>b</em>-order B+ tree with <em>h</em> levels of index:</p>
<ul>
    <li>The maximum number of records stored is <em>n</em><sub><em>m</em><em>a</em><em>x</em></sub> = <em>b</em><sup><em>h</em></sup> &#8722; <em>b</em><sup><em>h</em> &#8722; 1</sup></li>
    <li>The minimum number of keys is <img class="tex" alt="n_{kmin} = 2\left(\frac{b}{2}\right)^{h-1}" src="http://upload.wikimedia.org/math/1/3/b/13ba2035838d12b53eaf120a84948972.png"></li>
    <li>The space required to store the tree is <em>O</em>(<em>n</em>)</li>
    <li>Inserting a record requires <em>O</em>(log<sub><em>b</em></sub><em>n</em>) operations in the worst case</li>
    <li>Finding a record requires <em>O</em>(log<sub><em>b</em></sub><em>n</em>) operations in the worst case</li>
    <li>Removing a (previously located) record requires <em>O</em>(log<sub><em>b</em></sub><em>n</em>) operations in the worst case</li>
    <li>Performing a <a href="http://en.wikipedia.org/wiki/Range_query" title="Range query">range query</a> with <em>k</em> elements occurring within the range requires <em>O</em>(log<sub><em>b</em></sub><em>n</em> + <em>k</em>) operations in the worst case.</li>
</ul>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=5" title="Edit section: Implementation">edit</a>] Implementation</h2>
<p>The leaves (the bottom-most index blocks) of the B+ tree are often linked to one another in a <a href="http://en.wikipedia.org/wiki/Linked_list" title="Linked list">linked list</a>; this makes <a href="http://en.wikipedia.org/wiki/Range_query" title="Range query">range queries</a>
or an (ordered) iteration through the blocks simpler and more efficient
(though the aforementioned upper bound can be achieved even without
this addition). This does not substantially increase space consumption
or maintenance on the tree. This illustrates one of the significant
advantages of a B+-tree over a <a href="http://en.wikipedia.org/wiki/B-tree" title="B-tree">B-tree</a>;
in a B-tree, since not all keys are present in the leaves, such an
ordered linked list cannot be constructed. A B+-tree is thus
particularly useful as a database system index, where the data typically
resides on disk, as it allows the B+-tree to actually provide an
efficient structure for housing the data itself (this is described in <sup id="cite_ref-7" class="reference"><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_note-7">[8]</a></sup> as index structure "Alternative 1").</p>
<p>If a storage system has a <a href="http://en.wikipedia.org/wiki/Block_%28data_storage%29" title="Block (data storage)">block size</a> of <em>B</em> <a href="http://en.wikipedia.org/wiki/Bytes" title="Bytes" class="mw-redirect">bytes</a>, and the keys to be stored have a size of <em>k</em>, arguably the most efficient B+ tree is one where <em>b</em> = (<em>B</em> / <em>k</em>) &#8722; 1.
Although theoretically the one-off is unnecessary, in practice there is
often a little extra space taken up by the index blocks (for example,
the linked list references in the leaf blocks). Having an index block
which is slightly larger than the storage system's actual block
represents a significant performance decrease; therefore erring on the
side of caution is preferable.</p>
<p>If nodes of the B+ tree are organized as arrays of elements, then it
may take a considerable time to insert or delete an element as half of
the array will need to be shifted on average. To overcome this problem,
elements inside a node can be organized in a binary tree or a B+ tree
instead of an array.</p>
<p>B+ trees can also be used for data stored in RAM. In this case a
reasonable choice for block size would be the size of processor's <a href="http://en.wikipedia.org/wiki/Cache_line" title="Cache line" class="mw-redirect">cache line</a>.
However, some studies have proved that a block size a few times larger
than the processor's cache line can deliver better performance if <a href="http://en.wikipedia.org/wiki/Cache" title="Cache">cache</a> <a href="http://en.wikipedia.org/wiki/Prefetch_buffer" title="Prefetch buffer">prefetching</a> is used.</p>
<p>Space efficiency of B+ trees can be improved by using some compression techniques. One possibility is to use <a href="http://en.wikipedia.org/wiki/Delta_encoding" title="Delta encoding">delta encoding</a>
to compress keys stored into each block. For internal blocks, space
saving can be achieved by either compressing keys or pointers. For
string keys, space can be saved by using the following technique:
Normally the <em>i</em>th entry of an internal block contains the first
key of block i+1. Instead of storing the full key, we could store the
shortest prefix of the first key of block i+1 that is strictly greater
(in lexicographic order) than last key of block i. There is also a
simple way to compress pointers: if we suppose that some consecutive
blocks i, i+1...i+k are stored contiguously, then it will suffice to
store only a pointer to the first block and the count of consecutive
blocks.</p>
<p>All the above compression techniques have some drawbacks. First, a
full block must be decompressed to extract a single element. One
technique to overcome this problem is to divide each block into
sub-blocks and compress them separately. In this case searching or
inserting an element will only need to decompress or compress a
sub-block instead of a full block. Another drawback of compression
techniques is that the number of stored elements may vary considerably
from a block to another depending on how well the elements are
compressed inside each block.</p>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=6" title="Edit section: History">edit</a>] History</h2>
<p>The B tree was first described in the paper <em>Organization and Maintenance of Large Ordered Indices. Acta Informatica 1</em>: 173&#8211;189 (1972) by <a href="http://en.wikipedia.org/wiki/Rudolf_Bayer" title="Rudolf Bayer">Rudolf Bayer</a>
and Edward M. McCreight. There is no single paper introducing the B+
tree concept. Instead, the notion of maintaining all data in leaf nodes
is repeatedly brought up as an interesting variant. An early survey of B
trees also covering B+ trees is Douglas Comer: "<a href="http://doi.acm.org/10.1145/356770.356776" class="external text" rel="nofollow">The Ubiquitous B-Tree</a>",
ACM Computing Surveys 11(2): 121&#8211;137 (1979). Comer notes that the B+
tree was used in IBM's VSAM data access software and he refers to an IBM
published article from 1973.</p>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=7" title="Edit section: See also">edit</a>] See also</h2>
<ul>
    <li><a href="http://en.wikipedia.org/wiki/Binary_Search_Tree" title="Binary search tree" class="mw-redirect">Binary Search Tree</a></li>
    <li><a href="http://en.wikipedia.org/wiki/B_sharp_tree" title="B sharp tree">B# Tree</a></li>
    <li><a href="http://en.wikipedia.org/wiki/B-tree" title="B-tree">B-tree</a></li>
    <li><a href="http://en.wikipedia.org/wiki/Bitmap_index" title="Bitmap index">Bitmap index</a></li>
    <li><a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" title="Divide and conquer algorithm">Divide and conquer algorithm</a></li>
</ul>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=8" title="Edit section: References">edit</a>] References</h2>
<div class="references-small">
<ol class="references">
    <li id="cite_note-DMS-0">^ <a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-DMS_0-0"><sup><em><strong>a</strong></em></sup></a> <a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-DMS_0-1"><sup><em><strong>b</strong></em></sup></a> <a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-DMS_0-2"><sup><em><strong>c</strong></em></sup></a> <a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-DMS_0-3"><sup><em><strong>d</strong></em></sup></a> <a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-DMS_0-4"><sup><em><strong>e</strong></em></sup></a>
    Ramakrishnan Raghu, Gehrke Johannes - Database Management Systems,
    McGraw-Hill Higher Education (2000), 2nd edition (en) page 267</li>
    <li id="cite_note-PostgreSQL-1"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-PostgreSQL_1-0">^</a></strong> <a href="http://www.postgresql.org/docs/9.0/static/indexes-types.html" class="external text" rel="nofollow">PostgreSQL documentation</a></li>
    <li id="cite_note-CCA-2"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-CCA_2-0">^</a></strong> <a href="http://www.bytebot.net/blog/archives/2008/01/28/morning-sessions-at-mysql-miniconf" class="external text" rel="nofollow">Colin Charles Agenda - Morning sessions at MySQL MiniConf</a></li>
    <li id="cite_note-SQLite-3"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-SQLite_3-0">^</a></strong> <a href="http://sqlite.org/version3.html" class="external text" rel="nofollow">SQLite Version 3 Overview</a></li>
    <li id="cite_note-CouchDB-4"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-CouchDB_4-0">^</a></strong> <a href="http://guide.couchdb.org/draft/btree.html" class="external text" rel="nofollow">CouchDB Guide (see note after 3rd paragraph)</a></li>
    <li id="cite_note-TC-5"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-TC_5-0">^</a></strong> <a href="http://1978th.net/tokyocabinet/" class="external text" rel="nofollow">Tokyo Cabinet reference</a></li>
    <li id="cite_note-InfinityDB-6"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-InfinityDB_6-0">^</a></strong> <a href="http://boilerbay.com/infinitydb/TheDesignOfTheInfinityDatabaseEngine.htm" class="external text" rel="nofollow">The Design Of The InfinityDB Database Engine</a></li>
    <li id="cite_note-7"><strong><a href="http://en.wikipedia.org/wiki/B%2B_tree#cite_ref-7">^</a></strong> Ramakrishnan, R. and Gehrke, J. Database Management Systems, McGraw-Hill Higher Education (2002), 3rd edition</li>
</ol>
</div>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=B%2B_tree&amp;action=edit&amp;section=9" title="Edit section: External links">edit</a>] External links</h2>
<table class="metadata mbox-small plainlinks" style="border: 1px solid #aaaaaa; background-color: #f9f9f9;">
    <tbody>
        <tr>
            <td class="mbox-image"><img alt="" src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/40px-Wikibooks-logo-en-noslogan.svg.png" width="40" height="40"></td>
            <td class="mbox-text">Wikibooks has a book on the topic of
            <div style="margin-left: 10px;"><em><strong><a href="http://en.wikibooks.org/wiki/Transwiki:B%2B_tree" class="extiw" title="wikibooks:Transwiki:B+ tree">Transwiki:B+ tree</a></strong></em></div>
            </td>
        </tr>
    </tbody>
</table>
<ul>
    <li><a href="http://pypi.python.org/pypi/blist" class="external text" rel="nofollow">B+ tree in Python, used to implement a list</a></li>
    <li><a href="http://idlebox.net/2007/stx-btree/" class="external text" rel="nofollow">B+ tree implementation as C++ template library</a></li>
    <li><a href="http://www.cecs.csulb.edu/%7emonge/classes/share/B+TreeIndexes.html" class="external text" rel="nofollow">Dr. Monge's B+ Tree index notes</a></li>
    <li><a href="http://www.scalingweb.com/opensource.php" class="external text" rel="nofollow">Open Source C++ B+ Tree Implementation</a></li>
    <li><a href="http://www.amittai.com/prose/bplustree.html" class="external text" rel="nofollow">Interactive B+ Tree Implementation in C</a></li>
    <li><a href="http://blog.conquex.com/?p=84" class="external text" rel="nofollow">Open Source Javascript B+ Tree Implementation</a></li>
    <li><a href="http://search.cpan.org/%7Ehanenkamp/Tree-BPTree-1.07" class="external text" rel="nofollow">Perl implementation of B+ trees</a></li>
    <li><a href="http://bplusdotnet.sourceforge.net/" class="external text" rel="nofollow">java/C#/python implementations of B+ trees</a></li>
    <li><a href="http://www2.enel.ucalgary.ca/%7Ewhassane/papers/CSB+_ccece_2007.pdf" class="external text" rel="nofollow">Evaluating the performance of CSB+-trees on Mutithreaded Architectures</a></li>
    <li><a href="http://www.eecs.umich.edu/%7Ejignesh/quickstep/publ/cci.pdf" class="external text" rel="nofollow">Effect of node size on the performance of cache conscious B+-trees</a></li>
    <li><a href="http://www.pittsburgh.intel-research.net/people/gibbons/papers/fpbptrees.pdf" class="external text" rel="nofollow">Fractal Prefetching B+-trees</a></li>
    <li><a href="http://gemo.futurs.inria.fr/events/EXPDB2006/PAPERS/Jonsson.pdf" class="external text" rel="nofollow">Towards pB+-trees in the field: implementations Choices and performance</a></li>
    <li><a href="https://oa.doria.fi/bitstream/handle/10024/2906/cachecon.pdf?sequence=1" class="external text" rel="nofollow">Cache-Conscious Index Structures for Main-Memory Databases</a></li>
    <li><a href="http://supertech.csail.mit.edu/cacheObliviousBTree.html" class="external text" rel="nofollow">Cache Oblivious B(+)-trees</a></li>
    <li><a href="http://books.couchdb.org/relax/appendix/btrees" class="external text" rel="nofollow">The Power of B-Trees: CouchDB B+ Tree Implementation</a></li>
</ul>
<br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/135145.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-12-01 11:48 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135145.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>R-Tree</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135144.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 01 Dec 2010 03:46:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135144.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/135144.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135144.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/135144.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/135144.html</trackback:ping><description><![CDATA[<h1 id="firstHeading" class="firstHeading">R-tree</h1>
<div id="siteSub">From Wikipedia, the free encyclopedia</div>
<div id="jump-to-nav">
Jump to: <a href="http://en.wikipedia.org/wiki/R-tree#mw-head">navigation</a>,
<a href="http://en.wikipedia.org/wiki/R-tree#p-search">search</a>
</div>
<div class="dablink">This article is about the data structure.  For the type of metric space, see <a href="http://en.wikipedia.org/wiki/Real_tree" title="Real tree">Real tree</a>.</div>
<div class="thumb tright">
<div class="thumbinner" style="width: 402px;"><a href="http://en.wikipedia.org/wiki/File:R-tree.svg" class="image"><img alt="" src="http://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/R-tree.svg/400px-R-tree.svg.png" class="thumbimage" width="400" height="343"></a>
<div class="thumbcaption">
<div class="magnify"><a href="http://en.wikipedia.org/wiki/File:R-tree.svg" class="internal" title="Enlarge"><img src="http://bits.wikimedia.org/skins-1.5/common/images/magnify-clip.png" alt="" width="15" height="11"></a></div>
Simple example of an R-tree for 2D rectangles</div>
</div>
</div>
<div class="thumb tright">
<div class="thumbinner" style="width: 402px;"><a href="http://en.wikipedia.org/wiki/File:RTree-Visualization-3D.svg" class="image"><img alt="" src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/57/RTree-Visualization-3D.svg/400px-RTree-Visualization-3D.svg.png" class="thumbimage" width="400" height="400"></a>
<div class="thumbcaption">
<div class="magnify"><a href="http://en.wikipedia.org/wiki/File:RTree-Visualization-3D.svg" class="internal" title="Enlarge"><img src="http://bits.wikimedia.org/skins-1.5/common/images/magnify-clip.png" alt="" width="15" height="11"></a></div>
Visualization of an R*-tree for 3D cubes using <a href="http://en.wikipedia.org/wiki/Environment_for_DeveLoping_KDD-Applications_Supported_by_Index-Structures" title="Environment for DeveLoping KDD-Applications Supported by Index-Structures">ELKI</a></div>
</div>
</div>
<p><strong>R-trees</strong> are <a href="http://en.wikipedia.org/wiki/Tree_data_structure" title="Tree data structure" class="mw-redirect">tree data structures</a> that are similar to <a href="http://en.wikipedia.org/wiki/B-tree" title="B-tree">B-trees</a>, but are used for <a href="http://en.wikipedia.org/wiki/Spatial_index" title="Spatial index">spatial access methods</a>,
i.e., for indexing multi-dimensional information; for example, the (X,
Y) coordinates of geographical data. A common real-world usage for an
R-tree might be: "Find all museums within 2&nbsp;kilometres (1.2 mi) of my
current location".</p>
<p>The data structure splits space with hierarchically nested, and possibly overlapping, <a href="http://en.wikipedia.org/wiki/Minimum_bounding_rectangle" title="Minimum bounding rectangle">minimum bounding rectangles</a> (MBRs, otherwise known as bounding boxes, i.e. "rectangle", what the "R" in R-tree stands for).</p>
<p>Each node of an R-tree has a variable number of entries (up to some pre-defined maximum). Each entry within a non-<a href="http://en.wikipedia.org/wiki/Leaf_node" title="Leaf node">leaf node</a> stores two pieces of data: a way of identifying a <a href="http://en.wikipedia.org/wiki/Child_node" title="Child node" class="mw-redirect">child node</a>, and the <a href="http://en.wikipedia.org/wiki/Bounding_box" title="Bounding box" class="mw-redirect">bounding box</a> of all entries within this child node.</p>
<p>The insertion and deletion algorithms use the bounding boxes from the
nodes to ensure that "nearby" elements are placed in the same <a href="http://en.wikipedia.org/wiki/Leaf_node" title="Leaf node">leaf node</a>
(in particular, a new element will go into the leaf node that requires
the least enlargement in its bounding box). Each entry within a leaf
node stores two pieces of information; a way of identifying the actual
data element (which, alternatively, may be placed directly in the node),
and the bounding box of the data element.</p>
<p>Similarly, the searching algorithms (e.g., <a href="http://en.wikipedia.org/wiki/Intersection_%28set_theory%29" title="Intersection (set theory)">intersection</a>,
containment, nearest) use the bounding boxes to decide whether or not
to search inside a child node. In this way, most of the nodes in the
tree are never "touched" during a search. Like B-trees, this makes
R-trees suitable for <a href="http://en.wikipedia.org/wiki/Database" title="Database">databases</a>, where nodes can be paged to memory when needed.</p>
<p>Different algorithms can be used to split nodes when they become too full, resulting in the <em>quadratic</em> and <em>linear</em> R-tree sub-types.</p>
<p>R-trees do not historically guarantee good <a href="http://en.wikipedia.org/wiki/Worst-case_performance" title="Worst-case performance" class="mw-redirect">worst-case performance</a>, but generally perform well with real-world data.<sup class="Template-Fact" title="This claim needs references to reliable sources from october 2008" style="white-space: nowrap;">[<em><a href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed">citation needed</a></em>]</sup> However, a new algorithm was published in 2004 that defines the <a href="http://en.wikipedia.org/w/index.php?title=Priority_R-Tree&amp;action=edit&amp;redlink=1" class="new" title="Priority R-Tree (page does not exist)">Priority R-Tree</a>, which claims to be as efficient as the most efficient methods of 2004 and is at the same time worst-case optimal.<sup id="cite_ref-prtree_0-0" class="reference"><a href="http://en.wikipedia.org/wiki/R-tree#cite_note-prtree-0">[1]</a></sup></p>
<p>When data is organized in an R-Tree, the <a href="http://en.wikipedia.org/wiki/K_nearest_neighbors" title="K nearest neighbors" class="mw-redirect">k nearest neighbors</a> (for any <a href="http://en.wikipedia.org/wiki/Lp_space" title="Lp space">L<sup>p</sup>-Norm</a>) of all points can efficiently be computed using a spatial join<sup id="cite_ref-1" class="reference"><a href="http://en.wikipedia.org/wiki/R-tree#cite_note-1">[2]</a></sup>. This is beneficial for many algorithms based on the k nearest neighbors, for example the <a href="http://en.wikipedia.org/wiki/Local_Outlier_Factor" title="Local Outlier Factor">Local Outlier Factor</a>.</p>
<table id="toc" class="toc">
    <tbody>
        <tr>
            <td style="border: 1px dashed #7f7c75;">
            <div id="toctitle">
            <h2>Contents</h2>
            [<a href="http://en.wikipedia.org/wiki/R-tree#" class="internal" id="togglelink">hide</a>]</div>
            <ul>
                <li class="toclevel-1 tocsection-1"><a href="http://en.wikipedia.org/wiki/R-tree#Variants">1 Variants</a></li>
                <li class="toclevel-1 tocsection-2"><a href="http://en.wikipedia.org/wiki/R-tree#Algorithm">2 Algorithm</a>
                <ul>
                    <li class="toclevel-2 tocsection-3"><a href="http://en.wikipedia.org/wiki/R-tree#Search">2.1 Search</a></li>
                    <li class="toclevel-2 tocsection-4"><a href="http://en.wikipedia.org/wiki/R-tree#Insertion">2.2 Insertion</a></li>
                    <li class="toclevel-2 tocsection-5"><a href="http://en.wikipedia.org/wiki/R-tree#Bulk-loading">2.3 Bulk-loading</a></li>
                </ul>
                </li>
                <li class="toclevel-1 tocsection-6"><a href="http://en.wikipedia.org/wiki/R-tree#See_also">3 See also</a></li>
                <li class="toclevel-1 tocsection-7"><a href="http://en.wikipedia.org/wiki/R-tree#References">4 References</a></li>
                <li class="toclevel-1 tocsection-8"><a href="http://en.wikipedia.org/wiki/R-tree#External_links">5 External links</a></li>
            </ul>
            </td>
        </tr>
    </tbody>
</table>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=1" title="Edit section: Variants">edit</a>] Variants</h2>
<ul>
    <li><a href="http://en.wikipedia.org/wiki/R*_tree" title="R* tree">R* tree</a></li>
    <li><a href="http://en.wikipedia.org/wiki/R%2B_tree" title="R+ tree">R+ tree</a></li>
    <li><a href="http://en.wikipedia.org/wiki/Hilbert_R-tree" title="Hilbert R-tree">Hilbert R-tree</a></li>
    <li>Priority R-Tree (<a href="http://en.wikipedia.org/w/index.php?title=PR-Tree&amp;action=edit&amp;redlink=1" class="new" title="PR-Tree (page does not exist)">PR-Tree</a>)
    - The PR-tree performs similarly to the best known R-tree variants on
    real-life and relatively evenly distributed data, but outperforms them
    significantly on more extreme data.<sup id="cite_ref-prtree_0-1" class="reference"><a href="http://en.wikipedia.org/wiki/R-tree#cite_note-prtree-0">[1]</a></sup></li>
</ul>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=2" title="Edit section: Algorithm">edit</a>] Algorithm</h2>
<h3>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=3" title="Edit section: Search">edit</a>] Search</h3>
<p>The input is a search rectangle (Query box). Searching is quite similar to searching in a <a href="http://en.wikipedia.org/wiki/B%2Btree" title="B+tree" class="mw-redirect">B+tree</a>.
The search starts from the root node of the tree. Every internal node
contains a set of rectangles and pointers to the corresponding child
node and every leaf node contains the rectangles of spatial objects (the
pointer to some spatial object can be there). For every rectangle in a
node, it has to be decided if it overlaps the search rectangle or not.
If yes, the corresponding child node has to be searched also. Searching
is done like this in a recursive manner until all overlapping nodes have
been traversed. When a leaf node is reached, the contained bounding
boxes (rectangles) are tested against the search rectangle and their
objects (if there are any) are put into the result set if they lie
within the search rectangle.</p>
<h3>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=4" title="Edit section: Insertion">edit</a>] Insertion</h3>
<p>To insert an object, the tree is traversed recursively from the root
node. All rectangles in the current internal node are examined. The
constraint of least coverage is employed to insert an object, i.e., the
box that needs least enlargement to enclose the new object is selected.
In the case where there is more than one rectangle that meets this
criterion, the one with the smallest area is chosen. Inserting continues
recursively in the chosen node. Once a leaf node is reached, a
straightforward insertion is made if the leaf node is not full. If the
leaf node is full, it must be split before the insertion is made. A few
splitting algorithms have been proposed for good R-tree performance.</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice">
    <tbody>
        <tr>
            <td style="border: 1px dashed #7f7c75;" class="mbox-image"><a href="http://en.wikipedia.org/wiki/File:Wiki_letter_w_cropped.svg" class="image"><img alt="Wiki letter w cropped.svg" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Wiki_letter_w_cropped.svg/20px-Wiki_letter_w_cropped.svg.png" width="20" height="14"></a></td>
            <td style="border: 1px dashed #7f7c75;" class="mbox-text">This section requires <a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit" class="external text" rel="nofollow">expansion</a>.</td>
        </tr>
    </tbody>
</table>
<h3>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=5" title="Edit section: Bulk-loading">edit</a>] Bulk-loading</h3>
<ul>
    <li>Sort-Tile-Recursive (STR) <sup id="cite_ref-2" class="reference"><a href="http://en.wikipedia.org/wiki/R-tree#cite_note-2">[3]</a></sup></li>
    <li>Packed <a href="http://en.wikipedia.org/wiki/Hilbert_R-tree" title="Hilbert R-tree">Hilbert R-Tree</a> - Uses the Hilbert value of the center of a rectangle to sort the leaf nodes and recursively builds the tree.</li>
    <li>Nearest-X - Rectangles are sorted on the x-coordinate and nodes are created.</li>
</ul>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice">
    <tbody>
        <tr>
            <td style="border: 1px dashed #7f7c75;" class="mbox-image"><a href="http://en.wikipedia.org/wiki/File:Wiki_letter_w_cropped.svg" class="image"><img alt="Wiki letter w cropped.svg" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Wiki_letter_w_cropped.svg/20px-Wiki_letter_w_cropped.svg.png" width="20" height="14"></a></td>
            <td style="border: 1px dashed #7f7c75;" class="mbox-text">This section requires <a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit" class="external text" rel="nofollow">expansion</a>.</td>
        </tr>
    </tbody>
</table>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=6" title="Edit section: See also">edit</a>] See also</h2>
<ul>
    <li><a href="http://en.wikipedia.org/wiki/Segment_tree" title="Segment tree">Segment tree</a></li>
    <li><a href="http://en.wikipedia.org/wiki/Interval_tree" title="Interval tree">Interval tree</a> - A degenerate R-Tree for 1 dimension (usually time).</li>
    <li><a href="http://en.wikipedia.org/wiki/Bounding_volume_hierarchy" title="Bounding volume hierarchy">Bounding volume hierarchy</a></li>
    <li><a href="http://en.wikipedia.org/wiki/Spatial_index" title="Spatial index">Spatial index</a></li>
    <li><a href="http://en.wikipedia.org/wiki/GiST" title="GiST">GiST</a></li>
</ul>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=7" title="Edit section: References">edit</a>] References</h2>
<ol class="references">
    <li id="cite_note-prtree-0">^ <a href="http://en.wikipedia.org/wiki/R-tree#cite_ref-prtree_0-0"><sup><em><strong>a</strong></em></sup></a> <a href="http://en.wikipedia.org/wiki/R-tree#cite_ref-prtree_0-1"><sup><em><strong>b</strong></em></sup></a> Lars Arge, Mark de Berg, Herman J. Haverkort, Ke Yi: ""<a href="http://www.win.tue.nl/%7Emdberg/Papers/prtree.pdf" class="external text" rel="nofollow">The Priority R-Tree: A Practically Efficient and Worst-Case Optimal RTree</a>"", SIGMOD 2004, June 13&#8211;18, Paris, France</li>
    <li id="cite_note-1"><strong><a href="http://en.wikipedia.org/wiki/R-tree#cite_ref-1">^</a></strong> Brinkhoff, T.; Kriegel, H. P.; Seeger, B. (1993). "Efficient processing of spatial joins using R-trees". <em>ACM SIGMOD Record</em> <strong>22</strong>: 237. <a href="http://en.wikipedia.org/wiki/Digital_object_identifier" title="Digital object identifier">doi</a>:<a href="http://dx.doi.org/10.1145%2F170036.170075" class="external text" rel="nofollow">10.1145/170036.170075</a>.&nbsp; <a href="http://en.wikipedia.org/w/index.php?title=Template:Cite_doi/10.1145.2F170036.170075&amp;action=edit&amp;editintro=Template:Cite_doi/editintro2" class="external text" rel="nofollow">edit</a></li>
    <li id="cite_note-2"><strong><a href="http://en.wikipedia.org/wiki/R-tree#cite_ref-2">^</a></strong> Scott T. Leutenegger, Jeffrey M. Edgington and Mario A. Lopez: <a href="http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=4D62F569DDC2B520D1658983F40AC9DC?doi=10.1.1.106.4996&amp;rep=rep1&amp;type=pdf" class="external text" rel="nofollow">STR: A Simple and Efficient Algorithm for R-Tree Packing</a></li>
</ol>
<ul>
    <li><a href="http://en.wikipedia.org/w/index.php?title=Antonin_Guttman&amp;action=edit&amp;redlink=1" class="new" title="Antonin Guttman (page does not exist)">Antonin Guttman</a>: <em><a href="http://www-db.deis.unibo.it/courses/SI-LS/papers/Gut84.pdf" class="external text" rel="nofollow">R-Trees: A Dynamic Index Structure for Spatial Searching</a></em>, Proc. 1984 ACM SIGMOD International Conference on Management of Data, pp.&nbsp;47&#8211;57. <a href="http://en.wikipedia.org/wiki/Special:BookSources/0897911288" class="internal mw-magiclink-isbn">ISBN 0-89791-128-8</a></li>
    <li><a href="http://en.wikipedia.org/w/index.php?title=Yannis_Manolopoulos&amp;action=edit&amp;redlink=1" class="new" title="Yannis Manolopoulos (page does not exist)">Yannis Manolopoulos</a>, <a href="http://en.wikipedia.org/w/index.php?title=Alexandros_Nanopoulos&amp;action=edit&amp;redlink=1" class="new" title="Alexandros Nanopoulos (page does not exist)">Alexandros Nanopoulos</a>, <a href="http://en.wikipedia.org/w/index.php?title=Apostolos_N._Papadopoulos&amp;action=edit&amp;redlink=1" class="new" title="Apostolos N. Papadopoulos (page does not exist)">Apostolos N. Papadopoulos</a>, <a href="http://en.wikipedia.org/w/index.php?title=Yannis_Theodoridis&amp;action=edit&amp;redlink=1" class="new" title="Yannis Theodoridis (page does not exist)">Yannis Theodoridis</a>: <em>R-Trees: Theory and Applications</em>, Springer, 2005. <a href="http://en.wikipedia.org/wiki/Special:BookSources/1852339772" class="internal mw-magiclink-isbn">ISBN 1-85233-977-2</a></li>
    <li><a href="http://dbs.mathematik.uni-marburg.de/publications/myPapers/1990/BKSS90.pdf" class="external text" rel="nofollow">N.
    Beckmann, H.-P. Kriegel, R. Schneider, B. Seeger: The R*-Tree: An
    Efficient and Robust Access Method for Points and Rectangles. SIGMOD
    Conference 1990: 322-331</a></li>
</ul>
<h2>[<a href="http://en.wikipedia.org/w/index.php?title=R-tree&amp;action=edit&amp;section=8" title="Edit section: External links">edit</a>] External links</h2>
<ul>
    <li><a href="http://www.rtreeportal.org/" class="external text" rel="nofollow">R-tree portal</a></li>
    <li>R-Tree implementations: <a href="http://superliminal.com/sources/sources.htm#C%20&amp;%20C++%20Code" class="external text" rel="nofollow">C &amp; C++</a>, <a href="http://gis.umb.no/gis/applets/rtree2/jdk1.1/" class="external text" rel="nofollow">Java applet</a>, <a href="http://www.cliki.net/spatial-trees" class="external text" rel="nofollow">Common Lisp</a>, <a href="http://pypi.python.org/pypi/Rtree/" class="external text" rel="nofollow">Python</a>, <a href="http://github.com/imbcmdth/RTree" class="external text" rel="nofollow">Javascript</a>.</li>
</ul>
<table class="navbox" cellspacing="0">
    <tbody>
        <tr>
            <td style="padding: 2px; border: 1px dashed #7f7c75;">
            <table id="collapsibleTable0" class="nowraplinks collapsible autocollapse" style="width: 100%; background: none repeat scroll 0% 0% transparent; color: inherit;" cellspacing="0">
                <tbody>
                    <tr>
                        <th style="border: 1px dashed #7f7c75;" colspan="2" class="navbox-title">[<a href="http://en.wikipedia.org/wiki/R-tree#" id="collapseButton0">hide</a>]
                        <div style="float: left; width: 6em; text-align: left;">
                        <div class="noprint plainlinks navbar" style="background: none repeat scroll 0% 0% transparent; padding: 0pt; font-weight: normal; border: medium none; font-size: xx-small;"><a href="http://en.wikipedia.org/wiki/Template:CS_trees" title="Template:CS trees">v</a>&nbsp;&#8226;&nbsp;<a href="http://en.wikipedia.org/wiki/Template_talk:CS_trees" title="Template talk:CS trees">d</a>&nbsp;&#8226;&nbsp;<a href="http://en.wikipedia.org/w/index.php?title=Template:CS_trees&amp;action=edit" class="external text" rel="nofollow">e</a></div>
                        </div>
                        <a href="http://en.wikipedia.org/wiki/Tree_%28data_structure%29" title="Tree (data structure)">Trees</a> in <a href="http://en.wikipedia.org/wiki/Computer_science" title="Computer science">computer science</a></th>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group"><a href="http://en.wikipedia.org/wiki/Binary_tree" title="Binary tree">Binary trees</a></td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-odd">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/Binary_search_tree" title="Binary search tree">Binary search tree (BST)</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Van_Emde_Boas_tree" title="Van Emde Boas tree">Van Emde Boas tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Cartesian_tree" title="Cartesian tree">Cartesian tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Top_Tree" title="Top Tree">Top Tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/T-tree" title="T-tree">T-tree</a></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group"><a href="http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree" title="Self-balancing binary search tree">Self-balancing binary search trees</a></td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-even">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/Red-black_tree" title="Red-black tree">Red-black tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/AVL_tree" title="AVL tree">AVL tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/AA_tree" title="AA tree">AA tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Splay_tree" title="Splay tree">Splay tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Scapegoat_tree" title="Scapegoat tree">Scapegoat tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Treap" title="Treap">Treap</a></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group"><a href="http://en.wikipedia.org/wiki/B-tree" title="B-tree">B-trees</a></td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-odd">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/B%2B_tree" title="B+ tree">B+ tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/B*-tree" title="B*-tree">B*-tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/UB-tree" title="UB-tree">UB-tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/2-3_tree" title="2-3 tree">2-3 tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/2-3-4_tree" title="2-3-4 tree">2-3-4 tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/%28a,b%29-tree" title="(a,b)-tree">(a,b)-tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Dancing_tree" title="Dancing tree">Dancing tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Htree" title="Htree">Htree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Bx-tree" title="Bx-tree">B<small><sup>x</sup></small>-tree</a></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group"><a href="http://en.wikipedia.org/wiki/Trie" title="Trie">Tries</a></td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-even">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/Suffix_tree" title="Suffix tree">Suffix tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Radix_tree" title="Radix tree">Radix tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Ternary_search_tree" title="Ternary search tree">Ternary search tree</a></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group"><a href="http://en.wikipedia.org/wiki/Binary_space_partitioning" title="Binary space partitioning">Binary space partitioning (BSP)</a> trees</td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-odd">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/Quadtree" title="Quadtree">Quadtree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Octree" title="Octree">Octree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Kd-tree" title="Kd-tree">kd-tree</a> (<a href="http://en.wikipedia.org/wiki/Implicit_kd-tree" title="Implicit kd-tree">implicit</a>)&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/VP-tree" title="VP-tree">VP-tree</a></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group">Non-binary trees</td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-even">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/Exponential_tree" title="Exponential tree">Exponential tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Fusion_tree" title="Fusion tree">Fusion tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Interval_tree" title="Interval tree">Interval tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/PQ_tree" title="PQ tree">PQ tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Range_tree" title="Range tree">Range tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/SPQR_tree" title="SPQR tree">SPQR tree</a></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group"><a href="http://en.wikipedia.org/wiki/Spatial_index" title="Spatial index">Spatial</a> data partitioning trees</td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-odd">
                        <div style="padding: 0em 0.25em;"><strong class="selflink">R-tree &nbsp;&#183; <a href="http://en.wikipedia.org/wiki/R%2B_tree" title="R+ tree">R+ tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/R*_tree" title="R* tree">R* tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/X-tree" title="X-tree">X-tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/M-tree" title="M-tree">M-tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Segment_tree" title="Segment tree">Segment tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Fenwick_tree" title="Fenwick tree">Fenwick tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Hilbert_R-tree" title="Hilbert R-tree">Hilbert R-tree</a></strong></div>
                        </td>
                    </tr>
                    <tr style="height: 2px;">
                        <td style="border: 1px dashed #7f7c75;"><br></td>
                    </tr>
                    <tr>
                        <td style="border: 1px dashed #7f7c75;" class="navbox-group">Other trees</td>
                        <td style="text-align: left; border: 1px dashed #7f7c75; width: 100%; padding: 0px;" class="navbox-list navbox-even">
                        <div style="padding: 0em 0.25em;"><a href="http://en.wikipedia.org/wiki/Heap_%28data_structure%29" title="Heap (data structure)">Heap</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Hash_tree" title="Hash tree">Hash tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Finger_tree" title="Finger tree">Finger tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Metric_tree" title="Metric tree">Metric tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Cover_tree" title="Cover tree">Cover tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/BK-tree" title="BK-tree">BK-tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/Doubly-chained_tree" title="Doubly-chained tree">Doubly-chained tree</a>&nbsp;&#183; <a href="http://en.wikipedia.org/wiki/The_iDistance_Technique" title="The idistance technique" class="mw-redirect">iDistance</a></div>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<div class="printfooter">
Retrieved from "<a href="http://en.wikipedia.org/wiki/R-tree">http://en.wikipedia.org/wiki/R-tree</a>"</div>
<div id="mw-normal-catlinks"><a href="http://en.wikipedia.org/wiki/Special:Categories" title="Special:Categories">Categories</a>: <a href="http://en.wikipedia.org/wiki/Category:R-tree" title="Category:R-tree">R-tree</a> | <a href="http://en.wikipedia.org/wiki/Category:Database_index_techniques" title="Category:Database index techniques">Database index techniques</a></div>
<br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/135144.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-12-01 11:46 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/12/01/135144.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HOW TO CONNECT APACHE  AND TOMCAT</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/29/134977.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Mon, 29 Nov 2010 05:07:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/29/134977.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/134977.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/29/134977.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/134977.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/134977.html</trackback:ping><description><![CDATA[<p><strong>简介：</strong>&nbsp;整合 Apache Http Server 和 Tomcat 可以提升对静态文件的处理性能、利用 Web 服务器来做负载均衡以及容错、无缝的升级应用程序。本文介绍了三种整合 Apache 和 Tomcat 的方式
<br></p>
<p><a title="Author liudong"  href="http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/">Author LiuDong</a><br></p>
<p>首先我们先介绍一下为什么要让 Apache 与 Tomcat 之间进行连接。事实上 Tomcat 本身已经提供了 HTTP
服务，该服务默认的端口是 8080，装好 tomcat 后通过 8080 端口可以直接使用 Tomcat 所运行的应用程序，你也可以将该端口改为
80。</p>
<p>既然 Tomcat 本身已经可以提供这样的服务，我们为什么还要引入 Apache 或者其他的一些专门的 HTTP 服务器呢？原因有下面几个：</p>
<p>1.	提升对静态文件的处理性能</p>
<p>2.	利用 Web 服务器来做负载均衡以及容错</p>
<p>3.	无缝的升级应用程序</p>
<p>这三点对一个 web 网站来说是非常之重要的，我们希望我们的网站不仅是速度快，而且要稳定，不能因为某个 Tomcat
宕机或者是升级程序导致用户访问不了，而能完成这几个功能的、最好的 HTTP 服务器也就只有 apache 的 http server 了，它跟
tomcat 的结合是最紧密和可靠的。</p>
<p>接下来我们介绍三种方法将 apache 和 tomcat 整合在一起。</p>
<p><a name="N10058">JK</a></p>
<p>这是最常见的方式，你可以在网上找到很多关于配置JK的网页，当然最全的还是其官方所提供的文档。JK 本身有两个版本分别是 1 和 2，目前 1 最新的版本是 1.2.19，而版本 2 早已经废弃了，以后不再有新版本的推出了，所以建议你采用版本 1。</p>
<p>JK 是通过 AJP 协议与 Tomcat 服务器进行通讯的，Tomcat 默认的 AJP Connector 的端口是
8009。JK 本身提供了一个监控以及管理的页面 jkstatus，通过 jkstatus 可以监控 JK 目前的工作状态以及对到 tomcat
的连接进行设置，如下图所示：</p>
<br><a name="N10066"><strong>图 1：监控以及管理的页面 jkstatus</strong></a><br>
<img alt="图 1：监控以及管理的页面 jkstatus" src="http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/images/image002.jpg" border="0" width="554" height="228">
<br>
<p>在这个图中我们可以看到当前JK配了两个连接分别到 8109 和 8209 端口上，目前 s2 这个连接是停止状态，而 s1
这个连接自上次重启后已经处理了 47 万多个请求，流量达到 6.2 个 G，最大的并发数有 13 等等。我们也可以利用 jkstatus
的管理功能来切换 JK 到不同的 Tomcat 上，例如将 s2 启用，并停用
s1，这个在更新应用程序的时候非常有用，而且整个切换过程对用户来说是透明的，也就达到了无缝升级的目的。关于 JK
的配置文章网上已经非常多了，这里我们不再详细的介绍整个配置过程，但我要讲一下配置的思路，只要明白了配置的思路，JK 就是一个非常灵活的组件。</p>
<p>JK 的配置最关键的有三个文件，分别是 </p>
<p>
<strong> httpd.conf </strong>
<br>
Apache 服务器的配置文件，用来加载 JK 模块以及指定 JK 配置文件信息</p>
<p>
<strong> workers.properties</strong>
<br>
到 Tomcat 服务器的连接定义文件</p>
<p>
<strong> uriworkermap.properties</strong>
<br>
URI 映射文件，用来指定哪些 URL 由 Tomcat 处理，你也可以直接在 httpd.conf 中配置这些 URI，但是独立这些配置的好处是 JK 模块会定期更新该文件的内容，使得我们修改配置的时候无需重新启动 Apache 服务器。</p>
<p>其中第二、三个配置文件名都可以自定义。下面是一个典型的 httpd.conf 对 JK 的配置</p>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="code-outline">
            <pre class="displaycode"># (httpd.conf)<br># 加载 mod_jk 模块<br>LoadModule jk_module modules/mod_jk.so<br><br>#<br># Configure mod_jk<br>#<br><br>JkWorkersFile conf/workers.properties<br>JkMountFile conf/uriworkermap.properties<br>JkLogFile logs/mod_jk.log<br>JkLogLevel warn<br></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>接下来我们在 Apache 的 conf 目录下新建两个文件分别是 workers.properties、uriworkermap.properties。这两个文件的内容大概如下</p>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="code-outline">
            <pre class="displaycode">#<br># workers.properties<br>#<br><br><br># list the workers by name<br><br>worker.list=DLOG4J, status<br><br># localhost server 1<br># ------------------------<br>worker.s1.port=8109<br>worker.s1.host=localhost<br>worker.s1.type=ajp13<br><br># localhost server 2<br># ------------------------<br>worker.s2.port=8209<br>worker.s2.host=localhost<br>worker.s2.type=ajp13<br>worker.s2.stopped=1<br><br>worker.DLOG4J.type=lb<br>worker.retries=3<br>worker.DLOG4J.balanced_workers=s1, s2<br>worker.DLOG4J.sticky_session=1<br><br>worker.status.type=status<br></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>以上的 workers.properties 配置就是我们前面那个屏幕抓图的页面所用的配置。首先我们配置了两个类型为 ajp13 的
worker 分别是 s1 和 s2，它们指向同一台服务器上运行在两个不同端口 8109 和 8209 的 Tomcat
上。接下来我们配置了一个类型为 lb（也就是负载均衡的意思）的 worker，它的名字是 DLOG4J，这是一个逻辑的
worker，它用来管理前面配置的两个物理连接 s1 和 s2。最后还配置了一个类型为 status 的 worker，这是用来监控 JK
本身的模块。有了这三个 worker 还不够，我们还需要告诉 JK，哪些 worker 是可用的，所以就有 <strong> worker.list = DLOG4J, status</strong>  这行配置。</p>
<p>接下来便是 URI 的映射配置了，我们需要指定哪些链接是由 Tomcat 处理的，哪些是由 Apache 直接处理的，看看下面这个文件你就能明白其中配置的意义</p>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="code-outline">
            <pre class="displaycode">/*=DLOG4J<br>/jkstatus=status<br><br>!/*.gif=DLOG4J<br>!/*.jpg=DLOG4J<br>!/*.png=DLOG4J<br>!/*.css=DLOG4J<br>!/*.js=DLOG4J<br>!/*.htm=DLOG4J<br>!/*.html=DLOG4J<br></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>相信你已经明白了一大半了：所有的请求都由 DLOG4J 这个 worker 进行处理，但是有几个例外，/jkstatus 请求由
status 这个 worker 处理。另外这个配置中每一行数据前面的感叹号是什么意思呢？感叹号表示接下来的 URI 不要由 JK
进行处理，也就是 Apache 直接处理所有的图片、css 文件、js 文件以及静态 html 文本文件。</p>
<p>
通过对 workers.properties 和 uriworkermap.properties 的配置，可以有各种各样的组合来满足我们前面提出对一个 web 网站的要求。您不妨动手试试！</p>
<div class="ibm-alternate-rule"><hr></div>
<p><a href="http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/#ibm-pcon" class="ibm-anchor-up-link">回页首</a></p>
<p><a name="N100C4">http_proxy</a></p>
<p>这是利用 Apache 自带的 mod_proxy 模块使用代理技术来连接 Tomcat。在配置之前请确保是否使用的是 2.2.x 版本的 Apache 服务器。因为 2.2.x 版本对这个模块进行了重写，大大的增强了其功能和稳定性。</p>
<p>http_proxy 模式是基于 HTTP 协议的代理，因此它要求 Tomcat 必须提供 HTTP 服务，也就是说必须启用 Tomcat 的 HTTP Connector。一个最简单的配置如下</p>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="code-outline">
            <pre class="displaycode">ProxyPass /images !<br>ProxyPass /css !<br>ProxyPass /js !<br>ProxyPass / http://localhost:8080/<br></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>在这个配置中，我们把所有 http://localhost 的请求代理到 http://localhost:8080/ ，这也就是
Tomcat 的访问地址，除了 images、css、js 几个目录除外。我们同样可以利用 mod_proxy 来做负载均衡，再看看下面这个配置</p>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="code-outline">
            <pre class="displaycode">ProxyPass /images !<br>ProxyPass /css ! <br>ProxyPass /js !<br><br>ProxyPass / balancer://example/<br>&lt;Proxy balancer://example/&gt;<br>BalancerMember http://server1:8080/<br>BalancerMember http://server2:8080/<br>BalancerMember http://server3:8080/<br>&lt;/Proxy&gt;<br></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>配置比 JK 简单多了，而且它也可以通过一个页面来监控集群运行的状态，并做一些简单的维护设置。</p>
<br><a name="N100EA"><strong>图 2：监控集群运行状态</strong></a><br>
<img alt="图 2：监控集群运行状态" src="http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/images/image004.gif" border="0" width="553" height="427">
<br>
<div class="ibm-alternate-rule"><hr></div>
<p><a href="http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/#ibm-pcon" class="ibm-anchor-up-link">回页首</a></p>
<p><a name="N100FA">ajp_proxy</a></p>
<p>ajp_proxy 连接方式其实跟 http_proxy 方式一样，都是由 mod_proxy 所提供的功能。配置也是一样，只需要把
http:// 换成 ajp:// ，同时连接的是 Tomcat 的 AJP Connector 所在的端口。上面例子的配置可以改为：</p>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td class="code-outline">
            <pre class="displaycode">ProxyPass /images !<br>ProxyPass /css ! <br>ProxyPass /js !<br><br>ProxyPass / balancer://example/<br>&lt;Proxy balancer://example/&gt;<br>BalancerMember ajp://server1:8080/<br>BalancerMember ajp://server2:8080/<br>BalancerMember ajp://server3:8080/<br>&lt;/Proxy&gt;<br></pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>采用 proxy 的连接方式，需要在 Apache 上加载所需的模块，mod_proxy 相关的模块有
mod_proxy.so、mod_proxy_connect.so、mod_proxy_http.so、mod_proxy_ftp.so、
mod_proxy_ajp.so， 其中 mod_proxy_ajp.so 只在 Apache 2.2.x 中才有。如果是采用
http_proxy 方式则需要加载 mod_proxy.so 和 mod_proxy_http.so；如果是 ajp_proxy 则需要加载
mod_proxy.so 和 mod_proxy_ajp.so这两个模块。</p>
<div class="ibm-alternate-rule"><hr></div>
<p><a href="http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/#ibm-pcon" class="ibm-anchor-up-link">回页首</a></p>
<p><a name="N1010F">三者比较</a></p>
<p>相对于 JK 的连接方式，后两种在配置上是比较简单的，灵活性方面也一点都不逊色。但就稳定性而言就不像 JK 这样久经考验，毕竟
Apache 2.2.3 推出的时间并不长，采用这种连接方式的网站还不多，因此，如果是应用于关键的互联网网站，还是建议采用 JK 的连接方式。</p>
<br>
<p><a name="resources">参考资料 </a></p>
<ul>
    <li>
    获得 <a href="http://httpd.apache.org/">Apache Http Server</a>。
    <br><br></li>
    <li>
    获得 <a href="http://tomcat.apache.org/">Apache Tomcat</a>。
    <br><br></li>
    <li>
    <a href="http://tomcat.apache.org/connectors-doc/">JK 文档</a>。
    <br><br></li>
</ul>
<p><a name="author">关于作者</a></p>
<br> <img src ="http://www.cppblog.com/JonsenElizee/aggbug/134977.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-29 13:07 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/29/134977.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Such a Nice Java Music Player</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/25/134607.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 25 Nov 2010 03:44:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/25/134607.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/134607.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/25/134607.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/134607.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/134607.html</trackback:ping><description><![CDATA[Look for this kind of nice player long long time.<br>It's promoted by "QianLiBingFeng" and completed at 2008/08/02 with continuing mantenance on google code web site:<br><a href="http://code.google.com/p/yoyoplayer">http://code.google.com/p/yoyoplayer</a>
<br><br><br><br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/134607.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-25 11:44 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/25/134607.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Alizee</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/12/133454.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 12 Nov 2010 12:04:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/12/133454.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/133454.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/12/133454.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/133454.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/133454.html</trackback:ping><description><![CDATA[<a title="MTV"  href="http://www.yinyuetai.com/playlist/72411">MTV</a><br><br><br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/133454.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-12 20:04 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/12/133454.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Add Dictionary Pane on Any Webpage</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/10/133171.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 10 Nov 2010 00:54:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/10/133171.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/133171.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/10/133171.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/133171.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/133171.html</trackback:ping><description><![CDATA[save this line as a url:<br>javascript:void((function()%20{var%20element=document.createElement('script');%20element.setAttribute('src',%20'http://dict.cn/hc/init.php');%20document.body.appendChild(element);})())
<br><br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/133171.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-10 08:54 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/10/133171.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Assembly Language for x86 Processors, 6th edition</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/09/133106.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Tue, 09 Nov 2010 07:03:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/09/133106.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/133106.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/09/133106.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/133106.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/133106.html</trackback:ping><description><![CDATA[&nbsp;
<table border=0 cellSpacing=0 cellPadding=0 width="98%">
    <tbody>
        <tr>
            <td style="HEIGHT: 10px" colSpan=3></td>
        </tr>
        <tr>
            <td height=49 vAlign=center colSpan=3>
            <div class=BookTitle>Assembly Language for x86 Processors<span style="FONT-SIZE: 0.5em">, 6th edition</span> </div>
            </td>
        </tr>
        <tr>
            <td style="HEIGHT: 10px" colSpan=3></td>
        </tr>
        <tr>
            <td width=1></td>
            <td class=style16><a href="http://www.kipirvine.com/asm/images/cover6th_large.jpg" target=_blank><img border=0 alt="Book cover" align=left src="http://www.kipirvine.com/asm/images/cover6th_small.jpg" height=175></a></td>
            <td class=style15 vAlign=top>
            <table border=0 cellSpacing=0 cellPadding=0 width="100%">
                <tbody>
                    <tr>
                        <td width="92%">&nbsp; </td>
                        <td width="8%">&nbsp;</td>
                    </tr>
                    <tr>
                        <td height=31 width="92%">
                        <p><strong><em>by Kip Irvine, Florida International University</em></strong></p>
                        </td>
                        <td height=31 width="8%">&nbsp;</td>
                    </tr>
                    <tr>
                        <td class=style19 width="92%">
                        <p style="HEIGHT: 22px">ISBN: 0-13-602212-X<br></p>
                        </td>
                        <td class=style19 width="8%"></td>
                    </tr>
                    <tr>
                        <td class=style18 width="92%">
                        <p>Published by: Prentice-Hall (<a href="http://www.mypearsonstore.com/bookstore/product.asp?isbn=013602212X"><u><font color=#0000ff>Pearson Education</font></u></a>), <em>February 2010</em></p>
                        </td>
                        <td class=style18 width="8%"></td>
                    </tr>
                    <tr>
                        <td height=25 width="92%"><a href="http://www.kipirvine.com/asm/5th/index.htm"><u><font color=#0000ff>Visit the Web site for the Fifth Edition</font></u></a> </td>
                        <td height=25 width="8%">&nbsp;</td>
                    </tr>
                    <tr>
                        <td height=25 width="92%"><a class=slink onclick=addFav() href="http://www.kipirvine.com/asm/#"><u><font color=#0000ff>Bookmark this Web Site</font></u></a></td>
                        <td height=25 width="8%">&nbsp;</td>
                    </tr>
                </tbody>
            </table>
            </td>
            <td vAlign=center width=153><img border=1 alt="Picture of Kip Irvine" src="http://www.kipirvine.com/asm/images/Irvine3.jpg" width=116 height=144></td>
        </tr>
        <tr>
            <td height=6 colSpan=4>&nbsp;</td>
        </tr>
        <tr>
            <td bgColor=#a5d8f6 height=6 colSpan=4></td>
        </tr>
        <tr>
            <td height=6 colSpan=4></td>
        </tr>
        <tr>
            <td height=6 colSpan=4>
            <table class=HomePageTable cellSpacing=0 cellPadding=3>
                <tbody>
                    <tr>
                        <td class=style20><a href="http://www.pearsonhighered.com/irvine/" target=_blank><u><font color=#0000ff>Online Resources</font></u></a> <span style="FONT-SIZE: 0.8em">(pearsonhighered.com)</span></td>
                        <td class=style21></td>
                        <td class=style22><a href="http://www.kipirvine.com/asm/gettingStartedVS2010/index.htm"><u><font color=#0000ff>Getting started with MASM</font></u></a></td>
                        <td class=style23><u><font color=#0000ff></font></u></td>
                        <td class=style24><a href="http://www.kipirvine.com/asm/files/chapterObjectives.htm"><u><font color=#0000ff>Chapter objectives</font></u></a></td>
                    </tr>
                    <tr>
                        <td class=style8><a href="http://www.kipirvine.com/asm/editions.htm"><u><font color=#0000ff>International Editions</font></u></a></td>
                        <td class=style14>&nbsp;</td>
                        <td class=style6><a href="http://www.kipirvine.com/asm/videos.htm"><u><font color=#0000ff>Sample chapters and videos</font></u></a></td>
                        <td class=style13>&nbsp;</td>
                        <td><a class=normal href="http://www.kipirvine.com/asm/instructors.htm"><u><font color=#0000ff>Instructor resources</font></u></a></td>
                    </tr>
                    <tr>
                        <td class=style8><a href="http://www.amazon.com/Assembly-Language-x86-Processors-6th/dp/013602212X/ref=sr_1_7?ie=UTF8&amp;s=books&amp;qid=1269636175&amp;sr=1-7" target=_blank><u><font color=#0000ff>Buy at Amazon.com</font></u></a> or <a href="http://www.bookfinder.com/search/?keywords=0-13-602212-X&amp;st=sh&amp;ac=qr&amp;submit=" target=_blank><u><font color=#0000ff>Bookfinder.com</font></u></a></td>
                        <td class=style14>&nbsp;</td>
                        <td class=style6><a href="http://www.kipirvine.com/asm/debug/index.htm"><u><font color=#0000ff>Debugging tools</font></u></a></td>
                        <td class=style13>&nbsp;</td>
                        <td><a class=normal href="http://groups.yahoo.com/group/ASM_Irvine/" target=_blank><u><font color=#0000ff>Discussion group </font></u></a><span style="FONT-SIZE: 0.8em">(Yahoo.com)</span></td>
                    </tr>
                    <tr>
                        <td class=style8>&nbsp;</td>
                        <td class=style14>&nbsp;</td>
                        <td class=style6><a href="http://www.kipirvine.com/asm/examples/index.htm"><u><font color=#0000ff>Link libraries and example programs</font></u></a></td>
                        <td class=style13>&nbsp;</td>
                        <td><a href="http://www.kipirvine.com/asm/files/index.html"><u><font color=#0000ff>Supplemental files</font></u></a></td>
                    </tr>
                    <tr>
                        <td class=style25><a href="http://www.kipirvine.com/asm/othersites.htm"><u><font color=#0000ff>Links to Assembly Language sites</font></u></a></td>
                        <td class=style14>&nbsp;</td>
                        <td class=style6><a href="http://www.kipirvine.com/asm/articles/index.htm"><u><font color=#0000ff>Articles</font></u></a></td>
                        <td class=style13>&nbsp;</td>
                        <td style="FONT-SIZE: 0.8em" rowSpan=2>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class=style8><a href="http://www.kipirvine.com/asm/workbook/index.htm"><u><font color=#0000ff>Assembly language workbook</font></u></a></td>
                        <td class=style14>&nbsp;</td>
                        <td class=style6><a href="http://kipirvine.com/asm6errata/"><u><font color=#0000ff>Bug reports</font></u></a></td>
                        <td class=style13>&nbsp;</td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
        <tr vAlign=top>
            <td colSpan=4>
            <hr>
            </td>
        </tr>
    </tbody>
</table>
<table class=LowerTable border=0 cellSpacing=0 cellPadding=3 width="98%">
    <tbody>
        <tr>
            <td class=style26 colSpan=2><span class=style17>New feature!</span> This edition offers recorded videos by the author, working through solutions to selected programming exercises. </td>
        </tr>
        <tr>
            <td style="HEIGHT: 5px" colSpan=2></td>
        </tr>
        <tr>
            <td colSpan=2>Thanks to James Brink, Gerald Cahill, David Topham, and W.A. Barrett. John Taylor is the offical corrections manager. </td>
        </tr>
        <tr>
            <td class=style12 colSpan=2>
            <hr style="COLOR: gray">
            </td>
        </tr>
        <tr>
            <td style="TEXT-ALIGN: center; FONT-SIZE: 0.7em">Copyright 2010 Kip Irvine. All rights reserved.</td>
        </tr>
    </tbody>
</table>
<img src ="http://www.cppblog.com/JonsenElizee/aggbug/133106.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-09 15:03 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/09/133106.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Bash Reference</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132501.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 04 Nov 2010 15:06:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132501.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/132501.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132501.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/132501.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/132501.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: [Top]            [Contents]            [Index]            [ ? ]            Bash Reference ManualThis text is a brief description of the features that are present inthe Bash shell....&nbsp;&nbsp;<a href='http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132501.html'>阅读全文</a><img src ="http://www.cppblog.com/JonsenElizee/aggbug/132501.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-04 23:06 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132501.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux sed reference</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132500.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 04 Nov 2010 15:02:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132500.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/132500.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132500.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/132500.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/132500.html</trackback:ping><description><![CDATA[<a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/getnav/man/html.C/sed.C.html">
<h1>sed(C)</h1>
</a>
<hr>
<strong>sed -- <!--meta NM "sed"-->invoke the stream editor </strong><!--meta DN "invoke the stream editor"-->
<p>
</p>
<h2>Syntax</h2>
<strong>sed</strong> [ <strong>-n</strong> ] [ <em>script</em> ] [ <strong>-f</strong>
<em>sfile</em> ] [ <em>file</em> ... ]
<p><strong>sed</strong> [ <strong>-n</strong> ] [ <strong>-e</strong> <em>script</em> ] ... [ <strong>-f</strong>
<em>sfile</em> ] ... [ <em>file</em> ... ]
</p>
<h2>Description</h2>
The <strong>sed</strong> command copies the named <em>files</em>
(standard input default) to the standard output, edited according to a script of
commands.
<p><strong>sed</strong> takes the following options:
</p>
<dl compact="compact">
<p>
</p>
<dt><strong>-e</strong> <em>script</em>
</dt><dd>Read the command <em>script</em>; usually quoted to protect it from the
shell.
</dd><dt><strong>-f</strong> <em>sfile</em>
</dt><dd>Take the script from the file <em>sfile</em>; these options accumulate. If
there is just one <strong>-e</strong> option and no <strong>-f</strong> options, the flag
<strong>-e</strong> may be omitted.
</dd><dt><strong>-n</strong>
</dt><dd>Suppress the default output. </dd></dl>A <em>script</em> consists of editing
commands, one per line, of the following form: <br><br>[ <em>address</em> [ ,
<em>address</em> ] ] <em>function</em> [ <em>arguments</em> ]
<p>In normal operation, <strong>sed</strong> cyclically copies a line of input into a
pattern space (unless there is something left after a <strong>D</strong> command), applies
in sequence all commands whose addresses select that pattern space, and at the
end of the script copies the pattern space to the standard output (except under
<strong>-n</strong>) and deletes the pattern space.
</p>
<p>A semicolon ``;'' can be used as a command delimiter.
</p>
<p>Some of the commands use a hold space to save all or part of the pattern
space for subsequent retrieval (see the ``Limitations'' section).
</p>
<p>An address is either a decimal number that counts input lines cumulatively
across files, a ``$'' that addresses the last line of input, or a context
address, that is, a <em>/regular expression/</em> as described in <a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?regexp+M"><strong>regexp</strong>(M)</a>,
modified as follows:
</p>
<ul>
    <p>
    </p>
    <li>In a context address, the construction <em>\?regular expression?</em>, where
    ``?'' is any character, is identical to <em>/regular expression/</em>. Note that
    in the context address <em>\xabc\xdefx</em>, the second <em>x</em> stands for
    itself, so that the standard expression is <em>abcxdef</em>.
    </li>
    <li>The escape sequence \n matches a newline embedded in the pattern space.
    </li>
    <li>A dot (.) matches any character except the terminal newline of the pattern
    space.
    </li>
    <li>A command line with no addresses selects every pattern space.
    </li>
    <li>A command line with one address selects each pattern space that matches
    the address.
    </li>
    <li>A command line with two addresses separated by a comma selects the
    inclusive range from the first pattern space that matches the first address
    through the next pattern space that matches the second. (If the second address
    is a number less than or equal to the line number first selected, only one
    line is selected.) Thereafter, the process is repeated, looking again for the
    first address. </li>
</ul>
Editing commands can be applied only to nonselected
pattern spaces by use of the negation function ``!'' described in the next
section.
<h3>Functions</h3>
In the following list of functions, the maximum number of
permissible addresses for each function is indicated in parentheses.
<p>The <em>text</em> argument consists of one or more lines, all but the last of
which end with backslashes to hide the newlines. Backslashes in text are treated
like backslashes in the replacement string of an <strong>s</strong> command, and may be
used to protect initial blanks and tabs against the stripping that is done on
every script line.
</p>
<p>The <em>rfile</em> or <em>wfile</em> argument must terminate the command line and
must be preceded by one blank. Each <em>wfile</em> is created before processing
begins. There can be at most 10 distinct <em>wfile</em> arguments.
</p>
<dl compact="compact">
<p>
</p>
<dt>(1) <strong>a\</strong><br><em>text</em>
</dt><dd>Appends <em>text</em>, placing it on the output before reading the next
input line. Note that there must be a line break between the command and the
<em>text</em>.
</dd><dt>(2) <strong>b</strong> <em>label</em>
</dt><dd>Branches to the <strong>:</strong> command bearing the <em>label</em>. If <em>label</em>
is empty, branches to the end of the script.
</dd><dt>(2) <strong>c\</strong><br><em>text</em>
</dt><dd>Changes text by deleting the pattern space and then appending <em>text</em>.
With 0 or 1 address or at the end of a 2-address range, places <em>text</em> on
the output and starts the next cycle. Note that there must be a line break
between the command and the <em>text</em>.
</dd><dt>(2) <strong>d</strong>
</dt><dd>Deletes the pattern space and starts the next cycle.
</dd><dt>(2) <strong>D</strong>
</dt><dd>Deletes the initial segment of the pattern space through the first newline
and starts the next cycle.
</dd><dt>(2) <strong>g</strong>
</dt><dd>Replaces the contents of the pattern space with the contents of the hold
space.
</dd><dt>(2) <strong>G</strong>
</dt><dd>Appends the contents of the hold space to the pattern space.
</dd><dt>(2) <strong>h</strong>
</dt><dd>Replaces the contents of the hold space with the contents of the pattern
space.
</dd><dt>(2) <strong>H</strong>
</dt><dd>Appends the contents of the pattern space to the hold space.
</dd><dt>(1) <strong>i\</strong><br><em>text</em>
</dt><dd>Insert. Places <em>text</em> on the standard output. Note that there must be
a line break between the command and the <em>text</em>.
</dd><dt>(2) <strong>l</strong>
</dt><dd>Lists the pattern space on the standard output in an unambiguous way.
Nonprinting characters are displayed as a three digit octal number preceded by
a backslash ``\''. The following characters are printed as escape sequences:
<pre> -------------------------------------------<br> Character              Output<br> -------------------------------------------<br> backslash              \\<br> alert (bell)           \a<br> backspace              \b<br> form feed              \f<br> carriage return        \r<br> horizontal tab         \t<br> vertical tab           \v<br></pre>
Any output lines that are longer than the output device width
(determined by the environment variable COLUMNS) are folded into multiple
lines. New lines, inserted when folding a long line, are escaped by a
preceding backslash character. The ends of each line in the pattern space are
denoted by a dollar character ``$''.
</dd><dt>(2) <strong>n</strong>
</dt><dd>Copies the pattern space to the standard output. Replaces the pattern
space with the next line of input.
</dd><dt>(2) <strong>N</strong>
</dt><dd>Appends the next line of input to the pattern space with an embedded
newline. (The current line number changes.)
</dd><dt>(2) <strong>p</strong>
</dt><dd>Prints (copies) the pattern space on the standard output.
</dd><dt>(2) <strong>P</strong>
</dt><dd>Prints (copies) the initial segment of the pattern space through the first
newline to the standard output.
</dd><dt>(1) <strong>q</strong>
</dt><dd>Quits <strong>sed</strong> by branching to the end of the script. No new cycle is
started.
</dd><dt>(2) <strong>r</strong> <em>rfile</em>
</dt><dd>Reads the contents of <em>rfile</em> and places them on the output before
reading the next input line.
</dd><dt>(2) <strong>s</strong> <em>/regular expression/replacement/flags</em>
</dt><dd>Substitutes the <em>replacement</em> string for instances of the <em>regular
expression</em> in the pattern space. Any character may be used instead of
``/''. For a more detailed description, see <a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?regexp+M"><strong>regexp</strong>(M)</a>.
<p><em>Flags</em> is zero or more of:
</p>
<dl compact="compact">
<p>
</p>
<dt><em>n</em>
</dt><dd>Substitute for just the <em>n</em>th occurrence of the <em>regular
expression</em>. <em>n</em> must be an integer greater than zero.
</dd><dt><strong>g</strong>
</dt><dd>Globally substitutes for all non-overlapping instances of the <em>regular
expression</em> rather than just the first one.
</dd><dt><strong>p</strong>
</dt><dd>Prints the pattern space if a replacement was made.
</dd><dt><strong>w</strong> <em>wfile</em>
</dt><dd>Writes the pattern space to <em>wfile</em> if a replacement was made.
</dd></dl>
</dd><dt>(2) <strong>t</strong> <em>label</em>
</dt><dd>Branches to the colon (:) command bearing <em>label</em> if any
substitutions have been made since the most recent reading of an input line or
execution of a <strong>t</strong> command. If <em>label</em> is empty, <strong>t</strong> branches
to the end of the script.
</dd><dt>(2) <strong>w</strong> <em>wfile</em>
</dt><dd>Writes the pattern space to <em>wfile</em>.
</dd><dt>(2) <strong>x</strong>
</dt><dd>Exchanges the contents of the pattern and hold spaces.
</dd><dt>(2) <strong>y</strong> <em>/string1/string2/</em>
</dt><dd>Replaces all occurrences of characters in <em>string1</em> with the
corresponding characters in <em>string2</em>. The lengths of <em>string1</em> and
<em>string2</em> must be equal.
</dd><dt>(2) <strong>!</strong> <em>function</em>
</dt><dd>Applies the <em>function</em> (or group, if <em>function</em> is ``{'') only
to lines <em>not</em> selected by the address(es).
</dd><dt>(0) <strong>:</strong> <em>label</em>
</dt><dd>This command does nothing; it bears a <em>label</em> for <strong>b</strong> and
<strong>t</strong> commands to branch to. Labels can be at most 8 characters long.
</dd><dt>(1) <strong>=</strong>
</dt><dd>Places the current line number on the standard output as a line.
</dd><dt>(2) <strong>{</strong>
</dt><dd>Executes the following commands through a matching ``}'' only when the
pattern space is selected.
</dd><dt>(2) <strong>!{</strong>
</dt><dd>Executes the following commands through a matching ``}'' only when the
pattern space is not selected.
</dd><dt>(0)
</dt><dd>An empty command is ignored.
</dd><dt>(0) <strong>#</strong>
</dt><dd>Ignore the remainder of the line if <strong>#</strong> is followed by any other
character than ``n'' (treat the line as a comment); if the character ``n''
follows <strong>#</strong>, suppress the default output (equivalent to the command line
option <strong>-n</strong>). </dd></dl>
<h3>Environment variables</h3>
<dl compact="compact">
<p>
</p>
<dt>COLUMNS
</dt><dd>The width of the standard output device in characters; used by the
<strong>l</strong> command for folding long lines. If this variable is not set or it has
an invalid value, <strong>sed</strong> uses the default value 72. </dd></dl>
<h2>Exit values</h2>
<strong>sed</strong> continues to process all <em>file</em> arguments
even if one or more of them produces an open error. If there is an open error,
<strong>sed</strong> will exit with a value of 1 when it has finished processing the
files. A value of 2 indicates a usage error.
<h2>Examples</h2>
The following examples assume the use of <a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?sh+C"><strong>sh</strong>(C)</a>
or <a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?ksh+C"><strong>ksh</strong>(C)</a>.
<p>A common use of <strong>sed</strong> is to edit a file from within a shell script. In
this example, every occurrence of the string ``sysman'' in the file
<em>infile</em> is replaced by ``System Manager''. A temporary file TMP is used to
hold the intermediate result of the edit: </p>
<pre>   TMP=/usr/tmp/tmpfile_$$<br>   sed -e 's/sysman/System Manager/g' &lt; infile &gt; $TMP<br>   mv $TMP infile<br></pre>
In this example, <strong>sed</strong> removes all blank lines (including those with
just &lt;Tab&gt; and &lt;Space&gt; characters) from <em>padded_file</em>:
<pre>   sed '<br>   /^$/ d<br>   /^[&lt;Tab&gt;&lt;Space&gt;]*$/ d<br>   &#180; padded_file <br></pre>
<strong>sed</strong> can be used to strip all lines from a file which do not contain
a certain string. In this example, all lines in the file <em>infile</em> which
start with a hash ``#'' are echoed to the screen: <br><br><strong>sed -e '/^#/!d'
&lt; infile</strong>
<p>If several editing commands must be carried out on a file, but the parameters
for the edit are to be supplied by the user, then use <strong>echo</strong> to append
command lines to a <strong>sed</strong> script. The following example removes all
occurrences of the strings given as arguments to the script from the file
<em>infile</em>. The name of the temporary file is held by the variable SCRIPT: </p>
<pre>   SCRIPT=/usr/tmp/script_$$<br>
<p><br>   for name in $*<br>   do<br>   	echo "s/${name}//g" &gt;&gt; $SCRIPT<br>   done<br>   </p>
<p><br>   TMPFILE=/usr/tmp/tmpfile_$$<br>   </p>
<p><br>   sed -f $SCRIPT &lt; infile &gt; $TMPFILE<br>   </p>
<p><br>   mv $TMPFILE infile<br>   </p>
<p><br>   rm $SCRIPT<br></p>
</pre>
Another use of <strong>sed</strong> is to process the output from other
commands. Here the <strong>ps</strong> command is filtered using <strong>sed</strong> to report the
status of all processes other than those owned by the super user: <br><br><strong>ps
-ef | sed -e '/^[&lt;Space&gt;&lt;Tab&gt;]*root/d'</strong>
<p>
</p>
<h2>Limitations</h2>
Both the hold space and pattern space can hold a maximum of
8192 bytes. <br>
<h2>See also</h2>
<a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?awk+C"><strong>awk</strong>(C)</a>,
<a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?ed+C"><strong>ed</strong>(C)</a>,
<a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?grep+C"><strong>grep</strong>(C)</a>,
<a  href="http://osr5doc.ca.caldera.com:457/cgi-bin/man/man?regexp+M"><strong>regexp</strong>(M)</a>
<p><a  href="http://osr5doc.ca.caldera.com:457/OSUserG/_Manipulating_text_with_sed.html">Chapter
14, ``Manipulating text with sed''</a> in the <em>Operating System User's
Guide</em>
</p>
<h2>Standards conformance</h2>
<strong>sed</strong> is conformant with:
<p>ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System
Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
<br>AT&amp;T SVID Issue 2; <br>X/Open CAE Specification, Commands and Utilities,
Issue 4, 1992.
</p>
<p><em>SCO OpenServer Release 5.0.6 -- 1 August 2000</em>
<link href="http://www.oldlinux.org/man/html.C/CONTENTS.html" rel="contents">
<link href="http://www.oldlinux.org/cgi-bin/getnav/man/html.C/sed.C.html" rel="navigate">
<link href="http://www.oldlinux.org/cgi-bin/man/man?setcolor+C" rel="next">
<link href="http://www.oldlinux.org/cgi-bin/man/man?sdiff+C" rel="previous"><!--navigate
<titlegreaterthannavigational information for sed(C)</titlegreaterthan -->
<!--navigate
<h2greaterthannavigational information for sed(C)</h2greaterthan -->
<!--navigate
<ulgreaterthan --><!--navigate
<ligreaterthan --><!--navigate Book title: man(C) --><!--navigate
<ligreaterthan --><!--navigate Next topic:   --><!--navigate <a href="/cgi-bin/man/man?setcolor+CgReATerTHansetcolor(C)</agreaterthan" --><!--navigate
<ligreaterthan --><!--navigate Previous topic:   --><!--navigate <a href="/cgi-bin/man/man?sdiff+CgReATerTHansdiff(C)</agreaterthan" --><!--navigate
<ligreaterthan --><!--navigate <a href="/man/html.C/CONTENTS.htmlgReATerTHanTable" of contents</agreaterthan --><!--navigate </ulgreaterthan -->
<!--navigate <brgreaterthan --><!--navigate <brgreaterthan --><!--navigate <a href="/man/html.C/COPYRIGHT.htmlgReATerTHan&#169;" 2000 The Santa Cruz Operation, Inc.  All rights reserved.</agreaterthan --><!--navigate
<link href="/man/html.C/CONTENTS.html" rel="contentsgReATerTHan" --><!--navigate
<link href="/cgi-bin/man/man?setcolor+C" rel="nextgReATerTHan" --><!--navigate
<link href="/cgi-bin/man/man?sdiff+C" rel="previousgReATerTHan" --><!--navigate  --></p><img src ="http://www.cppblog.com/JonsenElizee/aggbug/132500.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-04 23:02 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132500.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C Lib Guide</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132499.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 04 Nov 2010 14:50:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132499.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/132499.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132499.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/132499.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/132499.html</trackback:ping><description><![CDATA[<center>
<h1>The C Library Reference Guide</h1>
<em>by Eric Huss</em><br>
&#169; Copyright 1997 Eric Huss<br>
<br>
Release 1
</center>
<hr size="5">
<pre><strong><a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/introduction.html">Introduction</a></strong><br><br>1.   <strong>Language</strong><br>     1.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.1.html">Characters</a><br>          1.1.1     Trigraph Characters<br>          1.1.2     Escape Sequences<br>          1.1.3     Comments<br>     1.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html">Identifiers</a><br>          1.2.1     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#keywords">Keywords</a><br>          1.2.2     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#variables">Variables</a><br>          1.2.3     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#enum">Enumerated Tags</a><br>          1.2.4     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#arrays">Arrays</a><br>          1.2.5     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#struct">Structures and Unions</a><br>          1.2.6     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#const">Constants</a><br>          1.2.7     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#strings">Strings</a><br>          1.2.8     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.2.html#sizeof">sizeof Keyword</a><br>     1.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.3.html">Functions</a><br>          1.3.1     Definition<br>          1.3.2     Program Startup<br>     1.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.4.html">References</a><br>          1.4.1     Pointers and the Address Operator<br>          1.4.2     Typecasting<br>     1.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.5.html">Operators</a><br>          1.5.1     Postfix<br>          1.5.2     Unary and Prefix<br>          1.5.3     Normal<br>          1.5.4     Boolean<br>          1.5.5     Assignment<br>          1.5.6     Precedence<br>     1.6  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html">Statements</a><br>          1.6.1     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#if">if</a><br>          1.6.2     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#switch">switch</a><br>          1.6.3     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#while">while</a>  <br>          1.6.4     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#do">do</a><br>          1.6.5     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#for">for</a><br>          1.6.6     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#goto">goto</a><br>          1.6.7     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#continue">continue</a><br>          1.6.8     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#break">break</a><br>          1.6.9     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.6.html#return">return</a><br>     1.7  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html">Preprocessing Directives</a><br>          1.7.1     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#conditional">#if, #elif, #else, #endif</a><br>          1.7.2     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#define">#define, #undef, #ifdef, #ifndef</a><br>          1.7.3     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#include">#include</a><br>          1.7.4     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#line">#line</a><br>          1.7.5     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#error">#error</a><br>          1.7.6     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#pragma">#pragma</a><br>          1.7.7     <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/1.7.html#macros">Predefined Macros</a><br>2.   <strong>Library</strong><br>     2.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.1.html">assert.h</a><br>          2.1.1     assert<br>     2.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.2.html">ctype.h</a><br>          2.2.1     is... Functions<br>          2.2.2     to... Functions<br>     2.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.3.html">errno.h</a> <br>          2.3.1     EDOM<br>          2.3.2     ERANGE<br>          2.3.3     errno   <br>     2.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.4.html">float.h</a><br>          2.4.1     Defined Values<br>     2.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.5.html">limits.h</a><br>          2.5.1     Defined Values<br>     2.6  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.6.html">locale.h</a><br>          2.6.1     Variables and Definitions<br>          2.6.2     setlocale<br>          2.6.3     localeconv<br>     2.7  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html">math.h</a><br>          2.7.1     Error Conditions<br>          2.7.2     Trigonometric Functions<br>               2.7.2.1   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#acos">acos</a><br>               2.7.2.2   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#asin">asin</a><br>               2.7.2.3   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#atan">atan</a><br>               2.7.2.4   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#atan2">atan2</a><br>               2.7.2.5   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#cos">cos</a><br>               2.7.2.6   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#cosh">cosh</a><br>               2.7.2.7   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#sin">sin</a><br>               2.7.2.8   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#sinh">sinh</a><br>               2.7.2.9   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#tan">tan</a><br>               2.7.2.10  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#tanh">tanh</a><br>          2.7.3     Exponential, Logarithmic, and Power Functions<br>               2.7.3.1   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#exp">exp</a><br>               2.7.3.2   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#frexp">frexp</a><br>               2.7.3.3   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#ldexp">ldexp</a><br>               2.7.3.4   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#log">log</a><br>               2.7.3.5   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#log10">log10</a><br>               2.7.3.6   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#modf">modf</a><br>               2.7.3.7   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#pow">pow</a><br>               2.7.3.8   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#sqrt">sqrt</a><br>          2.7.4     Other Math Functions<br>               2.7.4.1   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#ceil">ceil</a><br>               2.7.4.2   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#fabs">fabs</a><br>               2.7.4.3   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#floor">floor</a><br>               2.7.4.4   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.7.html#fmod">fmod</a><br>     2.8  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.8.html">setjmp.h</a><br>          2.8.1     Variables and Definitions<br>          2.8.2     setjmp<br>          2.8.3     longjmp<br>     2.9  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.9.html">signal.h</a><br>          2.9.1     Variables and Definitions<br>          2.9.2     signal<br>          2.9.3     raise<br>     2.10 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.10.html">stdarg.h</a><br>          2.10.1    Variables and Definitions<br>          2.10.2    va_start<br>          2.10.3    va_arg<br>          2.10.4    va_end<br>     2.11 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.11.html">stddef.h</a><br>          2.11.1    Variables and Definitions<br>     2.12 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html">stdio.h</a><br>          2.12.1    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#variables">Variables and Definitions</a><br>          2.12.2    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#streams">Streams and Files</a><br>          2.12.3    File Functions<br>               2.12.3.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#clearerr">clearerr</a><br>               2.12.3.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fclose">fclose</a><br>               2.12.3.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#feof">feof</a><br>               2.12.3.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#ferror">ferror</a><br>               2.12.3.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fflush">fflush</a><br>               2.12.3.6  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fgetpos">fgetpos</a><br>               2.12.3.7  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fopen">fopen</a><br>               2.12.3.8  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fread">fread</a><br>               2.12.3.9  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#freopen">freopen</a><br>               2.12.3.10 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fseek">fseek</a><br>               2.12.3.11 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fsetpos">fsetpos</a><br>               2.12.3.12 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#ftell">ftell</a><br>               2.12.3.13 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fwrite">fwrite</a><br>               2.12.3.14 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#remove">remove</a><br>               2.12.3.15 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#rename">rename</a><br>               2.12.3.16 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#rewind">rewind</a><br>               2.12.3.17 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#setbuf">setbuf</a><br>               2.12.3.18 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#setvbuf">setvbuf</a><br>               2.12.3.19 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#tmpfile">tmpfile</a><br>               2.12.3.20 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#tmpnam">tmpnam</a><br>          2.12.4    Formatted I/O Functions<br>               2.12.4.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#printf">...printf Functions</a><br>               2.12.4.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#scanf">...scanf Functions</a><br>          2.12.5    Character I/O Functions<br>               2.12.5.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fgetc">fgetc</a><br>               2.12.5.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fgets">fgets</a><br>               2.12.5.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fputc">fputc</a><br>               2.12.5.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#fputs">fputs</a><br>               2.12.5.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#getc">getc</a><br>               2.12.5.6  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#getchar">getchar</a><br>               2.12.5.7  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#gets">gets</a><br>               2.12.5.8  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#putc">putc</a><br>               2.12.5.9  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#putchar">putchar</a><br>               2.12.5.10 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#puts">puts</a><br>               2.12.5.11 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#ungetc">ungetc</a><br>          2.12.7    Error Functions<br>               2.12.7.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.12.html#perror">perror</a><br>     2.13 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html">stdlib.h</a><br>          2.13.1    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#variables">Variables and Definitions</a><br>          2.13.2    String Functions<br>               2.13.2.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#atof">atof</a><br>               2.13.2.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#atoi">atoi</a><br>               2.13.2.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#atol">atol</a><br>               2.13.2.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#strtod">strtod</a><br>               2.13.2.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#strtol">strtol</a><br>               2.13.2.6  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#strtoul">strtoul</a><br>          2.13.3    Memory Functions<br>               2.13.3.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#calloc">calloc</a><br>               2.13.3.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#free">free</a><br>               2.13.3.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#malloc">malloc</a><br>               2.13.3.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#realloc">realloc</a><br>          2.13.4    Environment Functions<br>               2.13.4.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#abort">abort</a><br>               2.13.4.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#atexit">atexit</a><br>               2.13.4.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#exit">exit</a><br>               2.13.4.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#getenv">getenv</a><br>               2.13.4.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#system">system</a><br>          2.13.5    Searching and Sorting Functions<br>               2.13.5.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#bsearch">bsearch</a><br>               2.13.5.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#qsort">qsort</a><br>          2.13.6    Math Functions  <br>               2.13.6.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#abs">abs</a><br>               2.13.6.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#div">div</a><br>               2.13.6.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#labs">labs</a><br>               2.13.6.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#ldiv">ldiv</a><br>               2.13.6.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#rand">rand</a><br>               2.13.6.6  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#srand">srand</a><br>          2.13.7    Multibyte Functions<br>               2.13.7.1  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#mblen">mblen</a><br>               2.13.7.2  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#mbstowcs">mbstowcs</a><br>               2.13.7.3  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#mbtowc">mbtowc</a><br>               2.13.7.4  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#wcstombs">wcstombs</a><br>               2.13.7.5  <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.13.html#wctomb">wctomb</a><br>     2.14 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html">string.h</a><br>          2.14.1    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#variables">Variables and Definitions</a><br>          2.14.2    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#memchr">memchr</a><br>          2.14.3    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#memcmp">memcmp</a><br>          2.14.4    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#memcpy">memcpy</a><br>          2.14.5    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#memmove">memmove</a><br>          2.14.6    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#memset">memset</a><br>          2.14.7    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strcat">strcat</a><br>          2.14.8    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strncat">strncat</a><br>          2.14.9    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strchr">strchr</a><br>          2.14.10   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strcmp">strcmp</a><br>          2.14.11   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strncmp">strncmp</a><br>          2.14.12   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strcoll">strcoll</a><br>          2.14.13   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strcpy">strcpy</a><br>          2.14.14   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strncpy">strncpy</a><br>          2.14.15   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strcspn">strcspn</a><br>          2.14.16   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strerror">strerror</a><br>          2.14.17   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strlen">strlen</a><br>          2.14.18   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strpbrk">strpbrk</a><br>          2.14.19   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strrchr">strrchr</a><br>          2.14.20   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strspn">strspn</a><br>          2.14.21   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strstr">strstr</a><br>          2.14.22   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strtok">strtok</a><br>          2.14.23   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.14.html#strxfrm">strxfrm</a><br>     2.15 <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html">time.h</a> <br>          2.15.1    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#variables">Variables and Definitions</a><br>          2.15.2    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#asctime">asctime</a><br>          2.15.3    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#clock">clock</a><br>          2.15.4    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#ctime">ctime</a><br>          2.15.5    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#difftime">difftime</a><br>          2.15.6    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#gmtime">gmtime</a><br>          2.15.7    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#localtime">localtime</a><br>          2.15.8    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#mktime">mktime</a><br>          2.15.9    <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#strftime">strftime</a><br>          2.15.10   <a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/2.15.html#time">time</a><br><strong>Appendix A</strong><br>	<a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/a.html">ASCII Chart</a><br><strong>Index</strong><br>	<a  href="http://www.oldlinux.org/Linux.old/Ref-docs/c_lib_guide/index2.html">Index</a><br></pre>
<hr>
<center>
<font size="-1">Questions, comments, or error reports?  Please send them to
<a  href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#101;&#45;&#104;&#117;&#115;&#115;&#64;&#117;&#105;&#117;&#99;&#46;&#101;&#100;&#117;">Eric Huss</a>
</font></center><img src ="http://www.cppblog.com/JonsenElizee/aggbug/132499.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-04 22:50 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132499.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Greatest Common Factor</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132437.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 04 Nov 2010 06:48:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132437.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/132437.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/132437.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/132437.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>unsigned&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;greatest_common_factor(&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;M,&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;N&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;Rem;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(&nbsp;N&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rem&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;M&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;N;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;M&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;N;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Rem;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;M;&nbsp;<br>}<br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()&nbsp;<br>{&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;temp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;a,b;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a);&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">b);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">the&nbsp;greatest&nbsp;common&nbsp;factor&nbsp;of&nbsp;%d&nbsp;and&nbsp;%d&nbsp;is&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">,a,b);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,greatest_common_factor(a,b));<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;getchar();<br>}</span></div>
this is a demo from ischarles.<br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/132437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-04 14:48 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>pow(x, n)</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132436.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 04 Nov 2010 06:35:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132436.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/132436.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132436.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/132436.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/132436.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;pow(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;x,&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n)<br>{<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;p&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">&nbsp;(n&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(n&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;p&nbsp;</span><span style="color: #000000;">*=</span><span style="color: #000000;">&nbsp;x;<br>&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000;">*=</span><span style="color: #000000;">&nbsp;x;<br>&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</span><span style="color: #000000;">&gt;&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;p;<br>}<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;">this&nbsp;is&nbsp;a&nbsp;demo&nbsp;from&nbsp;micheal</span></div>
<br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/132436.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-04 14:35 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/04/132436.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Pthread Tutorial</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/11/01/131984.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Mon, 01 Nov 2010 06:20:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/11/01/131984.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/131984.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/11/01/131984.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/131984.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/131984.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;                                                                                                                                    addNavigation()                         ...&nbsp;&nbsp;<a href='http://www.cppblog.com/JonsenElizee/archive/2010/11/01/131984.html'>阅读全文</a><img src ="http://www.cppblog.com/JonsenElizee/aggbug/131984.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-11-01 14:20 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/11/01/131984.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How Many Times Is Needed to Pick Out The Bad Ball</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/29/131709.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 29 Oct 2010 01:21:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/29/131709.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/131709.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/29/131709.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/131709.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/131709.html</trackback:ping><description><![CDATA[There are y ball with a bad one between them.<br>all the ball are the same look.<br>now, give you a balance, how many times do you need to pick out the bad one?<br><br>My resolution: need Ball(y) times to pick out the bad one.<br><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">math.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;Ball(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(y&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">+</span><span style="color: #000000;">Ball(&nbsp;y</span><span style="color: #000000;">/</span><span style="color: #000000;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;y&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;y</span><span style="color: #000000;">/</span><span style="color: #000000;">3</span><span style="color: #000000;">&nbsp;:&nbsp;y</span><span style="color: #000000;">/</span><span style="color: #000000;">3</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;);<br>}<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">200</span><span style="color: #000000;">;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">Ball(%3d)=%d&nbsp;\t</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;i,&nbsp;Ball(i));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(i</span><span style="color: #000000;">%</span><span style="color: #000000;">5</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;puts(</span><span style="color: #000000;">""</span><span style="color: #000000;">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="color: #000000;">""</span><span style="color: #000000;">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>}<br></span></div>
<br><br><br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/131709.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-29 09:21 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/29/131709.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Find Median(s) in Billion data with Memory 4M</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/28/131644.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 28 Oct 2010 08:39:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/28/131644.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/131644.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/28/131644.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/131644.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/131644.html</trackback:ping><description><![CDATA[Issue:<br>内存只能容纳100W个数,<br>现在有1亿数,混序,<br>然后求这一亿个数的中位数,<br>中位数(就是一个有序数组的中间数字,数组长度为偶数就是两个,奇数则只有一个)
<br><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">/*</span><span style="color: #008000;">***************************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;this&nbsp;program&nbsp;should&nbsp;find&nbsp;out&nbsp;the&nbsp;median(s)&nbsp;in&nbsp;N(logN)/(logB)&nbsp;no&nbsp;matter&nbsp;there<br>/*&nbsp;is&nbsp;duplicated&nbsp;data&nbsp;or&nbsp;not.<br>/*<br>/*&nbsp;with&nbsp;memory&nbsp;limitation:&nbsp;4MB&nbsp;memory&nbsp;available. in fact, it can work on 1MB.<br>/*&nbsp;I&nbsp;would&nbsp;like&nbsp;to&nbsp;express&nbsp;the&nbsp;idea&nbsp;with&nbsp;code.<br>/*&nbsp;here&nbsp;is&nbsp;just&nbsp;the&nbsp;pseudocode&nbsp;with&nbsp;out&nbsp;any&nbsp;optimization&nbsp;for&nbsp;easy&nbsp;understanding.<br>/*&nbsp;<br>/*&nbsp;maybe,&nbsp;there&nbsp;is&nbsp;no&nbsp;need&nbsp;to&nbsp;check&nbsp;sum&nbsp;==&nbsp;N/2&nbsp;or&nbsp;N/2+1,&nbsp;but&nbsp;this&nbsp;will&nbsp;get&nbsp;the&nbsp;<br>/*&nbsp;median(s)&nbsp;as&nbsp;soon&nbsp;as&nbsp;possible&nbsp;with&nbsp;out&nbsp;more&nbsp;recursive&nbsp;invoking.<br>/*<br>/*&nbsp;sorry&nbsp;for&nbsp;any&nbsp;logical&nbsp;problem.&nbsp;please&nbsp;let&nbsp;me&nbsp;know&nbsp;if&nbsp;you&nbsp;found&nbsp;any.<br>/****************************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br><br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;N&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">100000000</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;total&nbsp;data&nbsp;number&nbsp;is&nbsp;N</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;B&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1000000</span><span style="color: #000000;">;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;number&nbsp;of&nbsp;buckets.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;min,&nbsp;max;<br>scan&nbsp;data&nbsp;from&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;to&nbsp;N</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;min&nbsp;and&nbsp;max;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;O(N);</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br>// num the data number that is less than min<br>// min the minimum data in the target data range to find median(s)<br>// max the maximum data in the target data range to find median(s)<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;find_median(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;num</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;min,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;max)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(min&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;max)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(N</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;medians&nbsp;are&nbsp;min&nbsp;and&nbsp;max;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;print&nbsp;median&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;min;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;show&nbsp;you&nbsp;the&nbsp;result</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;exit</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;"> count&nbsp;all&nbsp;the&nbsp;data&nbsp;in&nbsp;O(N)</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;m&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;max</span><span style="color: #000000;">-</span><span style="color: #000000;">min&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;B&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;"> ((max-min)%B ? (max-min)/B + 1 : (max-min)/B)</span><span style="color: #000000;"> :&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;cnt[B]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{</span><span style="color: #000000;">0</span><span style="color: #000000;">};&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;count&nbsp;the&nbsp;data&nbsp;read.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">EOF)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;read_data()</span><span style="color: #000000;">-</span><span style="color: #000000;">min;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // count data in this range [min, max]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // data/m will get the position of data to increase the cnt[?] value<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;min&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;max)&nbsp;cnt[data</span><span style="color: #000000;">/</span><span style="color: #000000;">m]</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;sum&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;num,&nbsp;median_1,&nbsp;median_2,&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(sum&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;N</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">)&nbsp;sum&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;cnt[i</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000;">--</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;median&nbsp;is&nbsp;in&nbsp;the&nbsp;range&nbsp;of&nbsp;[i*m,&nbsp;(i+1)*m-1]</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;median(s)&nbsp;when&nbsp;sum&nbsp;is&nbsp;N/2&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(sum&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;N</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(N</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">)&nbsp;{&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;N&nbsp;is&nbsp;even&nbsp;and&nbsp;there&nbsp;are&nbsp;two&nbsp;medians.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(i</span><span style="color: #000000;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">m</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">*</span><span style="color: #000000;">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">EOF)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;read_data()</span><span style="color: #000000;">-</span><span style="color: #000000;">min;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data</span><span style="color: #000000;">/</span><span style="color: #000000;">m&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;i)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;median_2&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;median_2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data</span><span style="color: #000000;">/</span><span style="color: #000000;">m&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;median_1&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;median_1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pintf&nbsp;medians&nbsp;are&nbsp;median_1&nbsp;and&nbsp;median_2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;N&nbsp;is&nbsp;an&nbsp;odd&nbsp;number&nbsp;and&nbsp;there&nbsp;is&nbsp;one&nbsp;median.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; median_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(i</span><span style="color: #000000;">+</span><span style="color: #000000;">2</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">m</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">EOF)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;read_data();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data</span><span style="color: #000000;">/</span><span style="color: #000000;">m&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;median_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;median_1&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;median_1);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; print&nbsp;median&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;median_1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;median(s)&nbsp;when&nbsp;sum&nbsp;is&nbsp;N/2+1&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(sum&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;N</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(N</span><span style="color: #000000;">%</span><span style="color: #000000;">2</span><span style="color: #000000;">)&nbsp;{&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;N&nbsp;is&nbsp;even&nbsp;and&nbsp;there&nbsp;are&nbsp;two&nbsp;medians.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">*</span><span style="color: #000000;">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">*</span><span style="color: #000000;">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">EOF)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;read_data();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data</span><span style="color: #000000;">/</span><span style="color: #000000;">m&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;i)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;max;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;median_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;median_2&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;median_2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pintf&nbsp;medians&nbsp;are&nbsp;median_1&nbsp;and&nbsp;median_2;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; median_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">*</span><span style="color: #000000;">m;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;get&nbsp;(N/2+1)th&nbsp;data.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">EOF)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;read_data();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data</span><span style="color: #000000;">/</span><span style="color: #000000;">m&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;i)&nbsp;median_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;median_2&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;median_2);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; print&nbsp;median&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;median_2;<br>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;"> if sum is not N/2 or (N/2 + 1), recursively&nbsp;to&nbsp;find&nbsp;out&nbsp;the&nbsp;median(s)</span><span style="color: #008000;"></span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;min&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">m</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;max&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">*</span><span style="color: #000000;">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;get&nbsp;min&nbsp;and&nbsp;max&nbsp;for&nbsp;next&nbsp;processing</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">EOF)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;data&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;read_data()</span><span style="color: #000000;">-</span><span style="color: #000000;">min;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(data</span><span style="color: #000000;">/</span><span style="color: #000000;">m&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;min&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;min);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;max&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(data&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;max&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;data&nbsp;:&nbsp;max);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;find_median(sum,&nbsp;min,&nbsp;max);<br>}<br><br></span></div>
<br>  <img src ="http://www.cppblog.com/JonsenElizee/aggbug/131644.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-28 16:39 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/28/131644.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Kth Star</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/27/131505.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 27 Oct 2010 07:18:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/27/131505.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/131505.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/27/131505.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/131505.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/131505.html</trackback:ping><description><![CDATA[ISSUE: 输入上百万个行星的位置， 求距离第K近的两个行星。
<br><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;"><br>Precondition:<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">.&nbsp;star&nbsp;field&nbsp;simulation&nbsp;graph&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;planar.<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">.&nbsp;the&nbsp;coordinate&nbsp;of&nbsp;star&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;(x,&nbsp;y)&nbsp;that&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;treated&nbsp;</span><span style="color: #0000ff;">as</span><span style="color: #000000;">&nbsp;a&nbsp;point。<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">.&nbsp;N&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1000000</span><span style="color: #000000;"><br><br></span><span style="color: #000000;">1</span><span style="color: #000000;">.&nbsp;bucket</span><span style="color: #000000;">-</span><span style="color: #000000;">sort&nbsp;all&nbsp;points&nbsp;according&nbsp;to&nbsp;x</span><span style="color: #000000;">-</span><span style="color: #000000;">coordinate.&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;B&nbsp;buckets.<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">is</span><span style="color: #000000;">&nbsp;will&nbsp;completed&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;O(NlogN).<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;bucket&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;num;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;number&nbsp;of&nbsp;points&nbsp;in&nbsp;this&nbsp;bucket.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;point</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;points;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;points&nbsp;in&nbsp;this&nbsp;bucket.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;value&nbsp;of&nbsp;x-coordinate.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;bck[B];&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;B&nbsp;buckets&nbsp;got.</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #000000;">2</span><span style="color: #000000;">.&nbsp;<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;distance&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;point&nbsp;p1;<br>&nbsp;&nbsp;&nbsp;&nbsp;point&nbsp;p2;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;d;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;distance&nbsp;between&nbsp;p1&nbsp;and&nbsp;p2.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;distance&nbsp;kdis[K];&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;to&nbsp;record&nbsp;K&nbsp;small&nbsp;distance.&nbsp;and&nbsp;it's&nbsp;a&nbsp;eliminating-tree.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;kdis[</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;to&nbsp;K</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;B</span><span style="color: #000000;">-</span><span style="color: #000000;">2</span><span style="color: #000000;">])&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;O(B)</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;check&nbsp;bck[i]&nbsp;and&nbsp;bck[i+1]</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(bck[i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">].x&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;bck[i].x&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;kdis[K</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">].d&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;kdis[K</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;there&nbsp;is&nbsp;no&nbsp;need&nbsp;to&nbsp;check&nbsp;these&nbsp;two&nbsp;buckets.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">continue</span><span style="color: #000000;">;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;save&nbsp;time.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;point</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;poi&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;bck[i].points;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;bck.num</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">])&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;O(N/B)</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;point&nbsp;p&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;poi[j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find&nbsp;K&nbsp;points&nbsp;in&nbsp;bck[i+1]&nbsp;near&nbsp;to&nbsp;p<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;binary&nbsp;searching&nbsp;method&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;according&nbsp;to&nbsp;p.y.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kp[K];&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;K&nbsp;points&nbsp;got&nbsp;in&nbsp;bck[i+1]</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(m&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;[</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;K</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">])&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;O(K)</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distance&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;get_distance(kp[m],&nbsp;p);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(tmp.d&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;kdis[K</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">].d)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kdis[K</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;adjust&nbsp;kdis[K-1],&nbsp;for&nbsp;it's&nbsp;a&nbsp;eliminating&nbsp;tree.</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;finally,&nbsp;the&nbsp;kdis[K-1]&nbsp;is&nbsp;the&nbsp;kth&nbsp;distance&nbsp;in&nbsp;this&nbsp;star&nbsp;graph.<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;the&nbsp;whole&nbsp;processing&nbsp;will&nbsp;be&nbsp;completed&nbsp;in&nbsp;O(NlogN)&nbsp;+&nbsp;O(B*N/B*K).<br>&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;and&nbsp;SC&nbsp;is&nbsp;O(N)&nbsp;+&nbsp;O(K)&nbsp;=&nbsp;O(N).</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br>HOW&nbsp;TO&nbsp;FIND&nbsp;K&nbsp;POINTS</span><span style="color: #000000;">?</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;find&nbsp;K&nbsp;points&nbsp;in&nbsp;bck[i+1]&nbsp;near&nbsp;to&nbsp;p<br>&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;binary&nbsp;searching&nbsp;method&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;according&nbsp;to&nbsp;p.y.<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>it</span><span style="color: #000000;">'</span><span style="color: #000000;">s&nbsp;not&nbsp;complecated!&nbsp;and&nbsp;could&nbsp;be&nbsp;found&nbsp;in&nbsp;O(log(N/B)).</span><span style="color: #000000;"><br></span></div>
<br><br> <img src ="http://www.cppblog.com/JonsenElizee/aggbug/131505.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-27 15:18 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/27/131505.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Get number of 1 from 1 to n</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131081.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Sun, 24 Oct 2010 11:04:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131081.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/131081.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131081.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/131081.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/131081.html</trackback:ping><description><![CDATA[Here is a simple implementation for this issue. And it's TC is O(n/10), O(n);<br>Any better one is expected.<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;onenumber(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n)<br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;bak&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;n,&nbsp;hig&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hig&nbsp;</span><span style="color: #000000;">*=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</span><span style="color: #000000;">/=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;bak</span><span style="color: #000000;">-</span><span style="color: #000000;">hig</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;((bak</span><span style="color: #000000;">-</span><span style="color: #000000;">hig&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;:&nbsp;(bak</span><span style="color: #000000;">-</span><span style="color: #000000;">hig&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;:&nbsp;onenumber(bak</span><span style="color: #000000;">-</span><span style="color: #000000;">hig)));<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;(hig</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;:&nbsp;onenumber(hig</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(bak&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;n</span><span style="color: #000000;">*</span><span style="color: #000000;">hig&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;:&nbsp;(bak&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;n</span><span style="color: #000000;">*</span><span style="color: #000000;">hig&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;:&nbsp;onenumber(bak&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;n</span><span style="color: #000000;">*</span><span style="color: #000000;">hig)));<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;(n</span><span style="color: #000000;">*</span><span style="color: #000000;">hig</span><span style="color: #000000;"></span><span style="color: #000000;"></span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">9</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;:&nbsp;onenumber(n</span><span style="color: #000000;">*</span><span style="color: #000000;"></span><span style="color: #000000;"></span><span style="color: #000000;"></span><span style="color: #000000;"></span><span style="color: #000000;"></span><span style="color: #000000;">hig</span><span style="color: #000000;">-</span><span style="color: #000000;"></span><span style="color: #000000;">1</span><span style="color: #000000;">));<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;num;<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">}</span></div>
<br>   <img src ="http://www.cppblog.com/JonsenElizee/aggbug/131081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-24 19:04 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Find kth in O(n) TC</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131025.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Sat, 23 Oct 2010 17:17:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131025.html</guid><description><![CDATA[<code><span style="color: #000000;"><font face="NSimsun"><span style="color: #0000cc;"><br><a title="Refer find kth number in o(n) tc" href="http://hi.baidu.com/beibeiboo/blog/item/b280f88bb0ea02dafc1f101c.html">Refer Find kth number in O(n) TC</a><br><br>#</span><span style="color: #ff0000;">include</span> <span style="color: #0000cc;">&lt;</span>stdio<span style="color: #0000cc;">.</span>h<span style="color: #0000cc;">&gt;</span><br>
<span style="color: #0000cc;">#</span><span style="color: #ff0000;">include</span> <span style="color: #0000cc;">&lt;</span>stdlib<span style="color: #0000cc;">.</span>h<span style="color: #0000cc;">&gt;</span><br>
<span style="color: #0000cc;">#</span><span style="color: #ff0000;">include</span> <span style="color: #0000cc;">&lt;</span><span style="color: #ff0000;">time</span><span style="color: #0000cc;">.</span>h<span style="color: #0000cc;">&gt;</span><br>
<br>
<span style="color: #0000cc;">#</span><span style="color: #ff0000;">define</span> N 20<br>
<br>
<span style="color: #0000ff;">void</span> init_a<span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span> A<span style="color: #0000cc;">[</span><span style="color: #0000cc;">]</span><span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">int</span> i <span style="color: #0000cc;">=</span> 0<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">for</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">;</span>i<span style="color: #0000cc;">&lt;</span>N<span style="color: #0000cc;">;</span>i<span style="color: #0000cc;">+</span><span style="color: #0000cc;">+</span><span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp; A<span style="color: #0000cc;">[</span>i<span style="color: #0000cc;">]</span> <span style="color: #0000cc;">=</span> <span style="color: #ff0000;">rand</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">%</span>100<span style="color: #0000cc;">;</span> <br>
<span style="color: #0000cc;">}</span><br>
<br>
<span style="color: #0000ff;">void</span> print_array<span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span> A<span style="color: #0000cc;">[</span><span style="color: #0000cc;">]</span><span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> i <span style="color: #0000cc;">=</span> 0<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">for</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">;</span>i<span style="color: #0000cc;">&lt;</span>N<span style="color: #0000cc;">;</span>i<span style="color: #0000cc;">+</span><span style="color: #0000cc;">+</span><span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span><span style="color: #0000cc;">(</span>i<span style="color: #0000cc;">=</span><span style="color: #0000cc;">=</span>9<span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff0000;">printf</span><span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"%d\n"</span><span style="color: #0000cc;">,</span>A<span style="color: #0000cc;">[</span>i<span style="color: #0000cc;">]</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">else</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff0000;">printf</span><span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"%d\t"</span><span style="color: #0000cc;">,</span>A<span style="color: #0000cc;">[</span>i<span style="color: #0000cc;">]</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">}</span> <br>
<span style="color: #0000cc;">}</span><br>
<br>
<span style="color: #0000ff;">void</span> <span style="color: #ff0000;">swap</span><span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span><span style="color: #0000cc;">*</span> a<span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span><span style="color: #0000cc;">*</span> b<span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> tmp <span style="color: #0000cc;">=</span> <span style="color: #0000cc;">*</span>a<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">*</span>a <span style="color: #0000cc;">=</span> <span style="color: #0000cc;">*</span>b<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">*</span>b <span style="color: #0000cc;">=</span> tmp<span style="color: #0000cc;">;</span> <br>
<span style="color: #0000cc;">}</span><br>
<br>
<span style="color: #0000ff;">int</span> rand_partition<span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span> A<span style="color: #0000cc;">[</span><span style="color: #0000cc;">]</span><span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> start<span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> end<span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> i <span style="color: #0000cc;">=</span> start<span style="color: #0000cc;">-</span>1<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> j <span style="color: #0000cc;">=</span> start<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> num <span style="color: #0000cc;">=</span> <span style="color: #ff0000;">rand</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">%</span><span style="color: #0000cc;">(</span>end<span style="color: #0000cc;">-</span>start<span style="color: #0000cc;">+</span>1<span style="color: #0000cc;">)</span><span style="color: #0000cc;">+</span>start<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #ff0000;">swap</span><span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">+</span>num<span style="color: #0000cc;">,</span>A<span style="color: #0000cc;">+</span>end<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> x <span style="color: #0000cc;">=</span> A<span style="color: #0000cc;">[</span>end<span style="color: #0000cc;">]</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">for</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">;</span>j<span style="color: #0000cc;">&lt;</span>end<span style="color: #0000cc;">;</span>j<span style="color: #0000cc;">+</span><span style="color: #0000cc;">+</span><span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span><span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">[</span>j<span style="color: #0000cc;">]</span><span style="color: #0000cc;">&lt;</span><span style="color: #0000cc;">=</span>x<span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i<span style="color: #0000cc;">+</span><span style="color: #0000cc;">+</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff0000;">swap</span><span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">+</span>i<span style="color: #0000cc;">,</span>A<span style="color: #0000cc;">+</span>j<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">}</span> <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">}</span><br>
&nbsp;&nbsp;&nbsp;<span style="color: #ff0000;">swap</span><span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">+</span>i<span style="color: #0000cc;">+</span>1<span style="color: #0000cc;">,</span>A<span style="color: #0000cc;">+</span>end<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
<br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> i<span style="color: #0000cc;">+</span>1<span style="color: #0000cc;">;</span><br>
<span style="color: #0000cc;">}</span><br>
<br>
<br>
<span style="color: #0000ff;">int</span> random_select<span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span> A<span style="color: #0000cc;">[</span><span style="color: #0000cc;">]</span><span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> start<span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> end<span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> k<span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">int</span> i <span style="color: #0000cc;">=</span> 0<span style="color: #0000cc;">;</span> <br>
&nbsp;&nbsp;<span style="color: #0000ff;">if</span><span style="color: #0000cc;">(</span>start <span style="color: #0000cc;">=</span><span style="color: #0000cc;">=</span> end<span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> A<span style="color: #0000cc;">[</span>start<span style="color: #0000cc;">]</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;<span style="color: #0000ff;">int</span> q <span style="color: #0000cc;">=</span> rand_partition<span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">,</span> start<span style="color: #0000cc;">,</span> end<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp; i <span style="color: #0000cc;">=</span> q<span style="color: #0000cc;">-</span>start<span style="color: #0000cc;">+</span>1<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;<span style="color: #0000ff;">if</span><span style="color: #0000cc;">(</span>i <span style="color: #0000cc;">=</span><span style="color: #0000cc;">=</span> k<span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> A<span style="color: #0000cc;">[</span>q<span style="color: #0000cc;">]</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #0000cc;">(</span>i<span style="color: #0000cc;">&lt;</span>k<span style="color: #0000cc;">)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> random_select<span style="color: #0000cc;">(</span> A<span style="color: #0000cc;">,</span> q<span style="color: #0000cc;">+</span>1<span style="color: #0000cc;">,</span> end<span style="color: #0000cc;">,</span> k<span style="color: #0000cc;">-</span>i<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">else</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> random_select<span style="color: #0000cc;">(</span> A<span style="color: #0000cc;">,</span> start<span style="color: #0000cc;">,</span> q<span style="color: #0000cc;">-</span>1<span style="color: #0000cc;">,</span> k<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span> <br>
<span style="color: #0000cc;">}</span> <br>
<br>
<span style="color: #0000ff;">void</span> quick_sort<span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span> A<span style="color: #0000cc;">[</span><span style="color: #0000cc;">]</span><span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> start<span style="color: #0000cc;">,</span> <span style="color: #0000ff;">int</span> end<span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">if</span><span style="color: #0000cc;">(</span>start<span style="color: #0000cc;">&lt;</span>end<span style="color: #0000cc;">)</span> <br>
&nbsp;&nbsp;&nbsp;<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">int</span> q <span style="color: #0000cc;">=</span> rand_partition<span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">,</span> start<span style="color: #0000cc;">,</span> end<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quick_sort<span style="color: #0000cc;">(</span> A<span style="color: #0000cc;">,</span> q<span style="color: #0000cc;">+</span>1<span style="color: #0000cc;">,</span> end<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; quick_sort<span style="color: #0000cc;">(</span> A<span style="color: #0000cc;">,</span> start<span style="color: #0000cc;">,</span> q<span style="color: #0000cc;">-</span>1<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #0000cc;">}</span> <br>
<span style="color: #0000cc;">}</span> <br>
&nbsp;&nbsp;&nbsp;<br>
<span style="color: #0000ff;">int</span> main<span style="color: #0000cc;">(</span><span style="color: #0000ff;">int</span> argc<span style="color: #0000cc;">,</span> <span style="color: #0000ff;">char</span> <span style="color: #0000cc;">*</span>argv<span style="color: #0000cc;">[</span><span style="color: #0000cc;">]</span><span style="color: #0000cc;">)</span><br>
<span style="color: #0000cc;">{</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">int</span> A<span style="color: #0000cc;">[</span>N<span style="color: #0000cc;">]</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">int</span> num <span style="color: #0000cc;">=</span> 0<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #0000ff;">int</span> res <span style="color: #0000cc;">=</span> 0<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #ff0000;">srand</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">(</span><span style="color: #0000ff;">unsigned</span><span style="color: #0000cc;">)</span><span style="color: #ff0000;">time</span><span style="color: #0000cc;">(</span><span style="color: #ff0000;">NULL</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp; init_a<span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp; print_array<span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp; num <span style="color: #0000cc;">=</span> <span style="color: #ff0000;">rand</span><span style="color: #0000cc;">(</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">%</span><span style="color: #0000cc;">(</span>N<span style="color: #0000cc;">-</span>10<span style="color: #0000cc;">)</span><span style="color: #0000cc;">+</span>5<span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #ff0000;">printf</span><span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"we begin select %d minimun number\n"</span><span style="color: #0000cc;">,</span>num<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp; res <span style="color: #0000cc;">=</span> random_select<span style="color: #0000cc;">(</span> A<span style="color: #0000cc;">,</span> 0 <span style="color: #0000cc;">,</span> N<span style="color: #0000cc;">-</span>1<span style="color: #0000cc;">,</span> num<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span> <br>
&nbsp;&nbsp;<span style="color: #ff0000;">printf</span><span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"and num is %d\n"</span><span style="color: #0000cc;">,</span>res<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp; quick_sort<span style="color: #0000cc;">(</span> A<span style="color: #0000cc;">,</span> 0 <span style="color: #0000cc;">,</span> N<span style="color: #0000cc;">-</span>1<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #ff0000;">printf</span><span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"after quick sort the array is:\n"</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp; print_array<span style="color: #0000cc;">(</span>A<span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span><br>
&nbsp;&nbsp;<span style="color: #ff0000;">system</span><span style="color: #0000cc;">(</span><span style="color: #ff00ff;">"PAUSE"</span><span style="color: #0000cc;">)</span><span style="color: #0000cc;">;</span>&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;<span style="color: #0000ff;">return</span> 0<span style="color: #0000cc;">;</span><br>
<span style="color: #0000cc;">}</span></font></span></code> <img src ="http://www.cppblog.com/JonsenElizee/aggbug/131025.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-24 01:17 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/24/131025.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>It's Not 1575</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130903.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 22 Oct 2010 08:46:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130903.html</guid><description><![CDATA[there is a car near to a desert with width of 563Km.<br>and there is a gas station at the left border of the desert.<br>the car could take 315L gas at most one time.<br>it can store the gas in the desert for a longer trip.<br>now, how much gas it need at least to go through the desert?<br>from the internet, I got a answer 1575. but it's not 1575 in my analysis.<span style="font-family: 隶书;"></span><br><img src="http://www.cppblog.com/images/cppblog_com/jonsenelizee/snap_0000245.png" border="0"><br><br><br>    <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130903.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-22 16:46 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130903.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Debugging Programs with Multiple Threads</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130873.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 22 Oct 2010 05:09:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130873.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130873.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130873.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130873.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130873.html</trackback:ping><description><![CDATA[<br><br>
<div style="border: 1px solid #666666;">Debugging Programs with Multiple Threads<br><br>In some operating systems, such as HP-UX and Solaris, a single program may have more<br>than one thread of execution. The precise semantics of threads differ from one operating<br>system to another, but in general the threads of a single program are akin to multiple<br>processes—except that they share one address space (that is, they can all examine and<br>modify the same variables). On the other hand, each thread has its own registers and<br>execution stack, and perhaps private memory.<br>gdb provides these facilities for debugging multi-thread programs:<br>&#8226; automatic notification of new threads<br>&#8226; &#8216;thread threadno&#8217;, a command to switch among threads<br>&#8226; &#8216;info threads&#8217;, a command to inquire about existing threads<br>&#8226; &#8216;thread apply [threadno] [all] args&#8217;, a command to apply a command to a list of<br>threads<br>&#8226; thread-specific breakpoints<br>&#8226; &#8216;set print thread-events&#8217;, which controls printing of messages on thread start and<br>exit.<br>&#8226; &#8216;set libthread-db-search-path path&#8217;, which lets the user specify which libthread_<br>db to use if the default choice isn&#8217;t compatible with the program.<br>Warning: These facilities are not yet available on every gdb configuration<br>where the operating system supports threads. If your gdb does not support<br>threads, these commands have no effect. For example, a system without thread<br>support shows no output from &#8216;info threads&#8217;, and always rejects the thread<br>command, like this:<br>(gdb) info threads<br>(gdb) thread 1<br>Thread ID 1 not known. Use the "info threads" command to<br>see the IDs of currently known threads.<br>The gdb thread debugging facility allows you to observe all threads while your program<br>runs—but whenever gdb takes control, one thread in particular is always the focus of<br>debugging. This thread is called the current thread. Debugging commands show program<br>information from the perspective of the current thread.
<br>like &#8216;process 368&#8217;, with no further qualifier.<br>For debugging purposes, gdb associates its own thread number—always a single<br>integer—with each thread in your program.<br>info threads<br>Display a summary of all threads currently in your program. gdb displays for<br>each thread (in this order):<br>1. the thread number assigned by gdb<br>2. the target system&#8217;s thread identifier (systag)<br>3. the current stack frame summary for that thread<br>An asterisk &#8216;*&#8217; to the left of the gdb thread number indicates the current thread.<br>For example,<br>(gdb) info threads<br>3 process 35 thread 27 0x34e5 in sigpause ()<br>2 process 35 thread 23 0x34e5 in sigpause ()<br>* 1 process 35 thread 13 main (argc=1, argv=0x7ffffff8)<br>at threadtest.c:68<br>On HP-UX systems:<br>For debugging purposes, gdb associates its own thread number—a small integer assigned<br>in thread-creation order—with each thread in your program.<br>Whenever gdb detects a new thread in your program, it displays both gdb&#8217;s thread<br>number and the target system&#8217;s identification for the thread with a message in the form<br>&#8216;[New systag]&#8217;. systag is a thread identifier whose form varies depending on the particular<br>system. For example, on HP-UX, you see<br>[New thread 2 (system thread 26594)]<br>when gdb notices a new thread.<br>info threads<br>Display a summary of all threads currently in your program. gdb displays for<br>each thread (in this order):<br>1. the thread number assigned by gdb<br>2. the target system&#8217;s thread identifier (systag)<br>3. the current stack frame summary for that thread<br>An asterisk &#8216;*&#8217; to the left of the gdb thread number indicates the current thread.<br>For example,<br>(gdb) info threads<br>* 3 system thread 26607 worker (wptr=0x7b09c318 "@") \<br>at quicksort.c:137
<br>2 system thread 26606 0x7b0030d8 in __ksleep () \<br>from /usr/lib/libc.2<br>1 system thread 27905 0x7b003498 in _brk () \<br>from /usr/lib/libc.2<br>On Solaris, you can display more information about user threads with a Solaris-specific<br>command:<br>maint info sol-threads<br>Display info on Solaris user threads.<br>thread threadno<br>Make thread number threadno the current thread. The command argument<br>threadno is the internal gdb thread number, as shown in the first field of the<br>&#8216;info threads&#8217; display. gdb responds by displaying the system identifier of the<br>thread you selected, and its current stack frame summary:<br>(gdb) thread 2<br>[Switching to process 35 thread 23]<br>0x34e5 in sigpause ()<br>As with the &#8216;[New ...]&#8217; message, the form of the text after &#8216;Switching to&#8217;<br>depends on your system&#8217;s conventions for identifying threads.<br>The debugger convenience variable &#8216;$_thread&#8217; contains the number of the current<br>thread. You may find this useful in writing breakpoint conditional expressions,<br>command scripts, and so forth. See See hundefinedi [Convenience<br>Variables], page hundefinedi, for general information on convenience variables.<br>thread apply [threadno] [all] command<br>The thread apply command allows you to apply the named command to one<br>or more threads. Specify the numbers of the threads that you want affected<br>with the command argument threadno. It can be a single thread number, one<br>of the numbers shown in the first field of the &#8216;info threads&#8217; display; or it could<br>be a range of thread numbers, as in 2-4. To apply a command to all threads,<br>type thread apply all command.<br>set print thread-events<br>set print thread-events on<br>set print thread-events off<br>The set print thread-events command allows you to enable or disable printing<br>of messages when gdb notices that new threads have started or that threads<br>have exited. By default, these messages will be printed if detection of these<br>events is supported by the target. Note that these messages cannot be disabled<br>on all targets.<br>show print thread-events<br>Show whether messages will be printed when gdb detects that threads have<br>started and exited.<br>See hundefinedi [Stopping and Starting Multi-thread Programs], page hundefinedi, for<br>more information about how gdb behaves when you stop and start programs with multiple<br>threads.
<br>See hundefinedi [Setting Watchpoints], page hundefinedi, for information about watchpoints<br>in programs with multiple threads.<br>set libthread-db-search-path [path]<br>If this variable is set, path is a colon-separated list of directories gdb will use<br>to search for libthread_db. If you omit path, &#8216;libthread-db-search-path&#8217;<br>will be reset to an empty list.<br>On gnu/Linux and Solaris systems, gdb uses a &#8220;helper&#8221; libthread_db library<br>to obtain information about threads in the inferior process. gdb will<br>use &#8216;libthread-db-search-path&#8217; to find libthread_db. If that fails, gdb will<br>continue with default system shared library directories, and finally the directory<br>from which libpthread was loaded in the inferior process.<br>For any libthread_db library gdb finds in above directories, gdb attempts<br>to initialize it with the current inferior process. If this initialization fails<br>(which could happen because of a version mismatch between libthread_db<br>and libpthread), gdb will unload libthread_db, and continue with the next<br>directory. If none of libthread_db libraries initialize successfully, gdb will<br>issue a warning and thread debugging will be disabled.<br>Setting libthread-db-search-path is currently implemented only on some<br>platforms.<br>show libthread-db-search-path<br>Display current libthread db search path.
<br></div>
<br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/130873.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-22 13:09 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130873.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Funny Program</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130851.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 22 Oct 2010 00:55:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130851.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130851.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130851.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130851.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130851.html</trackback:ping><description><![CDATA[This is a funny programm and I don't why it happens in this way.<br>No matter what data input that is composed with different numbers, like 123456, <br>124578, 235689, 789456, 147896, 321478 and so on. through this program, it will <br>get a same data ever got.<br><br><img src="http://www.cppblog.com/images/cppblog_com/jonsenelizee/snap_0000243.png" border="0"><br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;"> funny program by JonsenElizee.<br></span><span style="color: #008080;">&nbsp;&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #008000;">//<br></span><span style="color: #008080;">&nbsp;&nbsp;3</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;4</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">stdafx.h</span><span style="color: #000000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;5</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">math.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;6</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">assert.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;7</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;&nbsp;8</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;set&nbsp;value&nbsp;in&nbsp;specified&nbsp;position.<br></span><span style="color: #008080;">&nbsp;&nbsp;9</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;void*&nbsp;ptr&nbsp;pointer&nbsp;of&nbsp;the&nbsp;start&nbsp;point&nbsp;of&nbsp;bit.<br></span><span style="color: #008080;">&nbsp;10</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;int&nbsp;pos&nbsp;position&nbsp;of&nbsp;the&nbsp;bit&nbsp;to&nbsp;be&nbsp;set&nbsp;and&nbsp;pos&nbsp;starts&nbsp;from&nbsp;0.<br></span><span style="color: #008080;">&nbsp;11</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;int&nbsp;val&nbsp;value&nbsp;to&nbsp;set.&nbsp;val&nbsp;is&nbsp;0&nbsp;or&nbsp;1.<br></span><span style="color: #008080;">&nbsp;12</span>&nbsp;<span style="color: #008000;">&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;13</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;setbit(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ptr,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;val)<br></span><span style="color: #008080;">&nbsp;14</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;cast&nbsp;to&nbsp;unsigned&nbsp;char*&nbsp;type&nbsp;pointer&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;uch&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">)ptr;<br></span><span style="color: #008080;">&nbsp;17</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;move&nbsp;pointer&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;uch&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;pos</span><span style="color: #000000;">/</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">&nbsp;21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;set&nbsp;the&nbsp;bit&nbsp;with&nbsp;right&nbsp;value,&nbsp;0&nbsp;or&nbsp;1.&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;(pos</span><span style="color: #000000;">%</span><span style="color: #000000;">8</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(&nbsp;val&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;(unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)(</span><span style="color: #000000;">*</span><span style="color: #000000;">uch&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;tmp)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">uch&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;tmp;<br></span><span style="color: #008080;">&nbsp;24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">uch&nbsp;</span><span style="color: #000000;">-=</span><span style="color: #000000;">&nbsp;tmp;<br></span><span style="color: #008080;">&nbsp;25</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;26</span>&nbsp;<span style="color: #000000;">unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;getbit(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ptr,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;pos)&nbsp;<br></span><span style="color: #008080;">&nbsp;27</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;cast&nbsp;to&nbsp;unsigned&nbsp;char*&nbsp;type&nbsp;pointer&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;uch&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">)ptr;<br></span><span style="color: #008080;">&nbsp;30</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;move&nbsp;pointer&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;uch&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;pos</span><span style="color: #000000;">/</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;33</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;set&nbsp;the&nbsp;bit&nbsp;with&nbsp;right&nbsp;value,&nbsp;0&nbsp;or&nbsp;1.&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;35</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;(pos</span><span style="color: #000000;">%</span><span style="color: #000000;">8</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)(</span><span style="color: #000000;">*</span><span style="color: #000000;">uch&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;tmp);<br></span><span style="color: #008080;">&nbsp;37</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;38</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;39</span>&nbsp;<span style="color: #000000;">unsigned&nbsp;revert_unsigned_int(unsigned&nbsp;ipt)<br></span><span style="color: #008080;">&nbsp;40</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;opt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;42</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">short</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;43</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(opt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ipt;&nbsp;opt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;opt&nbsp;</span><span style="color: #000000;">/=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">);&nbsp;<br></span><span style="color: #008080;">&nbsp;44</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(opt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;ipt&nbsp;</span><span style="color: #000000;">/=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;45</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opt&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;(ipt&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;ipt&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;((unsigned)pow(</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">--</span><span style="color: #000000;">i));<br></span><span style="color: #008080;">&nbsp;46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;opt;<br></span><span style="color: #008080;">&nbsp;47</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;48</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;49</span>&nbsp;<span style="color: #000000;">unsigned&nbsp;sort_unsigned_int(unsigned&nbsp;ipt,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ascend)<br></span><span style="color: #008080;">&nbsp;50</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;51</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;opt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;52</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;53</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">&nbsp;54</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;length&nbsp;of&nbsp;input&nbsp;data&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;55</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(opt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ipt;&nbsp;opt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;opt&nbsp;</span><span style="color: #000000;">/=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">);&nbsp;<br></span><span style="color: #008080;">&nbsp;56</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;57</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">)malloc(i);<br></span><span style="color: #008080;">&nbsp;58</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;memset(num,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;i);<br></span><span style="color: #008080;">&nbsp;59</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ptr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;num;<br></span><span style="color: #008080;">&nbsp;60</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;mov&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ptr&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;61</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;n&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i;<br></span><span style="color: #008080;">&nbsp;62</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;63</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;each&nbsp;number&nbsp;char&nbsp;of&nbsp;input&nbsp;data&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;64</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(;&nbsp;ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;ipt&nbsp;</span><span style="color: #000000;">/=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;65</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num[</span><span style="color: #000000;">--</span><span style="color: #000000;">i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)(ipt&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;ipt&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">);<br></span><span style="color: #008080;">&nbsp;66</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;67</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;sort&nbsp;the&nbsp;number&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;68</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">&nbsp;69</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(ascend)&nbsp;{<br></span><span style="color: #008080;">&nbsp;70</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(mov&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;num&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">])&nbsp;{<br></span><span style="color: #008080;">&nbsp;71</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;72</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;73</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;74</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;75</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;76</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br></span><span style="color: #008080;">&nbsp;77</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br></span><span style="color: #008080;">&nbsp;78</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(mov&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;num&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">])&nbsp;{<br></span><span style="color: #008080;">&nbsp;79</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;80</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;81</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;mov[</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">];<br></span><span style="color: #008080;">&nbsp;82</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;83</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;84</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;85</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptr</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;86</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ptr</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;87</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;88</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">&nbsp;89</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;convert&nbsp;to&nbsp;a&nbsp;unsigned&nbsp;int&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;90</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">&nbsp;(opt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;n;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">&nbsp;91</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;92</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opt&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;num[i]&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;((unsigned)pow(</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;n</span><span style="color: #000000;">-</span><span style="color: #000000;">i</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br></span><span style="color: #008080;">&nbsp;93</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;94</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">&nbsp;95</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;free(num);<br></span><span style="color: #008080;">&nbsp;96</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;opt;<br></span><span style="color: #008080;">&nbsp;97</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;98</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;99</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;loop_to_original_data(unsigned&nbsp;ipt)<br></span><span style="color: #008080;">100</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">101</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(ipt&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #000000;">1000000</span><span style="color: #000000;">);<br></span><span style="color: #008080;">102</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;his[</span><span style="color: #000000;">1000000</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{</span><span style="color: #000000;">0</span><span style="color: #000000;">};<br></span><span style="color: #008080;">103</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">104</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;ipt);<br></span><span style="color: #008080;">105</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;a&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sort_unsigned_int(ipt,&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">106</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;b&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;sort_unsigned_int(ipt,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br></span><span style="color: #008080;">107</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(a&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;b)&nbsp;{<br></span><span style="color: #008080;">108</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;ipt);<br></span><span style="color: #008080;">109</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br></span><span style="color: #008080;">110</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">111</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;rst&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;&nbsp;b&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;a;<br></span><span style="color: #008080;">112</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(getbit(his,&nbsp;rst))&nbsp;{<br></span><span style="color: #008080;">113</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;rst);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">114</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br></span><span style="color: #008080;">115</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">116</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;<br></span><span style="color: #008080;">117</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setbit(his,&nbsp;rst,&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">118</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipt&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;rst;<br></span><span style="color: #008080;">119</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">120</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">121</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">122</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br></span><span style="color: #008080;">123</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">124</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;loop_to_original_data(</span><span style="color: #000000;">123456</span><span style="color: #000000;">);<br></span><span style="color: #008080;">125</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="color: #008080;">126</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">127</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">128</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">129</span>&nbsp;<span style="color: #000000;"></span></div>
<br><br><br>    <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130851.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-22 08:55 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/22/130851.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>String to 0x String</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130728.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 21 Oct 2010 03:40:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130728.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130728.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130728.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130728.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130728.html</trackback:ping><description><![CDATA[Here is a simple implementation to convert string to 0x string.<br>Better one is expected ......<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">assert.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;convert&nbsp;string&nbsp;to&nbsp;0x&nbsp;string.<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;const&nbsp;char*&nbsp;ipt&nbsp;input&nbsp;string.<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;char*&nbsp;opt&nbsp;output&nbsp;0x&nbsp;string.<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #008000;">&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;str2Xstr(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ipt,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;opt)<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;opt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL);<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;add&nbsp;prefix&nbsp;0x&nbsp;into&nbsp;opt&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">x</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;bak&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;opt;<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;the&nbsp;char&nbsp;and&nbsp;cast&nbsp;it&nbsp;to&nbsp;int&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;cha&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">short</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;cha;<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;assert&nbsp;sizeof&nbsp;char&nbsp;is&nbsp;1&nbsp;(8bits)&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&lt;&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&gt;&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">;<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(tmp&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp</span><span style="color: #000000;">-</span><span style="color: #000000;">10</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">A</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;cha;<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&lt;&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">;<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&gt;&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">12</span><span style="color: #000000;">;<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(tmp&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp</span><span style="color: #000000;">-</span><span style="color: #000000;">10</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">A</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;terminate&nbsp;string </span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;"></span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br></span><span style="color: #008080;">39</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;opt[</span><span style="color: #000000;">64</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{</span><span style="color: #000000;">0</span><span style="color: #000000;">};<br></span><span style="color: #008080;">41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;str2Xstr(</span><span style="color: #000000;">"</span><span style="color: #000000;">2009-09</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;opt);<br></span><span style="color: #008080;">42</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;puts(opt);&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;0x323030392D3039&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">43</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="color: #008080;">44</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">45</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br>Using &amp; operation is a better way:<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">/*</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;convert&nbsp;string&nbsp;to&nbsp;0x&nbsp;string.<br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;const&nbsp;char*&nbsp;ipt&nbsp;input&nbsp;string.<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;char*&nbsp;opt&nbsp;output&nbsp;0x&nbsp;string.<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #008000;">&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;str2Xstr(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ipt,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;opt)<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;opt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL);<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;add&nbsp;prefix&nbsp;0x&nbsp;into&nbsp;opt&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">x</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;bak&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;opt;<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;the&nbsp;char&nbsp;and&nbsp;cast&nbsp;it&nbsp;to&nbsp;int&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;cha&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;cha;<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;assert&nbsp;sizeof&nbsp;char&nbsp;is&nbsp;1&nbsp;(8bits)&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&amp;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\xF0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&gt;&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">;<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;tmp</span><span style="color: #000000;">-</span><span style="color: #000000;">10</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">A</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;:&nbsp;tmp</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;cha;<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp&nbsp;</span><span style="color: #000000;">&amp;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\x0F</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tmp&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;tmp</span><span style="color: #000000;">-</span><span style="color: #000000;">10</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">A</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;:&nbsp;tmp</span><span style="color: #000000;">+</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;terminate&nbsp;string&nbsp;and&nbsp;turn&nbsp;back&nbsp;one&nbsp;step&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">opt</span><span style="color: #000000;">--</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">}</span></div>
<br> <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130728.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-21 11:40 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130728.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Int to 0x String</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130724.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Thu, 21 Oct 2010 02:24:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130724.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130724.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130724.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130724.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130724.html</trackback:ping><description><![CDATA[Here is simple implementation for converting int value to 0x string with out sprintf and so on.<br>Any better way is expected ......<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">assert.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;convert&nbsp;int&nbsp;value&nbsp;to&nbsp;0x&nbsp;string.<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #008000;">&nbsp;*<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;int&nbsp;ipt&nbsp;input&nbsp;int&nbsp;value.<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;char*&nbsp;opt&nbsp;output&nbsp;string.<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #008000;">&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;int2Xstr(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ipt,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;opt)<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(ipt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;opt&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL);<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;add&nbsp;prefix&nbsp;"0x"&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp; *opt</span><span style="color: #000000;">++ = </span><span style="color: #000000;"></span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp; *opt</span><span style="color: #000000;">++ = </span><span style="color: #000000;">'</span><span style="color: #000000;">x</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;opt&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">7</span><span style="color: #000000;">;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;number&nbsp;of&nbsp;4bits-unit&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;num&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;using&nbsp;&lt;&lt;&nbsp;and&nbsp;&gt;&gt;&nbsp;to&nbsp;get&nbsp;4bits&nbsp;value&nbsp;and&nbsp;convert&nbsp;it&nbsp;to&nbsp;0x&nbsp;char&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;num;&nbsp;i&nbsp;</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;negative&nbsp;value&nbsp;should&nbsp;be&nbsp;converted&nbsp;to&nbsp;unsigned&nbsp;one&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;var&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ipt&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;(unsigned)(</span><span style="color: #000000;">-</span><span style="color: #000000;">ipt)&nbsp;:&nbsp;(unsigned)ipt;<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;&gt;&gt;&nbsp;and&nbsp;&lt;&lt;&nbsp;to&nbsp;clear&nbsp;other&nbsp;bits&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #000000;">&gt;&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">4</span><span style="color: #000000;">*</span><span style="color: #000000;">i;<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #000000;">&lt;&lt;=</span><span style="color: #000000;">&nbsp;(num</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">4</span><span style="color: #000000;">;<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #000000;">&gt;&gt;=</span><span style="color: #000000;">&nbsp;(num</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">4</span><span style="color: #000000;">;<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;convert&nbsp;to&nbsp;0x&nbsp;char&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(var&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">) *opt</span><span style="color: #000000;">-- = </span><span style="color: #000000;">(var&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">10</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">A</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> *opt</span><span style="color: #000000;">-- = </span><span style="color: #000000;">var&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;opt[</span><span style="color: #000000;">32</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{</span><span style="color: #000000;">0</span><span style="color: #000000;">};<br></span><span style="color: #008080;">39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;int2Xstr(</span><span style="color: #000000;">1234567890</span><span style="color: #000000;">,&nbsp;opt);<br></span><span style="color: #008080;">40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;puts(opt);&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;0x499602D2&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">41</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="color: #008080;">42</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">43</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br> <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130724.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-21 10:24 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/21/130724.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Get The Whole Arrangement of String ( PNN )</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130581.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 20 Oct 2010 08:48:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130581.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130581.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130581.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130581.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130581.html</trackback:ping><description><![CDATA[Get the whole arrangement of a string.<br>I got no better method to get this kind of sorting sequence.<br>Here is a simple implementation of printing whole arrangement.<br>Better one is expected now...<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">assert.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;N&nbsp;10&nbsp;/*&nbsp;do&nbsp;not&nbsp;set&nbsp;a&nbsp;big&nbsp;number&nbsp;for&nbsp;n!&nbsp;*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;">*<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;str&nbsp;target&nbsp;string.<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #008000;">&nbsp;*&nbsp;oka&nbsp;sorted&nbsp;content.<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #008000;">&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;pnn(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;str,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;oka)<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;check&nbsp;str&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;assert(str&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;NULL&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;strlen(str)&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;N&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;strlen(str)&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(strlen(str)&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%s%c\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;oka,&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">str);<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;i&nbsp;to&nbsp;record&nbsp;which&nbsp;char&nbsp;need&nbsp;to&nbsp;be&nbsp;sort&nbsp;now&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;define&nbsp;new&nbsp;container&nbsp;for&nbsp;input&nbsp;str&nbsp;to&nbsp;swap&nbsp;char&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;ipt[N];<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(ipt,&nbsp;str,&nbsp;strlen(str)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;define&nbsp;new&nbsp;container&nbsp;for&nbsp;output&nbsp;str&nbsp;to&nbsp;add&nbsp;char&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;opt[N];<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(opt,&nbsp;oka,&nbsp;strlen(oka)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;get&nbsp;position&nbsp;of&nbsp;char&nbsp;to&nbsp;be&nbsp;sort&nbsp;now&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ptr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;ipt&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;swap&nbsp;char&nbsp;with&nbsp;the&nbsp;first&nbsp;one&nbsp;in&nbsp;ipt&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(ptr&nbsp;</span><span style="color: #000000;">!=</span><span style="color: #000000;">&nbsp;ipt)&nbsp;{<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt;<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr;<br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr&nbsp;</span><span style="color: #000000;">^=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt;<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;add&nbsp;char&nbsp;into&nbsp;opt&nbsp;as&nbsp;the&nbsp;last&nbsp;one&nbsp;and&nbsp;set&nbsp;'\0'&nbsp;to&nbsp;end&nbsp;the&nbsp;opt&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opt[strlen(oka)]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ipt;<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opt[strlen(oka)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br></span><span style="color: #008080;">38</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;recursively&nbsp;sort&nbsp;next&nbsp;char&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">39</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pnn(ipt</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;opt);<br></span><span style="color: #008080;">40</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">41</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">42</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">43</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> permutation(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;str)<br></span><span style="color: #008080;">44</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">45</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;aim[N];<br></span><span style="color: #008080;">46</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;memcpy(aim,&nbsp;str,&nbsp;strlen(str)</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br></span><span style="color: #008080;">47</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;oka[</span><span style="color: #000000;">1</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">};<br></span><span style="color: #008080;">48</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;pnn(aim,&nbsp;oka);<br></span><span style="color: #008080;">49</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">50</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">51</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argc,&nbsp;_TCHAR</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;argv[])<br></span><span style="color: #008080;">52</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">53</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp; permutation(</span><span style="color: #000000;">"</span><span style="color: #000000;">1234567</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br></span><span style="color: #008080;">54</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="color: #008080;">55</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">56</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br> <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130581.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-20 16:48 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130581.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Four Lines Implementing String to Long</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130567.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 20 Oct 2010 06:29:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130567.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130567.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130567.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130567.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130567.html</trackback:ping><description><![CDATA[<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;string2long(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;str,&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;rtv)<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;strlen(str),&nbsp;j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">rtv&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">str&nbsp;</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">str&nbsp;</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">9</span><span style="color: #000000;">'</span><span style="color: #000000;">;&nbsp;i</span><span style="color: #000000;">--</span><span style="color: #000000;">,&nbsp;str</span><span style="color: #000000;">++</span><span style="color: #000000;">){<br></span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">rtv&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">)((</span><span style="color: #000000;">*</span><span style="color: #000000;">str&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">0</span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">*</span><span style="color: #000000;">(pow(</span><span style="color: #000000;">10</span><span style="color: #000000;">,&nbsp;i</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)));<br></span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">str&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">\0</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;:&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">7</span>&nbsp;<span style="color: #000000;">}</span></div>
Please pay attention to j = *rtv = 0;<br>if there is no "j = ", *rtv will be redefined as int *rtv after ", " and address of rtv will be 0xcccccc.<br>So, inorder to set value of rtv in if sentence, just use a non-usage variable j.<br><br>to invoke the function in this way:<br><br>long rtv;<br>if(string2long("321", &amp;rtv)) printf("rtv = %ld\n", rtv);<br>else puts("failure");<br><br><br><img src ="http://www.cppblog.com/JonsenElizee/aggbug/130567.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-20 14:29 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130567.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Avoiding Dupliated Computing in Fabonaci</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130555.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 20 Oct 2010 03:48:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130555.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130555.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130555.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130555.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130555.html</trackback:ping><description><![CDATA[How to avoiding duplicated computing of recursive function like this one:<br>func(n) = func(n-1) + func(n-2) + func(n-3) and func(1) == func(2) == 1 and func(3) == 2. <br>or function like this: fabonaci:<br>fabonaci(n) = fabonaci(n-1) + fabonaci(n-2).<br><br><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fabo(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;ptr)<br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">fabo(%d)\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;n);<br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">)&nbsp;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;{<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;tmp;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;fabo(n&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">tmp);<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">ptr&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;tmp;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fabonaci(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n)<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;tmp;<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;fabo(n,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">tmp);<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;xxx(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n)<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">xxx(%d)\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;n);<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">)</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;xxx(n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #000000;">xxx(n</span><span style="color: #000000;">-</span><span style="color: #000000;">2</span><span style="color: #000000;">);<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;fabonaci(</span><span style="color: #000000;">10</span><span style="color: #000000;">);<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">n&nbsp;=&nbsp;%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;n);<br></span><span style="color: #008080;">32</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">33</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;xxx(</span><span style="color: #000000;">10</span><span style="color: #000000;">);<br></span><span style="color: #008080;">34</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">n&nbsp;=&nbsp;%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;n);<br></span><span style="color: #008080;">35</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="color: #008080;">36</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">37</span>&nbsp;<span style="color: #000000;">}</span></div>
<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">/*</span><span style="color: #008000;">**********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;func(n)&nbsp;=&nbsp;func(n-1)&nbsp;+&nbsp;func(n-2)&nbsp;+&nbsp;func(n-3)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;func(1)&nbsp;=&nbsp;1;&nbsp;func(2)&nbsp;=&nbsp;1;&nbsp;func(3)&nbsp;=&nbsp;2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #008000;">/*</span><span style="color: #008000;">**********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;funk(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;n_1,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;n_2)<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">funk(%d)\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;n);<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(n&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">3</span><span style="color: #000000;">){<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">n_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">n_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n_1_1,&nbsp;n_1_2;<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">n_1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;funk(n</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n_1_1,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n_1_2);<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">n_2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;n_1_1;<br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">n_1&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">n_2&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;n_1_2;<br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">20</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;func(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n)<br></span><span style="color: #008080;">21</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">22</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n_1,&nbsp;n_2;<br></span><span style="color: #008080;">23</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;funk(n,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n_1,&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n_2);<br></span><span style="color: #008080;">24</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">25</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br></span><span style="color: #008080;">26</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">27</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;func(</span><span style="color: #000000;">10</span><span style="color: #000000;">);<br></span><span style="color: #008080;">28</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">n&nbsp;=&nbsp;%d\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;n);<br></span><span style="color: #008080;">29</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="color: #008080;">30</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br></span><span style="color: #008080;">31</span>&nbsp;<span style="color: #000000;">}</span></div>
<br>  <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130555.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-20 11:48 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/20/130555.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Postorder for Binary Tree</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/16/130126.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Sat, 16 Oct 2010 06:18:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/16/130126.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/130126.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/16/130126.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/130126.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/130126.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Post order accesing the binary tree with a flag to recording what kind of node is accessed.flag == 0 means made left move or accessed a left node.flag == 1 means made right move or accessed a right no...&nbsp;&nbsp;<a href='http://www.cppblog.com/JonsenElizee/archive/2010/10/16/130126.html'>阅读全文</a><img src ="http://www.cppblog.com/JonsenElizee/aggbug/130126.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-16 14:18 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/16/130126.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C I/O</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/15/130045.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 15 Oct 2010 08:34:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/15/130045.html</guid><description><![CDATA[<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%; font-family: 新宋体;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp; atoi(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">nptr);&nbsp;<br></span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;atof(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">nptr);&nbsp;<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;&nbsp; atol(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">nptr);&nbsp;</span></div>
<br>
<p>
</p>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff; font-family: 新宋体;">long</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;strtol(</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*nptr,</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;**endptr,</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">base</span><span style="color: #000000; font-family: 新宋体;">);<br>unsigned&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">long</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;strtoul(</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*nptr,</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;**endptr,</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">base</span><span style="color: #000000; font-family: 新宋体;">);<br></span><span style="color: #0000ff; font-family: 新宋体;">double</span><span style="color: #000000; font-family: 新宋体;">&nbsp;strtod(</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*nptr,</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;**endptr);<br></span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp; *strchr(</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*s,</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;c);&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;find&nbsp;first&nbsp;position&nbsp;of&nbsp;c&nbsp;in&nbsp;s&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br></span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;sscanf(&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*,&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*,&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">);&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;read&nbsp;data&nbsp;from&nbsp;string&nbsp;matching&nbsp;format&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br></span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp; scanf(&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*,&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">);&nbsp;<br></span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;fprintf(&nbsp;FILE&nbsp;*stream,&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*format,&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">&nbsp;);<br></span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;sprintf(&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*buffer,&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;*format&nbsp;[,&nbsp;argument]&nbsp;<img src="http://www.cppblog.com/Images/dot.gif">&nbsp;);</span><br></div>
<p>&nbsp;</p>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; width: 98%; font-size: 13px;"><span style="color: #0000ff;"></span><span style="color: #008000; font-family: 新宋体;">FILE I/O FUNCTIONS<br></span></div>
<br style="font-family: 新宋体;">
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; font-family: 新宋体;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">/*</span><span style="color: #008000;">**********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;FILE*&nbsp;FUNCTIONS<br>/***********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br>FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;fopen(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;filepath,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;mode);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">mode:&nbsp;"r":&nbsp;read;&nbsp;"w":&nbsp;write,&nbsp;create&nbsp;if&nbsp;not&nbsp;exist;&nbsp;"a":&nbsp;append,&nbsp;create&nbsp;if&nbsp;not&nbsp;exist;</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fread&nbsp;(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;buf,&nbsp;size_t&nbsp;size,&nbsp;size_t&nbsp;nitems,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;read&nbsp;nitems&nbsp;chars&nbsp;or&nbsp;to&nbsp;the&nbsp;EOF.&nbsp;so&nbsp;it&nbsp;could&nbsp;read&nbsp;multi-lines.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fwrite(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;buf,&nbsp;size_t&nbsp;size,&nbsp;size_t&nbsp;nitems,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;write&nbsp;nitems&nbsp;chars&nbsp;or&nbsp;to&nbsp;the&nbsp;'\0'&nbsp;of&nbsp;buf.</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;fgets(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buf,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;nitems,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;read&nbsp;at&nbsp;most&nbsp;one&nbsp;line&nbsp;with&nbsp;'\n'(holds&nbsp;it)&nbsp;or&nbsp;terminated&nbsp;by&nbsp;EOF.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;fputs(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buf,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;write&nbsp;strlen(buf)&nbsp;chars&nbsp;into&nbsp;stream&nbsp;and&nbsp;return&nbsp;real&nbsp;written&nbsp;char&nbsp;number.</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getc(FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;read&nbsp;next&nbsp;char&nbsp;and&nbsp;return&nbsp;EOF&nbsp;on&nbsp;end&nbsp;of&nbsp;file&nbsp;or&nbsp;error.&nbsp;</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;putc(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;c,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;return&nbsp;c&nbsp;on&nbsp;success,&nbsp;else&nbsp;EOF(-1).</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fgetc(FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;read&nbsp;next&nbsp;char&nbsp;and&nbsp;return&nbsp;EOF&nbsp;on&nbsp;end&nbsp;of&nbsp;file&nbsp;or&nbsp;error.&nbsp;</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fputc(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;c,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;return&nbsp;c&nbsp;on&nbsp;success,&nbsp;else&nbsp;EOF(-1).</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fflush(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;return&nbsp;0&nbsp;on&nbsp;success,&nbsp;else&nbsp;EOF(-1).</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;feof(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;return&nbsp;1&nbsp;if&nbsp;reach&nbsp;EOF,&nbsp;else&nbsp;0.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fclose(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;return&nbsp;0&nbsp;on&nbsp;success,&nbsp;else&nbsp;EOF(-1).</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br><br>FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;freopen(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;filename,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;type,&nbsp;FILE</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;stream);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;redirect&nbsp;stream&nbsp;to&nbsp;filename&nbsp;file</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fgetpos(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream，</span><span style="color: #000000;">*</span><span style="color: #000000;">fpos_t&nbsp;filepos);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;get&nbsp;current&nbsp;file&nbsp;pointer&nbsp;position.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fscanf(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">format,[argument]);<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fseek(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream,&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;offset,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fromwhere);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;SEEK_SET,&nbsp;SEEK_CUR,&nbsp;SEEK_END.&nbsp;</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;setbuf(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">steam,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buf);&nbsp;<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;setvbuf(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream,&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buf,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;type,&nbsp;unsigned&nbsp;size);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;call&nbsp;it&nbsp;just&nbsp;after&nbsp;opening&nbsp;file.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;ftell(FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">stream);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;get&nbsp;current&nbsp;file&nbsp;pointer&nbsp;position.</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">**********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;INT&nbsp;FD&nbsp;FUNCTIONS<br>/***********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;close(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd);<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fcntl(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd&nbsp;,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;cmd);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;#include&nbsp;&lt;fcntl.h&gt;&nbsp;</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fcntl(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;cmd,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;arg);&nbsp;<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fcntl(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd,</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;cmd,</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;flock&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">lock</span><span style="color: #000000;">);&nbsp;<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ioctl(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;cmd,[</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">argdx,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;argcx]);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;#include&nbsp;&lt;sys/ioctl.h&gt;&nbsp;</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;read(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd,&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buf,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;nbyte);&nbsp;<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;write(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd,&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buf,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;nbyte);&nbsp;&nbsp;<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;lseek(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fd,&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;offset,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fromwhere);&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;SEEK_SET,&nbsp;SEEK_CUR,&nbsp;SEEK_END.&nbsp;</span><span style="color: #008000;"><br></span><span style="color: #000000;"><br><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">**********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #008000;">/*</span><span style="color: #008000;">&nbsp;STDIO&nbsp;FUNCTIONS<br>/***********************************************************************</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;gets(&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">buffer&nbsp;);&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;from&nbsp;stdio,&nbsp;over&nbsp;at&nbsp;'\n'&nbsp;and&nbsp;EOF&nbsp;and&nbsp;set&nbsp;'\n'&nbsp;to&nbsp;'\0'.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;fgetchar(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;from&nbsp;stdio&nbsp;and&nbsp;return&nbsp;EOF&nbsp;when&nbsp;error&nbsp;occurs.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getch(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;#include&nbsp;&lt;conio.h&gt;,&nbsp;no&nbsp;echoing.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getchar(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;#define&nbsp;getchar()&nbsp;fgetc(stdin)</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getche(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;echo&nbsp;char&nbsp;from&nbsp;stdio&nbsp;and&nbsp;continue&nbsp;at&nbsp;'\n'.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;putchar(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ch);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;put&nbsp;char&nbsp;into&nbsp;stdout</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;puts(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #0000ff;">string</span><span style="color: #000000;">);&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;put&nbsp;string&nbsp;into&nbsp;stdout.</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;scanf(</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">format[,argument,]);</span></div>
<br><br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000; font-family: 新宋体;">/************************************************************************/</span><span style="color: #000000; font-family: 新宋体;"><br></span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;TEST&nbsp;ON&nbsp;WINDOWS</span><span style="color: #000000; font-family: 新宋体;"><br></span><span style="color: #008000; font-family: 新宋体;">/************************************************************************/</span><span style="color: #000000; font-family: 新宋体;"><br>#include&lt;stdio.h&gt;<br><br>#include&nbsp;&lt;assert.h&gt;&nbsp;<br></span><span style="color: #008000; font-family: 新宋体;">/************************************************************************/</span><span style="color: #000000; font-family: 新宋体;"><br></span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;read&nbsp;line_length&nbsp;chars&nbsp;into&nbsp;line&nbsp;from&nbsp;sourcefile<br>/*&nbsp;return&nbsp;number&nbsp;of&nbsp;chars&nbsp;really&nbsp;read,&nbsp;else&nbsp;-1&nbsp;for&nbsp;error.<br>/************************************************************************/</span><span style="color: #000000; font-family: 新宋体;"><br></span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;read_line(</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;line,&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;line_length,&nbsp;FILE*&nbsp;sourcefile)<br>{<br>&nbsp;&nbsp;&nbsp; assert(sizeof(char) == 1);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;n&nbsp;=&nbsp;0;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">while</span><span style="color: #000000; font-family: 新宋体;">&nbsp;(1)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;c&nbsp;=&nbsp;fgetc(sourcefile);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(ferror(sourcefile))&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">return</span><span style="color: #000000; font-family: 新宋体;">&nbsp;-1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(c&nbsp;==&nbsp;EOF&nbsp;||&nbsp;line_length--&nbsp;==&nbsp;0)&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">break</span><span style="color: #000000; font-family: 新宋体;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*line++&nbsp;=&nbsp;c;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n++;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">return</span><span style="color: #000000; font-family: 新宋体;">&nbsp;n;<br>}<br><br>FILE*&nbsp;open_file_demo(</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;filepath,&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">const</span><span style="color: #000000; font-family: 新宋体;">&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;options)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;FILE*&nbsp;fp&nbsp;=&nbsp;fopen(filepath,&nbsp;options);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(fp&nbsp;==&nbsp;NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("fopen&nbsp;failure!");<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">return</span><span style="color: #000000; font-family: 新宋体;">&nbsp;fp;<br>}<br><br></span><span style="color: #0000ff; font-family: 新宋体;">void</span><span style="color: #000000; font-family: 新宋体;">&nbsp;write_file_demo()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;filepath&nbsp;=&nbsp;"D:/xxx.txt";<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;options&nbsp;=&nbsp;"a";<br>&nbsp;&nbsp;&nbsp;&nbsp;FILE*&nbsp;fp&nbsp;=&nbsp;open_file_demo(filepath,&nbsp;options);<br>&nbsp;&nbsp;&nbsp;&nbsp;assert(fp&nbsp;!=&nbsp;NULL);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;content[][16]&nbsp;=&nbsp;{"write&nbsp;",&nbsp;"file&nbsp;",&nbsp;"demo"};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;i&nbsp;=&nbsp;0;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">for</span><span style="color: #000000; font-family: 新宋体;">(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;3;&nbsp;i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;str&nbsp;=&nbsp;content[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">//int&nbsp;siz&nbsp;=&nbsp;fwrite(str,&nbsp;1,&nbsp;strlen(str),&nbsp;fp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//if(ferror(fp))&nbsp;perror("fwrite&nbsp;failure!");<br></span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;write&nbsp;strlen(str)&nbsp;chars&nbsp;into&nbsp;fp.&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;rtv&nbsp;=&nbsp;fputs(str,&nbsp;fp);&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;rtv&nbsp;=&nbsp;0&nbsp;means&nbsp;success.&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(rtv&nbsp;==&nbsp;EOF)&nbsp;perror("fputs&nbsp;failure!");&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;rtv&nbsp;=&nbsp;EOF(-1)&nbsp;means&nbsp;error.&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;fputs("\n",&nbsp;fp);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(ferror(fp))&nbsp;perror("fputs&nbsp;failure!");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(fclose(fp)&nbsp;!=&nbsp;0)&nbsp;perror("fclose&nbsp;failure!");<br>}<br><br></span><span style="color: #0000ff; font-family: 新宋体;">void</span><span style="color: #000000; font-family: 新宋体;">&nbsp;read_file_demo()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;filepath&nbsp;=&nbsp;"D:/xxx.txt";<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">*&nbsp;options&nbsp;=&nbsp;"r";<br>&nbsp;&nbsp;&nbsp;&nbsp;FILE*&nbsp;fp&nbsp;=&nbsp;open_file_demo(filepath,&nbsp;options);<br>&nbsp;&nbsp;&nbsp;&nbsp;assert(fp&nbsp;!=&nbsp;NULL);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">while</span><span style="color: #000000; font-family: 新宋体;">&nbsp;(feof(fp)&nbsp;==&nbsp;0)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">char</span><span style="color: #000000; font-family: 新宋体;">&nbsp;line[128]&nbsp;=&nbsp;{0};<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;make&nbsp;sure&nbsp;sizeof(line)-1&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;fread&nbsp;could&nbsp;read&nbsp;many&nbsp;lines&nbsp;at&nbsp;one&nbsp;time.&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;it&nbsp;will&nbsp;read&nbsp;sizeof(line)-1&nbsp;chars&nbsp;or&nbsp;to&nbsp;the&nbsp;EOF.&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">//&nbsp;int&nbsp;siz&nbsp;=&nbsp;fread(line,&nbsp;1,&nbsp;sizeof(line)-1,&nbsp;fp);<br></span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">/*&nbsp;fgets&nbsp;just&nbsp;read&nbsp;a&nbsp;line&nbsp;or&nbsp;sizeof(line)-1&nbsp;chars&nbsp;at&nbsp;one&nbsp;time.&nbsp;*/</span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: 新宋体;">//&nbsp;char*&nbsp;rtv&nbsp;=&nbsp;fgets(line,&nbsp;sizeof(line)-1,&nbsp;fp);<br></span><span style="color: #000000; font-family: 新宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;siz&nbsp;=&nbsp;read_line(line,&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">sizeof</span><span style="color: #000000; font-family: 新宋体;">(line)-1,&nbsp;fp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">&nbsp;(ferror(fp))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("fread&nbsp;failure!");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">break</span><span style="color: #000000; font-family: 新宋体;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s",&nbsp;line);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">if</span><span style="color: #000000; font-family: 新宋体;">(fclose(fp)&nbsp;!=&nbsp;0)&nbsp;perror("fclose&nbsp;failure!");<br>}<br><br><br><br></span><span style="color: #0000ff; font-family: 新宋体;">int</span><span style="color: #000000; font-family: 新宋体;">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;write_file_demo();<br>&nbsp;&nbsp;&nbsp;&nbsp;read_file_demo();<br>&nbsp;&nbsp;&nbsp;&nbsp;puts("Exit");<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: 新宋体;">return</span><span style="color: #000000; font-family: 新宋体;">&nbsp;0</span><span style="color: #000000;"><span style="font-family: 新宋体;">;<br>}<br></span></span></div>
<br><br>     <img src ="http://www.cppblog.com/JonsenElizee/aggbug/130045.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-15 16:34 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/15/130045.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Pointers</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/15/129986.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Fri, 15 Oct 2010 02:07:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/15/129986.html</guid><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;argc,&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;argv[])<br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000">{&nbsp;<br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;str[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;{</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Welcome</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">to</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Fortemedia</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Nanjing</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">**</span><span style="COLOR: #000000">&nbsp;ptr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;str&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ptr&nbsp;=&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ptr);<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">1------------------------------</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;str[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ptr</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">*ptr&nbsp;取str+1所指的指针，*ptr&nbsp;+&nbsp;2&nbsp;指向了&#8220;to&#8221;后面的'\0'。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ptr&nbsp;=&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ptr);<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str&nbsp;=&nbsp;[%s]\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">[]</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">2------------------------------</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;str[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">(ptr&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ptr&nbsp;=&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ptr);<br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str&nbsp;=&nbsp;[%s]\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">[Nangjing]</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">3------------------------------</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;str[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ptr[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ptr&nbsp;=&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ptr);<br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str&nbsp;=&nbsp;[%s]\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">[jing]</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">4------------------------------</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">22</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(str[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">&nbsp;str[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">x&nbsp;=&nbsp;%d\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;x);<br></span><span style="COLOR: #008080">24</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;str[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;ptr[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;x;<br></span><span style="COLOR: #008080">25</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">str&nbsp;=&nbsp;[%s]\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;str[</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">]);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">[g]</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">26</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ptr[0]&nbsp;=&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,&nbsp;ptr[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">27</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;puts(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">5------------------------------</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">28</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">29</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></span><span style="COLOR: #008080">30</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span>&nbsp;<span style="COLOR: #000000">}</span></div>
<br><img border=0 src="http://www.cppblog.com/images/cppblog_com/jonsenelizee/snap_0000235.png"><br>
<img src ="http://www.cppblog.com/JonsenElizee/aggbug/129986.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-15 10:07 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/15/129986.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux Debug Tool</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/13/129791.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Wed, 13 Oct 2010 15:45:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/13/129791.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/129791.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/13/129791.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/129791.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/129791.html</trackback:ping><description><![CDATA[<div id="results">
<h4>Categories within Debugging</h4>
<ul>
    <li>
    <h4 class="category"><a  href="http://directory.fsf.org/category/btrack/">Bug tracking systems</a></h4>
    </li>
</ul>
<h4>Projects within Debugging</h4>
<ul>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/ald/">ald</a></h4>
    -
    <em>The Assembly Language Debugger is a tool for debugging executable programs at the assembly level.</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/antiparser/">Antiparser</a></h4>
    -
    <em>a fuzz testing and fault injection API.</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/bashdb/">BASH Debugger</a></h4>
    -
    <em>Patched BASH shell with extra debugging support</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/Benchmark-Timer/">Benchmark-Timer</a></h4>
    -
    <em>Times and becnhmarks portions of code</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/bugbuddy/">Bug-buddy</a></h4>
    -
    <em>GNOME bug-reporting utility</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/BugPort/">BugPort</a></h4>
    -
    <em>QA system for software development</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/BuildBot/">BuildBot</a></h4>
    -
    <em>Automates the compile/test cycle of software development</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/check/">Check</a></h4>
    -
    <em>Unit testing framework for C</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/checker/">checker</a></h4>
    -
    <em>Finds memory errors at runtime</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/clisp/">clisp</a></h4>
    -
    <em>ANSI Common Lisp compiler, debugger, and interpreter</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/CMUCL/">CMUCL</a></h4>
    -
    <em>Free implementation of Common Lisp</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/condenser/">Condenser</a></h4>
    -
    <em>Findis and removes duplicate Java code</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/CrocoPat/">CrocoPat</a></h4>
    -
    <em>Tool for relational querying</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/crossroadsloadbalancer/">Crossroads Load Balancer</a></h4>
    -
    <em>Load balance utility for TCP</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/CUT/">CUT</a></h4>
    -
    <em>Unit-testing framework for C, C++, and Objective-C</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/CxxTest/">CxxTest</a></h4>
    -
    <em>JUnit/CppUnit/xUnit-like framework for C++</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/dbg/">dbg</a></h4>
    -
    <em>C++ debugging utilities</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/DBGClient/">DBG-Client</a></h4>
    -
    <em>Client for the DBG debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/DBGServer/">DBG-Server</a></h4>
    -
    <em>PHP debugger and profiler</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/dbug/">Dbug</a></h4>
    -
    <em>Macro-based debugging library</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/ddd/">ddd</a></h4>
    -
    <em>Graphical front end for command line debuggers</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/dejagnu/">dejaGnu</a></h4>
    -
    <em>Framework to test programs</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/diapergluforth/">diapergluforth</a></h4>
    -
    <em>Testing tool for shared object libraries</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/dvbsnoop/">dvbsnoop</a></h4>
    -
    <em>Presents streamed (live) information in human readable form</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/DynamicProbes/">Dynamic Probes</a></h4>
    -
    <em>Debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/elate/">ELATE</a></h4>
    -
    <em>extensible logging software</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/ElectricFence/">Electric Fence</a></h4>
    -
    <em>Memory debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/f2whelpdesk/">f2w helpdesk </a></h4>
    -
    <em>Web-based helpdesk package</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/gcl/">GCL</a></h4>
    -
    <em>Compiler and interpreter for Common Lisp</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/gdb/">Gdb</a></h4>
    -
    <em>GNU Debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/ggcov/">ggcov</a></h4>
    -
    <em>GUI for reporting test coverage data</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/gnusim8085/">Gnu 8085 Simulator</a></h4>
    -
    <em>A graphical 8085 simulator and assembler with a debugger.</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/gnuVisualDebugger/">GNU Visual Debugger</a></h4>
    -
    <em>Graphical front end for debuggers</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/greg/">Greg</a></h4>
    -
    <em>Software testing framework </em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/GUSS/">GUSS</a></h4>
    -
    <em>Hardware simulator for debugging</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/Javachecker/">Javachecker</a></h4>
    -
    <em>Performs static analysis for Java source file</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/jfunittest/">jf-unittest</a></h4>
    -
    <em>a C++ unit test framework</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/Jlint/">Jlint</a></h4>
    -
    <em>Java debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/kdbg/">kdbg</a></h4>
    -
    <em>GUI for gdb</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/KMD/">KMD</a></h4>
    -
    <em>Multi-processor debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/kodos/">Kodos</a></h4>
    -
    <em>Tool for working with regular expressions</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/ksymoops/">Ksymoops</a></h4>
    -
    <em>Kernel oops and error message decoder</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/linice/">linice</a></h4>
    -
    <em>Source-level kernel debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/log4php/">log4php</a></h4>
    -
    <em>Php port of log4j</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/memcheck/">memcheck</a></h4>
    -
    <em>Memory debugging tool</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/MemCheckDeluxe/">MemCheck Deluxe</a></h4>
    -
    <em>Memory usage tracker and leak finder</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/memwatch/">Memwatch</a></h4>
    -
    <em>Memory leak and corruption detection tool</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/mpatrol/">mpatrol</a></h4>
    -
    <em>Diagnoses run-time errors</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/nemiver/">Nemiver</a></h4>
    -
    <em>a standalone graphical debugger for the GNOME desktop.</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/OTRS/">OTRS</a></h4>
    -
    <em>Ticket request and email management system</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/pcritic/">Perl Critic</a></h4>
    -
    <em>Critiques Perl source code for best-practices</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/PMD/">PMD</a></h4>
    -
    <em>Java source code analyzer</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/pngcheck/">Pngcheck</a></h4>
    -
    <em>PNG integrity tester and dumper/debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/pounder/">Pounder</a></h4>
    -
    <em>Tests Java GUIs</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/pudb/">PuDB</a></h4>
    -
    <em>A full-screen, console-based Python debugger.</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/pvswitch/">pvswitch</a></h4>
    -
    <em>Lets users use different program installations on one machine</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/PyChecker/">PyChecker</a></h4>
    -
    <em>Helps fix bugs in Python source code</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/PyLint/">PyLint</a></h4>
    -
    <em>Pylint analyzes Python source code </em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/QtUnit/">QtUnit</a></h4>
    -
    <em>Testing framework for C++</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/redet/">redet</a></h4>
    -
    <em>Tool for constructing and executing regular expressions</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/RemoteGUD/">Remote GUD</a></h4>
    -
    <em>Lets you debug a program running on a remote host</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/RLog/">RLog</a></h4>
    -
    <em>Message logging facility for C++</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/rpmlint/">rpmlint</a></h4>
    -
    <em>RPM error checker</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/Scmbug/">Scmbug</a></h4>
    -
    <em>Combines any source code version control system with any bug-tracking system</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/sedsed/">Sedsed</a></h4>
    -
    <em>Generates debug files in sed</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/sipsak/">Sipsak</a></h4>
    -
    <em>Tests SIP devices and applications</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/sbcl/">Steel Bank Common Lisp</a></h4>
    -
    <em>Development environment for Common Lisp</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/strace/">Strace</a></h4>
    -
    <em>Debugging tool</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/synopsis/">Synopsis</a></h4>
    -
    <em>multi-language source code introspection tool</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/TCLP/">TCLP</a></h4>
    -
    <em>Type checker for Prolog dialects</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/UDSCollection/">UDS Collection</a></h4>
    -
    <em>C++ development and debugging library </em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/upnpinspector/">UPnP-Inspector</a></h4>
    -
    <em>The Inspector is an UPnP Device and Service analyzer.</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/valgrind/">Valgrind</a></h4>
    -
    <em>Memory debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/winpdb/">Winpdb</a></h4>
    -
    <em>Python debugger</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/xpvm/">xpvm</a></h4>
    -
    <em>Graphical console and monitor for PVM</em>
    </li>
    <li>
    <h4 class="project"><a  href="http://directory.fsf.org/project/Z80-ASM/">Z80-ASM</a></h4>
    -
    <em>Compiler for the Z80 CPU assembler</em>
    </li>
</ul>
</div><img src ="http://www.cppblog.com/JonsenElizee/aggbug/129791.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-13 23:45 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/13/129791.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Get duplicated data number in 0.25 billion data</title><link>http://www.cppblog.com/JonsenElizee/archive/2010/10/11/129407.html</link><dc:creator>JonsenElizee</dc:creator><author>JonsenElizee</author><pubDate>Mon, 11 Oct 2010 01:42:00 GMT</pubDate><guid>http://www.cppblog.com/JonsenElizee/archive/2010/10/11/129407.html</guid><wfw:comment>http://www.cppblog.com/JonsenElizee/comments/129407.html</wfw:comment><comments>http://www.cppblog.com/JonsenElizee/archive/2010/10/11/129407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/JonsenElizee/comments/commentRss/129407.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/JonsenElizee/services/trackbacks/129407.html</trackback:ping><description><![CDATA[Get the data number that is not a duplicated one in 0.25 billion data set. the data in this big data set is of type int. And the limitation is that<br>you can only use 600M memory at most. the time complexity should be O(n).<br><br>For the data type is int, so, the range of data is [0, 2^32).<br>Here is my idea.<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;using&nbsp;two&nbsp;256M&nbsp;array&nbsp;to&nbsp;record&nbsp;the&nbsp;existing&nbsp;state&nbsp;is&nbsp;the&nbsp;best&nbsp;way.&nbsp;</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;duplicated(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;all[],&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;size)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;bit&nbsp;st1[256M]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">},&nbsp;st2[256M]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;{</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">};<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;cnt&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(dat&nbsp;:&nbsp;all)</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;for&nbsp;each&nbsp;data&nbsp;in&nbsp;all-data&nbsp;set</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(dat&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">31</span><span style="COLOR: #000000">)&nbsp;{&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(st1[dat]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;st1[dat]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(st2[dat]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;{&nbsp;st2[dat]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;cnt</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;reset&nbsp;st1&nbsp;and&nbsp;st2;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(dat&nbsp;:&nbsp;all)</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;for&nbsp;each&nbsp;data&nbsp;in&nbsp;all-data&nbsp;set</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(dat&nbsp;</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">31</span><span style="COLOR: #000000">)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dat&nbsp;</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">^</span><span style="COLOR: #000000">31</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(st1[dat]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;st1[dat]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(st2[dat]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;{&nbsp;st2[dat]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;cnt</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;cnt;<br>}<br><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;the&nbsp;time&nbsp;complexity&nbsp;is&nbsp;not&nbsp;a&nbsp;constant,&nbsp;O(1),&nbsp;it's&nbsp;O(2&nbsp;*&nbsp;size/2),&nbsp;it&nbsp;means&nbsp;O(n).&nbsp;</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;space&nbsp;complexity&nbsp;is&nbsp;O(1),&nbsp;a&nbsp;constant&nbsp;size&nbsp;depending&nbsp;on&nbsp;your&nbsp;processing&nbsp;division.</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;if&nbsp;we&nbsp;need&nbsp;to&nbsp;process&nbsp;one&nbsp;billion&nbsp;data&nbsp;with&nbsp;type&nbsp;int,&nbsp;this&nbsp;algorithm&nbsp;can&nbsp;also&nbsp;</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;work&nbsp;well.&nbsp;because&nbsp;the&nbsp;precondition:&nbsp;0&nbsp;&lt;=&nbsp;all[i]&nbsp;&lt;&nbsp;2^32.&nbsp;</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;Whitout&nbsp;real&nbsp;implementation&nbsp;and&nbsp;full&nbsp;test,&nbsp;if&nbsp;any&nbsp;bug,&nbsp;any&nbsp;reply&nbsp;will&nbsp;be&nbsp;</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">&nbsp;appreciated.&nbsp;</span><span style="COLOR: #008000">*/</span></div>
<br>
<img src ="http://www.cppblog.com/JonsenElizee/aggbug/129407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/JonsenElizee/" target="_blank">JonsenElizee</a> 2010-10-11 09:42 <a href="http://www.cppblog.com/JonsenElizee/archive/2010/10/11/129407.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>