exception handling
This commit is contained in:
parent
2a87562d50
commit
50844a0dba
3 changed files with 276 additions and 114 deletions
|
@ -5,50 +5,27 @@
|
|||
#include<cstdlib>
|
||||
#include<ctime>
|
||||
|
||||
void BubbleSort(long double *array, unsigned short amount);
|
||||
void BubbleSort(int *array, unsigned short amount);
|
||||
void BubbleSort(long double *array, short amount);
|
||||
void BubbleSort(int *array, short amount);
|
||||
void program();
|
||||
void printArray(long double *array, short amount);
|
||||
void printArray(short *array, short amount);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string input;
|
||||
unsigned short amount = 1;
|
||||
|
||||
std::cout << "Gib die Anzahl der Zahlen ein: ";
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> amount;
|
||||
|
||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input == "y" || input == "Y")
|
||||
{
|
||||
int *array = new int[amount];
|
||||
srand(time(0));
|
||||
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
||||
BubbleSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
else
|
||||
{
|
||||
long double *array = new long double[amount];
|
||||
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
||||
{
|
||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||
getline(std::cin,input);
|
||||
}
|
||||
BubbleSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
program();
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void BubbleSort(int *array, unsigned short amount)
|
||||
void BubbleSort(short *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||
printArray(array, amount);
|
||||
|
||||
bool changed = true;
|
||||
|
||||
|
@ -62,17 +39,15 @@ void BubbleSort(int *array, unsigned short amount)
|
|||
array[idx2-1] = array[idx2], array[idx2] = temp, changed = true;
|
||||
}
|
||||
|
||||
for(unsigned short idx2 = 0; idx2+1<amount; idx2++) std::cout << array[idx2] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void BubbleSort(long double *array, unsigned short amount)
|
||||
void BubbleSort(long double *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||
printArray(array, amount);
|
||||
|
||||
bool changed = true;
|
||||
|
||||
|
@ -86,7 +61,86 @@ void BubbleSort(long double *array, unsigned short amount)
|
|||
array[idx2-1] = array[idx2], array[idx2] = temp, changed = true;
|
||||
}
|
||||
|
||||
for(unsigned short idx2 = 0; idx2+1<amount; idx2++) std::cout << array[idx2] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void program()
|
||||
{
|
||||
std::string input;
|
||||
short amount = 1;
|
||||
|
||||
do
|
||||
{
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> amount;
|
||||
if(amount >= USHRT_MAX) std::cerr << "Error: Die eingegebene Anzahl ist zu gro\xE1 oder klein. Gib eine andere Anzahl ein: ";
|
||||
}while(amount>=SHRT_MAX || amount<2);
|
||||
|
||||
do
|
||||
{
|
||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input != "y" && input != "Y" && input == "n" && input == "N") std::cerr << "Error: Diese Eingabe ist unzulässig. ";
|
||||
}while(input != "y" && input != "Y" && input == "n" && input == "N");
|
||||
|
||||
if(input == "y" || input == "Y")
|
||||
{
|
||||
try
|
||||
{
|
||||
short *array = new short[amount];
|
||||
srand(time(0));
|
||||
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
||||
BubbleSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
catch(std::bad_alloc& ba)
|
||||
{
|
||||
std::cerr << "Error: Es konnte kein Array dieser Gr\x94\xE1 \be erzeugt werden. Gib eine andere Anzahl ein: ";
|
||||
program();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: Beim Erstellen des Arrays kam es zu einem unbekannten Fehler. Versuche es mit einer anderen Anzahl: ";
|
||||
program();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
long double *array = new long double[amount];
|
||||
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
||||
{
|
||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||
getline(std::cin,input);
|
||||
}
|
||||
BubbleSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
catch(std::bad_alloc& ba)
|
||||
{
|
||||
std::cerr << "Error: Es konnte kein Array dieser Gr\x94\xE1 \be erzeugt werden. Gib eine andere Anzahl ein: ";
|
||||
program();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: Beim Erstellen des Arrays kam es zu einem unbekannten Fehler. Versuche es mit einer anderen Anzahl: ";
|
||||
program();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(long double *array, short amount)
|
||||
{
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
||||
void printArray(short *array, short amount)
|
||||
{
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
|
|
@ -5,50 +5,27 @@
|
|||
#include<cstdlib>
|
||||
#include<ctime>
|
||||
|
||||
void InsertionSort(long double *array, unsigned short amount);
|
||||
void InsertionSort(int *array, unsigned short amount);
|
||||
void InsertionSort(long double *array, short amount);
|
||||
void InsertionSort(int *array, short amount);
|
||||
void program();
|
||||
void printArray(long double *array, short amount);
|
||||
void printArray(short *array, short amount);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string input;
|
||||
unsigned short amount = 1;
|
||||
|
||||
std::cout << "Gib die Anzahl der Zahlen ein: ";
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> amount;
|
||||
|
||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input == "y" || input == "Y")
|
||||
{
|
||||
int *array = new int[amount];
|
||||
srand(time(0));
|
||||
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
||||
InsertionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
else
|
||||
{
|
||||
long double *array = new long double[amount];
|
||||
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
||||
{
|
||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||
getline(std::cin,input);
|
||||
}
|
||||
InsertionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
program();
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void InsertionSort(long double *array, unsigned short amount)
|
||||
void InsertionSort(long double *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++) std::cout << array[idx] <<", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 1; idx < amount; idx++)
|
||||
{
|
||||
|
@ -57,17 +34,15 @@ void InsertionSort(long double *array, unsigned short amount)
|
|||
while(idx2>=0 && array[idx2]>temp) array[idx2+1] = array[idx2--];
|
||||
array[idx2+1] = temp;
|
||||
|
||||
for(idx2 = 0; idx2<amount-1; idx2++) std::cout << array[idx2] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void InsertionSort(int *array, unsigned short amount)
|
||||
void InsertionSort(short *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++) std::cout << array[idx] <<", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 1; idx < amount; idx++)
|
||||
{
|
||||
|
@ -76,7 +51,86 @@ void InsertionSort(int *array, unsigned short amount)
|
|||
while(idx2>=0 && array[idx2]>temp) array[idx2+1] = array[idx2--];
|
||||
array[idx2+1] = temp;
|
||||
|
||||
for(unsigned short idx2 = 0; idx2<amount-1; idx2++) std::cout << array[idx2] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void program()
|
||||
{
|
||||
std::string input;
|
||||
short amount = 1;
|
||||
|
||||
do
|
||||
{
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> amount;
|
||||
if(amount >= USHRT_MAX) std::cerr << "Error: Die eingegebene Anzahl ist zu gro\xE1 oder klein. Gib eine andere Anzahl ein: ";
|
||||
}while(amount>=SHRT_MAX || amount<2);
|
||||
|
||||
do
|
||||
{
|
||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input != "y" && input != "Y" && input == "n" && input == "N") std::cerr << "Error: Diese Eingabe ist unzulässig. ";
|
||||
}while(input != "y" && input != "Y" && input == "n" && input == "N");
|
||||
|
||||
if(input == "y" || input == "Y")
|
||||
{
|
||||
try
|
||||
{
|
||||
short *array = new short[amount];
|
||||
srand(time(0));
|
||||
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
||||
InsertionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
catch(std::bad_alloc& ba)
|
||||
{
|
||||
std::cerr << "Error: Es konnte kein Array dieser Gr\x94\xE1 \be erzeugt werden. Gib eine andere Anzahl ein: ";
|
||||
program();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: Beim Erstellen des Arrays kam es zu einem unbekannten Fehler. Versuche es mit einer anderen Anzahl: ";
|
||||
program();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
long double *array = new long double[amount];
|
||||
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
||||
{
|
||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||
getline(std::cin,input);
|
||||
}
|
||||
InsertionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
catch(std::bad_alloc& ba)
|
||||
{
|
||||
std::cerr << "Error: Es konnte kein Array dieser Gr\x94\xE1 \be erzeugt werden. Gib eine andere Anzahl ein: ";
|
||||
program();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: Beim Erstellen des Arrays kam es zu einem unbekannten Fehler. Versuche es mit einer anderen Anzahl: ";
|
||||
program();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(long double *array, short amount)
|
||||
{
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
||||
void printArray(short *array, short amount)
|
||||
{
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
|
|
@ -5,50 +5,27 @@
|
|||
#include<cstdlib>
|
||||
#include<ctime>
|
||||
|
||||
void SelectionSort(long double *array, unsigned short amount);
|
||||
void SelectionSort(int *array, unsigned short amount);
|
||||
void SelectionSort(long double *array, short amount);
|
||||
void SelectionSort(int *array, short amount);
|
||||
void program();
|
||||
void printArray(long double *array, short amount);
|
||||
void printArray(short *array, short amount);
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string input;
|
||||
unsigned short amount = 1;
|
||||
|
||||
std::cout << "Gib die Anzahl der Zahlen ein: ";
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> amount;
|
||||
|
||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input == "y" || input == "Y")
|
||||
{
|
||||
int *array = new int[amount];
|
||||
srand(time(0));
|
||||
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
||||
SelectionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
else
|
||||
{
|
||||
long double *array = new long double[amount];
|
||||
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
||||
{
|
||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||
getline(std::cin,input);
|
||||
}
|
||||
SelectionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
program();
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void SelectionSort(long double *array, unsigned short amount)
|
||||
void SelectionSort(long double *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++)
|
||||
{
|
||||
|
@ -61,17 +38,15 @@ void SelectionSort(long double *array, unsigned short amount)
|
|||
array[idx] = array[indexMin], array[indexMin] = temp;
|
||||
}
|
||||
|
||||
for(unsigned short idx2 = 0; idx2<amount-1; idx2++) std::cout << array[idx2] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void SelectionSort(int *array, unsigned short amount)
|
||||
void SelectionSort(short *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++)
|
||||
{
|
||||
|
@ -84,7 +59,86 @@ void SelectionSort(int *array, unsigned short amount)
|
|||
array[idx] = array[indexMin], array[indexMin] = temp;
|
||||
}
|
||||
|
||||
for(unsigned short idx2 = 0; idx2<amount-1; idx2++) std::cout << array[idx2] << ", ";
|
||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void program()
|
||||
{
|
||||
std::string input;
|
||||
short amount = 1;
|
||||
|
||||
do
|
||||
{
|
||||
getline(std::cin, input);
|
||||
std::stringstream(input) >> amount;
|
||||
if(amount >= USHRT_MAX) std::cerr << "Error: Die eingegebene Anzahl ist zu gro\xE1 oder klein. Gib eine andere Anzahl ein: ";
|
||||
}while(amount>=SHRT_MAX || amount<2);
|
||||
|
||||
do
|
||||
{
|
||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||
getline(std::cin, input);
|
||||
if(input != "y" && input != "Y" && input == "n" && input == "N") std::cerr << "Error: Diese Eingabe ist unzulässig. ";
|
||||
}while(input != "y" && input != "Y" && input == "n" && input == "N");
|
||||
|
||||
if(input == "y" || input == "Y")
|
||||
{
|
||||
try
|
||||
{
|
||||
short *array = new short[amount];
|
||||
srand(time(0));
|
||||
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
||||
SelectionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
catch(std::bad_alloc& ba)
|
||||
{
|
||||
std::cerr << "Error: Es konnte kein Array dieser Gr\x94\xE1 \be erzeugt werden. Gib eine andere Anzahl ein: ";
|
||||
program();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: Beim Erstellen des Arrays kam es zu einem unbekannten Fehler. Versuche es mit einer anderen Anzahl: ";
|
||||
program();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
long double *array = new long double[amount];
|
||||
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
||||
{
|
||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||
getline(std::cin,input);
|
||||
}
|
||||
SelectionSort(array, amount);
|
||||
delete[] array;
|
||||
}
|
||||
catch(std::bad_alloc& ba)
|
||||
{
|
||||
std::cerr << "Error: Es konnte kein Array dieser Gr\x94\xE1 \be erzeugt werden. Gib eine andere Anzahl ein: ";
|
||||
program();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Error: Beim Erstellen des Arrays kam es zu einem unbekannten Fehler. Versuche es mit einer anderen Anzahl: ";
|
||||
program();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(long double *array, short amount)
|
||||
{
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
||||
void printArray(short *array, short amount)
|
||||
{
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
|
Reference in a new issue