Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. //ex 1 + desen pe foaie
  6. struct Proces
  7. {
  8. int pid; //unic in multimea proceselor
  9. char* denumire;
  10. char* descriere;
  11. int mem_ocupata_KB;
  12. char* user;
  13. };
  14.  
  15. Proces initProces(int pid, const char* denumire, const char* descriere, int mem_ocupata, const char* user) {
  16. Proces p;
  17. p.pid = pid;
  18. p.denumire = (char*)malloc(sizeof(char)*strlen(denumire) + 1);
  19. strcpy(p.denumire, denumire);
  20. p.descriere = (char*)malloc(sizeof(char)*strlen(descriere) + 1);
  21. strcpy(p.descriere, descriere);
  22. p.mem_ocupata_KB = mem_ocupata;
  23. p.user = (char*)malloc(sizeof(char)*strlen(user) + 1);
  24. strcpy(p.user, user);
  25.  
  26. return p;
  27.  
  28. }
  29.  
  30. void afisareProces(Proces p) {
  31. 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 ",
  32. p.pid, p.denumire, p.descriere, p.mem_ocupata_KB, p.user);
  33. }
  34. //ex2 bagarea in arbore si apoi afisarea in Inordine care-i jos
  35. struct NOD
  36. {
  37. Proces info;
  38. NOD * st;
  39. NOD *dr;
  40. };
  41.  
  42. NOD* inserareInArboreABC(NOD*rad, Proces p) {
  43. if (rad) {
  44. if (rad->info.pid > p.pid) {
  45. rad->st = inserareInArboreABC(rad->st, p);
  46. }
  47. else
  48. {
  49. rad->dr = inserareInArboreABC(rad->dr, p);
  50. }
  51. return rad;
  52. }
  53. else {
  54. NOD* nou = (NOD*)malloc(sizeof(NOD));
  55. nou->info = p;
  56. nou->dr = nou->st = NULL;
  57. return nou;
  58. }
  59. }
  60. //ex3.
  61. void nrDescendenti(NOD* r, int id, int &nrd) {
  62. if (r)
  63. {
  64. if (r->info.pid == id)
  65. {
  66. if (r->st && r->dr)
  67. {
  68. nrd = 2;
  69. }
  70. else
  71. {
  72. if (r->st)
  73. {
  74. nrd = 1;
  75. }
  76. else
  77. {
  78. if (r->dr)
  79. {
  80. nrd = 1;
  81. }
  82. else
  83. {
  84. nrd = 0;
  85. }
  86. }
  87. }
  88. }
  89. nrDescendenti(r->st, id, nrd);
  90. nrDescendenti(r->dr, id, nrd);
  91. }
  92. }
  93. //ex4.
  94. int memorieOcupata(NOD* root)
  95. {
  96. if (root)
  97. {
  98. return
  99. sizeof(root->info.pid) +
  100. sizeof(root->info.mem_ocupata_KB) +
  101. sizeof(char)*strlen(root->info.denumire) +
  102. sizeof(char)*strlen(root->info.descriere) +
  103. sizeof(char)*strlen(root->info.user) +
  104. 3 * sizeof(char*) +
  105. memorieOcupata(root->st) +
  106. memorieOcupata(root->dr);
  107. }
  108. else
  109. return 0;
  110. }
  111. //ex5
  112. void majorareMemorie(NOD* root, double procent)
  113. {
  114. if (root)
  115. {
  116. root->info.mem_ocupata_KB += procent * root->info.mem_ocupata_KB;
  117. majorareMemorie(root->st, procent);
  118. majorareMemorie(root->dr, procent);
  119. }
  120.  
  121. }
  122. //ex6
  123. void inserareinVector(NOD* root, int nivelCautat, int nivelCurent, int** rezultat, int* contor)
  124. {
  125. if (root)
  126. {
  127. if (nivelCautat == nivelCurent)
  128. {
  129. (*rezultat)[(*contor)++] = root->info.pid;
  130. }
  131. else
  132. {
  133. nivelCurent++;
  134. inserareinVector(root->st, nivelCautat, nivelCurent, rezultat, contor);
  135. inserareinVector(root->dr, nivelCautat, nivelCurent, rezultat, contor);
  136. }
  137. }
  138. }
  139.  
  140. void nrNoduriPeNivel(NOD* root, int nivelCautat, int nivelCurent, int* nrNoduri)
  141. {
  142. if (root)
  143. {
  144. if (nivelCautat == nivelCurent)
  145. {
  146. (*nrNoduri)++;
  147. }
  148. else
  149. {
  150. nivelCurent++;
  151. nrNoduriPeNivel(root->st, nivelCautat, nivelCurent, nrNoduri);
  152. nrNoduriPeNivel(root->dr, nivelCautat, nivelCurent, nrNoduri);
  153. }
  154. }
  155.  
  156. }
  157. int* vectorId(NOD* root, int nivelCautat, int nivelCurent, int* nrNoduri, int* contor)
  158. {
  159. nrNoduriPeNivel(root, nivelCautat, nivelCurent, nrNoduri);
  160.  
  161. int* rezultat = NULL;
  162.  
  163. if ((*nrNoduri) > 0)
  164. {
  165. rezultat = (int*)malloc(sizeof(int)*(*nrNoduri));
  166. inserareinVector(root, nivelCautat, nivelCurent, &rezultat, contor);
  167. }
  168.  
  169. return rezultat;
  170.  
  171. }
  172. //ex7.
  173. NOD* cautareMaxim(NOD* root)
  174. {
  175. if (root)
  176. {
  177. NOD* temp = root;
  178. while (temp->dr)
  179. temp = temp->dr;
  180. return temp;
  181.  
  182. }
  183. else return NULL;
  184.  
  185. }
  186.  
  187. // desi se modifica, radacina va fi returnata
  188.  
  189. NOD* stergereNod(NOD* root, int idCautat)
  190. {
  191. if (root)
  192. {
  193. if (root->info.pid == idCautat)
  194. {
  195. if (root->st == NULL)
  196. {
  197. NOD* temp = root->dr;
  198. free(root->info.denumire);
  199. free(root->info.descriere);
  200. free(root->info.user);
  201. root = temp;
  202. }
  203. else
  204. if (root->dr == NULL)
  205. {
  206. NOD* temp = root->st;
  207. free(root->info.denumire);
  208. free(root->info.descriere);
  209. free(root->info.user);
  210. root = temp;
  211. }
  212. else
  213. {
  214. NOD* maxim = cautareMaxim(root->st);
  215. Proces aux = root->info;
  216. root->info = maxim->info;
  217. maxim->info = aux;
  218. root->st = stergereNod(root->st, idCautat);
  219. }
  220. }
  221. else
  222. if (idCautat < root->info.pid)
  223. root->st = stergereNod(root->st, idCautat);
  224. else if (idCautat > root->info.pid)
  225. root->dr = stergereNod(root->dr, idCautat);
  226. }
  227. return root;
  228. }
  229. //ex2
  230. void afisareABCinOrdine(NOD* rad) {
  231. if (rad) {
  232. afisareABCinOrdine(rad->st);
  233. afisareProces(rad->info);
  234. afisareABCinOrdine(rad->dr);
  235. }
  236. }
  237. void main() {
  238. /*Proces p1 = initProces(30, "Chrome", "motor de cautare", 1337, "Andrei");
  239. afisareProces(p1);*/
  240. //ex1 +2 *************************************************
  241. // vector de task-uri pt ca e mai usor de bagat in arbore
  242. int nrp = 5; // numar de procese
  243. Proces * vProcese = (Proces*)malloc(sizeof(Proces) *nrp);
  244.  
  245. vProcese[0] = initProces(3, "chrome.exe", "Google Chrome", 132696, "gigel");
  246. vProcese[1] = initProces(1, "explorer.exe", "Windows Explorer", 64765, "gigel");
  247. vProcese[2] = initProces(5, "devenv.exe *32", "Microsoft Visual Studio 2012", 141028, "gigel");
  248. vProcese[3] = initProces(4, "chrome.exe", "Google Chrome", 34760, "gigel");
  249. vProcese[4] = initProces(0, "chrome.exe", "Google Chrome", 2836, "gigel");
  250. NOD *rad = NULL;
  251. for (int i = 0; i < nrp; i++) {
  252. rad = inserareInArboreABC(rad, vProcese[i]);
  253. }
  254. afisareABCinOrdine(rad);
  255. //EX3*********************************************
  256. int numar_descendenti = 0;
  257. nrDescendenti(rad, 3, numar_descendenti);
  258. printf("\n Nr descendenti %d \n", numar_descendenti);
  259. //EX4*********************************************
  260. printf("\n Memorie ocupata: %d \n", memorieOcupata(rad));
  261. //EX5*********************************************
  262. majorareMemorie(rad, 0.15);
  263. printf("\n\n");
  264. afisareABCinOrdine(rad);
  265. //ex6*********************************************************
  266. printf("\n\n");
  267. int contor = 0;
  268. int nrNoduri = 0;
  269. int* rezultat = vectorId(rad, 1, 0, &nrNoduri, &contor);
  270. for (int i = 0; i < nrNoduri; i++)
  271. {
  272. printf(" %d ", rezultat[i]);
  273. }
  274. //EX7******************************************************
  275. printf("\n\nArbore dupa stergere: \n");
  276. stergereNod(rad, 0);
  277. afisareABCinOrdine(rad);
  278. getch();
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement