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

📄 arithmeticoperator.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>算术运算、型态转换</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip: 算术运算、型态转换</a></h1>

<br>

在C++中提供与算术相关的
加(+)、减(-)、乘(*)、除(/)的运算子,另外还有一个也很常用的余除运算子(%)或称模数(Modulus)运算子,这类以数学运算为主的
运算子,称之为“算术运算子”(Arithmetic operator)。 <br>

<br>

这类运算子的使用基本上由左而右进行运算,遇到加减乘除的顺序问题时,也是先乘除后加减,必要时加上括号表示运算的先后顺序,例如这个程式码会在主控台显
示7: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt; (1 + 2 *
3);</span><br>

</div>

<br>

编译器在读取程式码时,是由左往右读取的,而初学者往往会犯一个错误,例如(1+2+3) /
4,由于我们习惯将分子写在上面,而分母写在下面的方式,使得初学者往往将之写成了: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt; 1+2+3 / 4;</span><br>

</div>

<br>

这个程式事实上会是这样运算的:1+2+(3/4);为了避免这样的错误,在必要的时候为运算式加上括号才是最保险的,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt; (1+2+3) /
4;</span><br>

</div>

<br>

%运算子是余除运算子
它计算除法后的余数,一个例子是若您要产生指定位数的乱数,就可以使用%运算子,假设乱数产生函式为rand(),它可以产生正整数乱数,但您并不知道它
的最大范围是多少,您可以这样产生0到99的乱数: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt; rand() %
100;</span><br>

</div>

<br>

您也可以利用%来作循环计数之用,例如由0计数至9不断循环: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">couter = (counter + 1)
% 10;</span><br>

</div>

<br>

算术运算子使用不难,但要注意型态转换的问题,请您先看看这段程式会印出什么结果? <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int number = 10; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
number / 3;</span><br>

</div>

<br>

答案不是3.3333,而是3,小数点之后的部份被自动消去了,这是因为您的number是整数,而除数3也是整数,运算出来的程式被自动转换为整数了,
那下面这个程式呢?<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double number = 10.0;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
number / 3;</span><br>

</div>

<br>

这个程式的结果会显示3.3333,这是C++的隐式型态转换(Implicit type
conversion),在一个型态混杂的算式中,长度较长的资料型态会成为目标型态,较小的型态会自动提升为目标型态,因而在上例中3会被提升为3.0
再进行运算,结果就可以显示无误,这样的转换又称算术转换(Arithmetic conversion)。<br>

<br>

在一个指定的动作中,左边的数值会成为目标型态,当右边的数值型态比左边的数值型态长度小时,右边的数值会自动提升为目标型态,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int num = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double number =
num;</span><br>

</div>

<br>

在上例中,number的值最后会是10.0,在指定的动作时,如果右边的数值型态比左边的数值型态型态长度大时,超出可储存范围的部份会被自动消去,例
如将浮点数指定给整数变数,则小数的部份会被自动消去,例子如下,程式会显示3而不是3.14:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int&nbsp;num = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double number =
3.14; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">num = number; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt;&nbsp;num;</span><br>

</div>

<br>

由于失去的精度,在编译时编译器会提出警讯:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace; color: rgb(255, 0, 0);">[Warning] converting to
`int' from `double' </span><br>

</div>

<br>

如果确定这个动作是您想要的,那么您可以使用显式型态转换(Explicit type conversion)或称之为强制转型(Cast),例如:<br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">int&nbsp;num = 0; </span><br style="font-weight: bold;">

<span style="font-weight: bold;">double number =
3.14; </span><br>

</div>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">num =
static_cast&lt;int&gt;(number);</span><br>

</div>

<br>

在两个整数型态相除时,您也可以进行型态转换,将其中一个型态转换至double型态再进行运算,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int number = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
static_cast&lt;double&gt;(number) / 3;</span><br>

</div>

<br>

上例中结果会显示3.3333。<br>

<br>

static_cast是Standard C++新增加的转型语法,在这之前显式型态转换可以使用以下的旧语法:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int number = 10;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout &lt;&lt;
(double) number/ 3;</span><br>

</div>

<br>

基于向后相容,Standard
C++仍支援这种旧语法,但鼓励您使用新风格的语法,在新风格语法中还增加有const_cast、reinterpret_cast
与dynamic_cast,适用于不同的转型场合,这在之后还会再介绍。<br>

<br>

<br>

</body>
</html>

⌨️ 快捷键说明

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