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

📄 format.htm

📁 自编的一个String类
💻 HTM
字号:
<html><head><meta http-equiv="Content-Language" content="en-nz"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Format class</title><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><link rel="stylesheet" type="text/css" href="rbd.css"></head><body><h1 align="center"><a name="Top"></a>Format class</h1><p><b>July, 2001</b></p><ul>  <li><a href="#Introduction">Introduction</a></li>  <li><a href="#Example">Example</a></li>  <li><a href="ol_doc.htm">To online documentation page</a></li>  <li><a href="string.htm">To description of string library</a></li></ul><h2><a name="Introduction"></a>Introduction</h2><p>The Format class is distributed as part of the <a href="string.htm">stringlibrary</a>. See the string library documentation on how to customise the<tt>include.h</tt> file for your compiler and for the file list.</p><p>This class is intended to provide a more convenient facility for formatting numerical datathan is provided by the standard IO classes.</p><p>A typical application would look like this</p><pre>   Format F1, F2;   ... load attributes into F1, F2;   cout &lt;&lt; F1 &lt;&lt; x &lt;&lt; y &lt;&lt; F2 &lt;&lt; z &lt;&lt; endl;</pre><p>Format <i>F1</i> would be applied to <i>x</i> and <i>y</i> and <i> F2</i> to <i>z</i>.</p><p>The variables <i>x</i>, <i>y</i> and <i>z</i> can be any of the followingtypes: char, char*, String, int, unsigned int, long, unsigned long, double,float.</p><p>The manipulators <i>endl</i>, <i>ends</i> and <i>flush</i> are passeddirectly through to the ostream (such as <i>cout</i>).</p><p>One can also use the code</p><pre>   OstreamWithFormat f1(cout, F1);   OstreamWithFormat f2(cout, F2);   f1 &lt;&lt; x &lt;&lt; y; f2 &lt;&lt; z &lt;&lt; endl;</pre><p>The attributes are loaded with statements like</p><pre>   F1.Precision(2);</pre><p>The following table shows the attributes</p><table border="1" cellspacing="0" width="100%" cellpadding="2">  <tr>    <td width="20%" height="23" valign="top"><b>attribute</b></td>    <td width="20%" height="23" valign="top"><b>default value</b></td>    <td width="20%" height="23" valign="top"><b>possible values</b></td>    <td width="40%" height="23" valign="top"><b>description</b></td>  </tr>  <tr>    <td width="20%" height="22" valign="top">MinWidth</td>    <td width="20%" height="22" valign="top">0</td>    <td width="20%" height="22" valign="top">non-negative integer</td>    <td width="40%" height="22" valign="top">minimum field width</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">MaxWidth</td>    <td width="20%" height="22" valign="top">12</td>    <td width="20%" height="22" valign="top">positive integer</td>    <td width="40%" height="22" valign="top">maximum field width</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Width</td>    <td width="20%" height="22" valign="top">&nbsp;</td>    <td width="20%" height="22" valign="top">positive integer</td>    <td width="40%" height="22" valign="top">sets both MinWidth      and MaxWidth</td>  </tr>  <tr>    <td width="20%" height="44" rowspan="2" valign="top">OverFlowPolicy</td>    <td width="20%" height="44" rowspan="2" valign="top">Format::E</td>    <td width="20%" height="22" valign="top">Format::E</td>    <td width="40%" height="22" valign="top">on overflow use      scientific format</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::HASH</td>    <td width="40%" height="22" valign="top">on overflow replace      field with # characters</td>  </tr>  <tr>    <td width="20%" height="66" rowspan="3" valign="top">UnderFlowPolicy</td>    <td width="20%" height="66" rowspan="3" valign="top">Format::ZERO</td>    <td width="20%" height="22" valign="top">Format::E</td>    <td width="40%" height="22" valign="top">on underflow use      scientific format</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::HASH</td>    <td width="40%" height="22" valign="top">on underflow replace      field with # characters</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::ZERO</td>    <td width="40%" height="22" valign="top">reduce precision</td>  </tr>  <tr>    <td width="20%" height="88" rowspan="4" valign="top">Alignment</td>    <td width="20%" height="88" rowspan="4" valign="top">Format::RIGHT</td>    <td width="20%" height="22" valign="top">Format::LEFT</td>    <td width="40%" height="22" valign="top">left alignment</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::RIGHT</td>    <td width="40%" height="22" valign="top">right alignment</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::CENTRE</td>    <td width="40%" height="44" rowspan="2" valign="top">centre      (center) alignment</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::CENTER</td>  </tr>  <tr>    <td width="20%" height="88" rowspan="4" valign="top">FormatType</td>    <td width="20%" height="88" rowspan="4" valign="top">Format::SIG_FIGS</td>    <td width="20%" height="22" valign="top">Format::SIG_FIGS</td>    <td width="40%" height="22" valign="top">Precision shows      number of significant figures</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::DEC_FIGS</td>    <td width="40%" height="22" valign="top">Precision shows      number of decimal figures</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::SCIENTIFIC</td>    <td width="40%" height="22" valign="top">Scientific (E)      format</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::INTEGER</td>    <td width="40%" height="22" valign="top">Round to an integer</td>  </tr>  <tr>    <td width="20%" height="66" rowspan="3" valign="top">Positive</td>    <td width="20%" height="66" rowspan="3" valign="top">Format::NX</td>    <td width="20%" height="22" valign="top">Format::PLUS</td>    <td width="40%" height="22" valign="top">non-negative numbers      preceded by +</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::SPACE</td>    <td width="40%" height="22" valign="top">non-negative numbers      preceded by space</td>  </tr>  <tr>    <td width="20%" height="22" valign="top">Format::NX</td>    <td width="40%" height="22" valign="top">no additional      character preceding non-negative number </td>  </tr>  <tr>    <td width="20%" height="15" valign="top">Precision</td>    <td width="20%" height="15" valign="top">2</td>    <td width="20%" height="15" valign="top">non-negative integer</td>    <td width="40%" height="15" valign="top">number of decimal or      signficant figures</td>  </tr>  <tr>    <td width="20%" height="15" valign="top">Prefix</td>    <td width="20%" height="15" valign="top">&quot;&quot;</td>    <td width="20%" height="15" valign="top">String or char*</td>    <td width="40%" height="15" valign="top">prefix string</td>  </tr>  <tr>    <td width="20%" height="15" valign="top">Suffix</td>    <td width="20%" height="15" valign="top">&quot;&quot;</td>    <td width="20%" height="15" valign="top">String or char*</td>    <td width="40%" height="15" valign="top">suffix string</td>  </tr>  <tr>    <td width="20%" height="15" valign="top"><span lang="en-nz">    Separator</span></td>    <td width="20%" height="15" valign="top"><span lang="en-nz">    &quot;,&quot;</span></td>    <td width="20%" height="15" valign="top"><span lang="en-nz">    String or char*</span></td>    <td width="40%" height="15" valign="top"><span lang="en-nz">    separator string</span></td>  </tr></table><p><i>MinWidth</i> sets the minimum field width and <i> MaxWidth</i> the maximum field width.The program uses the smallest width required to present the number to therequired precision and format type. If this is less than the minimum field widthspaces are inserted to reach the desired field width. If the number won't fitwithin the maximum field width the action taken depends on the overflow orunderflow policy.</p><p>The <i> Prefix</i> and <i> Suffix</i> strings are printed before and after the field. Theirspace is in addition to the space set by <i> MinWidth</i> and <i>MaxWidth</i>.</p><p><span lang="en-nz">The <i>Separator</i> string is for use by other libraries when outputting data structures. </span></p><p>Integer objects use only the width, alignment, over flow policy, prefixand suffix properties. Convert an integer to double if you really want a decimalor scientific format type.</p><p><i>char*</i> and <i>String</i> objects use only the width, alignment, prefixand suffix properties. Special characters such as <tt>\n</tt> are unlikely to be handledcorrectly.</p><p><i>char</i> objects always use a field width of 1 and ignore theformat properties.</p><p>To use the format library you need <tt>#include &quot;format.h&quot;</tt> in your .cppfiles.</p><h2><a name="Example"></a>Example</h2><pre>   // Scientific format   Format SCI;   SCI.FormatType(Format::SCIENTIFIC);   SCI.Precision(8);   SCI.Width(14);   SCI.Suffix(&quot;|&quot;);   // Decimal format - switch to scientific on under-flow or over-flow   Format DEC1;   DEC1.UnderFlowPolicy(Format::E);   DEC1.FormatType(Format::DEC_FIGS);   DEC1.Precision(4);   DEC1.Width(12);   DEC1.Suffix(&quot;|&quot;);   // Decimal format - no action on under-flow; hashes on over-flow   Format DEC2;   DEC2.OverFlowPolicy(Format::HASH);   DEC2.FormatType(Format::DEC_FIGS);   DEC2.Precision(4);   DEC2.Width(12);   DEC2.Suffix(&quot;|&quot;);   // Significant figures - switch to scientific on under-flow or over-flow    Format SIG1;   SIG1.UnderFlowPolicy(Format::E);   SIG1.FormatType(Format::SIG_FIGS);   SIG1.Precision(8);   SIG1.Width(11);   SIG1.Positive(Format::SPACE);   SIG1.Prefix(&quot; &quot;);   SIG1.Suffix(&quot;|&quot;);   // Significant figures - reduce precision on under-flow; hashes on over-flow   Format SIG2;   SIG2.OverFlowPolicy(Format::HASH);   SIG2.FormatType(Format::SIG_FIGS);   SIG2.Precision(8);   SIG2.Width(11);   SIG2.Positive(Format::SPACE);   SIG2.Prefix(&quot; &quot;);   SIG2.Suffix(&quot;|&quot;);   // Integer format - switch to scientific on over-flow   Format INT;   INT.FormatType(Format::INTEGER);   INT.Width(12);   double x = 1.23456789;   double mult[] = {1E-100, 1E-20, 0.0001, 0.1, 1, 10, 1000, 1E7,      1E10, 1E20, 1E100};   // print column headings   cout &lt;&lt; SCI &lt;&lt; &quot;SCIENTFIC&quot; &lt;&lt; DEC1 &lt;&lt; &quot;DEC 1&quot; &lt;&lt; DEC2 &lt;&lt; &quot;DEC 2&quot;      &lt;&lt; SIG1 &lt;&lt; &quot;SIG 1&quot; &lt;&lt; SIG2 &lt;&lt; &quot;SIG 2&quot; &lt;&lt; INT &lt;&lt; &quot;INTEGER&quot; &lt;&lt; endl;   for (int i = 0; i &lt; 11; ++i)   {      double y = x * mult[i];      // print same number in a variety of formats      cout &lt;&lt; SCI &lt;&lt; y &lt;&lt; DEC1 &lt;&lt; y &lt;&lt; DEC2 &lt;&lt; y &lt;&lt; SIG1 &lt;&lt; y         &lt;&lt; SIG2 &lt;&lt; y &lt;&lt; INT &lt;&lt; y &lt;&lt; endl;   }</pre><p>produces this output</p><pre>    SCIENTIFIC|       DEC 1|       DEC 2|       SIG 1|       SIG 2|     INTEGER1.2345679e-100| 1.2346e-100|      0.0000|  1.235e-100|  0.00000000|           0 1.2345679e-20|  1.2346e-20|      0.0000|  1.2346e-20|  0.00000000|           0 1.2345679e-04|  1.2346e-04|      0.0001|  1.2346e-04|  0.00012346|           0 1.2345679e-01|  1.2346e-01|      0.1235|  0.12345679|  0.12345679|           0 1.2345679e+00|      1.2346|      1.2346|   1.2345679|   1.2345679|           1 1.2345679e+01|     12.3457|     12.3457|   12.345679|   12.345679|          12 1.2345679e+03|   1234.5679|   1234.5679|   1234.5679|   1234.5679|        1235 1.2345679e+07|1.234568e+07|############|    12345679|    12345679|    12345679 1.2345679e+10|1.234568e+10|############|  1.2346e+10| ###########| 12345678900 1.2345679e+20|1.234568e+20|############|  1.2346e+20| ###########|1.234568e+201.2345679e+100|1.23457e+100|############|  1.235e+100| ###########|1.23457e+100</pre><ul>  <li><a href="#Top">Go to top</a></li>  <li><a href="ol_doc.htm">To online documentation page</a></li>  <li><a href="string.htm">To description of string library</a></li></ul></body></html>

⌨️ 快捷键说明

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