Çözüldü C++ String ifadeler icinden random seçim

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

bedrhnaliakcy

Kilopat
Katılım
16 Temmuz 2018
Mesajlar
148
Çözümler
1
Daha fazla  
Cinsiyet
Erkek
Meslek
İndie software
Merhaba, kullanıcıdan 10 adet string ifade alınacak ve bu 10 string ifadelerin arasından sadece 1 tane seçilecek. Bu işlemi birkaç kez yapılacak 3 kez seçilen olursa program son bulacak.
 
Çözüm
Spesifik açıklama yapmadığınız için varsayımda bulunarak ilerlerdim. Umarım işinize yarar.

C++:
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <locale>

using namespace std;
int main()
{
    int random, loopcounter = 0; //random seçim için random ve 3 kere giriş için loopcounter tanımladık.
    string ch1, ch2, ch3;//seçilen stringler
    string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;//girilen stringler

    srand(time(NULL));//gerçekten rastgele sayı üretmek için gereken kod
    setlocale(LC_ALL, "Turkish");//konsol ekranında türkçe harflerin yazılabilmesi için

    while (loopcounter < 3) {//loopcounter sıfırdan başladı: 0 1 2 olmak üzere üç kez çalışıcak
        cout << "1. stringi giriniz : ";
        getline(cin, s1);// (cin >> s1) şeklinde okuma yapmak isterseniz cin.ignore() komutunu hemen ardından yazın
        cout << "2. stringi giriniz : ";
        getline(cin, s2);
        cout << "3. stringi giriniz : ";
        getline(cin, s3);
        cout << "4. stringi giriniz : ";
        getline(cin, s4);
        cout << "5. stringi giriniz : ";
        getline(cin, s5);
        cout << "6. stringi giriniz : ";
        getline(cin, s6);
        cout << "7. stringi giriniz : ";
        getline(cin, s7);
        cout << "8. stringi giriniz : ";
        getline(cin, s8);
        cout << "9. stringi giriniz : ";
        getline(cin, s9);
        cout << "10. stringi giriniz : ";
        getline(cin, s10);

        random = rand() % 10 + 1; //rastgele sayı üretiliyor 1 ile 10 arasında (+1 olmazsa 0 da dahil olur)
        cout << "\r\nRandom = " << random << endl;//alogoritma kontrolü için istenirse silinebilir
        if (loopcounter == 0)//ilk kez çalışma
        {
            if (random == 1)
                ch1 = s1;
            if (random == 2)
                ch1 = s2;
            if (random == 3)
                ch1 = s3;
            if (random == 4)
                ch1 = s4;
            if (random == 5)
                ch1 = s5;
            if (random == 6)
                ch1 = s6;
            if (random == 7)
                ch1 = s7;
            if (random == 8)
                ch1 = s8;
            if (random == 9)
                ch1 = s9;
            if (random == 10)
                ch1 = s10;
        }
        
        if (loopcounter == 1)
        {
            if (random == 1)
                ch2 = s1;
            if (random == 2)
                ch2 = s2;
            if (random == 3)
                ch2 = s3;
            if (random == 4)
                ch2 = s4;
            if (random == 5)
                ch2 = s5;
            if (random == 6)
                ch2 = s6;
            if (random == 7)
                ch2 = s7;
            if (random == 8)
                ch2 = s8;
            if (random == 9)
                ch2 = s9;
            if (random == 10)
                ch2 = s10;
        }
        
        if (loopcounter == 2)
        {
            if (random == 1)
                ch3 = s1;
            if (random == 2)
                ch3 = s2;
            if (random == 3)
                ch3 = s3;
            if (random == 4)
                ch3 = s4;
            if (random == 5)
                ch3 = s5;
            if (random == 6)
                ch3 = s6;
            if (random == 7)
                ch3 = s7;
            if (random == 8)
                ch3 = s8;
            if (random == 9)
                ch3 = s9;
            if (random == 10)
                ch3 = s10;
        }
        loopcounter++;
    }
    cout << "\r\n1.Seçilen String = " << ch1;
    cout << "\r\n2.Seçilen String = " << ch2;
    cout << "\r\n3.Seçilen String = " << ch3;
    system("pause");
    return 0;
}
Spesifik açıklama yapmadığınız için varsayımda bulunarak ilerlerdim. Umarım işinize yarar.

C++:
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <locale>

using namespace std;
int main()
{
    int random, loopcounter = 0; //random seçim için random ve 3 kere giriş için loopcounter tanımladık.
    string ch1, ch2, ch3;//seçilen stringler
    string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;//girilen stringler

    srand(time(NULL));//gerçekten rastgele sayı üretmek için gereken kod
    setlocale(LC_ALL, "Turkish");//konsol ekranında türkçe harflerin yazılabilmesi için

    while (loopcounter < 3) {//loopcounter sıfırdan başladı: 0 1 2 olmak üzere üç kez çalışıcak
        cout << "1. stringi giriniz : ";
        getline(cin, s1);// (cin >> s1) şeklinde okuma yapmak isterseniz cin.ignore() komutunu hemen ardından yazın
        cout << "2. stringi giriniz : ";
        getline(cin, s2);
        cout << "3. stringi giriniz : ";
        getline(cin, s3);
        cout << "4. stringi giriniz : ";
        getline(cin, s4);
        cout << "5. stringi giriniz : ";
        getline(cin, s5);
        cout << "6. stringi giriniz : ";
        getline(cin, s6);
        cout << "7. stringi giriniz : ";
        getline(cin, s7);
        cout << "8. stringi giriniz : ";
        getline(cin, s8);
        cout << "9. stringi giriniz : ";
        getline(cin, s9);
        cout << "10. stringi giriniz : ";
        getline(cin, s10);

        random = rand() % 10 + 1; //rastgele sayı üretiliyor 1 ile 10 arasında (+1 olmazsa 0 da dahil olur)
        cout << "\r\nRandom = " << random << endl;//alogoritma kontrolü için istenirse silinebilir
        if (loopcounter == 0)//ilk kez çalışma
        {
            if (random == 1)
                ch1 = s1;
            if (random == 2)
                ch1 = s2;
            if (random == 3)
                ch1 = s3;
            if (random == 4)
                ch1 = s4;
            if (random == 5)
                ch1 = s5;
            if (random == 6)
                ch1 = s6;
            if (random == 7)
                ch1 = s7;
            if (random == 8)
                ch1 = s8;
            if (random == 9)
                ch1 = s9;
            if (random == 10)
                ch1 = s10;
        }
        
        if (loopcounter == 1)
        {
            if (random == 1)
                ch2 = s1;
            if (random == 2)
                ch2 = s2;
            if (random == 3)
                ch2 = s3;
            if (random == 4)
                ch2 = s4;
            if (random == 5)
                ch2 = s5;
            if (random == 6)
                ch2 = s6;
            if (random == 7)
                ch2 = s7;
            if (random == 8)
                ch2 = s8;
            if (random == 9)
                ch2 = s9;
            if (random == 10)
                ch2 = s10;
        }
        
        if (loopcounter == 2)
        {
            if (random == 1)
                ch3 = s1;
            if (random == 2)
                ch3 = s2;
            if (random == 3)
                ch3 = s3;
            if (random == 4)
                ch3 = s4;
            if (random == 5)
                ch3 = s5;
            if (random == 6)
                ch3 = s6;
            if (random == 7)
                ch3 = s7;
            if (random == 8)
                ch3 = s8;
            if (random == 9)
                ch3 = s9;
            if (random == 10)
                ch3 = s10;
        }
        loopcounter++;
    }
    cout << "\r\n1.Seçilen String = " << ch1;
    cout << "\r\n2.Seçilen String = " << ch2;
    cout << "\r\n3.Seçilen String = " << ch3;
    system("pause");
    return 0;
}
 
Çözüm
Object Oriented taraftarı olsam da bu problem için fonksiyonel programlamanın yeterli olduğunu düşünüyorum.

C++:
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

string getInput(int index)
{
    cout << "Enter value "<< index <<" ->";
    string ret = ""; // string object
    cin >> ret; // fill the object
    return ret; // return it
}

void fillTenValue(string *arr)
{
    // Get values 10 times
    for (short i = 0; i < 10; i++)
    {
        arr[i] = getInput(i + 1);
    }
}

string getRandomItem(string *arr)
{
    short index = rand() % 10; // [0 - 9] 0, 1, 2, 3, ... 9
    return arr[index];
}

void pickRandomItemInArray(string *arr)
{
    fillTenValue(arr);
    cout << "Random selected value is :" << getRandomItem(arr) << endl;
}


int main()
{
    srand(time(NULL)); // psudo random number generator seed value.
    string strArray[10]; // 10 object wide static array
    cout << "Iteration count-> ";
    int iterCount = 0;
    cin >> iterCount;
    for (short i = 0; i < iterCount; i++)
    {
        pickRandomItemInArray(strArray);
    }
    return 0;
}
 
