Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- typedef struct runda
- {
- char ime[20];
- struct runda *leva;
- struct runda *desna;
- int nosioc_br;
- }Runda;
- Runda *runde[555];
- int counter = 0;
- typedef struct queue
- {
- int niz[555];
- int front, rear;
- }Queue;
- int queue_is_empty(Queue *q)
- {
- if (q->front == q->rear)
- return 1;
- else
- return 0;
- }
- void queueInsert(Queue *q, Runda *runda)
- {
- q->rear += 1;
- q->niz[q->rear] = runda;
- }
- Runda *queueGet(Queue *q)
- {
- q->front++;
- Runda *runda = q->niz[q->front];
- return runda;
- }
- void setChildToNull(Runda *runda)
- {
- runda->leva = NULL;
- runda->desna = NULL;
- }
- int additon = 0;
- void skeleton(Runda *runda,int n){
- int max;
- Queue *q;
- q = (Queue*)malloc(sizeof(Queue));
- q->front = 0;
- q->rear = 0;
- queueInsert(q,runda);
- Runda *pom1;
- Runda *pom2;
- runde[counter++] = runda;
- max = 1;
- while (1){
- for (int i = 0; i < max; i++) {
- Runda *from_queue = queueGet(q);
- pom1 = (Runda*)malloc(sizeof(Runda));
- pom2 = (Runda*)malloc(sizeof(Runda));
- setChildToNull(pom1);
- setChildToNull(pom2);
- pom1->nosioc_br = from_queue->nosioc_br;
- pom2->nosioc_br = 2 * max - from_queue->nosioc_br + 1;
- from_queue->leva = pom1;
- from_queue->desna = pom2;
- queueInsert(q,pom1); queueInsert(q,pom2);
- runde[counter++] = pom1;
- runde[counter++] = pom2;
- }
- max *= 2;
- if (max * 2 > n)
- break;
- }
- int last_level = log2(n);
- last_level = n - pow(2, last_level);
- int last = n-last_level;
- for (int i = 0; i < last_level; i++)
- {
- int j = counter;
- j--;
- while (j > 0){
- if (runde[j]->nosioc_br == last){
- last--;
- int f;
- printf("Koji broj zelite da zakacite na %d?:\n", last + 1);
- scanf("%d",&f);
- Runda *pom1 = (Runda*)malloc(sizeof(Runda));
- setChildToNull(pom1);
- Runda *pom2 = (Runda*)malloc(sizeof(Runda));
- setChildToNull(pom2);
- pom1->nosioc_br = runde[j]->nosioc_br;
- pom2->nosioc_br = f;
- runde[j]->leva = pom1;
- runde[j]->desna = pom2;
- additon += 2;
- break;
- }
- j--;
- }
- }
- }
- void UnosenjeIgraca(Runda *runda)
- {
- Queue *q = (Queue*)malloc(sizeof(Queue));
- q->rear = 0;
- q->front = 0;
- int x = 1;
- int cnt = 0;
- queueInsert(q, runda);
- while (!queue_is_empty(q))
- {
- Runda *pom = queueGet(q);
- //printf("%d-", pom->nosioc_br);
- //printf("%s ", pom->ime);
- if (pom->leva == NULL && pom->desna == NULL)
- {
- printf("Ime igraca br: %d?",pom->nosioc_br); scanf("%s", &pom->ime);
- }
- else
- {
- queueInsert(q, pom->leva); queueInsert(q, pom->desna);
- }
- }
- }
- void printaj(Runda *runda){
- Queue *q = (Queue*)malloc(sizeof(Queue));
- q->rear = 0;
- q->front = 0;
- int x = 1;
- int cnt = 0;
- queueInsert(q, runda);
- while (!queue_is_empty(q))
- {
- Runda *pom = queueGet(q);
- printf("%d-", pom->nosioc_br);
- //printf("%s ", pom->ime);
- cnt++;
- if (cnt == x)
- {
- cnt = 0; x *= 2;
- printf("\nSledeci nivo:\n");
- }
- if (pom->leva != NULL && pom->desna != NULL)
- {
- queueInsert(q, pom->leva);queueInsert(q, pom->desna);
- }
- }
- }
- void izbrisi(Runda *runda)
- {
- Queue *q = (Queue*)malloc(sizeof(Queue));
- q->rear = 0;
- q->front = 0;
- int x = 1;
- int cnt = 0;
- queueInsert(q, runda);
- while (!queue_is_empty(q))
- {
- Runda *pom = queueGet(q);
- printf("%d-", pom->nosioc_br);
- //printf("%s ", pom->ime);
- cnt++;
- if (cnt == x)
- {
- cnt = 0; x *= 2;
- //printf("\nSledeci nivo:\n");
- }
- if (pom->leva != NULL && pom->desna != NULL)
- {
- queueInsert(q, pom->leva); queueInsert(q, pom->desna);
- }
- free(pom);
- }
- }
- int popunjeno[555];
- void popuniRezultate(Runda *root)
- {
- int start;
- int i = counter - 1;
- while (1)
- {
- if (runde[i]->leva != NULL && runde[i]->desna != NULL)
- {
- break;
- }
- i--;
- }
- for (int j = i; j >= 0; j--)
- {
- if (runde[j]->leva != NULL && runde[j]->desna != NULL)
- {
- printf("Rezultat za %s versus %s:? \n", runde[j]->leva->ime, runde[j]->desna->ime);
- int a1, a2; scanf("%d%d", &a1, &a2);
- if (a1 > a2)
- {
- strcpy(runde[j], runde[j]->leva);
- }
- else if (a1 < a2)
- {
- strcpy(runde[j], runde[j]->desna);
- }
- }
- }
- printf("Svi rezultati su uneti!\n");
- }
- void InOrder(Runda *root)
- {
- if (root != NULL) {
- InOrder(root->leva);
- printf("%d ", root->nosioc_br);
- InOrder(root->desna);
- }
- }
- int getVisina()
- {
- int visina = floor(log2(counter+additon));
- return visina;
- }
- int main()
- {
- Runda *root = (Runda*)malloc(sizeof(Runda));
- root->nosioc_br = 1;
- //Queue *q = (Queue*)malloc(sizeof(Queue));
- //q->rear = 0;
- //q->front = 0;
- //queueInsert(q, root);
- setChildToNull(root);
- while (1)
- {
- int x;
- printf("\n1 - Napravi skelet\n");
- printf("2 - Unesi igrace\n");
- printf("3 - Popuni rezultate\n");
- printf("4 - Printaj po level order\n");
- printf("5 - pobednik?\n");
- printf("6 - Visina stabla?\n");
- scanf("%d", &x);
- int n;
- if (x == 1)
- {
- printf("broj igraca?");
- scanf("%d", &n);
- skeleton(root, n);
- }
- else if (x == 2)
- {
- UnosenjeIgraca(root);
- }else if(x == 3) {
- popuniRezultate(root);
- }
- else if (x == 4)
- {
- printaj(root);
- }
- else if (x == 5)
- {
- printf("Winner: %s", runde[0]->ime);
- }
- else if (x == 6)
- {
- printf("Visina : %d",getVisina());
- }
- }
- //InOrder(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement