Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- typedef struct elev
- {
- char nume[20];
- float nota;
- struct elev *stang;
- struct elev *drept;
- }nod;
- nod *root = NULL;
- nod *adaugare(nod *t, float nota, char nume[])
- {
- if (t == NULL)
- if ((t = (nod*)malloc(sizeof(nod))) == NULL)
- {
- printf("EROARE de alocare!");
- }
- else
- {
- t->nota = nota;
- strcpy(t->nume, nume);
- t->stang = t->drept = NULL;
- }
- else
- if (strcmp(t->nume, nume)<0)
- t->drept = adaugare(t->drept, nota, nume);
- else
- if (strcmp(t->nume, nume)>0)
- t->stang = adaugare(t->stang, nota, nume);
- else
- printf("Nodul nu a fsot gasit");
- return t;
- }
- void citire()
- {
- FILE *f;
- char nume[20];
- float nota;
- if ((f = fopen("Elev.txt", "rt")) == NULL)
- printf("Eroare la deschiderea fisierului");
- else
- while (!feof(f))
- {
- fscanf(f, "%s %f", nume, ¬a);
- root = adaugare(root, nota, nume);
- }
- fclose(f);
- }
- void afisare(nod *t)
- {
- if (t != NULL)
- {
- afisare(t->stang);
- printf("\n %s %.2f", t->nume, t->nota);
- afisare(t->drept);
- }
- }
- nod *supred(nod *t, nod *p) {
- nod *q, *q_aux;
- q = t;
- if (q->drept != NULL)
- q->drept = supred(q->drept, p);
- else {
- strcpy(p->nume, q->nume);
- p->nota = q->nota;
- q_aux = q;
- q = q->stang;
- free(q_aux);
- }
- return q;
- }
- nod * elimina(nod *p, char v[]) {
- nod *q, *q1;
- char *s;
- if (p == NULL)
- printf("Eroare: %d nu apare in evidenta \n", v);
- else
- if (strcmp(p->nume, v)<0)
- p->drept = elimina(p->drept, v);
- else
- if (strcmp(p->nume, v)>0)
- p->stang = elimina(p->stang, v);
- else
- if (p->stang == NULL) {
- q = p->drept;
- free(p);
- return q;
- }
- else
- if (p->drept == NULL) {
- q = p->stang;
- free(p);
- return q;
- }
- else
- p->stang = supred(p->stang, p);
- return p;
- }
- int main()
- {
- int opt;
- char x[10];
- do {
- puts("1. Citire \n");
- puts("2. Afisare \n");
- puts("3. Stergere \n");
- puts("0. EXIT \n");
- puts("Introduceti optiunea: \n");
- scanf("%d", &opt);
- switch (tolower(opt))
- {
- case 1:
- citire();
- break;
- case 2:
- afisare(root);
- break;
- case 3:
- printf("introduceti numele care va fi sters \n");
- scanf("%s", x);
- root = elimina(root, x);
- break;
- case 0:
- exit(EXIT_FAILURE);
- }
- } while (opt);
- getch();
- return 0;
- }
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef enum{stanga,dreapta}mana;
- typedef struct nod {
- char nume[100];
- int varsta;
- char tara[100];
- int loc;
- int titluri;
- mana man;
- struct nod *st;
- struct nod *dr;
- }nod;
- nod *root = NULL;
- nod *adauga(nod *t, char *num, int varst, char *tar, int lc, mana mn, int titl)
- {
- if (t == NULL)
- {
- t = (nod*)malloc(sizeof(nod));
- strcpy(t->nume, num);
- t->varsta = varst;
- strcpy(t->tara, tar);
- t->loc = lc;
- t->man = mn;
- t->titluri = titl;
- t->st = t->dr = NULL;
- }
- else if (strcmp(num, t->nume) < 0)
- t->st = adauga(t->st, num, varst, tar, lc, titl,mn);
- else if (strcmp(num, t->nume) > 0)
- t->dr = adauga(t->dr, num, varst, tar, lc, titl,mn);
- else
- printf("Nodul exista.");
- return t;
- }
- void afisare(nod *t)
- {
- if (t != NULL)
- {
- afisare(t->st);
- printf("%s %d %s %d %d ", t->nume, t->varsta, t->tara, t->loc, t->titluri);
- if (t->man == 0)
- printf("stanga\n");
- else
- printf("dreapta\n");
- afisare(t->dr);
- }
- }
- void afisare_cond(nod *t)
- {
- if (t != NULL)
- {
- afisare_cond(t->st);
- if (t->varsta < 30 && t->man == 1)
- {
- printf("%s %d %s %d %d ", t->nume, t->varsta, t->tara, t->loc, t->titluri);
- if (t->man == 0)
- printf("stanga\n");
- else
- printf("dreapta\n");
- }
- afisare_cond(t->dr);
- }
- }
- nod *supred(nod *t, nod *p)
- {
- nod *q, *aux;
- q = t;
- if (q->dr != NULL)
- q->dr = supred(q->dr, p);
- else
- {
- strcpy(p->nume, q->nume);
- strcpy(p->tara, q->tara);
- p->varsta = q->varsta;
- p->loc = q->loc;
- p->titluri = q->titluri;
- p->man = q->man;
- aux = q;
- q = q->st;
- free(aux);
- }
- return q;
- }
- void citire()
- {
- FILE *f;
- char nume[100];
- int varsta;
- char tara[100];
- int loc;
- int titluri;
- mana man;
- f = fopen("tenis.txt", "rt");
- if (f == NULL)
- printf("Nu exista.\n");
- else
- {
- while (!feof(f))
- {
- fscanf(f, "%s %d %s %d %d %d", nume, &varsta, tara, &loc, &titluri, &man);
- root = adauga(root, nume, varsta, tara, loc,titluri, man);
- }
- }
- }
- nod *elimina(nod *t, char nume[100])
- {
- nod *aux;
- if (t == NULL)
- printf("Nodul nu exista!\n");
- else
- {
- if (strcmp(nume, t->nume) < 0)
- t->st = elimina(t->st, nume);
- else if (strcmp(nume, t->nume) > 0)
- t->dr = elimina(t->dr, nume);
- else
- {
- if (t->st == NULL)
- {
- aux = t;
- t = t->st;
- free(aux);
- }
- else if (t->dr == NULL)
- {
- aux = t;
- t = t->dr;
- free(aux);
- }
- else
- t->st = supred(t->st, t);
- }
- }
- return t;
- }
- int x;
- nod *stergere(nod *t, int loc)
- {
- nod *aux;
- if (t == NULL)
- x++;
- else if (t->loc < 19)
- {
- t->st = stergere(t->st, loc);
- t->dr = stergere(t->dr, loc);
- }
- else
- if (t->st == NULL)
- {
- aux = t;
- t = t->dr;
- free(aux);
- }
- else if (t->dr == NULL)
- {
- aux = t;
- t = t->st;
- free(aux);
- }
- else
- t->st = supred(t->st, t);
- return t;
- }
- int main()
- {
- citire();
- afisare(root);
- printf("\n");
- _getch();
- afisare_cond(root);
- printf("\n");
- _getch();
- while (x < 50)
- root = stergere(root, 49);
- printf("\n");
- afisare(root);
- system("pause");
- return 0;
- }
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef enum { GDDR3, DDR3, GDDR5, HBM2 }memorie;
- typedef struct nod {
- char nume_placa[50];
- memorie mem;
- int dim_mem;
- float pret;
- struct nod *st, *dr;
- }nod;
- nod *root = NULL;
- nod *adaugare(nod *t, char nume[50], memorie m, int dim_mem, float pret) {
- if (t == NULL) {
- t = (nod*)malloc(sizeof(nod));
- strcpy(t->nume_placa, nume);
- t->mem = m;
- t->dim_mem = dim_mem;
- t->pret = pret;
- t->st = NULL;
- t->dr = NULL;
- }
- else
- if (strcmp(t->nume_placa, nume) > 0)
- t->st = adaugare(t->st, nume, m, dim_mem, pret);
- else
- if (strcmp(t->nume_placa, nume) < 0)
- t->dr = adaugare(t->dr, nume, m, dim_mem, pret);
- else
- printf("Exista in arbore.\n");
- return t;
- }
- void citire_fisier() {
- char nume[50];
- char mem[50];
- memorie t_memorie;
- int dim;
- float pret;
- FILE *f;
- fopen_s(&f, "Graphic_Cards.txt", "rt");
- while (!feof(f)) {
- fscanf(f, "%s", nume);
- fscanf(f, "%s", mem);
- if (strcmp(mem, "GDDR3") == 0)
- t_memorie = GDDR3;
- else
- if (strcmp(mem, "DDR3") == 0)
- t_memorie = DDR3;
- else
- if (strcmp(mem, "GDDR5") == 0)
- t_memorie = GDDR5;
- else
- if (strcmp(mem, "HBM2") == 0)
- t_memorie = HBM2;
- fscanf(f, "%d", &dim);
- fscanf(f, "%f", &pret);
- root = adaugare(root, nume, t_memorie, dim, pret);
- }
- }
- nod *supred(nod *t, nod *p)
- {
- nod *q, *aux;
- q = t;
- if (q->dr != NULL)
- {
- q->dr = supred(q->dr, p);
- }
- else
- {
- strcpy(p->nume_placa, q->nume_placa);
- p->mem = q->mem;
- p->dim_mem = q->dim_mem;
- p->pret = q->pret;
- aux = q;
- q = q->st;
- free(aux);
- }
- return q;
- }
- int chestie;
- nod* stergere_ss(nod *t, char *prenume)
- {
- nod *aux;
- if (t == NULL)
- {
- chestie++;
- }
- else if (strcmp(t->nume_placa, prenume) != 0)
- {
- t->st = stergere_ss(t->st, prenume);
- t->dr = stergere_ss(t->dr, prenume);
- }
- else
- if (t->st == NULL)
- {
- aux = t;
- t = t->dr;
- free(aux);
- }
- else if (t->dr == NULL)
- {
- aux = t;
- t = t->st;
- free(aux);
- }
- else
- {
- t->st = supred(t->st, t);
- }
- return t;
- }
- //int chestie;
- nod *elimina2(nod *p, int v)
- {
- nod *q;
- if (p == NULL)
- return NULL;
- p->st = elimina2(p->st, v);
- p->dr = elimina2(p->dr, v);
- if (p->st == NULL && (p->dim_mem < v))
- {
- q = p->dr;
- free(p);
- return q;
- }
- else
- if (p->dr == NULL && (p->dim_mem < v))
- {
- q = p->st;
- free(p);
- return q;
- }
- else
- if (p->dim_mem < v)
- p->st = supred(p->st, p);
- return p;
- }
- void afisare(nod *t) {
- if (t != NULL) {
- printf("\n");
- afisare(t->st);
- printf("Numele placii: %s \n Tipul memoriei : %d \n Dimensiunea memoriei: %d \n Pretul: %f \n\n", t->nume_placa, t->mem, t->dim_mem, t->pret);
- afisare(t->dr);
- }
- }
- void afisare_criteriu(nod *t)
- {
- if (t != NULL)
- {
- printf("\n");
- afisare_criteriu(t->st);
- if (t->mem == 0 && t->pret < 300)
- printf("Numele placii: %s\nTipul memoriei: %d\nDimensiunea memoriei: %d\nPretul: %f\n\n", t->nume_placa, t->mem, t->dim_mem, t->pret);
- afisare_criteriu(t->dr);
- }
- }
- int main()
- {
- int dimensiune;
- int opt;
- do {
- printf("1.Citire in arbore.\n");
- printf("2.Afisarea arborelui.\n");
- printf("3.Afisare criteriu.\n");
- printf("4.Stergere \n");
- printf("5.Stergere cond \n");
- printf("0.Exit.\n");
- printf("Alegeti: ");
- scanf("%d", &opt);
- switch (opt)
- {
- case 1:
- citire_fisier();
- break;
- case 2:
- afisare(root);
- break;
- case 3:
- afisare_criteriu(root);
- break;
- case 4: {
- root = stergere_ss(root, "GIGABYTE_GeForce_GT_730");
- root = stergere_ss(root, "GIGABYTE_GeForce_GTX_1070_Ti");
- afisare(root);
- break;
- }
- case 5: {
- printf("dati dimensiunea : \n");
- scanf("%d", &dimensiune);
- root = elimina2(root, dimensiune);
- afisare(root);
- break;
- }
- }
- } while (opt != 0);
- getch();
- return 0;
- }
- #include<conio.h>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef struct arbore {
- char nume[100];
- char profesie[100];
- float venit;
- struct arbore *stang, *drept;
- }nod;
- nod *adaugare(nod *t, char *nume, char *profesie, float venit)
- {
- if (t == NULL)
- {
- t = (nod*)malloc(sizeof(nod));
- strcpy(t->nume, nume);
- strcpy(t->profesie, profesie);
- t->venit = venit;
- t->stang = t->drept = NULL;
- }
- else
- if (strcmp(t->nume, nume) < 0)
- t->stang = adaugare(t->stang, nume, profesie, venit);
- else
- if (strcmp(t->nume, nume) > 0)
- t->drept = adaugare(t->drept, nume, profesie, venit);
- else
- printf("Valoarea deja exista in evidenta!\n");
- return t;
- }
- nod *citirefisier(nod *r)
- {
- FILE *f;
- char nume[100];
- char profesie[100];
- float venit;
- f = fopen("Text.txt", "rt");
- if (f == NULL)
- {
- printf("eroare la deschidere");
- _getch();
- return r;
- }
- while (!feof(f))
- {
- fscanf(f, "%s %s %f", nume, profesie, &venit);
- r = adaugare(r, nume, profesie, venit);
- }
- fclose(f);
- return r;
- }
- nod *supred(nod *t, nod *p)
- {
- nod *q, *q_aux;
- q = t;
- if (q->drept != NULL)
- q->drept = supred(q->drept, p);
- else
- {
- strcpy(p->nume, q->nume);
- strcpy(p->profesie, q->profesie);
- p->venit = q->venit;
- q_aux = q;
- q = q->stang;
- free(q_aux);
- }
- return q;
- }
- nod *elimina(nod *p, char *nume)
- {
- nod *q;
- if (p == NULL)
- printf("Nu apare in evidenta!");
- else
- if (strcmp(p->nume, nume) > 0)
- p->drept = elimina(p->drept, nume);
- else
- if (strcmp(p->nume, nume) < 0)
- p->stang = elimina(p->stang, nume);
- else
- if (p->stang == NULL)
- {
- q = p->drept;
- free(p);
- return q;
- }
- else
- if (p->drept == NULL)
- {
- q = p->stang;
- free(p);
- return q;
- }
- else
- p->stang = supred(p->stang, p);
- return p;
- }
- nod *elimina2(nod *p, int v)
- {
- nod *q;
- if (p == NULL)
- return NULL;
- p->stang = elimina2(p->stang, v);
- p->drept = elimina2(p->drept, v);
- if (p->stang == NULL&&(p->venit < v))
- {
- q = p->drept;
- free(p);
- return q;
- }
- else
- if (p->drept == NULL && (p->venit < v))
- {
- q = p->stang;
- free(p);
- return q;
- }
- else
- if (p->venit < v)
- p->stang = supred(p->stang, p);
- return p;
- }
- void afisare(nod *p)
- {
- if (p != NULL)
- {
- afisare(p->stang);
- printf("\n %s %s %f", p->nume, p->profesie, p->venit);
- afisare(p->drept);
- }
- }
- void afisare_salariu30(nod *p)
- {
- if (p != NULL)
- {
- afisare_salariu30(p->stang);
- if (p->venit>30)
- printf("\n %s %s %f", p->nume, p->profesie, p->venit);
- afisare_salariu30(p->drept);
- }
- }
- void main()
- {
- int opt;
- nod *radacina;
- radacina = NULL;
- char nume[100];
- int v;
- float venit;
- do {
- printf("\n1.citire din fisier\n");
- printf("2.afisare\n");
- printf("3.Stergere dupa nume\n");
- printf("4.afisare salariu mai mic decat 30\n");
- printf("5.elimina toti oamenii cu un salariu mai mic decat:");
- printf("da-ti optiunea:\n");
- scanf("%d", &opt);
- switch (opt)
- {
- case 1:
- radacina = citirefisier(radacina);
- break;
- case 2:
- afisare(radacina);
- break;
- case 3:
- printf("Dati numele care urmeaza sa fie sters:");
- scanf("%s", nume);
- radacina = elimina(radacina, nume);
- afisare(radacina);
- break;
- case 4:
- afisare_salariu30(radacina);
- break;
- case 5:
- printf("Dati valoarea salariului:");
- scanf("%d", &v);
- radacina=elimina2(radacina, v);
- afisare(radacina);
- break;
- }
- } while (opt != 0);
- _getch();
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- arbori binari ordonati test
- Fisierul stud.txt contine nume_student , disciplina si nota
- a) incarcati informatiile din fisier intr-un arbore binar cu subliste(in arbore se incarca disciplina in subliste nume student si note) ordonat in fuctie de nota
- b) afisati arborele
- c) stergeti un student din evidenta ( se citeste de la tastatura)
- */
- typedef struct sublista
- {
- char nume_student[50];
- float nota;
- struct sublista *urm;
- }sublista;
- typedef struct nod
- {
- char disciplina[50];
- struct nod *st;
- struct nod *dr;
- sublista *prim;
- }nod;
- nod *root = NULL;
- nod *supred(nod *t, nod *p);
- sublista *adauga_nod_lista(sublista *l, char *student,float nota)
- {
- sublista *q1, *q2, *aux;
- aux = (sublista*)malloc(sizeof(sublista));
- strcpy(aux->nume_student,student);
- aux->nota = nota;
- for (q1 = q2 = l; q1 != NULL && aux->nota < q1->nota; q2 = q1, q1 = q1->urm);
- if (q1 == q2)
- {
- aux->urm = l;
- l = aux;
- }
- else
- {
- q2->urm = aux;
- aux->urm = q1;
- }
- return l;
- }
- sublista *stergere_lista(sublista *l, char *x)
- {
- sublista *q1, *q2;
- for (q1 = q2 = l; q1 != NULL; q2 = q1, q1 = q1->urm)
- if (q1 != NULL && strcmp(q1->nume_student, x) == 0) {
- if (q1 != q2)
- q2->urm = q1->urm;
- else
- l = l->urm;
- free(q1);
- return l;
- }
- printf("Eroare:identificatorul %s nu apare in lista\n", x);
- return l;
- }
- void stergere_student(nod *t,char *x)//inordine
- {
- if (t != NULL)
- {
- stergere_student(t->st,x);
- //printf("\n %s ", t->disciplina);
- sublista *ap;
- ap = t->prim;
- if (ap)
- {
- while (ap)
- {
- if (strcmp(ap->nume_student, x)==0)
- {
- t->prim = stergere_lista(t->prim, x);
- ap = t->prim;
- }
- if(ap!=NULL)
- ap = ap->urm;
- }
- }
- else
- {
- printf("Lista este goala");
- }
- stergere_student(t->dr,x);
- }
- }
- nod *adauga_arbore(nod *t, char *disciplina,char *student,float nota)
- {
- if (t == NULL)
- {
- t = (nod*)malloc(sizeof(nod));
- strcpy(t->disciplina, disciplina);
- t->st = t->dr = NULL;
- t->prim = NULL;
- t->prim = adauga_nod_lista(t->prim, student, nota);
- }
- else if (strcmp(disciplina,t->disciplina)<0)
- {
- t->st = adauga_arbore(t->st, disciplina,student,nota);
- }
- else if (strcmp(disciplina, t->disciplina)>0)
- {
- t->dr = adauga_arbore(t->dr, disciplina,student,nota);
- }
- else
- {
- //printf("Nodul exista");
- t->prim = adauga_nod_lista(t->prim, student, nota);
- }
- return t;
- }
- void afisare(nod *t)//inordine
- {
- if (t != NULL)
- {
- afisare(t->st);
- printf("\n %s ", t->disciplina);
- sublista *ap;
- ap = t->prim;
- if (ap)
- {
- while (ap)
- {
- printf(" %s %f", ap->nume_student, ap->nota);
- ap= ap->urm;
- }
- }
- afisare(t->dr);
- }
- }
- nod* stergere(nod *t, char *disciplina)
- {
- nod *aux;
- if (t == NULL)
- {
- printf("Nodul nu exista");
- }
- else if (strcmp(disciplina, t->disciplina)<0)
- {
- t->st = stergere(t->st, disciplina);
- }
- else if (strcmp(disciplina, t->disciplina)>0)
- {
- t->dr = stergere(t->dr, disciplina);
- }
- else
- if (t->st == NULL)
- {
- aux = t;
- t = t->dr;
- free(aux);
- }
- else if (t->dr == NULL)
- {
- aux = t;
- t = t->st;
- free(aux);
- }
- else
- {
- t->st = supred(t->st, t);
- }
- return t;
- }
- nod *supred(nod *t, nod *p)
- {
- nod *q, *aux;
- q = t;
- if (q->dr != NULL)
- {
- q->dr = supred(q->dr, p);
- }
- else
- {
- //p->nr = q->nr;
- strcpy(p->disciplina, q->disciplina);
- aux = q;
- q = q->st;
- free(aux);
- }
- return q;
- }
- int main()
- {
- char disciplina[50],student[50];
- float nota;
- int i;
- FILE *f;
- f=fopen("stud.txt", "rt");
- if (f)
- {
- for (i = 0; i < 10; i++)
- {
- fscanf(f,"%s %s %f.1", disciplina, student, ¬a);
- printf("%s %s %f.1\n", disciplina, student, nota);
- root = adauga_arbore(root, disciplina, student, nota);
- }
- afisare(root);
- printf("\n \n");
- //root = stergere(root,"Matematica");
- stergere_student(root,"Alin_Gale");
- afisare(root);
- }else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement