Guest User

Untitled

a guest
Jun 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #include <pthread.h>
  7.  
  8. #include "springer.h"
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. if(argc < 3)
  13. {
  14. fprintf(stderr, "Usage: %s <Höhe> <Breite> [<Threads>]", argv[0]);
  15. exit(1);
  16. }
  17. if(argc == 3)
  18. {
  19. struct springer_zustand *zustand = initialisiere_zustand((unsigned int)atoi(argv[1]), (unsigned long)atoi(argv[2]));
  20. sprung(zustand, 1, 0, 0);
  21. }
  22. else if(argc == 4)
  23. {
  24. //TODO: Implement multithreading
  25. }
  26. else
  27. {
  28. fprintf(stderr, "Usage: %s <Höhe> <Breite> <Threads>", argv[0]);
  29. exit(1);
  30. }
  31.  
  32. return 0;
  33. }
  34.  
  35. struct springer_ausgabe *kopiere_ausgabe(struct springer_ausgabe *ausgabe)
  36. {
  37. struct springer_ausgabe *neue_ausgabe = malloc(sizeof(struct springer_ausgabe));
  38. neue_ausgabe->numlen = ausgabe->numlen; neue_ausgabe->zeilenlaenge = ausgabe->zeilenlaenge;
  39. neue_ausgabe->formatstring = ausgabe->formatstring;
  40. neue_ausgabe->feld = malloc(strlen(ausgabe->feld) + 1);
  41. return neue_ausgabe;
  42. }
  43.  
  44. struct springer_zustand *kopiere_zustand(struct springer_zustand *zustand)
  45. {
  46. struct springer_zustand *neuer_zustand = malloc(sizeof(struct springer_zustand));
  47. neuer_zustand->hoehe = zustand->hoehe; neuer_zustand->breite = zustand->breite;
  48. neuer_zustand->ausgabe = kopiere_ausgabe(zustand->ausgabe);
  49. neuer_zustand->feld = malloc(sizeof(unsigned long int) * zustand->hoehe * zustand->breite);
  50. memcpy(neuer_zustand->feld, zustand->feld, sizeof(unsigned long int) * zustand->hoehe * zustand->breite);
  51. return neuer_zustand;
  52. }
  53.  
  54. struct springer_thread *initialisiere_thread(struct springer_zustand *zustand, unsigned long int nr, unsigned int x, unsigned int y)
  55. {
  56. struct springer_thread *thread;
  57. thread->zustand = kopiere_zustand(zustand);
  58. thread->nr = nr;
  59. thread->x = x;
  60. thread->y = y;
  61. return thread;
  62. }
  63.  
  64. struct springer_ausgabe *initialisiere_ausgabe(unsigned int breite, unsigned int hoehe)
  65. {
  66. struct springer_ausgabe *ausgabe = malloc(sizeof(struct springer_ausgabe));
  67. if(ausgabe == NULL)
  68. {
  69. perror("Konnte den Speicher für die Augabe nicht allokieren.");
  70. exit(1);
  71. }
  72.  
  73. //Länge der Ausgabe der größten möglichen Nummer.
  74. char tmpstr[100];
  75. sprintf(tmpstr, "%ld", (unsigned long int)hoehe * breite);
  76. ausgabe->numlen = strlen(tmpstr);
  77.  
  78. ausgabe->zeilenlaenge = (ausgabe->numlen + 1) * breite + 2;
  79.  
  80. ausgabe->feld = malloc(ausgabe->zeilenlaenge * (1 + hoehe * 2));
  81. if(ausgabe->feld == NULL)
  82. {
  83. perror("Konnte den Speicher für das Schachbrett der Ausgabe nicht allokieren.");
  84. exit(1);
  85. }
  86.  
  87. sprintf(tmpstr, "%%%dld", ausgabe->numlen);
  88. //printf("%%%dld", zustand->ausgabe->numlen);
  89. ausgabe->formatstring = malloc(strlen(tmpstr) + 1);
  90. if(ausgabe->formatstring == NULL)
  91. {
  92. perror("Konnte den Speicher für den Formatstring der Ausgabe nicht allokieren.");
  93. exit(1);
  94. }
  95. memcpy(ausgabe->formatstring, &tmpstr, strlen(tmpstr) + 1);
  96. //printf("numlen: %d\nzeilenlaenge: %d\n", numlen, zustand->ausgabe->zeilenlaenge);
  97.  
  98. char *runner = ausgabe->feld;
  99. unsigned int x, y, i;
  100. *runner++ = '+';
  101. for(x = 0; x < breite; x++)
  102. {
  103. for(i = 0; i < ausgabe->numlen; i++)
  104. {
  105. *runner++ = '-';
  106. }
  107. *runner++ = '+';
  108. }
  109. *runner++ = '\n';
  110. for(y = 0; y < hoehe; y++)
  111. {
  112. *runner++ = '|';
  113. for(x = 0; x < breite; x++)
  114. {
  115. for(i = 0; i < ausgabe->numlen; i++)
  116. {
  117. *runner++ = ' ';
  118. }
  119. *runner++ = '|';
  120. }
  121. *runner++ = '\n';
  122. *runner++ = '+';
  123. for(x = 0; x < breite; x++)
  124. {
  125. for(i = 0; i < ausgabe->numlen; i++)
  126. {
  127. *runner++ = '-';
  128. }
  129. *runner++ = '+';
  130. }
  131. *runner++ = '\n';
  132. }
  133. *runner = (char) 0;
  134. //printf("%s\n", zustand->ausgabe->feld);
  135. return ausgabe;
  136. }
  137.  
  138. struct springer_zustand *initialisiere_zustand(unsigned int breite, unsigned int hoehe)
  139. {
  140. struct springer_zustand *zustand = malloc(sizeof(struct springer_zustand));
  141. if(zustand == NULL)
  142. {
  143. perror("Zustand");
  144. exit(1);
  145. }
  146.  
  147. zustand->hoehe = hoehe; zustand->breite = breite;
  148.  
  149. zustand->feld = malloc(sizeof(unsigned long int) * hoehe * breite);
  150. if(zustand->feld == NULL)
  151. {
  152. perror("Schachbrett");
  153. exit(1);
  154. }
  155.  
  156. zustand->ausgabe = initialisiere_ausgabe(breite, hoehe);
  157.  
  158. return zustand;
  159. }
  160.  
  161. void zerstoere_ausgabe(struct springer_ausgabe *ausgabe)
  162. {
  163. free(ausgabe->feld);
  164. free(ausgabe);
  165. }
  166.  
  167. void zerstoere_zustand(struct springer_zustand *zustand)
  168. {
  169. zerstoere_ausgabe(zustand->ausgabe);
  170. free(zustand->feld);
  171. free(zustand);
  172. }
  173.  
  174. void zerstoere_thread(struct springer_thread *thread)
  175. {
  176. zerstoere_zustand(thread->zustand);
  177. free(thread);
  178. }
  179.  
  180. void starte_thread(struct springer_zustand *zustand, unsigned long int nr, unsigned int x, unsigned int y)
  181. {
  182. struct springer_thread *thread = initialisiere_thread(zustand, nr, x, y);
  183. //TODO: pthread starten!
  184. }
  185.  
  186. void gestarteter_thread(struct springer_thread *thread)
  187. {
  188. //TODO: Inkrementiere Anzahl der Threads.
  189. thread_sprung(thread->zustand, thread->nr, thread->x, thread->y);
  190. zerstoere_thread(thread);
  191. //TODO: Dekrementiere Anzahl der Threads.
  192. }
  193.  
  194. void thread_sprung(struct springer_zustand *zustand, unsigned long int nr, unsigned int x, unsigned int y)
  195. {
  196. if(x < 0 || x >= zustand->breite || y < 0 || y >= zustand->hoehe)
  197. {
  198. return;
  199. }
  200.  
  201. if(zustand->feld[x + (y * zustand->breite)] > 0)
  202. {
  203. return;
  204. }
  205.  
  206. zustand->feld[x + (y * zustand->breite)] = nr;
  207.  
  208. if(nr == zustand->hoehe * zustand->breite)
  209. {
  210. ausgabe(zustand);
  211. }
  212. else
  213. {
  214. sprung(zustand, nr + 1, x + 2, y + 1);
  215. sprung(zustand, nr + 1, x + 2, y - 1);
  216. sprung(zustand, nr + 1, x + 1, y + 2);
  217. sprung(zustand, nr + 1, x + 1, y - 2);
  218. sprung(zustand, nr + 1, x - 2, y + 1);
  219. sprung(zustand, nr + 1, x - 2, y - 1);
  220. sprung(zustand, nr + 1, x - 1, y + 2);
  221. sprung(zustand, nr + 1, x - 1, y - 2);
  222. }
  223.  
  224. zustand->feld[x + (y * zustand->breite)] = 0;
  225. }
  226.  
  227. void sprung(struct springer_zustand *zustand, unsigned long int nr, unsigned int x, unsigned int y)
  228. {
  229. if(x < 0 || x >= zustand->breite || y < 0 || y >= zustand->hoehe)
  230. {
  231. return;
  232. }
  233.  
  234. if(zustand->feld[x + (y * zustand->breite)] > 0)
  235. {
  236. return;
  237. }
  238.  
  239. zustand->feld[x + (y * zustand->breite)] = nr;
  240.  
  241. if(nr == zustand->hoehe * zustand->breite)
  242. {
  243. ausgabe(zustand);
  244. }
  245. else
  246. {
  247. sprung(zustand, nr + 1, x + 2, y + 1);
  248. sprung(zustand, nr + 1, x + 2, y - 1);
  249. sprung(zustand, nr + 1, x + 1, y + 2);
  250. sprung(zustand, nr + 1, x + 1, y - 2);
  251. sprung(zustand, nr + 1, x - 2, y + 1);
  252. sprung(zustand, nr + 1, x - 2, y - 1);
  253. sprung(zustand, nr + 1, x - 1, y + 2);
  254. sprung(zustand, nr + 1, x - 1, y - 2);
  255. }
  256.  
  257. zustand->feld[x + (y * zustand->breite)] = 0;
  258. }
  259.  
  260. unsigned long int anzahl = 1;
  261. void ausgabe(struct springer_zustand *zustand)
  262. {
  263. unsigned int x, y;
  264. char tmpstr[100];
  265. for(y = 0; y < zustand->hoehe; y++)
  266. {
  267. for(x = 0; x < zustand->breite; x++)
  268. {
  269. sprintf(tmpstr, zustand->ausgabe->formatstring, zustand->feld[x + (y * zustand->breite)]);
  270. memcpy(zustand->ausgabe->feld + ((2 * y + 1) * zustand->ausgabe->zeilenlaenge) + ((1 + zustand->ausgabe->numlen) * x) + 1, &tmpstr, strlen(tmpstr));
  271. }
  272. }
  273. printf("\n+----------\n|%10ld\n%s\n", anzahl++, zustand->ausgabe->feld);
  274. }
Add Comment
Please, Sign In to add comment