اذهب الي المحتوي
أوفيسنا
بحث مخصص من جوجل فى أوفيسنا
Custom Search

برنامج يحول الارقام من النظام العشري الى الرقام في النظام الثنائي ثم الى النظام الثماني وم ثم الى النظام السادس عشر


النهر العطشان

الردود الموصى بها

السلام عليكم

هذا برنام يحول الارقام من النظام العشري الى الرقام في النظام الثنائي ثم الى النظام الثماني وم ثم الى النظام السادس عشر حيث تظهر خيارات التحويل على الشاشة باستخدام دالة ال switch

#include <iostream>
#include <string>

std::string decimalToBinary(int decimal) {
    std::string binary = "";
    while (decimal > 0) {
        binary = std::to_string(decimal % 2) + binary;
        decimal /= 2;
    }
    return binary;
}

std::string decimalToOctal(int decimal) {
    std::string octal = "";
    while (decimal > 0) {
        octal = std::to_string(decimal % 8) + octal;
        decimal /= 8;
    }
    return octal;
}

std::string decimalToHexadecimal(int decimal) {
    std::string hex = "";
    while (decimal > 0) {
        int remainder = decimal % 16;
        if (remainder < 10) {
            hex = char(remainder + 48) + hex;
        } else {
            hex = char(remainder + 55) + hex;
        }
        decimal /= 16;
    }
    return hex;
}

int main() {
    int decimal;
    std::cout << "Enter a decimal number: ";
    std::cin >> decimal;

    int choice;
    std::cout << "Choose conversion:\n";
    std::cout << "1. Decimal to Binary\n";
    std::cout << "2. Decimal to Octal\n";
    std::cout << "3. Decimal to Hexadecimal\n";
    std::cout << "Enter your choice: ";
    std::cin >> choice;

    std::string result;
    switch(choice) {
        case 1:
            result = decimalToBinary(decimal);
            std::cout << "Binary: " << result << std::endl;
            break;
        case 2:
            result = decimalToOctal(decimal);
            std::cout << "Octal: " << result << std::endl;
            break;
        case 3:
            result = decimalToHexadecimal(decimal);
            std::cout << "Hexadecimal: " << result << std::endl;
            break;
        default:
            std::cout << "Invalid choice." << std::endl;
    }

    return 0;
}

البرنامج يعمل بصورة صحيحة 

المطلوب هو شرح طريقة عمل الدالة

جزاكم الله كل خير

رابط هذا التعليق
شارك

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

زائر
اضف رد علي هذا الموضوع....

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • تصفح هذا الموضوع مؤخراً   0 اعضاء متواجدين الان

    • لايوجد اعضاء مسجلون يتصفحون هذه الصفحه
×
×
  • اضف...

Important Information