yani çekiliş benzeri bir şey kullanıcıdan 3-4 isim almak ve bu isimleri random olarak program 3 kez yapacak. 3 defa seçilen olursa ekrana onun ismi yazacak.
yani tek seferliğine string verileri alacak ve ve bunlar arasından random seçim yapacak.
herhangi biri 3 kez seçilirse program duracak ve ekrana yazdıracak
Spesifik açıklama yapmadığınız için varsayımda bulunarak ilerlerdim. Umarım işinize yarar.

C++:
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <locale>

using namespace std;
int main()
{
    int random, loopcounter = 0; //random seçim için random ve 3 kere giriş için loopcounter tanımladık.
    string ch1, ch2, ch3;//seçilen stringler
    string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;//girilen stringler

    srand(time(NULL));//gerçekten rastgele sayı üretmek için gereken kod
    setlocale(LC_ALL, "Turkish");//konsol ekranında türkçe harflerin yazılabilmesi için

    while (loopcounter < 3) {//loopcounter sıfırdan başladı: 0 1 2 olmak üzere üç kez çalışıcak
        cout << "1. stringi giriniz : ";
        getline(cin, s1);// (cin >> s1) şeklinde okuma yapmak isterseniz cin.ignore() komutunu hemen ardından yazın
        cout << "2. stringi giriniz : ";
        getline(cin, s2);
        cout << "3. stringi giriniz : ";
        getline(cin, s3);
        cout << "4. stringi giriniz : ";
        getline(cin, s4);
        cout << "5. stringi giriniz : ";
        getline(cin, s5);
        cout << "6. stringi giriniz : ";
        getline(cin, s6);
        cout << "7. stringi giriniz : ";
        getline(cin, s7);
        cout << "8. stringi giriniz : ";
        getline(cin, s8);
        cout << "9. stringi giriniz : ";
        getline(cin, s9);
        cout << "10. stringi giriniz : ";
        getline(cin, s10);

        random = rand() % 10 + 1; //rastgele sayı üretiliyor 1 ile 10 arasında (+1 olmazsa 0 da dahil olur)
        cout << "\r\nRandom = " << random << endl;//alogoritma kontrolü için istenirse silinebilir
        if (loopcounter == 0)//ilk kez çalışma
        {
            if (random == 1)
                ch1 = s1;
            if (random == 2)
                ch1 = s2;
            if (random == 3)
                ch1 = s3;
            if (random == 4)
                ch1 = s4;
            if (random == 5)
                ch1 = s5;
            if (random == 6)
                ch1 = s6;
            if (random == 7)
                ch1 = s7;
            if (random == 8)
                ch1 = s8;
            if (random == 9)
                ch1 = s9;
            if (random == 10)
                ch1 = s10;
        }
      
        if (loopcounter == 1)
        {
            if (random == 1)
                ch2 = s1;
            if (random == 2)
                ch2 = s2;
            if (random == 3)
                ch2 = s3;
            if (random == 4)
                ch2 = s4;
            if (random == 5)
                ch2 = s5;
            if (random == 6)
                ch2 = s6;
            if (random == 7)
                ch2 = s7;
            if (random == 8)
                ch2 = s8;
            if (random == 9)
                ch2 = s9;
            if (random == 10)
                ch2 = s10;
        }
      
        if (loopcounter == 2)
        {
            if (random == 1)
                ch3 = s1;
            if (random == 2)
                ch3 = s2;
            if (random == 3)
                ch3 = s3;
            if (random == 4)
                ch3 = s4;
            if (random == 5)
                ch3 = s5;
            if (random == 6)
                ch3 = s6;
            if (random == 7)
                ch3 = s7;
            if (random == 8)
                ch3 = s8;
            if (random == 9)
                ch3 = s9;
            if (random == 10)
                ch3 = s10;
        }
        loopcounter++;
    }
    cout << "\r\n1.Seçilen String = " << ch1;
    cout << "\r\n2.Seçilen String = " << ch2;
    cout << "\r\n3.Seçilen String = " << ch3;
    system("pause");
    return 0;
}
sağ olasın mantık böyle ama her seferinde string veri istemeyecek bir sefer alıp onları kullanacak.
yine de çok teşekkür ederim.
 
Uyarı! Bu konu 6 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Technopat Haberler

Geri
Yukarı