<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C++ 程序:將八進制數轉換為十進制,反之亦然 > 原文: [https://www.programiz.com/cpp-programming/examples/octal-decimal-convert](https://www.programiz.com/cpp-programming/examples/octal-decimal-convert) #### 在此示例中,您將學習通過創建用戶定義的函數來手動將八進制數轉換為十進制,并將十進制數轉換為八進制。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ 函數](/cpp-programming/function) * [C++ 中用戶定義函數的類型](/cpp-programming/user-defined-function-types) * [C++ `if`,`if...else`和嵌套`if...else`](/cpp-programming/if-else) * [C++ `while`和`do...while`循環](/cpp-programming/do-while-loop) * * * ## 示例 1:將八進制數轉換為十進制 ```cpp #include <iostream> #include <cmath> using namespace std; int octalToDecimal(int octalNumber); int main() { int octalNumber; cout << "Enter an octal number: "; cin >> octalNumber; cout << octalNumber << " in octal = " << octalToDecimal(octalNumber) << " in decimal"; return 0; } // Function to convert octal number to decimal int octalToDecimal(int octalNumber) { int decimalNumber = 0, i = 0, rem; while (octalNumber != 0) { rem = octalNumber % 10; octalNumber /= 10; decimalNumber += rem * pow(8, i); ++i; } return decimalNumber; } ``` **輸出** ```cpp Enter an octal number: 2341 2341 in octal = 1249 in decimal ``` 在程序中,八進制數存儲在變量`octalNumber`中,并傳遞給函數`octalToDecimal()`。 該函數將用戶傳遞的八進制數轉換為其等效的十進制數,并將其返回給`main()`函數。 ## 示例 2:將十進制數轉換為八進制 ```cpp #include <iostream> #include <cmath> using namespace std; int decimalToOctal(int decimalNumber); int main() { int decimalNumber; cout << "Enter a decimal number: "; cin >> decimalNumber; cout << decimalNumber << " in decimal = " << decimalToOctal(decimalNumber) << " in octal"; return 0; } // Function to convert decimal number to octal int decimalToOctal(int decimalNumber) { int rem, i = 1, octalNumber = 0; while (decimalNumber != 0) { rem = decimalNumber % 8; decimalNumber /= 8; octalNumber += rem * i; i *= 10; } return octalNumber; } ``` **輸出** ```cpp Enter an decimal number: 78 78 in decimal = 116 in octal ``` 在程序中,十進制數存儲在變量`decimalNumber`中,并傳遞給函數`decimalToOctal()`。 該函數將用戶傳遞的十進制數轉換為其等效的八進制數,并將其返回給`main()`函數。
                  <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>

                              哎呀哎呀视频在线观看