Advertisement
Guest User

100 puta

a guest
Apr 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #include <string.h>
  6.  
  7. typedef struct runda
  8. {
  9. char ime[20];
  10. struct runda *leva;
  11. struct runda *desna;
  12. int nosioc_br;
  13. }Runda;
  14.  
  15. Runda *runde[555];
  16. int counter = 0;
  17.  
  18. typedef struct queue
  19. {
  20. int niz[555];
  21. int front, rear;
  22. }Queue;
  23.  
  24. int queue_is_empty(Queue *q)
  25. {
  26. if (q->front == q->rear)
  27. return 1;
  28. else
  29. return 0;
  30. }
  31.  
  32. void queueInsert(Queue *q, Runda *runda)
  33. {
  34. q->rear += 1;
  35. q->niz[q->rear] = runda;
  36. }
  37.  
  38. Runda *queueGet(Queue *q)
  39. {
  40. q->front++;
  41. Runda *runda = q->niz[q->front];
  42. return runda;
  43. }
  44.  
  45. void setChildToNull(Runda *runda)
  46. {
  47. runda->leva = NULL;
  48. runda->desna = NULL;
  49. }
  50. int additon = 0;
  51. void skeleton(Runda *runda,int n){
  52.  
  53. int max;
  54. Queue *q;
  55. q = (Queue*)malloc(sizeof(Queue));
  56. q->front = 0;
  57. q->rear = 0;
  58. queueInsert(q,runda);
  59.  
  60. Runda *pom1;
  61. Runda *pom2;
  62. runde[counter++] = runda;
  63.  
  64. max = 1;
  65. while (1){
  66. for (int i = 0; i < max; i++) {
  67. Runda *from_queue = queueGet(q);
  68. pom1 = (Runda*)malloc(sizeof(Runda));
  69. pom2 = (Runda*)malloc(sizeof(Runda));
  70. setChildToNull(pom1);
  71. setChildToNull(pom2);
  72. pom1->nosioc_br = from_queue->nosioc_br;
  73. pom2->nosioc_br = 2 * max - from_queue->nosioc_br + 1;
  74. from_queue->leva = pom1;
  75. from_queue->desna = pom2;
  76. queueInsert(q,pom1); queueInsert(q,pom2);
  77. runde[counter++] = pom1;
  78. runde[counter++] = pom2;
  79.  
  80. }
  81. max *= 2;
  82. if (max * 2 > n)
  83. break;
  84. }
  85. int last_level = log2(n);
  86. last_level = n - pow(2, last_level);
  87. int last = n-last_level;
  88.  
  89. for (int i = 0; i < last_level; i++)
  90. {
  91. int j = counter;
  92. j--;
  93. while (j > 0){
  94. if (runde[j]->nosioc_br == last){
  95.  
  96. last--;
  97. int f;
  98. printf("Koji broj zelite da zakacite na %d?:\n", last + 1);
  99. scanf("%d",&f);
  100. Runda *pom1 = (Runda*)malloc(sizeof(Runda));
  101. setChildToNull(pom1);
  102. Runda *pom2 = (Runda*)malloc(sizeof(Runda));
  103. setChildToNull(pom2);
  104. pom1->nosioc_br = runde[j]->nosioc_br;
  105. pom2->nosioc_br = f;
  106. runde[j]->leva = pom1;
  107. runde[j]->desna = pom2;
  108. additon += 2;
  109. break;
  110. }
  111. j--;
  112. }
  113. }
  114.  
  115. }
  116.  
  117. void UnosenjeIgraca(Runda *runda)
  118. {
  119. Queue *q = (Queue*)malloc(sizeof(Queue));
  120. q->rear = 0;
  121. q->front = 0;
  122.  
  123.  
  124. int x = 1;
  125. int cnt = 0;
  126. queueInsert(q, runda);
  127. while (!queue_is_empty(q))
  128. {
  129. Runda *pom = queueGet(q);
  130. //printf("%d-", pom->nosioc_br);
  131. //printf("%s ", pom->ime);
  132.  
  133. if (pom->leva == NULL && pom->desna == NULL)
  134. {
  135. printf("Ime igraca br: %d?",pom->nosioc_br); scanf("%s", &pom->ime);
  136. }
  137. else
  138. {
  139. queueInsert(q, pom->leva); queueInsert(q, pom->desna);
  140. }
  141. }
  142. }
  143.  
  144. void printaj(Runda *runda){
  145.  
  146. Queue *q = (Queue*)malloc(sizeof(Queue));
  147. q->rear = 0;
  148. q->front = 0;
  149.  
  150.  
  151. int x = 1;
  152. int cnt = 0;
  153. queueInsert(q, runda);
  154. while (!queue_is_empty(q))
  155. {
  156. Runda *pom = queueGet(q);
  157. printf("%d-", pom->nosioc_br);
  158. //printf("%s ", pom->ime);
  159. cnt++;
  160. if (cnt == x)
  161. {
  162. cnt = 0; x *= 2;
  163. printf("\nSledeci nivo:\n");
  164. }
  165. if (pom->leva != NULL && pom->desna != NULL)
  166. {
  167. queueInsert(q, pom->leva);queueInsert(q, pom->desna);
  168. }
  169. }
  170. }
  171.  
  172. void izbrisi(Runda *runda)
  173. {
  174. Queue *q = (Queue*)malloc(sizeof(Queue));
  175. q->rear = 0;
  176. q->front = 0;
  177.  
  178. int x = 1;
  179. int cnt = 0;
  180. queueInsert(q, runda);
  181. while (!queue_is_empty(q))
  182. {
  183. Runda *pom = queueGet(q);
  184. printf("%d-", pom->nosioc_br);
  185. //printf("%s ", pom->ime);
  186. cnt++;
  187. if (cnt == x)
  188. {
  189. cnt = 0; x *= 2;
  190. //printf("\nSledeci nivo:\n");
  191. }
  192. if (pom->leva != NULL && pom->desna != NULL)
  193. {
  194. queueInsert(q, pom->leva); queueInsert(q, pom->desna);
  195. }
  196. free(pom);
  197. }
  198. }
  199.  
  200.  
  201. int popunjeno[555];
  202. void popuniRezultate(Runda *root)
  203. {
  204. int start;
  205.  
  206. int i = counter - 1;
  207. while (1)
  208. {
  209. if (runde[i]->leva != NULL && runde[i]->desna != NULL)
  210. {
  211. break;
  212. }
  213. i--;
  214. }
  215.  
  216. for (int j = i; j >= 0; j--)
  217. {
  218. if (runde[j]->leva != NULL && runde[j]->desna != NULL)
  219. {
  220. printf("Rezultat za %s versus %s:? \n", runde[j]->leva->ime, runde[j]->desna->ime);
  221. int a1, a2; scanf("%d%d", &a1, &a2);
  222. if (a1 > a2)
  223. {
  224. strcpy(runde[j], runde[j]->leva);
  225. }
  226. else if (a1 < a2)
  227. {
  228. strcpy(runde[j], runde[j]->desna);
  229. }
  230. }
  231. }
  232.  
  233. printf("Svi rezultati su uneti!\n");
  234. }
  235.  
  236. void InOrder(Runda *root)
  237. {
  238. if (root != NULL) {
  239. InOrder(root->leva);
  240. printf("%d ", root->nosioc_br);
  241. InOrder(root->desna);
  242. }
  243. }
  244.  
  245. int getVisina()
  246. {
  247. int visina = floor(log2(counter+additon));
  248. return visina;
  249. }
  250.  
  251. int main()
  252. {
  253. Runda *root = (Runda*)malloc(sizeof(Runda));
  254. root->nosioc_br = 1;
  255. //Queue *q = (Queue*)malloc(sizeof(Queue));
  256. //q->rear = 0;
  257. //q->front = 0;
  258. //queueInsert(q, root);
  259. setChildToNull(root);
  260.  
  261. while (1)
  262. {
  263. int x;
  264. printf("\n1 - Napravi skelet\n");
  265. printf("2 - Unesi igrace\n");
  266. printf("3 - Popuni rezultate\n");
  267. printf("4 - Printaj po level order\n");
  268. printf("5 - pobednik?\n");
  269. printf("6 - Visina stabla?\n");
  270. scanf("%d", &x);
  271. int n;
  272.  
  273. if (x == 1)
  274. {
  275. printf("broj igraca?");
  276. scanf("%d", &n);
  277. skeleton(root, n);
  278. }
  279. else if (x == 2)
  280. {
  281. UnosenjeIgraca(root);
  282. }else if(x == 3) {
  283. popuniRezultate(root);
  284. }
  285. else if (x == 4)
  286. {
  287. printaj(root);
  288. }
  289. else if (x == 5)
  290. {
  291. printf("Winner: %s", runde[0]->ime);
  292. }
  293. else if (x == 6)
  294. {
  295. printf("Visina : %d",getVisina());
  296. }
  297.  
  298. }
  299.  
  300. //InOrder(root);
  301.  
  302. return 0;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement