<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # C++ 程序:將二進制數轉換為十進制,反之亦然 > 原文: [https://www.programiz.com/cpp-programming/examples/binary-decimal-convert](https://www.programiz.com/cpp-programming/examples/binary-decimal-convert) #### 在此示例中,您將學習通過創建用戶定義的函數將二進制數轉換為十進制,以及將十進制數手動轉換為二進制。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ 函數](/cpp-programming/function) * [C++ 中用戶定義函數的類型](/cpp-programming/user-defined-function-types) * [C++ 遞歸](/cpp-programming/recursion) * [C++ `if`,`if...else`和嵌套`if...else`](/cpp-programming/if-else) * [C++ `while`和`do...while`循環](/cpp-programming/do-while-loop) * * * 訪問此頁面以了解[如何將二進制數轉換為十進制](http://www.wikihow.com/Convert-from-Binary-to-Decimal "Binary number to Decimal")。 ## 示例 1:將二進制數轉換為十進制 ```cpp #include <iostream> #include <cmath> using namespace std; int convertBinaryToDecimal(long long); int main() { long long n; cout << "Enter a binary number: "; cin >> n; cout << n << " in binary = " << convertBinaryToDecimal(n) << "in decimal"; return 0; } int convertBinaryToDecimal(long long n) { int decimalNumber = 0, i = 0, remainder; while (n!=0) { remainder = n%10; n /= 10; decimalNumber += remainder*pow(2,i); ++i; } return decimalNumber; } ``` **輸出** ```cpp Enter a binary number: 1111 1111 in binary = 15 ``` 訪問此頁面以了解[如何將十進制數轉換為二進制](http://www.wikihow.com/Convert-from-Decimal-to-Binary "Binary number to Decimal")。 ## 示例 2:將十進制數轉換為二進制 ```cpp #include <iostream> #include <cmath> using namespace std; long long convertDecimalToBinary(int); int main() { int n, binaryNumber; cout << "Enter a decimal number: "; cin >> n; binaryNumber = convertDecimalToBinary(n); cout << n << " in decimal = " << binaryNumber << " in binary" << endl ; return 0; } long long convertDecimalToBinary(int n) { long long binaryNumber = 0; int remainder, i = 1, step = 1; while (n!=0) { remainder = n%2; cout << "Step " << step++ << ": " << n << "/2, Remainder = " << remainder << ", Quotient = " << n/2 << endl; n /= 2; binaryNumber += remainder*i; i *= 10; } return binaryNumber; } ``` **輸出** ```cpp Enter a decimal number: 19 Step 1: 19/2, Remainder = 1, Quotient = 9 Step 2: 9/2, Remainder = 1, Quotient = 4 Step 3: 4/2, Remainder = 0, Quotient = 2 Step 4: 2/2, Remainder = 0, Quotient = 1 Step 5: 1/2, Remainder = 1, Quotient = 0 19 in decimal = 10011 in binary ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看