Mrain

strukture

Jan 17th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<conio.h>
  5.  
  6. struct direktorij;
  7. typedef struct direktorij dir;
  8. typedef struct direktorij *Node;
  9.  
  10. struct SimStack;
  11. typedef struct SimStack *Stack;
  12.  
  13. struct direktorij
  14. {
  15.     char ime[10];
  16.     Node Next;
  17.     Node Child;
  18. };
  19.  
  20. struct SimStack
  21. {
  22.     Node Element;
  23.     Stack Next;
  24. };
  25.  
  26. void IspisDir(Node, char *);
  27. Node IzadjiDir(Node, char *, Stack);
  28. Node UdjiDir(Node, char *, char *, Stack);
  29. void StvoriDir(Node, char *);
  30. void Push(Stack, Node);
  31. Node Pop(Stack);
  32.  
  33.  
  34. void main()
  35. {
  36.     char instr[20], ime[10], naredba[10], path[100];
  37.     char *i, *j;
  38.     dir root;
  39.     struct SimStack S;
  40.  
  41.     Node UpDir = &root;
  42.     S.Next = NULL;
  43.  
  44.     printf("Unesi ime root direktorija: ");
  45.     scanf(" %s", root.ime);
  46.     root.Child = NULL;
  47.     root.Next = NULL;
  48.    
  49.     path[0] = 0;
  50.     strcat(path, root.ime);
  51.     strcat(path, ":\\");
  52.  
  53.     system("cls");
  54.    
  55.     printf("Unesi naredbu:");
  56.     printf("\n\tdir");
  57.     printf("\n\tcd..");
  58.     printf("\n\tcd ime_dir");
  59.     printf("\n\tmk ime_dir");
  60.     printf("\n\n\texit - za kraj");
  61.     printf("\n\n\t");
  62.  
  63.     fgets(instr, 20,stdin);
  64.    
  65.     while(strcmp(naredba, "exit")!=0)
  66.     {
  67.         printf("\n\n\t%s",path);
  68.        
  69.         strset(ime, 0);
  70.         strset(naredba, 0);
  71.  
  72.         fgets(instr, 20,stdin);
  73.  
  74.         i = strchr(instr, ' ');
  75.        
  76.         if (NULL == i)
  77.         {
  78.             strncpy(naredba, instr, sizeof(instr));
  79.             j = strchr(naredba, 10);
  80.             *j = 0;
  81.         }
  82.         else
  83.         {
  84.             strncpy(naredba, instr, i - instr);
  85.             naredba[i-instr] = 0;
  86.             strcpy(ime, i+1);
  87.         }
  88.  
  89.         if(strcmp(naredba, "dir")==0)
  90.             IspisDir(UpDir, path);
  91.         else if(strcmp(naredba, "cd..")==0)
  92.             UpDir = IzadjiDir(UpDir, path, &S);
  93.         else if(strcmp(naredba, "cd")==0)
  94.             if(ime[0] == 0)
  95.                 printf("Greska u sintaksi naredbe!\n Treba biti: cd ime_dir");
  96.             else
  97.                 UpDir = UdjiDir(UpDir, ime, path, &S);
  98.         else if(strcmp(naredba, "mk")==0)
  99.             if(ime[0] == 0)
  100.                 printf("Greska u sintaksi naredbe!\n Treba biti: mk ime_dir");
  101.             else
  102.                 StvoriDir(UpDir, ime);
  103.         else if(strcmp(naredba, "exit")!=0)
  104.             printf("\nPogresan unos!!\n");
  105.  
  106.     }
  107.  
  108. }
  109.  
  110.  
  111. void IspisDir(Node N, char * path)
  112. {
  113.     int i = 0;
  114.     printf("\nDirectory of %s", path);
  115.    
  116.     N = N->Child;
  117.     while(N != NULL)
  118.     {
  119.         printf("\n\t\t %s", N->ime);
  120.         N = N->Next;
  121.         i++;
  122.     }
  123.  
  124.     printf("\n\t\t %d Dir(s)", i);
  125.  
  126.  
  127. }
  128.  
  129.  
  130. Node IzadjiDir(Node N, char *path, Stack S)
  131. {
  132.     Node temp;
  133.     char *a;
  134.  
  135.     temp = Pop(S);
  136.     if(NULL == temp)
  137.     {
  138.         printf("Nalazimo se u root direktoriju!\nIz njega se ne moze izaci!");
  139.         return N;
  140.     }
  141.     else
  142.     {
  143.         a = strrchr(path, '\\');
  144.         *a = 0;
  145.  
  146.         a = strrchr(path, '\\');
  147.         *(a+1) = 0;
  148.    
  149.         return temp;
  150.     }
  151. }
  152.  
  153.  
  154. Node UdjiDir(Node N, char *ime, char *path, Stack S)
  155. {
  156.     Node temp = N->Child;
  157.     char *a;
  158.  
  159.  
  160.     while(temp != NULL && strcmp(temp->ime, ime)!= 0)
  161.         temp = temp->Next;
  162.     if (NULL == temp)
  163.         printf("\n Ne postoji pod-direktorij s tim imenom!!!");
  164.     else
  165.     {
  166.         Push(S, N);
  167.         strcat(path, temp->ime);
  168.         a = strchr(path, 10);
  169.         *a = '\\';
  170.         return temp;
  171.     }
  172.  
  173.     return N;
  174. }
  175.  
  176.  
  177. void StvoriDir(Node N, char *ime)
  178. {
  179.     Node q;
  180.  
  181.     q = (Node)malloc(sizeof(dir));
  182.     q->Child = NULL;
  183.     q->Next = NULL;
  184.  
  185.     strcpy(q->ime, ime);
  186.  
  187.     if(NULL == q)
  188.         printf("\nGreska u alokaciji memorije!!");
  189.     else
  190.     {
  191.         if ( NULL == N->Child)
  192.             N->Child = q;
  193.         else
  194.         {
  195.             q->Next = N->Child;
  196.             N->Child = q;
  197.         }
  198.     }
  199. }
  200.  
  201. void Push(Stack S, Node T)
  202. {
  203.     Stack q;
  204.  
  205.     q = (Stack)malloc(sizeof(struct SimStack));
  206.  
  207.     if( q==NULL)
  208.         printf("\nGreska kod alokacije memorije!!");
  209.     else
  210.     {
  211.         q->Element = T;
  212.         q->Next = S->Next;
  213.  
  214.         S->Next = q;
  215.     }
  216. }
  217.  
  218.  
  219. Node Pop(Stack S)
  220. {
  221.     Node q;
  222.     Stack temp;
  223.  
  224.     if(NULL == S->Next)
  225.         return NULL;
  226.     else
  227.     {
  228.         q = S->Next->Element;
  229.         temp = S->Next;
  230.         S->Next = temp->Next;
  231.  
  232.         free(temp);
  233.         return q;
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment