Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct nod_sublista
- {
- char *nume_companie;
- char *domeniu;
- float salar;
- struct nod_sublista *urm;
- }nod_s;
- typedef struct nod_arbore
- {
- char *oras;
- nod_s *cap_sublista;
- struct nod_arbore *st, *dr;
- }nod_a;
- nod_a *radacina = NULL;
- void CitireFisier();
- nod_a *adauga_arbore(nod_a *,char *,char *,char *, float);
- nod_s *adauga_sublista(nod_s *, char *, char *, float);
- void afisare_arbore(nod_a*);
- void afisare_sublista(nod_s*);
- void CitireFisier()
- {
- FILE *f;
- f = fopen("Text.txt", "rt");
- char oras[20], nume_companie[20], domeniu[20];
- float salar;
- while (fscanf(f, "%s %s %s %f", oras, nume_companie, domeniu, &salar) != EOF)
- radacina = adauga_arbore(radacina, oras, nume_companie, domeniu, salar);
- getch();
- fclose(f);
- }
- nod_a * adauga_arbore(nod_a *p, char *o, char *nume, char *dom, float s)
- {
- if (p == NULL)
- {
- p = (nod_a*)malloc(sizeof(nod_a));
- p->oras = (char*)malloc(strlen(o) + 1);
- strcpy(p->oras, o);
- p->st = p->dr = NULL;
- p->cap_sublista = NULL;
- p->cap_sublista = adauga_sublista(p->cap_sublista, nume, dom, s);
- }
- else
- if (strcmp(o, p->oras) < 0)
- p->st = adauga_arbore(p->st, o, nume, dom, s);
- else
- if (strcmp(o, p->oras) > 0)
- p->dr = adauga_arbore(p->dr, o, nume, dom, s);
- else
- p->cap_sublista = adauga_sublista(p->cap_sublista,nume,dom,s);
- return p;
- }
- nod_s * adauga_sublista(nod_s *p, char *n, char *dom, float s)
- {
- nod_s *nou, *p1, *p2;
- nou = (nod_s*)malloc(sizeof(nod_s));
- nou->nume_companie = (char*)malloc(strlen(n)+1);
- nou->domeniu = (char*)malloc(strlen(dom) + 1);
- strcpy(nou->domeniu, dom);
- strcpy(nou->nume_companie, n);
- nou->salar = s;
- for (p1 = p2 = p; p1 != NULL&&strcmp(p1->nume_companie, n) < 0; p2 = p1, p1 = p1->urm);
- nou->urm = p1;
- if (p1 == p2)
- return nou;
- else {
- p2->urm = nou;
- return p;
- }
- }
- void afisare_arbore(nod_a *p)
- {
- if (p != NULL)
- {
- afisare_arbore(p->st);
- printf("\n%s",p->oras);
- afisare_sublista(p->cap_sublista);
- afisare_arbore(p->dr);
- }
- }
- void afisare_sublista(nod_s *p)
- {
- nod_s *q;
- for(q=p;q!=NULL;q=q->urm)
- printf("\n\t%s %s %.2f", q->nume_companie, q->domeniu, q->salar);
- }
- int main()
- {
- int optiune;
- do
- {
- printf("\n\n\tMeniu\n");
- printf("0. Iesire\n");
- printf("1. Adaugare date din fisier\n");
- printf("2. Afsiare date in ordinea alfabetica\n");
- printf("Optiunea dv: ");
- scanf("%d", &optiune);
- switch (optiune)
- {
- case 1:
- CitireFisier();
- break;
- case 2:
- if (radacina == NULL)
- printf("\nArbore vid!\n");
- else
- afisare_arbore(radacina);
- break;
- }
- } while (optiune != 0);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement