Hiho,
da viel diskutiert wird und in diesem Gebiet noch ziemlich neu bin, wollte ich einfach mal fragen, was ihr von meinem Code haltet.
C
#include <iostream>
#include <string>
int main() {
/* Definition von Variabeln */
int iInputP, iDivisorP, iModulusP, iInputC, iCacheInputC, iCrossfootC, iInputF[4], iHighestArrayF, iLowestArrayF;
bool bRepeatP, bRepeatC, bPrimeP, bHigherThanF[4], bLowerThanF[4], bRepeatF;
std::string sSelection, sRepeatP, sRepeatC, sRepeatF;
/* Anfangsdialog */
do {
std::cout << "Moechten Sie ueberpruefen, ob eine Zahl eine Primzahl ist (P), moechten Sie eine Quersumme berechnen (C) oder die kleinste und groesste Zahl von 4 Zahlen (F)?" << std::endl;
std::cin >> sSelection;
if (sSelection != "p" && sSelection != "P" && sSelection != "c" && sSelection != "C" && sSelection != "f" && sSelection != "F") {
std::cout << "Sie haben versucht, eine nicht vorhandene Funktion auszuwaehlen." << std::endl;
}
} while (sSelection != "p" && sSelection != "P" && sSelection != "c" && sSelection != "C" && sSelection != "f" && sSelection != "F");
/* Primzahlüberprüfung */
if (sSelection == "p" || sSelection == "P") {
do {
iDivisorP = 2;
iModulusP = 1;
std::cout << "Bitte geben Sie eine Zahl ein." << std::endl;
std::cin >> iInputP;
while (iDivisorP != iInputP) {
iModulusP = iInputP % iDivisorP;
iDivisorP++;
if (iModulusP == 0) {
bPrimeP = false;
break;
} else {
bPrimeP = true;
}
}
if (bPrimeP == false) {
std::cout << iInputP << " ist keine Primzahl." << std::endl;
} else {
std::cout << iInputP << " ist eine Primzahl." << std::endl;
}
std::cout << "Noch eine Zahl ueberpruefen?" << std::endl << "Ja (J) oder Nein (N)" << std::endl;
std::cin >> sRepeatP;
if (sRepeatP == "J" || sRepeatP == "j") {
bRepeatP = true;
} else {
bRepeatP = false;
}
} while (bRepeatP == true);
/* Quersumme */
} else if (sSelection == "c" || sSelection == "C") {
do {
std::cout << "Bitte geben Sie eine Zahl ein." << std::endl;
std::cin >> iInputC;
iCacheInputC = iInputC;
iCrossfootC = 0;
while (iInputC) {
iCrossfootC += iInputC % 10;
iInputC /= 10;
}
std::cout << "Die Quersumme der Zahl " << iCacheInputC << " ist " << iCrossfootC << std::endl << std::endl;
std::cout << "Noch eine Quersumme ausrechnen?" << std::endl << "Ja (J) oder Nein (N)" << std::endl;
std::cin >> sRepeatC;
if (sRepeatC == "J" || sRepeatC == "j") {
bRepeatC = true;
} else {
bRepeatC = false;
}
} while (bRepeatC == true);
/* Größte und kleinste Zahl von 4 Zahlen */
} else if (sSelection == "f" || sSelection == "f") {
do {
std::cout << "Bitte geben Sie vier Zahlen ein." << std::endl;
std::cin >> iInputF[0];
std::cin >> iInputF[1];
std::cin >> iInputF[2];
std::cin >> iInputF[3];
std::cout << "Sie haben folgende Zahlen eingegeben: " << iInputF[0] << ", " << iInputF[1] << ", " << iInputF[2]<< " und " << iInputF[3] << "." << std::endl;
for (int i = 0; i < 4; i++) {
for (int x = 0; x < 4; x++) {
if (iInputF[i] >= iInputF[x]) {
bHigherThanF[x] = true;
} else {
bHigherThanF[x] = false;
}
}
for (int y = 0; y < 4; y++) {
if (iInputF[i] <= iInputF[y]) {
bLowerThanF[y] = true;
} else {
bLowerThanF[y] = false;
}
}
if (bHigherThanF[0] == true && bHigherThanF[1] == true && bHigherThanF[2] == true && bHigherThanF[3] == true) {
iHighestArrayF = i;
}
if (bLowerThanF[0] == true && bLowerThanF[1] == true && bLowerThanF[2] == true && bLowerThanF[3] == true) {
iLowestArrayF = i;
}
}
std::cout << "Groesste Zahl: " << iInputF[iHighestArrayF] << std::endl << "Kleinste Zahl: " << iInputF[iLowestArrayF] << std::endl;
std::cout << "Nochmal?" << std::endl << "Ja (J) oder Nein (N)" << std::endl;
std::cin >> sRepeatF;
if (sRepeatF == "J" || sRepeatF == "j") {
bRepeatF = true;
} else {
bRepeatF = false;
}
} while (bRepeatF == true);
}
/* Damit sich das Programm nicht schließt */
std::cin.sync();
std::cin.get();
}
Alles anzeigen
Programm funktioniert, geht mir nur um den Stil. Will mir ja keinen Stuss aneignen.
Edit: Habs erneuert. (Variabeln verändert und Größe / Kleine Zahl von 4 Zahlen finden hinzugefügt)
Alte Version im Spoiler.
[SPOILER2]
C
#include <iostream>
#include <string>
int main() {
/* Definition von Variabeln */
int inputP, divisorP, modulusP, inputC, cacheInputC, crossfootC;
bool bRepeatP, bRepeatC, primeP;
std::string selection, sRepeatP, sRepeatC;
/* Anfangsdialog */
do {
std::cout << "Moechten Sie ueberpruefen, ob eine Zahl eine Primzahl(P) ist, oder moechten Sie eine Quersumme(C) berechnen?" << std::endl;
std::cin >> selection;
if (selection != "p" && selection != "P" && selection != "c" && selection != "C") {
std::cout << "Sie haben versucht, eine nicht vorhandene Funktion auszuwaehlen." << std::endl;
}
} while (selection != "p" && selection != "P" && selection != "c" && selection != "C");
/* Primzahlüberprüfung */
if (selection == "p" || selection == "P") {
do {
divisorP = 2;
modulusP = 1;
std::cout << "Bitte geben Sie eine Zahl ein." << std::endl;
std::cin >> inputP;
while (modulusP == 0 || inputP - 1 == divisorP) {
modulusP = inputP % divisorP;
divisorP++;
if (modulusP == 0) {
primeP = false;
} else {
primeP = true;
}
}
if (primeP == false) {
std::cout << inputP << " ist eine Primzahl." << std::endl;
} else {
std::cout << inputP << " ist keine Primzahl." << std::endl;
}
std::cout << "Noch eine Zahl ueberpruefen?" << std::endl << "Ja (J) oder Nein (N)" << std::endl;
std::cin >> sRepeatP;
if (sRepeatP == "J" || sRepeatP == "j") {
bRepeatP = true;
} else {
bRepeatP = false;
}
} while (bRepeatP == true);
/* Quersumme */
} else if (selection == "c" || selection == "C") {
do {
std::cout << "Bitte geben Sie eine Zahl ein." << std::endl;
std::cin >> inputC;
cacheInputC = inputC;
crossfootC = 0;
while (inputC) {
crossfootC += inputC % 10;
inputC /= 10;
}
std::cout << "Die Quersumme der Zahl " << cacheInputC << " ist " << crossfootC << std::endl << std::endl;
std::cout << "Noch eine Quersumme ausrechnen?" << std::endl << "Ja (J) oder Nein (N)" << std::endl;
std::cin >> sRepeatC;
if (sRepeatC == "J" || sRepeatC == "j") {
bRepeatC = true;
} else {
bRepeatC = false;
}
} while (bRepeatC == true);
}
/* Damit sich das Programm nicht schließt */
std::cin.sync();
std::cin.get();
}
Alles anzeigen
[/SPOILER2]