www.hj6666.comD语言赋值运算符

算法 ww9992019com 发表于 1 周前

www.hj6666.com开户请拨15894452155警方通报操场埋尸上海卢湾区ULWwli<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="2605">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

下列是D语言支持赋值操作符:</p>

<table class="src">

<tbody>

    <tr>

        <th>

            运算符</th>

        <th>

            描述</th>

        <th>

            示例</th>

    </tr>

    <tr>

        <td>

            =</td>

        <td>

            简单赋值运算符,分配从右侧操作数的值,以左侧操作数</td>

        <td>

            C = A + B will assign value of A + B into C</td>

    </tr>

    <tr>

        <td>

            +=</td>

        <td>

            添加和赋值操作符,它增加了右操作数为左操作数和结果分配给左操作数</td>

        <td>

            C += A is equivalent to C = C + A</td>

    </tr>

    <tr>

        <td>

            -=</td>

        <td>

            减和赋值操作符,它减去右边的操作数从左边的操作数,并将结果赋值给左操作数</td>

        <td>

            C -= A is equivalent to C = C - A</td>

    </tr>

    <tr>

        <td>

            *=</td>

        <td>

            乘法和赋值操作符,它乘以右边的操作数与左操作数和结果分配给左操作数</td>

        <td>

            C *= A is equivalent to C = C * A</td>

    </tr>

    <tr>

        <td>

            /=</td>

        <td>

            除法和赋值操作符,它分为左操作数与右边的操作数,并将结果赋值给左操作数</td>

        <td>

            C /= A is equivalent to C = C / A</td>

    </tr>

    <tr>

        <td>

            %=</td>

        <td>

            模量和赋值操作符,它采用模使用两个操作数和结果分配给左操作数</td>

        <td>

            C %= A is equivalent to C = C % A</td>

    </tr>

    <tr>

        <td>

            &lt;&lt;=</td>

        <td>

            左移位并赋值运算符</td>

        <td>

            C &lt;&lt;= 2 is same as C = C &lt;&lt; 2</td>

    </tr>

    <tr>

        <td>

            &gt;&gt;=</td>

        <td>

            向右移位并赋值运算符</td>

        <td>

            C &gt;&gt;= 2 is same as C = C &gt;&gt; 2</td>

    </tr>

    <tr>

        <td>

            &amp;=</td>

        <td>

            按位AND赋值运算符</td>

        <td>

            C &amp;= 2 is same as C = C &amp; 2</td>

    </tr>

    <tr>

        <td>

            ^=</td>

        <td>

            按位异或和赋值运算符</td>

        <td>

            C ^= 2 is same as C = C ^ 2</td>

    </tr>

    <tr>

        <td>

            |=</td>

        <td>

            OR运算和赋值运算符</td>

        <td>

            C |= 2 is same as C = C | 2</td>

    </tr>

</tbody>

</table>

<h2>

示例</h2>

<p>

试试下面的例子就明白了提供D编程语言的所有赋值运算符:</p>

<pre class="prettyprint tryit prettyprinted">

<span class="kwd">import</span><span class="pln"> std</span><span class="pun">.</span><span class="pln">stdio</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> main</span><span class="pun">(</span><span class="kwd">string</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln">

</span><span class="pun">{</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="lit">21</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> c </span><span class="pun">;</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 1 - = Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">+=</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 2 - += Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">-=</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 3 - -= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">*=</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 4 - *= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">/=</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 5 - /= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> </span><span class="lit">200</span><span class="pun">;</span><span class="pln">

c </span><span class="pun">=</span><span class="pln"> c </span><span class="pun">%</span><span class="pln"> a</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 6 - %s= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="str">'x25'</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">&lt;&lt;=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 7 - &lt;&lt;= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">&gt;&gt;=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 8 - &gt;&gt;= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">&amp;=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 9 - &amp;= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">^=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 10 - ^= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

c </span><span class="pun">|=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">

writefln</span><span class="pun">(</span><span class="str">"Line 11 - |= Operator Example, Value of c = %d
"</span><span class="pun">,</span><span class="pln"> c </span><span class="pun">);</span><span class="pln">

</span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

当编译并执行上面的程序它会产生以下结果:</p>

<pre class="result">

Line 1 - = Operator Example, Value of c = 21

Line 2 - += Operator Example, Value of c = 42

Line 3 - -= Operator Example, Value of c = 21

Line 4 - *= Operator Example, Value of c = 441

Line 5 - /= Operator Example, Value of c = 21

Line 6 - %= Operator Example, Value of c = 11

Line 7 - &lt;&lt;= Operator Example, Value of c = 44

Line 8 - &gt;&gt;= Operator Example, Value of c = 11

Line 9 - &amp;= Operator Example, Value of c = 2

Line 10 - ^= Operator Example, Value of c = 0

Line 11 - |= Operator Example, Value of c = 2</pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="D语言位运算符"><i class="layui-icon"></i>
                            D语言位运算符</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:D语言sizeof运算符"><i class="layui-icon"></i>
                            D语言sizeof运算符</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复