Kod: |
#include <stdio.h>
#include <stdlib.h> #include <string.h> #include <ctype.h> #define B 1000 #define EMPTY "0" #define DELETED "-1" typedef char elementtype[21]; typedef elementtype DICTIONARY[B]; void MAKE_NULL (DICTIONARY *Ap){ int i; for(i=0; i<B; i++) strcpy((*Ap)[i],EMPTY); } int h(elementtype x){ int i, sum=0; for(i=0;i<20;i++) sum += x'[i]'; return sum%B; } int locate(elementtype x, DICTIONARY A){ int initail, i; initail=h(x); i=0; while((i<B) && strcmp(A[(initail+i)%B],x)!=0 && strcmp(A[(initail+i)%B],EMPTY)!=0) ++i; return ((initail+i)%B); } int locate1(elementtype x, DICTIONARY A){ int initail, i; initail=h(x); i=0; while( (i<B) && strcmp(A[(initail+i)%B],x)!=0 && strcmp(A[(initail+i)%B],EMPTY)!=0 && strcmp(A[(initail+i)%B],DELETED)!=0) ++i; return ((initail+i)%B); } int MEMBER(elementtype x, DICTIONARY A) { if(strcmp(A[locate(x,A)],x)==0) return 1; else return 0; } void INSERT(elementtype x, DICTIONARY *Ap) { int bucket; if(MEMBER(x,*Ap)) return; bucket = locate1(x,*Ap); if(strcmp((*Ap)[bucket],EMPTY)==0 || strcmp((*Ap)[bucket],DELETED)==0){ strcpy((*Ap)[bucket],x); } else{ printf("error-table is full"); exit(1); } } void DELETE(elementtype x, DICTIONARY *Ap) { int bucket; bucket=locate(x,*Ap); if(strcmp((*Ap)[bucket],x)==0) strcpy((*Ap)[bucket], DELETED); } void dodatno_ucitavanje(DICTIONARY *A){ while(1){ elementtype operacija, string; scanf("%s", operacija); scanf("%s", string); if(strcmp(operacija,"UNESI")==0){ INSERT(string,A); } else if(strcmp(operacija,"OBRISI")==0){ DELETE(string,A); } else if(strcmp(operacija,"JELICLAN")==0){ if(MEMBER(string,*A)){ printf("%s: DA!\n", string); } else{ printf("%s: NE!\n", string); } } else break; } } int main(void) { DICTIONARY A; MAKE_NULL(&A); INSERT("auto",&A); INSERT("break",&A); INSERT("case",&A) ;INSERT("char",&A); INSERT("const",&A) ;INSERT ("continue",&A); INSERT("do",&A); INSERT("double",&A) ;INSERT("default",&A); INSERT("else",&A); INSERT("enum",&A); INSERT("extern",&A); INSERT("float", &A); INSERT("for",&A); INSERT("goto",&A); INSERT("if",&A); INSERT("int",&A); INSERT("long",&A); INSERT("register",&A);INSERT("return",&A);INSERT("short",&A);INSERT ("signed", &A);INSERT ("sizeof", &A);INSERT ("static", &A); INSERT("struct",&A);INSERT("switch",&A);INSERT("typedef",&A);INSERT("union",&A);INSERT("void",&A);INSERT("volatile",&A); INSERT("while",&A); dodatno_ucitavanje(&A); return 0; } |
Kod: |
void PREPISI (int i, int j, DICTIONARY *Dp) { strcpy(Dp->elements[i], Dp->elements[j]); if (2*j+1 < MAXSIZE) PREPISI (2*i+1, 2*j+1, Dp); else if (2*j+2 < MAXSIZE) PREPISI (2*i+2, 2*j+2, Dp); } void DELETE (elementtype x, DICTIONARY *Dp) { int i=0, j; //trazimo x while ( i < MAXSIZE ) { if ( strcmp(Dp->elements[i], x) == 0 ) break; else if ( strcmp(Dp->elements[i], x) > 0) i=2*i+1; else i=2*i+2; } if ( i < MAXSIZE ) { //x je u listu if ( ((strcmp(Dp->elements[2*i+1], "PRAZNO") == 0) || (2*i+1 >= MAXSIZE)) && ((strcmp(Dp->elements[2*i+2], "PRAZNO") == 0) || (2*i+2 >= MAXSIZE)) ) { strcpy(Dp->elements[i], "PRAZNO"); } //x ima samo desno dijete else if ((strcmp(Dp->elements[2*i+1], "PRAZNO") == 0) || (2*i+1 >= MAXSIZE)) PREPISI(i, 2*i+2, Dp); //x ima samo lijevo dijete else if ((strcmp(Dp->elements[2*i+2], "PRAZNO") == 0) || (2*i+2 >= MAXSIZE)) PREPISI(i, 2*i+1, Dp); //x ima oba djeteta else { elementtype y = OBRISI_NAJMANJI(2*i+2, Dp); //brisemo najmanji cvor iz desnog podstabla strcpy(Dp->elements[i], y); } } if ( i > MAXSIZE ) printf("\nNema %s u rijecniku.\n", x); } elementtype OBRISI_NAJMANJI (int i, DICTIONARY *Dp) { elementtype mini; elementtype brisi; if ((strcmp(Dp->elements[2*i+1], "PRAZNO") == 0) || (2*i+1 >= MAXSIZE)) //i je najmanji { strcpy (mini,Dp->elements[i]); if((strcmp(Dp->elements[2*i+2], "PRAZNO") != 0) && (2*i+2 < MAXSIZE)) //i ima desno dijete PREPISI(i, 2*i+2, Dp); else if(((strcmp(Dp->elements[2*i+1], "PRAZNO") == 0) || (2*i+1 >= MAXSIZE)) && ((strcmp(Dp->elements[2*i+2], "PRAZNO") == 0) || (2*i+2 >= MAXSIZE))) //i je list strcpy(Dp->elements[i], "PRAZNO"); } //i ima lijevo dijete else if((strcmp(Dp->elements[2*i+1], "PRAZNO") != 0) || (2*i+1 < MAXSIZE)) strcpy(mini, OBRISI_NAJMANJI(2*i+1, Dp)); return mini; } |
Kod: |
#define MAXSIZE 100 //najveci dopusteni broj rijeci #define elementtype char* typedef struct { elementtype elements[MAXSIZE]; } DICTIONARY; |
Riječnik.c | |||
Description: |
|
Download |
|
Filename: | Riječnik.c | ||
Filesize: | 6.64 KB | ||
Downloaded: | 290 Time(s) |
Kod: |
#include<stdio.h>
#define max 100 typedef int node; typedef struct{ char label; node mama, brat, sin; }TREE; node MAKE_ROOT(char l, TREE* tree){ node i; tree->label = l; tree->mama = -1; tree->brat = -1; tree->sin = -1; for(i = 1; i < max; i++){ (tree + i)->label = '\0'; (tree + i)->mama = -1; (tree + i)->brat = -1; (tree + i)->sin = -1; } return 0; } node INSERT_CHILD(char l, node i, TREE* tree){ if(i >= max || tree->label == '\0') return -1; node j; for(j = 1; j < max; j++) if((tree + j)->label == '\0') break; if(j >= max) return -1; (tree + j)->label = l; (tree + j)->mama = i; (tree + j)->brat = (tree + i)->sin; (tree + j)->sin = -1; return j; } node INSERT_SIBLING(char l, node i, TREE* tree){ if(i >= max || tree->label == '\0' || i == 0) return -1; node j, k; for(j = 1; j < max; j++) if((tree + j)->label == '\0') break; if(j >= max) return -1; for(k = 1; k < max; k++) if((tree + k)->label == '\0') break; if(k >= max) return -1; (tree + j)->label = l; (tree + j)->mama = (tree+i)->mama; (tree + j)->brat = (tree + i)->brat; (tree + i)->brat = j; (tree + j)->sin = -1; return j; } void DELETE(node i, TREE* tree){ if(i >= max || tree->label == '\0' || i == 0 || (tree + i)->sin != -1) return; if((tree + i)->brat != -1){ if((tree + (tree + i)->mama)->sin == i)(tree + (tree + i)->mama)->sin = (tree + i)->brat; else{ node j; j == (tree + (tree + i)->mama)->sin; while((tree + j)->brat != i) j = (tree + j)->brat; (tree + j)->brat = (tree + i)->brat; } } (tree + i)->label = '\0'; (tree + i)->mama = -1; (tree + i)->brat = -1; (tree + i)->sin = -1; } node ROOT(TREE* tree){ return 0; } node FIRST_CHILD(node i, TREE* tree){ if(i >= max || tree->label == '\0') return -1; return (tree + i)->sin; } node NEXT_SIBLING(node i, TREE* tree){ if(i >= max || tree->label == '\0') return -1; return (tree + i)->brat; } node PARENT(node i, TREE* tree){ if(i >= max || tree->label == '\0') return -1; return (tree + i)->mama; } char LABEL(node i, TREE* tree){ if(i >= max || tree->label == '\0') return '\0'; return (tree + i)->label; } void CHANGE_LABEL(char l, node i, TREE* tree){ if(i >= max || tree->label == '\0') return; (tree + i)->label = l; } int broj_potomaka(node i, TREE* tree){ if(i >= max || LABEL(ROOT(tree), tree) == '\0' || i == -1) return 0; int suma = 0; node j; j = FIRST_CHILD(i, tree); while(j != -1){ suma = suma + 1 + broj_potomaka(j, tree); j = NEXT_SIBLING(j, tree); } return suma; } void unesi_stablo(TREE* tree, node i){ int n, k; char c; node j; if(i != ROOT(tree)) printf("Promatramo cvor %c", LABEL(i, tree)); else{ printf("Unesite ime korijena: "); scanf("%c", &c); CHANGE_LABEL(c, ROOT(tree), tree); } printf("Unesite broj djece cvora: "); scanf("%d", &n); if(n == 0) return; for(k = 0; k < n; k++){ printf("Unesite ime %d. djeteta: ", k+1); scanf(" %c", &c); INSERT_CHILD(c, i, tree); } for(k = 0; k < n; k++){ if(k == 0) j = FIRST_CHILD(i, tree); else j = NEXT_SIBLING(j, tree); unesi_stablo(tree, j); } } int main(void){ TREE T[max]; node i; int n, m; MAKE_ROOT('\0', T); unesi_stablo(T, ROOT(T)); printf("%c ", LABEL(ROOT(T), T)); CHANGE_LABEL('\0', ROOT(T), T); while(FIRST_CHILD(ROOT(T), T) != -1){ i = ROOT(T); while(FIRST_CHILD(i, T) != -1){ if(LABEL(i, T) != '\0'){ printf("%c ", LABEL(i,T)); CHANGE_LABEL('\0', i, T); i = NEXT_SIBLING(i, T); } } if(LABEL(i, T) != '\0'){ printf("%c ", LABEL(i,T)); CHANGE_LABEL('\0', i, T); } DELETE(i, T); } return 0; } |
Zenon (napisa): |
Da, to i jesam bio napisao, samo malo krivo pa me je kasnije zbunilo. Sada taj dio radi. Puno hvala! No, nešto drugo ne štima. Dio koda if(i != ROOT(tree)) printf("Promatramo cvor %c", LABEL(i, tree)); mi ispiše samo Promatramo cvor i ne razumijem zašto. Nikada ne ispiše label čvora. :S |
Zenon (napisa): |
Ne razumijem. U strukturi, label je tipa char, a kada predajem funkciji CHANGE_LABEL dajem joj na prvom mjestu char c. Funkcija LABEL samo čita oznaku čvora i. |
Kod: |
void CHANGE_LABEL(char l, node i, TREE* tree){
if(i >= max || tree->label == '\0') return; (tree + i)->label = l; } |
Kod: |
int MaCompute(Mapping *M, domain d, range r) { int l=0, m, n=M->last; domain dm; while (l<=n) { m=(l+n)/2; dm= M->pairs[m].domainelement; if (dm==d) {*r=M->pairs[m].rangeelement; return 1;} else if (dm<d) l=m+1; else n=m-1; } return 0; } |
Kod: |
#define Max 100 #define burek 20 typedef long range; typedef struct { char ime[burek]; }domain; typedef struct { domain domainelement; range rangeelement; }celltype; typedef struct { celltype pairs[Max]; int last; }Mapping; |
Kod: |
#define max 100
typedef int range; typedef struct{ int a; int b; } domain; typedef struct { range elements[max]; int last; } Mapping; |
Kod: |
#define max 100
typedef int range; typedef struct{ int a; int b; } domain; typedef struct { domain d; range r; } element; typedef struct{ element polje[max]; int last; } Mapping; |
output generated using printer-friendly topic mod. Vremenska zona: GMT + 01:00.