code-neon

C++ ile Tamsayıyı Üçlü Basamak Gruplarına Ayırma

Kullanıcıdan alınan tamsayıyı üçlü basamak gruplarına ayıran C++ programı.

#include < iostream > 

#include < string >  

std::string GroupDigits(int number);
int main()
{
    std::cout << "Sayıyı girin: ";
    int sayi;

    std::cin >> sayi; 

    std::string grupluSayi = GroupDigits(sayi); 

    std::cout << "Üçlü basamak grupları: " << grupluSayi << std::endl; 

    return 0; 
}

std::string GroupDigits(int number)
{
    std::string strNumber = std::to_string(number); 
    int digitCount = strNumber.length(); 
    int groupCount = (digitCount - 1) / 3 + 1; 
    std::string* groups = new std::string[groupCount]; 
    for (int i = 0; i < groupCount; i++)
    {
        int startIndex = digitCount - (i + 1) * 3;
        if (startIndex < 0)
            startIndex = 0;
        int length = (i == groupCount - 1) ? digitCount % 3 : 3;
        groups[i] = strNumber.substr(startIndex, length);
    }
    std::string grupluSayi = ""; 
    for (int i = 0; i < groupCount; i++)
    {
        grupluSayi += groups[i];
        if (i != groupCount - 1)
            grupluSayi += ",";
    }
    delete[] groups; 
    return grupluSayi; 
}

Giriş/Çıkış işlemleri için gerekli kütüphane

#include < iostream > 

String (metin) kullanacağız

#include < string >  

C++ ile Sayının basamaklarını üçlü gruplara bölen fonksiyon

std::string GroupDigits(int number);

C++ ile Kullanıcıdan bir sayı girmesi istenir

    std::cout << "Sayıyı girin: ";
    int sayi;

C++ ile Kullanıcının girdiği sayıyı alır

    std::cin >> sayi; 

C++ ile Sayının basamaklarını üçlü gruplara böler

    std::string grupluSayi = GroupDigits(sayi); 

C++ ile Sonucu ekrana yazdırır

    std::cout << "Üçlü basamak grupları: " << grupluSayi << std::endl; 

C++ ile Programın başarıyla sonlandığını belirtir

    return 0; 

C++ ile Sayının basamaklarını üçlü gruplara bölen fonksiyon

std::string GroupDigits(int number)
{
    std::string strNumber = std::to_string(number); // Sayıyı string olarak çevirir
    int digitCount = strNumber.length(); // Sayının basamak sayısını bulur
    int groupCount = (digitCount - 1) / 3 + 1; // Üçlü grup sayısını bulur
    std::string* groups = new std::string[groupCount]; // Üçlü grupları tutacak bir dizi oluşturur
    // Her üçlü grubu ayrı ayrı alır ve groups dizisine kaydeder
    for (int i = 0; i < groupCount; i++)
    {
        int startIndex = digitCount - (i + 1) * 3;
        if (startIndex < 0)
            startIndex = 0;
        int length = (i == groupCount - 1) ? digitCount % 3 : 3;
        groups[i] = strNumber.substr(startIndex, length);
    }
    std::string grupluSayi = ""; // Gruplu sayıyı tutacak bir string oluşturur
    // Her üçlü grubu virgülle birleştirerek grupluSayi'ya atar
    for (int i = 0; i < groupCount; i++)
    {
        grupluSayi += groups[i];
        if (i != groupCount - 1)
            grupluSayi += ",";
    }
    delete[] groups; // Bellekte ayrılan dizi alanını serbest bırakır
    return grupluSayi; // Gruplu sayıyı geri döndürür
}