Untitled


SUBMITTED BY: Guest

DATE: Oct. 26, 2016, 8:09 p.m.

FORMAT: C

SIZE: 6.3 kB

HITS: 1099

  1. main.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define C_ARRAY_LEN 20
  5. char **store_in_arry(char **storage, long length, char *newitem);
  6. char **delete_from_array(char **storage, long length, long delete_index);
  7. void sort(char **storage, long length, long sort_type);
  8. long ascending(char *string1, char* string2);
  9. long descending(char *string1, char* string2);
  10. long *alloc_number(long size);
  11. void free_number(long *mem);
  12. void print(long *mem, long size);
  13. long *change_size(long *mem, long old_size, long new_size);
  14. long add(long *a, long size1, long *b, long size2, long *c, long size3);
  15. int main()
  16. {
  17. char **test=0;
  18. char in[C_ARRAY_LEN];
  19. char arr1[C_ARRAY_LEN]="Hallo ", arr2[C_ARRAY_LEN]="Welt";
  20. char arr3[C_ARRAY_LEN]="1234 ", arr4[C_ARRAY_LEN]="Test";
  21. long len=0, length=0, i=0, choice=0, len1=10, len2=10, len3=10, added;
  22. //long *arr_num=0, *arr1=0, *arr2=0, *arr3=0;
  23. /*
  24. do{
  25. printf("\nWas wollen Sie tun?\n");
  26. printf("0... Exit\n");
  27. printf("1... Speicher reservieren\n");
  28. printf("2... Speicher freigeben\n");
  29. printf("3... Speicher ausgeben\n");
  30. printf("4... Initialisieren\n");
  31. printf("5... Speicher verdoppeln\n");
  32. scanf("%ld", &choice);
  33. getchar();
  34. switch(choice){
  35. case 1:
  36. len=10;
  37. arr_num=alloc_number(len);
  38. break;
  39. case 2:
  40. free_number(arr_num);
  41. arr_num=0;
  42. break;
  43. case 3:
  44. print(arr_num, len);
  45. break;
  46. case 4:
  47. for(i=0;i<len;i++)
  48. arr_num[i]=i;
  49. break;
  50. case 5:
  51. arr_num=change_size(arr_num, len, 2*len);
  52. len=2*len;
  53. break;
  54. }
  55. }while(choice<10&&choice!=0);
  56. arr1=alloc_number(len1);
  57. arr2=alloc_number(len2);
  58. for(i=0;i<10;i++)
  59. arr1[i]=arr2[i]=i;
  60. arr3=alloc_number(len3);
  61. added=add(arr1, len1, arr2, len2, arr3, len3);
  62. if(added)
  63. print(arr3, len3);
  64. else
  65. printf("\nFehler beim addieren der Felder!\n");
  66. free_number(arr1);
  67. free_number(arr2);
  68. free_number(arr3);
  69. */
  70. printf("Geben Sie einen String ein: ");
  71. scanf("%s", in);
  72. getchar();
  73. length++;
  74. test=store_in_arry(test, length, in);
  75. length++;
  76. test=store_in_arry(test, length, arr2);
  77. length++;
  78. test=store_in_arry(test, length, arr3);
  79. length++;
  80. test=store_in_arry(test, length, arr4);
  81. for(i=0; i<length; i++)
  82. {
  83. printf("%s \n", test[i]);
  84. }
  85. sort(test, length, 1);
  86. printf("\nSorted:\n");
  87. for(i=0; i<length; i++)
  88. {
  89. printf("%s \n", test[i]);
  90. }
  91. length--;
  92. test=delete_from_array(test, length, 0);
  93. printf("\nDeleted:\n");
  94. for(i=0; i<length; i++)
  95. {
  96. printf("%s \n", test[i]);
  97. }
  98. if(test)
  99. {
  100. free(test);
  101. test=0;
  102. }
  103. //printf("Hello world!\n");
  104. return 0;
  105. }
  106. long add(long *a, long size1, long *b, long size2, long *c, long size3)
  107. {
  108. long i, len;
  109. if(size3>=size1&&size3>=size2)
  110. {
  111. if(size1<=size2)
  112. len=size1;
  113. else
  114. len=size2;
  115. for(i=0;i<len;i++)
  116. c[i]=a[i]+b[i];
  117. return 1;
  118. }else{
  119. return 0;
  120. }
  121. }
  122. long *alloc_number(long size)
  123. {
  124. long *num;
  125. if(num=malloc(size*sizeof(long)))
  126. {
  127. return num;
  128. }else{
  129. fprintf(stderr, "Speicher konnte nicht angelegt werden!");
  130. exit(-1);
  131. }
  132. }
  133. void free_number(long *mem)
  134. {
  135. if(mem)
  136. {
  137. free(mem);
  138. mem=0;
  139. }else{
  140. fprintf(stderr, "Speicher konnte nicht freigegeben werden!");
  141. exit(-1);
  142. }
  143. }
  144. void print(long *mem, long size)
  145. {
  146. long i;
  147. for(i=0;i<size;i++)
  148. {
  149. printf("\n%ld", mem[i]);
  150. }
  151. }
  152. long *change_size(long *mem, long old_size, long new_size)
  153. {
  154. long *temp_num;
  155. long i;
  156. if(temp_num=realloc(mem, new_size*sizeof(long)))
  157. {
  158. if(new_size>old_size)
  159. {
  160. for(i=old_size;i<new_size;i++)
  161. {
  162. mem[i]=0;
  163. }
  164. }
  165. return temp_num;
  166. }else{
  167. fprintf(stderr, "Speicher konnte nicht geaendert werden!");
  168. exit(-1);
  169. }
  170. }
  171. char **store_in_arry(char **storage, long length, char *newitem)
  172. {
  173. char **dummy=0;
  174. if((dummy=realloc(storage, length*sizeof(char*))))
  175. {
  176. dummy[length-1]=newitem;
  177. return dummy;
  178. }else{
  179. fprintf(stderr, "Speicher konnte nicht angelegt werden");
  180. exit(-1);
  181. }
  182. }
  183. char **delete_from_array(char **storage, long length, long delete_index)
  184. {
  185. char **dummy=0, *dummy2=0;
  186. long i;
  187. if(delete_index>(length-1))
  188. {
  189. printf("Index nicht im Feldbereich!");
  190. return storage;
  191. }else{
  192. for(i=delete_index;i<length;i++)
  193. {
  194. storage[i]=storage[i+1];
  195. }
  196. if(storage[i])
  197. {
  198. free(storage[i]);
  199. storage[i]=0;
  200. }
  201. if((dummy=realloc(storage, length*sizeof(char*))))
  202. {
  203. return dummy;
  204. }else{
  205. fprintf(stderr, "Speicher konnte nicht angelegt werden");
  206. exit(-1);
  207. }
  208. }
  209. }
  210. void sort(char **storage, long length, long sort_type)
  211. {
  212. //Bubble Sort
  213. long i, j;
  214. char* h;
  215. long (*sort_dir)(char*, char*);
  216. if(sort_type==1)
  217. sort_dir=&ascending;
  218. else
  219. sort_dir=&descending;
  220. for(j=length-1; j>0; j=j-1)
  221. {
  222. for(i=0; i<j; i=i+1)
  223. {
  224. if(sort_dir(storage[i], storage[i+1])) //feld[i]>feld[i+1])
  225. {
  226. h=storage[i];
  227. storage[i]=storage[i+1];
  228. storage[i+1]=h;
  229. }
  230. }
  231. }
  232. }
  233. long ascending(char *string1, char* string2)
  234. {
  235. if(strcmp(string1, string2)>0)
  236. return 1;
  237. else
  238. return 0;
  239. }
  240. long descending(char *string1, char* string2)
  241. {
  242. if(strcmp(string1, string2)<0)
  243. return 1;
  244. else
  245. return 0;
  246. }

comments powered by Disqus