main.c
#include <stdio.h>
#include <stdlib.h>
#define C_ARRAY_LEN 20
char **store_in_arry(char **storage, long length, char *newitem);
char **delete_from_array(char **storage, long length, long delete_index);
void sort(char **storage, long length, long sort_type);
long ascending(char *string1, char* string2);
long descending(char *string1, char* string2);
long *alloc_number(long size);
void free_number(long *mem);
void print(long *mem, long size);
long *change_size(long *mem, long old_size, long new_size);
long add(long *a, long size1, long *b, long size2, long *c, long size3);
int main()
{
char **test=0;
char in[C_ARRAY_LEN];
char arr1[C_ARRAY_LEN]="Hallo ", arr2[C_ARRAY_LEN]="Welt";
char arr3[C_ARRAY_LEN]="1234 ", arr4[C_ARRAY_LEN]="Test";
long len=0, length=0, i=0, choice=0, len1=10, len2=10, len3=10, added;
//long *arr_num=0, *arr1=0, *arr2=0, *arr3=0;
/*
do{
printf("\nWas wollen Sie tun?\n");
printf("0... Exit\n");
printf("1... Speicher reservieren\n");
printf("2... Speicher freigeben\n");
printf("3... Speicher ausgeben\n");
printf("4... Initialisieren\n");
printf("5... Speicher verdoppeln\n");
scanf("%ld", &choice);
getchar();
switch(choice){
case 1:
len=10;
arr_num=alloc_number(len);
break;
case 2:
free_number(arr_num);
arr_num=0;
break;
case 3:
print(arr_num, len);
break;
case 4:
for(i=0;i<len;i++)
arr_num[i]=i;
break;
case 5:
arr_num=change_size(arr_num, len, 2*len);
len=2*len;
break;
}
}while(choice<10&&choice!=0);
arr1=alloc_number(len1);
arr2=alloc_number(len2);
for(i=0;i<10;i++)
arr1[i]=arr2[i]=i;
arr3=alloc_number(len3);
added=add(arr1, len1, arr2, len2, arr3, len3);
if(added)
print(arr3, len3);
else
printf("\nFehler beim addieren der Felder!\n");
free_number(arr1);
free_number(arr2);
free_number(arr3);
*/
printf("Geben Sie einen String ein: ");
scanf("%s", in);
getchar();
length++;
test=store_in_arry(test, length, in);
length++;
test=store_in_arry(test, length, arr2);
length++;
test=store_in_arry(test, length, arr3);
length++;
test=store_in_arry(test, length, arr4);
for(i=0; i<length; i++)
{
printf("%s \n", test[i]);
}
sort(test, length, 1);
printf("\nSorted:\n");
for(i=0; i<length; i++)
{
printf("%s \n", test[i]);
}
length--;
test=delete_from_array(test, length, 0);
printf("\nDeleted:\n");
for(i=0; i<length; i++)
{
printf("%s \n", test[i]);
}
if(test)
{
free(test);
test=0;
}
//printf("Hello world!\n");
return 0;
}
long add(long *a, long size1, long *b, long size2, long *c, long size3)
{
long i, len;
if(size3>=size1&&size3>=size2)
{
if(size1<=size2)
len=size1;
else
len=size2;
for(i=0;i<len;i++)
c[i]=a[i]+b[i];
return 1;
}else{
return 0;
}
}
long *alloc_number(long size)
{
long *num;
if(num=malloc(size*sizeof(long)))
{
return num;
}else{
fprintf(stderr, "Speicher konnte nicht angelegt werden!");
exit(-1);
}
}
void free_number(long *mem)
{
if(mem)
{
free(mem);
mem=0;
}else{
fprintf(stderr, "Speicher konnte nicht freigegeben werden!");
exit(-1);
}
}
void print(long *mem, long size)
{
long i;
for(i=0;i<size;i++)
{
printf("\n%ld", mem[i]);
}
}
long *change_size(long *mem, long old_size, long new_size)
{
long *temp_num;
long i;
if(temp_num=realloc(mem, new_size*sizeof(long)))
{
if(new_size>old_size)
{
for(i=old_size;i<new_size;i++)
{
mem[i]=0;
}
}
return temp_num;
}else{
fprintf(stderr, "Speicher konnte nicht geaendert werden!");
exit(-1);
}
}
char **store_in_arry(char **storage, long length, char *newitem)
{
char **dummy=0;
if((dummy=realloc(storage, length*sizeof(char*))))
{
dummy[length-1]=newitem;
return dummy;
}else{
fprintf(stderr, "Speicher konnte nicht angelegt werden");
exit(-1);
}
}
char **delete_from_array(char **storage, long length, long delete_index)
{
char **dummy=0, *dummy2=0;
long i;
if(delete_index>(length-1))
{
printf("Index nicht im Feldbereich!");
return storage;
}else{
for(i=delete_index;i<length;i++)
{
storage[i]=storage[i+1];
}
if(storage[i])
{
free(storage[i]);
storage[i]=0;
}
if((dummy=realloc(storage, length*sizeof(char*))))
{
return dummy;
}else{
fprintf(stderr, "Speicher konnte nicht angelegt werden");
exit(-1);
}
}
}
void sort(char **storage, long length, long sort_type)
{
//Bubble Sort
long i, j;
char* h;
long (*sort_dir)(char*, char*);
if(sort_type==1)
sort_dir=&ascending;
else
sort_dir=&descending;
for(j=length-1; j>0; j=j-1)
{
for(i=0; i<j; i=i+1)
{
if(sort_dir(storage[i], storage[i+1])) //feld[i]>feld[i+1])
{
h=storage[i];
storage[i]=storage[i+1];
storage[i+1]=h;
}
}
}
}
long ascending(char *string1, char* string2)
{
if(strcmp(string1, string2)>0)
return 1;
else
return 0;
}
long descending(char *string1, char* string2)
{
if(strcmp(string1, string2)<0)
return 1;
else
return 0;
}