Hier meine Lösung:
Code:
#include <iostream>
using namespace std;
unsigned int leseInteger(void);
void eingabe(unsigned int *);
void generate(unsigned int *);
bool vergleich(unsigned int *, unsigned int *);
int main(void) {
srand(time(NULL));
unsigned int zahlen[6], lotto_zahlen[6];
long int durchlaeufe = 0;
bool volltreffer;
cout << "Lotto Simulation v1.0" << endl << endl;
eingabe(zahlen);
do {
generate(lotto_zahlen);
volltreffer = vergleich(zahlen, lotto_zahlen);
durchlaeufe++;
if(durchlaeufe % 1000000 == 0) {
cout << endl << "Nach " << durchlaeufe/1000000 << " Millionen Durchlaeufen keine Uebereinstimmung" << endl
<< "aktuelle Zahlen: " << lotto_zahlen[0] << " " << lotto_zahlen[1] << " " << lotto_zahlen[2] << " " << lotto_zahlen[3] << " " << lotto_zahlen[4] << " " << lotto_zahlen[5];
}
} while(!volltreffer);
cout << endl << endl << "Nach " << durchlaeufe << " Durchlaeufen gab es einen Sechser" << endl << "aktuelle Zahlen: " << lotto_zahlen[0] << " " << lotto_zahlen[1] << " " << lotto_zahlen[2] << " " << lotto_zahlen[3] << " " << lotto_zahlen[4] << " " << lotto_zahlen[5]
<< endl << "Erfolgschancen: " << (1.0f/durchlaeufe * 100.0) << " %" << endl << endl;
system("pause");
return 0;
}
bool vergleich(unsigned int *zahlen, unsigned int *lotto_zahlen) {
unsigned int treffer = 0;
for(int i=0; i<6; i++) {
for(int j=0; j<6; j++) {
if(*(zahlen+i) == *(lotto_zahlen+j)) {
treffer++;
break;
}
}
}
if(treffer == 6) {
return true;
}
return false;
}
void generate(unsigned int *lotto_zahlen) {
for(int b=0; b<6;b++) {
*(lotto_zahlen+b) = 0;
}
unsigned int gen_zahl;
bool gen_ok;
for(int i=0; i<6; i++) {
do {
gen_ok = true;
gen_zahl = (1+rand()%49);
for(int j=0; j<i; j++) {
if(*(lotto_zahlen+j) == gen_zahl) {
gen_ok = false;
}
}
} while(!gen_ok);
*(lotto_zahlen+i) = gen_zahl;
}
}
void eingabe(unsigned int *zahlen) {
unsigned int zahl_eingabe;
bool eingabe_ok;
for(int i=0; i<6; i++) {
do {
eingabe_ok = true;
cout << i+1 << "te Zahl: ";
zahl_eingabe = leseInteger();
if((zahl_eingabe >= 50) || (zahl_eingabe <= 0)) {
eingabe_ok = false;
cout << "Die Zahl muss zwischen 0 und 50 sein\n";
}
for(int j=0; j<i; j++) {
if(*(zahlen+j) == zahl_eingabe) {
eingabe_ok = false;
cout << "Diese Zahl gab es bereits\n";
}
}
} while(!eingabe_ok);
*(zahlen+i) = zahl_eingabe;
}
}
unsigned int leseInteger(void) {
unsigned int wert;
cin >> wert;
while(cin.fail()) {
cout << "Fehler bei der Eingabe, bitte nochmal: ";
cin.clear();
cin.sync();
cin >> wert;
}
cin.clear();
cin.sync();
return wert;
}