exception handling
This commit is contained in:
parent
2a87562d50
commit
50844a0dba
3 changed files with 276 additions and 114 deletions
|
@ -5,29 +5,109 @@
|
|||
#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: ";
|
||||
|
||||
program();
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void BubbleSort(short *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
printArray(array, amount);
|
||||
|
||||
bool changed = true;
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1 && changed; idx++)
|
||||
{
|
||||
changed = false;
|
||||
|
||||
for(unsigned short idx2 = amount-1; idx2>idx; idx2--) if(array[idx2]<array[idx2-1])
|
||||
{
|
||||
int temp = array[idx2-1];
|
||||
array[idx2-1] = array[idx2], array[idx2] = temp, changed = true;
|
||||
}
|
||||
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void BubbleSort(long double *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
printArray(array, amount);
|
||||
|
||||
bool changed = true;
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1 && changed; idx++)
|
||||
{
|
||||
changed = false;
|
||||
|
||||
for(unsigned short idx2 = amount-1; idx2>idx; idx2--) if(array[idx2]<array[idx2-1])
|
||||
{
|
||||
int temp = array[idx2-1];
|
||||
array[idx2-1] = array[idx2], array[idx2] = temp, changed = true;
|
||||
}
|
||||
|
||||
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")
|
||||
{
|
||||
int *array = new int[amount];
|
||||
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++])
|
||||
|
@ -38,55 +118,29 @@ int main()
|
|||
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();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void BubbleSort(int *array, unsigned short amount)
|
||||
void printArray(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
|
||||
|
||||
bool changed = true;
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1 && changed; idx++)
|
||||
{
|
||||
changed = false;
|
||||
|
||||
for(unsigned short idx2 = amount-1; idx2>idx; idx2--) if(array[idx2]<array[idx2-1])
|
||||
{
|
||||
int temp = array[idx2-1];
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
void BubbleSort(long double *array, unsigned 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
|
||||
|
||||
bool changed = true;
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1 && changed; idx++)
|
||||
{
|
||||
changed = false;
|
||||
|
||||
for(unsigned short idx2 = amount-1; idx2>idx; idx2--) if(array[idx2]<array[idx2-1])
|
||||
{
|
||||
int temp = array[idx2-1];
|
||||
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
|
||||
}
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
|
|
@ -5,29 +5,99 @@
|
|||
#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: ";
|
||||
|
||||
program();
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void InsertionSort(long double *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 1; idx < amount; idx++)
|
||||
{
|
||||
int temp = array[idx], idx2 = idx-1;
|
||||
|
||||
while(idx2>=0 && array[idx2]>temp) array[idx2+1] = array[idx2--];
|
||||
array[idx2+1] = temp;
|
||||
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void InsertionSort(short *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 1; idx < amount; idx++)
|
||||
{
|
||||
int temp = array[idx], idx2 = idx-1;
|
||||
|
||||
while(idx2>=0 && array[idx2]>temp) array[idx2+1] = array[idx2--];
|
||||
array[idx2+1] = temp;
|
||||
|
||||
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")
|
||||
{
|
||||
int *array = new int[amount];
|
||||
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++])
|
||||
|
@ -38,45 +108,29 @@ int main()
|
|||
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();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void InsertionSort(long double *array, unsigned short amount)
|
||||
void printArray(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
|
||||
|
||||
for(unsigned short idx = 1; idx < amount; idx++)
|
||||
{
|
||||
int temp = array[idx], idx2 = idx-1;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
void InsertionSort(int *array, unsigned 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
|
||||
|
||||
for(unsigned short idx = 1; idx < amount; idx++)
|
||||
{
|
||||
int temp = array[idx], idx2 = idx-1;
|
||||
|
||||
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
|
||||
}
|
||||
for(unsigned short idx = 0; idx+1<amount; idx++) std::cout << array[idx] << ", ";
|
||||
std::cout << array[amount-1] << "\n";
|
||||
getch();
|
||||
}
|
||||
|
|
|
@ -5,29 +5,107 @@
|
|||
#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: ";
|
||||
|
||||
program();
|
||||
|
||||
getch();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void SelectionSort(long double *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++)
|
||||
{
|
||||
unsigned short indexMin = idx;
|
||||
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
||||
|
||||
if(indexMin!=idx)
|
||||
{
|
||||
long double temp = array[idx];
|
||||
array[idx] = array[indexMin], array[indexMin] = temp;
|
||||
}
|
||||
|
||||
printArray(array, amount);
|
||||
}
|
||||
}
|
||||
|
||||
void SelectionSort(short *array, short amount)
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
||||
printArray(array, amount);
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++)
|
||||
{
|
||||
unsigned short indexMin = idx;
|
||||
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
||||
|
||||
if(indexMin!=idx)
|
||||
{
|
||||
long double temp = array[idx];
|
||||
array[idx] = array[indexMin], array[indexMin] = temp;
|
||||
}
|
||||
|
||||
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")
|
||||
{
|
||||
int *array = new int[amount];
|
||||
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++])
|
||||
|
@ -38,53 +116,29 @@ int main()
|
|||
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();
|
||||
return(0);
|
||||
}
|
||||
|
||||
void SelectionSort(long double *array, unsigned short amount)
|
||||
void printArray(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
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++)
|
||||
{
|
||||
unsigned short indexMin = idx;
|
||||
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
||||
|
||||
if(indexMin!=idx)
|
||||
{
|
||||
long double temp = array[idx];
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
void SelectionSort(int *array, unsigned 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
|
||||
|
||||
for(unsigned short idx = 0; idx<amount-1; idx++)
|
||||
{
|
||||
unsigned short indexMin = idx;
|
||||
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
||||
|
||||
if(indexMin!=idx)
|
||||
{
|
||||
long double temp = array[idx];
|
||||
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
|
||||
}
|
||||
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