Guest User

Untitled

a guest
Jun 24th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. //#include "springer.h" START
  7. struct springer_ausgabe
  8. {
  9. int zeilenlaenge, numlen;
  10. char *feld, *formatstring;
  11. };
  12.  
  13. struct springer_zustand
  14. {
  15. int hoehe, breite;
  16. long *feld;
  17. struct springer_ausgabe *ausgabe;
  18. };
  19.  
  20. void ausgabe(struct springer_zustand *zustand);
  21. struct springer_zustand *initialisiere(int breite, int hoehe);
  22. void sprung(struct springer_zustand *zustand, int nr, int x, int y);
  23. //#include "springer.h" ENDE
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. if(argc < 3)
  28. {
  29. fprintf(stderr, "Usage: %s <Höhe> <Breite> [<Threads>]", argv[0]);
  30. exit(1);
  31. }
  32. if(argc == 3)
  33. {
  34. struct springer_zustand *zustand = initialisiere(atoi(argv[1]), atoi(argv[2]));
  35. sprung(zustand, 1, 0, 0);
  36. }
  37. else if(argc == 4)
  38. {
  39. //TODO: Implement multithreading
  40. }
  41. else
  42. {
  43. fprintf(stderr, "Usage: %s <Höhe> <Breite> <Threads>", argv[0]);
  44. exit(1);
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50. struct springer_zustand *initialisiere(int breite, int hoehe)
  51. {
  52. struct springer_zustand *zustand = malloc(sizeof(struct springer_zustand));
  53. if(zustand == NULL)
  54. {
  55. perror("Konnte den Speicher für den Zustand nicht allokieren.");
  56. exit(1);
  57. }
  58.  
  59. zustand->hoehe = hoehe; zustand->breite = breite;
  60.  
  61. zustand->feld = malloc(sizeof(long) * hoehe * breite);
  62. if(zustand->feld == NULL)
  63. {
  64. perror("Konnte den Speicher für das Schachbrett nicht allokieren.");
  65. exit(1);
  66. }
  67.  
  68.  
  69. zustand->ausgabe = malloc(sizeof(struct springer_ausgabe));
  70. if(zustand->ausgabe == NULL)
  71. {
  72. perror("Konnte den Speicher für die Augabe nicht allokieren.");
  73. exit(1);
  74. }
  75.  
  76. //Länge der Ausgabe der größten möglichen Nummer.
  77. char tmpstr[100];
  78. sprintf(tmpstr, "%ld", (long int)hoehe * breite);
  79. zustand->ausgabe->numlen = strlen(tmpstr);
  80.  
  81. zustand->ausgabe->zeilenlaenge = (zustand->ausgabe->numlen + 1) * breite + 2;
  82.  
  83. zustand->ausgabe->feld = malloc(zustand->ausgabe->zeilenlaenge * (1 + hoehe * 2));
  84. if(zustand->ausgabe->feld == NULL)
  85. {
  86. perror("Konnte den Speicher für das Schachbrett der Ausgabe nicht allokieren.");
  87. exit(1);
  88. }
  89.  
  90. sprintf(tmpstr, "%%%dld", zustand->ausgabe->numlen);
  91. //printf("%%%dld", zustand->ausgabe->numlen);
  92. zustand->ausgabe->formatstring = malloc(strlen(tmpstr) + 1);
  93. if(zustand->ausgabe->formatstring == NULL)
  94. {
  95. perror("Konnte den Speicher für den Formatstring der Ausgabe nicht allokieren.");
  96. exit(1);
  97. }
  98. memcpy(zustand->ausgabe->formatstring, &tmpstr, strlen(tmpstr) + 1);
  99. //printf("numlen: %d\nzeilenlaenge: %d\n", numlen, zustand->ausgabe->zeilenlaenge);
  100.  
  101. char *runner = zustand->ausgabe->feld;
  102. int x, y, i;
  103. *runner++ = '+';
  104. for(x = 0; x < breite; x++)
  105. {
  106. for(i = 0; i < zustand->ausgabe->numlen; i++)
  107. {
  108. *runner++ = '-';
  109. }
  110. *runner++ = '+';
  111. }
  112. *runner++ = '\n';
  113. for(y = 0; y < hoehe; y++)
  114. {
  115. *runner++ = '|';
  116. for(x = 0; x < breite; x++)
  117. {
  118. for(i = 0; i < zustand->ausgabe->numlen; i++)
  119. {
  120. *runner++ = ' ';
  121. }
  122. *runner++ = '|';
  123. }
  124. *runner++ = '\n';
  125. *runner++ = '+';
  126. for(x = 0; x < breite; x++)
  127. {
  128. for(i = 0; i < zustand->ausgabe->numlen; i++)
  129. {
  130. *runner++ = '-';
  131. }
  132. *runner++ = '+';
  133. }
  134. *runner++ = '\n';
  135. }
  136. *runner = (char) 0;
  137. //printf("%s\n", zustand->ausgabe->feld);
  138.  
  139. return zustand;
  140. }
  141.  
  142. void sprung(struct springer_zustand *zustand, int nr, int x, int y)
  143. {
  144. if(x < 0 || x >= zustand->breite || y < 0 || y >= zustand->hoehe)
  145. {
  146. return;
  147. }
  148.  
  149. if(zustand->feld[x + (y * zustand->breite)] > 0)
  150. {
  151. return;
  152. }
  153.  
  154. zustand->feld[x + (y * zustand->breite)] = nr;
  155.  
  156. if(nr == zustand->hoehe * zustand->breite)
  157. {
  158. ausgabe(zustand);
  159. }
  160. else
  161. {
  162. sprung(zustand, nr + 1, x + 2, y + 1);
  163. sprung(zustand, nr + 1, x + 2, y - 1);
  164.  
  165. sprung(zustand, nr + 1, x + 1, y + 2);
  166. sprung(zustand, nr + 1, x + 1, y - 2);
  167.  
  168. sprung(zustand, nr + 1, x - 2, y + 1);
  169. sprung(zustand, nr + 1, x - 2, y - 1);
  170.  
  171. sprung(zustand, nr + 1, x - 1, y + 2);
  172. sprung(zustand, nr + 1, x - 1, y - 2);
  173. }
  174.  
  175. zustand->feld[x + (y * zustand->breite)] = 0;
  176. }
  177.  
  178. int anzahl = 1;
  179. void ausgabe(struct springer_zustand *zustand)
  180. {
  181. int x, y;
  182. char tmpstr[100];
  183. for(y = 0; y < zustand->hoehe; y++)
  184. {
  185. for(x = 0; x < zustand->breite; x++)
  186. {
  187. sprintf(tmpstr, zustand->ausgabe->formatstring, zustand->feld[x + (y * zustand->breite)]);
  188. memcpy(zustand->ausgabe->feld + ((2 * y + 1) * zustand->ausgabe->zeilenlaenge) + ((1 + zustand->ausgabe->numlen) * x) + 1, &tmpstr, strlen(tmpstr));
  189. }
  190. }
  191. printf("\n+----------\n|%10d\n%s\n", anzahl++, zustand->ausgabe->feld);
  192. }
Add Comment
Please, Sign In to add comment