82 lines
2.4 KiB
C
82 lines
2.4 KiB
C
/*
|
|
* Erweitern Sie das folgende Programm, sodass es die Funktion qsort der Standard-Bibliothek verwendet,
|
|
* um das enthaltene String-Array sortiert auszugeben. Es sollen zuerst die Strings ohne Zahlen ausgegeben werden
|
|
* und am Ende die mit Zahlen. Die Strings ohne Zahlen sollen aufsteigend sortiert werden, die mit Zahlen absteigend.
|
|
* Sie können davon ausgehen, dass die im Array enthaltenen Strings immer nur aus Buchstaben oder nur aus Zahlen bestehen.
|
|
* Es ist also ausreichend, das erste Zeichen mittels isdigit(...) zu prüfen.
|
|
*
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
|
|
#define STRING_GROESSE 10
|
|
|
|
|
|
//void gibStringsAus(const char *strings[], unsigned int anzahl);
|
|
void gibStringsAus(const char strings[][STRING_GROESSE], unsigned int anzahl);
|
|
int compare(const void *arg1, const void *arg2);
|
|
|
|
|
|
int main()
|
|
{
|
|
//const char *strings[] = {"789", "Zorro", "Adam", "Werner", "123", "Bernd", "Hubert", "456"};
|
|
char strings[][STRING_GROESSE] = {"789", "Zorro", "Adam", "Werner", "123", "Bernd", "Hubert", "456"};
|
|
unsigned int anzahlStrings = sizeof(strings)/sizeof(strings[0]);
|
|
|
|
printf("Vor Sortierung:\n");
|
|
gibStringsAus(strings, anzahlStrings);
|
|
|
|
qsort(strings, anzahlStrings, sizeof(strings[0]), compare);
|
|
|
|
printf("\nNach Sortierung:\n");
|
|
gibStringsAus(strings, anzahlStrings);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
/*void gibStringsAus(const char *strings[], unsigned int anzahl)
|
|
{
|
|
for(int i = 0; i < anzahl; i++)
|
|
{
|
|
printf("%s\n", strings[i]);
|
|
}
|
|
}*/
|
|
|
|
void gibStringsAus(const char strings[][STRING_GROESSE], unsigned int anzahl)
|
|
{
|
|
for(int i = 0; i < anzahl; i++)
|
|
{
|
|
printf("%s\n", strings[i]);
|
|
}
|
|
}
|
|
|
|
int compare(const void *arg1, const void *arg2)
|
|
{
|
|
//const char *string1 = *(const char **)arg1;
|
|
//const char *string2 = *(const char **)arg2;
|
|
const char (*zgr1)[STRING_GROESSE] = (char (*)[STRING_GROESSE])arg1;
|
|
const char (*zgr2)[STRING_GROESSE] = (char (*)[STRING_GROESSE])arg2;
|
|
const char *string1 = *zgr1;
|
|
const char *string2 = *zgr2;
|
|
|
|
if(!isdigit(string1[0]) && !isdigit(string2[0]))
|
|
{
|
|
return strcmp(string1, string2);
|
|
}
|
|
else if(isdigit(string1[0]) && isdigit(string2[0]))
|
|
{
|
|
return -strcmp(string1, string2);
|
|
}
|
|
else
|
|
{
|
|
if(isdigit(string1[0]))
|
|
return 1;
|
|
else
|
|
return -1;
|
|
}
|
|
}
|