daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. /* tipul nodului arborelui */
  6. typedef struct nod_arbore {
  7. char *nume;
  8. int punctaj;
  9. struct nod_arbore *stang,*drept;
  10. }nod;
  11. /* prototipurile functiilor */
  12. nod *citireFisier(nod*);
  13. nod *adauga(nod*,char*,int);
  14. void afisare(nod*);
  15. nod *cautare(nod*,char*);
  16. void main(void)
  17. {
  18. nod *radacina,*p;
  19. char nume[21];
  20. int opt;
  21. radacina=NULL;
  22. clrscr();
  23. radacina=citireFisier(radacina);
  24. if(radacina==NULL) {
  25. printf("Arbore vid!");
  26. getch();
  27. }
  28. else {
  29. do {
  30. printf("\n\n1. Afisare punctaje concurenti");
  31. printf("\n2. Afisare punctaj pentru un concurent");
  32. printf("\n0. Terminare program");
  33. printf("\nOptinea: "); scanf("%d",&opt); fflush(stdin);
  34. switch(opt) {
  35. case 1: afisare(radacina); getch(); break;
  36. case 2: printf("\nNumele concurentului: ");
  37. scanf("%s",nume);
  38. if((p=cautare(radacina,nume))!=NULL)
  39. printf("\n%s are punctajul:
  40. %d",p->nume,p->punctaj);
  41. else
  42. printf("\n%s nu a concurat!");
  43. 1
  44.  
  45. getch(); break;
  46. case 0: printf("\nProgram terminat. Apasati o tasta
  47. ...");
  48. getch(); break;
  49. deafult: printf("\nOptiune gresita!"); getch();
  50. }
  51. }while(opt!=0);
  52. }
  53. }
  54. nod *citireFisier(nod *rad)
  55. {
  56. FILE *f;
  57. char nume[31];
  58. int pctj;
  59. f=fopen("concurs.txt","rt");
  60. if(f==NULL) {
  61. printf("\nEroare la deschidere fisier!"); getch();
  62. return rad;
  63. }
  64. while(fscanf(f,"%s %d",nume,&pctj)!=EOF)
  65. rad=adauga(rad,nume,pctj);
  66. fclose(f);
  67. return rad;
  68. }
  69. nod *adauga(nod *p,char *nume,int pctj)
  70. {
  71. if(p==NULL) { /* adaugare nod nou in arbore */
  72. p=(nod *)malloc(sizeof(nod));
  73. if(p==NULL) {
  74. printf("Eroare la alocare memorie!"); getch();
  75. return NULL;
  76. }
  77. p->nume=(char *)malloc(strlen(nume)+1);
  78. if(p->nume==NULL) {
  79. printf("Eroare la alocare memorie!"); getch();
  80. return NULL;
  81. }
  82. strcpy(p->nume,nume);
  83. p->punctaj=pctj;
  84. p->stang=p->drept=NULL;
  85. }
  86. else {
  87. if(pctj>p->punctaj) /* adauga in subarborele stang */
  88. p->stang=adauga(p->stang,nume,pctj);
  89. else /* adauga in subarborele drept */
  90. p->drept=adauga(p->drept,nume,pctj);
  91. }
  92. return p;
  93. }
  94. void afisare(nod *q)
  95. {
  96. if(q!=NULL) {
  97. afisare(q->stang);
  98. printf("\n%s %d",q->nume,q->punctaj);
  99. afisare(q->drept);
  100. }
  101.  
  102. 2
  103.  
  104. }
  105. nod *cautare(nod *p,char *nume)
  106. {
  107. nod *q=NULL;
  108. if(p!=NULL) {
  109. if(strcmp(p->nume,nume)==0)
  110. q=p;
  111. else {
  112. if(q==NULL)
  113. q=cautare(p->stang,nume);
  114. if(q==NULL)
  115. q=cautare(p->drept,nume);
  116. }
  117. }
  118. else
  119. q=NULL;
  120. return q;
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top