2017-04-02 15:59:33 +02:00
|
|
|
|
#include<iostream>
|
|
|
|
|
#include<conio.h>
|
|
|
|
|
#include<string>
|
|
|
|
|
#include<sstream>
|
2017-04-09 12:43:54 +02:00
|
|
|
|
#include<cstdlib>
|
|
|
|
|
#include<ctime>
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
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);
|
2017-04-10 14:57:05 +02:00
|
|
|
|
|
2017-04-02 15:59:33 +02:00
|
|
|
|
int main()
|
|
|
|
|
{
|
2017-04-08 17:51:54 +02:00
|
|
|
|
std::cout << "Gib die Anzahl der Zahlen ein: ";
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
program();
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
2017-04-10 14:57:05 +02:00
|
|
|
|
getch();
|
|
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
void SelectionSort(long double *array, short amount)
|
2017-04-10 14:57:05 +02:00
|
|
|
|
{
|
2017-04-02 15:59:33 +02:00
|
|
|
|
std::cout << "\n";
|
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
printArray(array, amount);
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
|
|
|
|
for(unsigned short idx = 0; idx<amount-1; idx++)
|
|
|
|
|
{
|
|
|
|
|
unsigned short indexMin = idx;
|
2017-04-05 13:12:55 +02:00
|
|
|
|
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
|
|
|
|
if(indexMin!=idx)
|
|
|
|
|
{
|
|
|
|
|
long double temp = array[idx];
|
2017-04-10 16:31:26 +02:00
|
|
|
|
array[idx] = array[indexMin], array[indexMin] = temp;
|
2017-04-02 15:59:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
printArray(array, amount);
|
2017-04-02 15:59:33 +02:00
|
|
|
|
}
|
2017-04-10 14:57:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
void SelectionSort(short *array, short amount)
|
2017-04-10 14:57:05 +02:00
|
|
|
|
{
|
|
|
|
|
std::cout << "\n";
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
printArray(array, amount);
|
2017-04-10 14:57:05 +02:00
|
|
|
|
|
|
|
|
|
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];
|
2017-04-10 16:31:26 +02:00
|
|
|
|
array[idx] = array[indexMin], array[indexMin] = temp;
|
2017-04-10 14:57:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 17:26:52 +02:00
|
|
|
|
printArray(array, amount);
|
2017-04-10 14:57:05 +02:00
|
|
|
|
}
|
2017-04-02 15:59:33 +02:00
|
|
|
|
}
|
2017-04-14 17:26:52 +02:00
|
|
|
|
|
|
|
|
|
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<75>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();
|
|
|
|
|
}
|