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

📄 c83_32.htm

📁 经典c语言教程
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>练习题二</title>
<script language="javascript">

	function init(){
		exp=expdiv.style;
		corp=corpdiv.style;
	}
	function show(obj){
		obj.display='';
	}
	function hide(obj){
		obj.display='none';
	}

</script>
</head>

<body bgcolor="#ccefcc" onLoad="init()">

<blockquote>
  <p>假如 dir 是一个结构数组, 结构的成员是名字, 
  地址和其它信息。假设名字是唯一的, 请编写一个程序, 
  它检索名字并给出相应的信息。由于一个名字是一个字符数组, 
  故我们必须比较各个字母。一种简单的方法是逐一比较各个字母。然而, 
  也有更方便的方法。<br>
  <br>
  位段的应用要表示一个英文字母, 八位似乎太多了, 五位就足够了, 
  请看下表。</p>
  <div align="center"><center><table border="1" width="132" bgcolor="#00FFFF">
    <tr>
      <td width="126">'A'= 00000000<br>
      'B'= 00000001<br>
      'C'= 00000010<br>
      'D'= 00000011<br>
      'E'= 00000100<br>
      ... ...<br>
      ... ...<br>
      'Y'= 00011000<br>
      'Z'= 00011001</td>
    </tr>
  </table>
  </center></div><p>在大多的计算机中, 短整数为 16 位长。因此, 
  你有办法把三个英文字母压缩在一个短整数中吗? 为了简单起见, 
  我们假设头三个字母就是以区分一个名字了。</p>
  <div align="center"><center><table border="6" width="330" cellspacing="0" cellpadding="6"
  height="201" bordercolor="#FF9933">
    <tr>
      <th width="554" bgcolor="#FF9933" height="11">程序</th>
    </tr>
    <tr>
      <td ALIGN="center" width="554" bgcolor="#00FFFF" height="162"><p align="left">struct 
      namecomp<br>
      {<br>
      &nbsp;&nbsp;&nbsp; unsigned a:5,b:5,c:5;<br>
      &nbsp;&nbsp;&nbsp; unsigned l:1;<br>
      };<br>
      ....<br>
      int find(char name[], short len)<br>
      {<br>
      &nbsp;&nbsp;&nbsp; struct namecomp namecp;<br>
      &nbsp;&nbsp;&nbsp; namecp.a=name[0];<br>
      &nbsp;&nbsp;&nbsp; namecp.b=name[1];<br>
      &nbsp;&nbsp;&nbsp; namecp.c=name[2];<br>
      &nbsp;&nbsp;&nbsp; namecp.l=len;<br>
      &nbsp;&nbsp;&nbsp; for (i=0; dir[i].name[0]; i++)<br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dir[i].nc==namecp) <br>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(i);<br>
      &nbsp;&nbsp;&nbsp; return -1;<br>
      }</td>
    </tr>
  </table>
  </center></div><p>这个程序使用位段实现了刚才叙述的想法。显然, 
  dir[i].nc 的类型为 namecomp 类型, dir[i].name[] 为字符串类型。但是, 
  这个程序是错的。你知道错在<a href="javascript:show(exp)">哪里</a>吗?</p>
  <div id="expdiv" style="display:'none'"><p>语句 if (dir[i].nc==namecp) return(i); 
  出错了。</p>
  <p><a href="javascript:show(corp)">这里</a>你可以看到一个正确的程序。</p>
  <div id="corpdiv" style="display:'none'"><div align="center"><center><table border="6"
  width="412" cellspacing="0" cellpadding="6" height="201" bordercolor="#FF9933">
    <tr>
      <th width="636" bgcolor="#FF9933" height="11">正确的程序</th>
    </tr>
    <tr>
      <td ALIGN="center" width="636" bgcolor="#00FFFF" height="162"><p align="left">struct 
      compaux <br>
      {<br>
      &nbsp;&nbsp;&nbsp; unsigned a:5, b:5, c:5;<br>
      &nbsp;&nbsp;&nbsp; unsigned l:l; <br>
      };<br>
      union namecomp <br>
      {<br>
      &nbsp;&nbsp;&nbsp; struct compaux prote;<br>
      &nbsp;&nbsp;&nbsp; unsigned cmp;<br>
      };<br>
      struct<br>
      {<br>
      &nbsp;&nbsp;&nbsp; unsigned nc;<br>
      &nbsp;&nbsp;&nbsp; char address[20];<br>
      } dir[1000];<br>
      int find(char name[0], short len)<br>
      {<br>
      &nbsp;&nbsp;&nbsp; union namecomp namecp;<br>
      &nbsp;&nbsp;&nbsp; namecp.prote.a=name[0];<br>
      &nbsp;&nbsp;&nbsp; namecp.prote.b=name[1];<br>
      &nbsp;&nbsp;&nbsp; namecp.prote.c=name[2];<br>
      &nbsp;&nbsp;&nbsp; namecp.prote.l=len;<br>
      &nbsp;&nbsp;&nbsp; ... ...<br>
      &nbsp;&nbsp;&nbsp; if (dir[i].nc==namecp.cmp) return i;<br>
      &nbsp;&nbsp;&nbsp; return -1;<br>
      }</td>
    </tr>
  </table>
  </center></div></div><p><br>
  位运算符也是经常使用的:&nbsp;&nbsp; &amp; | ^ &gt;&gt; &lt;&lt;<br>
  </p>
  </div><p align="center"><a href="javascript:close()">关闭</a></p>
</blockquote>
</body>
</html>

⌨️ 快捷键说明

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