Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #include <conio.h>
- //ex 1 + desen pe foaie
- struct Proces
- {
- int pid; //unic in multimea proceselor
- char* denumire;
- char* descriere;
- int mem_ocupata_KB;
- char* user;
- };
- Proces initProces(int pid, const char* denumire, const char* descriere, int mem_ocupata, const char* user) {
- Proces p;
- p.pid = pid;
- p.denumire = (char*)malloc(sizeof(char)*strlen(denumire) + 1);
- strcpy(p.denumire, denumire);
- p.descriere = (char*)malloc(sizeof(char)*strlen(descriere) + 1);
- strcpy(p.descriere, descriere);
- p.mem_ocupata_KB = mem_ocupata;
- p.user = (char*)malloc(sizeof(char)*strlen(user) + 1);
- strcpy(p.user, user);
- return p;
- }
- void afisareProces(Proces p) {
- printf("pId-ul procesului %d cu denumirea %s si descrierea %s ocupa o memorie de %d si user-ul care-l gestioneaza este: %s \n ",
- p.pid, p.denumire, p.descriere, p.mem_ocupata_KB, p.user);
- }
- //ex2 bagarea in arbore si apoi afisarea in Inordine care-i jos
- struct NOD
- {
- Proces info;
- NOD * st;
- NOD *dr;
- };
- NOD* inserareInArboreABC(NOD*rad, Proces p) {
- if (rad) {
- if (rad->info.pid > p.pid) {
- rad->st = inserareInArboreABC(rad->st, p);
- }
- else
- {
- rad->dr = inserareInArboreABC(rad->dr, p);
- }
- return rad;
- }
- else {
- NOD* nou = (NOD*)malloc(sizeof(NOD));
- nou->info = p;
- nou->dr = nou->st = NULL;
- return nou;
- }
- }
- //ex3.
- void nrDescendenti(NOD* r, int id, int &nrd) {
- if (r)
- {
- if (r->info.pid == id)
- {
- if (r->st && r->dr)
- {
- nrd = 2;
- }
- else
- {
- if (r->st)
- {
- nrd = 1;
- }
- else
- {
- if (r->dr)
- {
- nrd = 1;
- }
- else
- {
- nrd = 0;
- }
- }
- }
- }
- nrDescendenti(r->st, id, nrd);
- nrDescendenti(r->dr, id, nrd);
- }
- }
- //ex4.
- int memorieOcupata(NOD* root)
- {
- if (root)
- {
- return
- sizeof(root->info.pid) +
- sizeof(root->info.mem_ocupata_KB) +
- sizeof(char)*strlen(root->info.denumire) +
- sizeof(char)*strlen(root->info.descriere) +
- sizeof(char)*strlen(root->info.user) +
- 3 * sizeof(char*) +
- memorieOcupata(root->st) +
- memorieOcupata(root->dr);
- }
- else
- return 0;
- }
- //ex5
- void majorareMemorie(NOD* root, double procent)
- {
- if (root)
- {
- root->info.mem_ocupata_KB += procent * root->info.mem_ocupata_KB;
- majorareMemorie(root->st, procent);
- majorareMemorie(root->dr, procent);
- }
- }
- //ex6
- void inserareinVector(NOD* root, int nivelCautat, int nivelCurent, int** rezultat, int* contor)
- {
- if (root)
- {
- if (nivelCautat == nivelCurent)
- {
- (*rezultat)[(*contor)++] = root->info.pid;
- }
- else
- {
- nivelCurent++;
- inserareinVector(root->st, nivelCautat, nivelCurent, rezultat, contor);
- inserareinVector(root->dr, nivelCautat, nivelCurent, rezultat, contor);
- }
- }
- }
- void nrNoduriPeNivel(NOD* root, int nivelCautat, int nivelCurent, int* nrNoduri)
- {
- if (root)
- {
- if (nivelCautat == nivelCurent)
- {
- (*nrNoduri)++;
- }
- else
- {
- nivelCurent++;
- nrNoduriPeNivel(root->st, nivelCautat, nivelCurent, nrNoduri);
- nrNoduriPeNivel(root->dr, nivelCautat, nivelCurent, nrNoduri);
- }
- }
- }
- int* vectorId(NOD* root, int nivelCautat, int nivelCurent, int* nrNoduri, int* contor)
- {
- nrNoduriPeNivel(root, nivelCautat, nivelCurent, nrNoduri);
- int* rezultat = NULL;
- if ((*nrNoduri) > 0)
- {
- rezultat = (int*)malloc(sizeof(int)*(*nrNoduri));
- inserareinVector(root, nivelCautat, nivelCurent, &rezultat, contor);
- }
- return rezultat;
- }
- //ex7.
- NOD* cautareMaxim(NOD* root)
- {
- if (root)
- {
- NOD* temp = root;
- while (temp->dr)
- temp = temp->dr;
- return temp;
- }
- else return NULL;
- }
- // desi se modifica, radacina va fi returnata
- NOD* stergereNod(NOD* root, int idCautat)
- {
- if (root)
- {
- if (root->info.pid == idCautat)
- {
- if (root->st == NULL)
- {
- NOD* temp = root->dr;
- free(root->info.denumire);
- free(root->info.descriere);
- free(root->info.user);
- root = temp;
- }
- else
- if (root->dr == NULL)
- {
- NOD* temp = root->st;
- free(root->info.denumire);
- free(root->info.descriere);
- free(root->info.user);
- root = temp;
- }
- else
- {
- NOD* maxim = cautareMaxim(root->st);
- Proces aux = root->info;
- root->info = maxim->info;
- maxim->info = aux;
- root->st = stergereNod(root->st, idCautat);
- }
- }
- else
- if (idCautat < root->info.pid)
- root->st = stergereNod(root->st, idCautat);
- else if (idCautat > root->info.pid)
- root->dr = stergereNod(root->dr, idCautat);
- }
- return root;
- }
- //ex2
- void afisareABCinOrdine(NOD* rad) {
- if (rad) {
- afisareABCinOrdine(rad->st);
- afisareProces(rad->info);
- afisareABCinOrdine(rad->dr);
- }
- }
- void main() {
- /*Proces p1 = initProces(30, "Chrome", "motor de cautare", 1337, "Andrei");
- afisareProces(p1);*/
- //ex1 +2 *************************************************
- // vector de task-uri pt ca e mai usor de bagat in arbore
- int nrp = 5; // numar de procese
- Proces * vProcese = (Proces*)malloc(sizeof(Proces) *nrp);
- vProcese[0] = initProces(3, "chrome.exe", "Google Chrome", 132696, "gigel");
- vProcese[1] = initProces(1, "explorer.exe", "Windows Explorer", 64765, "gigel");
- vProcese[2] = initProces(5, "devenv.exe *32", "Microsoft Visual Studio 2012", 141028, "gigel");
- vProcese[3] = initProces(4, "chrome.exe", "Google Chrome", 34760, "gigel");
- vProcese[4] = initProces(0, "chrome.exe", "Google Chrome", 2836, "gigel");
- NOD *rad = NULL;
- for (int i = 0; i < nrp; i++) {
- rad = inserareInArboreABC(rad, vProcese[i]);
- }
- afisareABCinOrdine(rad);
- //EX3*********************************************
- int numar_descendenti = 0;
- nrDescendenti(rad, 3, numar_descendenti);
- printf("\n Nr descendenti %d \n", numar_descendenti);
- //EX4*********************************************
- printf("\n Memorie ocupata: %d \n", memorieOcupata(rad));
- //EX5*********************************************
- majorareMemorie(rad, 0.15);
- printf("\n\n");
- afisareABCinOrdine(rad);
- //ex6*********************************************************
- printf("\n\n");
- int contor = 0;
- int nrNoduri = 0;
- int* rezultat = vectorId(rad, 1, 0, &nrNoduri, &contor);
- for (int i = 0; i < nrNoduri; i++)
- {
- printf(" %d ", rezultat[i]);
- }
- //EX7******************************************************
- printf("\n\nArbore dupa stergere: \n");
- stergereNod(rad, 0);
- afisareABCinOrdine(rad);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement