问题描述:
阅读下面的程序,写出输出结果
(1)
<code class="hljs cpp has-numbering"><strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;"><span class="hljs-preprocessor">#include <iostream ></span> <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>; <span class="hljs-keyword">int</span> a[<span class="hljs-number">10</span>]= {<span class="hljs-number">1</span>,<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>}; <span class="hljs-keyword">int</span> fun( <span class="hljs-keyword">int</span> i); <span class="hljs-keyword">int</span> main() { <span class="hljs-keyword">int</span> i ,s=<span class="hljs-number">0</span>; <span class="hljs-keyword">for</span>( i=<span class="hljs-number">0</span>; i<=<span class="hljs-number">10</span>; i++) { <span class="hljs-keyword">try</span> { s=s+fun(i); } <span class="hljs-keyword">catch</span>(<span class="hljs-keyword">int</span>) { <span class="hljs-built_in">cout</span><<<span class="hljs-string">"数组下标越界!"</span><<endl; } } <span class="hljs-built_in">cout</span><<<span class="hljs-string">"s="</span><<s<<endl; <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; } <span class="hljs-keyword">int</span> fun( <span class="hljs-keyword">int</span> i) { <span class="hljs-keyword">if</span>(i>=<span class="hljs-number">10</span>) <span class="hljs-keyword">throw</span> i; <span class="hljs-keyword">return</span> a[i]; }</span></strong></code><ul style="FILTER: ; ZOOM: 1" class="pre-numbering"><li><strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;">1</span></strong></li></ul>
预计运行结果:
数组下标越界!
s=55
实际运行结果:
(2)
<code class="hljs cpp has-numbering"><strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;"><span class="hljs-preprocessor">#include <iostream></span> <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>; <span class="hljs-keyword">namespace</span> CounterNameSpace { <span class="hljs-keyword">int</span> upperbound; <span class="hljs-keyword">int</span> lowerbound; <span class="hljs-keyword">class</span> counter { <span class="hljs-keyword">int</span> count; <span class="hljs-keyword">public</span>: counter(<span class="hljs-keyword">int</span> n) { <span class="hljs-keyword">if</span> (n <= upperbound ) { count = n; } <span class="hljs-keyword">else</span> { count = upperbound; } } <span class="hljs-keyword">void</span> reset(<span class="hljs-keyword">int</span> n) { <span class="hljs-keyword">if</span> (n < upperbound) { count = n; } } <span class="hljs-keyword">int</span> run() { <span class="hljs-keyword">if</span> (count > lowerbound) { <span class="hljs-keyword">return</span> count--; } <span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> lowerbound; } }; } <span class="hljs-keyword">int</span> main() { CounterNameSpace::upperbound = <span class="hljs-number">100</span>; CounterNameSpace::lowerbound = <span class="hljs-number">0</span>; CounterNameSpace::counter ob1(<span class="hljs-number">10</span>); <span class="hljs-keyword">int</span> i; <span class="hljs-keyword">do</span> { i = ob1.run(); <span class="hljs-built_in">cout</span> << i << <span class="hljs-string">" "</span>; } <span class="hljs-keyword">while</span> (i > CounterNameSpace::lowerbound); <span class="hljs-built_in">cout</span> << endl; CounterNameSpace::counter ob2(<span class="hljs-number">20</span>); <span class="hljs-keyword">do</span> { i = ob2.run(); <span class="hljs-built_in">cout</span> << i << <span class="hljs-string">" "</span>; } <span class="hljs-keyword">while</span> (i > CounterNameSpace::lowerbound); <span class="hljs-built_in">cout</span> << endl; ob2.reset(<span class="hljs-number">100</span>); <span class="hljs-keyword">do</span> { i = ob2.run(); <span class="hljs-built_in">cout</span> << i << <span class="hljs-string">" "</span>; } <span class="hljs-keyword">while</span> (i > CounterNameSpace::lowerbound); <span class="hljs-built_in">cout</span> << endl; <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }</span></strong></code><ul style="FILTER: ; ZOOM: 1" class="pre-numbering"><li><strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;">1</span></strong></li></ul>
预计运行结果:
10 9 8 7 6 5 4 3 2 1 0
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1 0
0
实际运行结果:
(3)将(2)中的main函数换作如下形式,其余不变
<code class="hljs cpp has-numbering"><strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;"><span class="hljs-keyword">int</span> main() { <span class="hljs-keyword">using</span> CounterNameSpace::upperbound; upperbound = <span class="hljs-number">100</span>; <span class="hljs-comment">//(a)</span> CounterNameSpace::lowerbound = <span class="hljs-number">0</span>; <span class="hljs-comment">//(b)</span> CounterNameSpace::counter ob1(<span class="hljs-number">10</span>); <span class="hljs-keyword">int</span> i; <span class="hljs-keyword">do</span> { i = ob1.run(); <span class="hljs-built_in">cout</span> << i<<<span class="hljs-string">" "</span>; } <span class="hljs-keyword">while</span>( i > CounterNameSpace::lowerbound); <span class="hljs-built_in">cout</span> << endl; <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> CounterNameSpace; counter ob2(<span class="hljs-number">20</span>); <span class="hljs-keyword">do</span> { i = ob2.run(); <span class="hljs-built_in">cout</span> << i<<<span class="hljs-string">" "</span>; } <span class="hljs-keyword">while</span>( i > CounterNameSpace::lowerbound); <span class="hljs-comment">//(c)</span> <span class="hljs-built_in">cout</span> << endl; ob2.reset(<span class="hljs-number">100</span>); lowerbound = <span class="hljs-number">90</span>; <span class="hljs-comment">//(d)</span> <span class="hljs-keyword">do</span> { i = ob2.run(); <span class="hljs-built_in">cout</span> << i <<<span class="hljs-string">" "</span>; } <span class="hljs-keyword">while</span>( i > lowerbound); <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }</span></strong></code><ul style="FILTER: ; ZOOM: 1" class="pre-numbering"><li><strong><span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;">1</span></strong></li></ul>
预计运行结果:
10 9 8 7 6 5 4 3 2 1 0
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1 0
90
实际运行结果:
请回答:
(a)(d)处:为什么可以省去CounterNameSpace::?
因为之前使用过了!
(b)(c)处:是否可以省去CounterNameSpace::?
能!
时间: 2024-11-13 09:58:43