Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. typedef struct nod_sublista
  7. {
  8. char *nume_companie;
  9. char *domeniu;
  10. float salar;
  11. struct nod_sublista *urm;
  12. }nod_s;
  13.  
  14. typedef struct nod_arbore
  15. {
  16. char *oras;
  17. nod_s *cap_sublista;
  18. struct nod_arbore *st, *dr;
  19. }nod_a;
  20.  
  21. nod_a *radacina = NULL;
  22. void CitireFisier();
  23. nod_a *adauga_arbore(nod_a *,char *,char *,char *, float);
  24. nod_s *adauga_sublista(nod_s *, char *, char *, float);
  25. void afisare_arbore(nod_a*);
  26. void afisare_sublista(nod_s*);
  27.  
  28. void CitireFisier()
  29. {
  30. FILE *f;
  31. f = fopen("Text.txt", "rt");
  32. char oras[20], nume_companie[20], domeniu[20];
  33. float salar;
  34. while (fscanf(f, "%s %s %s %f", oras, nume_companie, domeniu, &salar) != EOF)
  35. radacina = adauga_arbore(radacina, oras, nume_companie, domeniu, salar);
  36. getch();
  37. fclose(f);
  38. }
  39.  
  40. nod_a * adauga_arbore(nod_a *p, char *o, char *nume, char *dom, float s)
  41. {
  42. if (p == NULL)
  43. {
  44. p = (nod_a*)malloc(sizeof(nod_a));
  45. p->oras = (char*)malloc(strlen(o) + 1);
  46. strcpy(p->oras, o);
  47. p->st = p->dr = NULL;
  48. p->cap_sublista = NULL;
  49. p->cap_sublista = adauga_sublista(p->cap_sublista, nume, dom, s);
  50. }
  51. else
  52. if (strcmp(o, p->oras) < 0)
  53. p->st = adauga_arbore(p->st, o, nume, dom, s);
  54. else
  55. if (strcmp(o, p->oras) > 0)
  56. p->dr = adauga_arbore(p->dr, o, nume, dom, s);
  57. else
  58. p->cap_sublista = adauga_sublista(p->cap_sublista,nume,dom,s);
  59. return p;
  60. }
  61.  
  62. nod_s * adauga_sublista(nod_s *p, char *n, char *dom, float s)
  63. {
  64. nod_s *nou, *p1, *p2;
  65. nou = (nod_s*)malloc(sizeof(nod_s));
  66. nou->nume_companie = (char*)malloc(strlen(n)+1);
  67. nou->domeniu = (char*)malloc(strlen(dom) + 1);
  68. strcpy(nou->domeniu, dom);
  69. strcpy(nou->nume_companie, n);
  70. nou->salar = s;
  71.  
  72. for (p1 = p2 = p; p1 != NULL&&strcmp(p1->nume_companie, n) < 0; p2 = p1, p1 = p1->urm);
  73. nou->urm = p1;
  74.  
  75. if (p1 == p2)
  76. return nou;
  77. else {
  78. p2->urm = nou;
  79. return p;
  80. }
  81. }
  82.  
  83. void afisare_arbore(nod_a *p)
  84. {
  85. if (p != NULL)
  86. {
  87. afisare_arbore(p->st);
  88. printf("\n%s",p->oras);
  89. afisare_sublista(p->cap_sublista);
  90. afisare_arbore(p->dr);
  91. }
  92. }
  93.  
  94. void afisare_sublista(nod_s *p)
  95. {
  96. nod_s *q;
  97. for(q=p;q!=NULL;q=q->urm)
  98. printf("\n\t%s %s %.2f", q->nume_companie, q->domeniu, q->salar);
  99. }
  100.  
  101. int main()
  102. {
  103. int optiune;
  104.  
  105. do
  106. {
  107. printf("\n\n\tMeniu\n");
  108. printf("0. Iesire\n");
  109. printf("1. Adaugare date din fisier\n");
  110. printf("2. Afsiare date in ordinea alfabetica\n");
  111. printf("Optiunea dv: ");
  112. scanf("%d", &optiune);
  113. switch (optiune)
  114. {
  115. case 1:
  116. CitireFisier();
  117. break;
  118. case 2:
  119. if (radacina == NULL)
  120. printf("\nArbore vid!\n");
  121. else
  122. afisare_arbore(radacina);
  123. break;
  124. }
  125. } while (optiune != 0);
  126.  
  127. system("pause");
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement