parse_8c-source.html
来自「仿真人工智能是指用人工的方法和技术」· HTML 代码 · 共 189 行
HTML
189 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Parse.C Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body bgcolor="#ffffff"><!-- Generated by Doxygen 1.2.12 --><center><a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> </center><hr><h1>Parse.C</h1><a href="Parse_8C.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">/*</font>00002 <font class="comment">Copyright (c) 2000,2001, Jelle Kok, University of Amsterdam</font>00003 <font class="comment">All rights reserved.</font>00004 <font class="comment"></font>00005 <font class="comment">Redistribution and use in source and binary forms, with or without </font>00006 <font class="comment">modification, are permitted provided that the following conditions are met:</font>00007 <font class="comment"></font>00008 <font class="comment">1. Redistributions of source code must retain the above copyright notice, this </font>00009 <font class="comment">list of conditions and the following disclaimer. </font>00010 <font class="comment"></font>00011 <font class="comment">2. Redistributions in binary form must reproduce the above copyright notice, </font>00012 <font class="comment">this list of conditions and the following disclaimer in the documentation </font>00013 <font class="comment">and/or other materials provided with the distribution. </font>00014 <font class="comment"></font>00015 <font class="comment">3. Neither the name of the University of Amsterdam nor the names of its </font>00016 <font class="comment">contributors may be used to endorse or promote products derived from this </font>00017 <font class="comment">software without specific prior written permission. </font>00018 <font class="comment"></font>00019 <font class="comment">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" </font>00020 <font class="comment">AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE </font>00021 <font class="comment">IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE </font>00022 <font class="comment">DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE </font>00023 <font class="comment">FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL </font>00024 <font class="comment">DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR </font>00025 <font class="comment">SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER </font>00026 <font class="comment">CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, </font>00027 <font class="comment">OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE </font>00028 <font class="comment">OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</font>00029 <font class="comment">*/</font>00042 <font class="preprocessor">#include "<a class="code" href="Parse_8h.html">Parse.h</a>"</font>00043 00044 <font class="preprocessor">#include <ctype.h></font> <font class="comment">// needed for isdigit</font>00045 <font class="preprocessor">#include <math.h></font> <font class="comment">// needed for pow</font>00046 <font class="preprocessor">#include <string.h></font> <font class="comment">// needed for strlen</font>00047 00048 <font class="comment">/******************************************************************************/</font>00049 <font class="comment">/*********************** CLASS PARSE ******************************************/</font>00050 <font class="comment">/******************************************************************************/</font>00051 <a name="l00058"></a><a class="code" href="classParse.html#d4">00058</a> <font class="keywordtype">char</font> <a class="code" href="classParse.html#d4">Parse::gotoFirstNonSpace</a>( <font class="keywordtype">char</font>** strMsg )00059 {00060 <font class="keywordflow">while</font>(*strMsg && isspace(**strMsg) )00061 (*strMsg)++;00062 <font class="keywordflow">return</font> (*strMsg) ? **strMsg : <font class="charliteral">'\0'</font>;00063 00064 }00065 <a name="l00074"></a><a class="code" href="classParse.html#d2">00074</a> <font class="keywordtype">char</font> <a class="code" href="classParse.html#d2">Parse::gotoFirstSpaceOrClosingBracket</a>( <font class="keywordtype">char</font>** strMsg )00075 {00076 <font class="keywordflow">while</font>( *strMsg && **strMsg!=<font class="charliteral">' '</font> && **strMsg!=<font class="charliteral">')'</font> )00077 (*strMsg)++;00078 <font class="keywordflow">return</font> (*strMsg) ? **strMsg : <font class="charliteral">'\0'</font>;00079 }00080 <a name="l00087"></a><a class="code" href="classParse.html#d3">00087</a> <font class="keywordtype">int</font> <a class="code" href="classParse.html#d3">Parse::gotoFirstOccurenceOf</a>( <font class="keywordtype">char</font> c, <font class="keywordtype">char</font>** strMsg )00088 {00089 <font class="keywordtype">int</font> i=0;00090 <font class="keywordflow">while</font>(**strMsg && **strMsg != c )00091 {00092 i++;00093 (*strMsg)++;00094 }00095 <font class="keywordflow">if</font>( ! **strMsg )00096 <font class="keywordflow">return</font> -1;00097 <font class="keywordflow">return</font> i;00098 }00099 <a name="l00107"></a><a class="code" href="classParse.html#d1">00107</a> <font class="keywordtype">int</font> <a class="code" href="classParse.html#d1">Parse::parseFirstInt</a>( <font class="keywordtype">char</font>** strMsg )00108 {00109 <font class="keywordtype">int</font> iRes = 0;00110 <font class="keywordtype">bool</font> bIsMin= <font class="keyword">false</font>;00111 <font class="keywordtype">char</font> *str = *strMsg;00112 00113 <font class="keywordflow">while</font>( *str != <font class="charliteral">'\0'</font> && !isdigit(*str) && *str!=<font class="charliteral">'-'</font>)00114 str++; <font class="comment">// walk to first non digit or minus sign</font>00115 00116 00117 <font class="keywordflow">if</font>( *str != <font class="charliteral">'\0'</font> && *str==<font class="charliteral">'-'</font>) <font class="comment">// if it was a minus sign, remember</font>00118 {00119 bIsMin=<font class="keyword">true</font>;00120 str++;00121 }00122 00123 <font class="keywordflow">while</font>( *str != <font class="charliteral">'\0'</font> && *str<='9' && *str>=<font class="charliteral">'0'</font> ) <font class="comment">// for all digits</font>00124 {00125 iRes=iRes*10+(int)(*str-<font class="charliteral">'0'</font>); <font class="comment">// multiply old res with 10</font>00126 str++; <font class="comment">// and add new value</font>00127 }00128 *strMsg = str;00129 <font class="keywordflow">return</font> (bIsMin) ? -iRes : iRes;00130 }00131 <a name="l00140"></a><a class="code" href="classParse.html#d0">00140</a> <font class="keywordtype">double</font> <a class="code" href="classParse.html#d0">Parse::parseFirstDouble</a>( <font class="keywordtype">char</font>** strMsg )00141 {00142 <font class="keywordtype">double</font> dRes=0.0, dFrac=1.0;00143 <font class="keywordtype">bool</font> bIsMin=<font class="keyword">false</font>, bInDecimal=<font class="keyword">false</font>, bCont=<font class="keyword">true</font>;00144 <font class="keywordtype">char</font> *str = *strMsg;00145 00146 <font class="comment">// go to first part of double (digit, minus sign or '.')</font>00147 <font class="keywordflow">while</font>( *str != <font class="charliteral">'\0'</font> && !isdigit(*str) && *str!=<font class="charliteral">'-'</font> && *str!=<font class="charliteral">'.'</font>)00148 {00149 <font class="comment">// when NaN or nan is double value, return -1000.0</font>00150 <font class="keywordflow">if</font>( (*str==<font class="charliteral">'N'</font> && strlen(str)>3 && *(str+1)==<font class="charliteral">'a'</font> && *(str+2)==<font class="charliteral">'N'</font>) ||00151 (*str==<font class="charliteral">'n'</font> && strlen(str)>3 && *(str+1)==<font class="charliteral">'a'</font> && *(str+2)==<font class="charliteral">'n'</font>) )00152 {00153 *strMsg = str+3;00154 <font class="keywordflow">return</font> -1000.0;00155 }00156 <font class="keywordflow">else</font>00157 str++;00158 }00159 00160 <font class="keywordflow">if</font>( *str != <font class="charliteral">'\0'</font> && *str==<font class="charliteral">'-'</font>) <font class="comment">// if minus sign, remember that</font>00161 {00162 bIsMin=<font class="keyword">true</font>;00163 str++;00164 }00165 00166 <font class="keywordflow">while</font>( *str != <font class="charliteral">'\0'</font> && bCont) <font class="comment">// process the number bit by bit</font>00167 {00168 <font class="keywordflow">if</font>( *str==<font class="charliteral">'.'</font> ) <font class="comment">// in decimal part after '.'</font>00169 bInDecimal = <font class="keyword">true</font>;00170 <font class="keywordflow">else</font> <font class="keywordflow">if</font>( bInDecimal && *str<='9' && *str>=<font class="charliteral">'0'</font>) <font class="comment">// number and in decimal</font>00171 {00172 dFrac=dFrac*10.0; <font class="comment">// shift decimal part to right</font>00173 dRes += (double)(*str-<font class="charliteral">'0'</font>)/dFrac; <font class="comment">// and add number</font>00174 }00175 <font class="keywordflow">else</font> <font class="keywordflow">if</font>( *str<='9' && *str>=<font class="charliteral">'0'</font> ) <font class="comment">// if number and not decimal</font>00176 dRes=dRes*10+(double)(*str-<font class="charliteral">'0'</font>); <font class="comment">// shift left and add number</font>00177 <font class="keywordflow">else</font> <font class="keywordflow">if</font>( *str==<font class="charliteral">'e'</font> ) <font class="comment">// 10.6e-08 // if to power e</font>00178 {00179 <font class="keywordflow">if</font>( *(str+1) == <font class="charliteral">'+'</font> ) <font class="comment">// determine sign</font>00180 dRes *= pow(10, <a class="code" href="classParse.html#d1">parseFirstInt</a>(&str)); <font class="comment">// and multiply with power</font>00181 <font class="keywordflow">else</font> <font class="keywordflow">if</font>( *(str+1) == <font class="charliteral">'-'</font> )00182 {00183 str = str+2; <font class="comment">// skip -</font>00184 dRes /= pow(10, <a class="code" href="classParse.html#d1">parseFirstInt</a>(&str)); <font class="comment">// and divide by power</font>00185 }00186 bCont = <font class="keyword">false</font>; <font class="comment">// after 'e' stop</font>00187 }00188 <font class="keywordflow">else</font>00189 bCont = <font class="keyword">false</font>; <font class="comment">// all other cases stop</font>00190 00191 <font class="keywordflow">if</font>( bCont == <font class="keyword">true</font> ) <font class="comment">// update when not stopped yet</font>00192 str++;00193 }00194 *strMsg = str;00195 <font class="keywordflow">return</font> (bIsMin && dRes != 0.0) ? -dRes : dRes;00196 }00197 00198 <font class="comment">/******************************************************************************/</font>00199 <font class="comment">/********************** TESTING PURPOSES *************************************/</font>00200 <font class="comment">/******************************************************************************/</font>00201 <font class="comment">/*</font>00202 <font class="comment">#include<iostream.h></font>00203 <font class="comment"></font>00204 <font class="comment">int main( void )</font>00205 <font class="comment">{</font>00206 <font class="comment"> double d = 13.6e+15;</font>00207 <font class="comment"> char str[] = "13.6e+15";</font>00208 <font class="comment"> double d2 = 13.6e-15;</font>00209 <font class="comment"> char str2[] = "13.6e-15";</font>00210 <font class="comment"></font>00211 <font class="comment"> char *strmsg;</font>00212 <font class="comment"> strmsg = &str[0];</font>00213 <font class="comment"> cout << d << endl;</font>00214 <font class="comment"> cout << Parse::parseFirstDouble( &strmsg )<< endl;</font>00215 <font class="comment"> strmsg = &str2[0];</font>00216 <font class="comment"> cout << d2 << endl;</font>00217 <font class="comment"> cout << Parse::parseFirstDouble( &strmsg ) << endl;</font>00218 <font class="comment"> return 0;</font>00219 <font class="comment">}</font>00220 <font class="comment">*/</font></pre></div><hr><address><small>Generated on Thu Mar 7 00:37:42 2002 for UvA Trilearn 2001 by<a href="http://www.doxygen.org/index.html"><img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.12 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2001</small></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?