Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. typedef enum{anuala,bianuala,perena}tip_planta;
  7.  
  8. typedef enum{primavara,vara,toamna}perioada_de_inflorire;
  9.  
  10. typedef struct nod_a{
  11. char tip_p[50];
  12. float pret;
  13. tip_planta tp;
  14. perioada_de_inflorire pi;
  15. struct nod_a *st, *dr;
  16. }nod;
  17.  
  18. nod *root=NULL;
  19.  
  20. nod *adauga(nod *t, char nume[50], tip_planta tp, float p, perioada_de_inflorire pi)
  21. {
  22. if(t==NULL)
  23. {
  24. t=(nod *)malloc(sizeof(nod)+1);
  25. strcpy(t->tip_p,nume);
  26. t->pret=p;
  27. t->tp=tp;
  28. t->pi=pi;
  29. t->st=t->dr=NULL;
  30. }
  31. else if(strcmp(nume, t->tip_p)<0)
  32. t->st=adauga(t->st,nume,tp,p,pi);
  33. else if(strcmp(nume,t->tip_p)>0)
  34. t->dr=adauga(t->dr,nume,tp,p,pi);
  35. else
  36. printf("\nNodul nu exista! ");
  37. return t;
  38. }
  39.  
  40. void afisare(nod *t)
  41. {
  42. if(t!=NULL)
  43. {
  44. afisare(t->st);
  45. printf("\n%s %d %f %d",t->tip_p,t->tp,t->pret,t->pi);
  46. afisare(t->dr);
  47. }
  48. }
  49.  
  50. void citire(){
  51. FILE *f;
  52. char nume[50];
  53. tip_planta tp;
  54. float pret;
  55. perioada_de_inflorire pi;
  56. f=fopen("plante.txt","rt");
  57. if(f==NULL)
  58. printf("\nFisierul nu exista! ");
  59. else
  60. {
  61. while(!feof(f))
  62. {
  63. fscanf(f,"%s %d %f %d",nume,&tp,&pret,&pi);
  64. root=adauga(root,nume,tp,pret,pi);
  65. }
  66. }
  67. }
  68.  
  69. nod *cautare(nod *r, char *numeplanta)
  70. {
  71. if(r==NULL)
  72. return NULL;
  73. else
  74. if(strcmp(numeplanta,r->tip_p)<0)
  75. return cautare(r->st,numeplanta);
  76. else
  77. if(strcmp(numeplanta,r->tip_p)>0)
  78. return cautare(r->dr,numeplanta);
  79. else
  80. return r;
  81. }
  82.  
  83. void salvare_fisier(char nume[50]){
  84. FILE *f, *g;
  85. nod *p;
  86. float pret_total=0;
  87. f=fopen("plante.txt","rt");
  88. g=fopen("fact.txt","wt");
  89. while(!feof(f))
  90. {
  91. printf("\nDati numele plantei cautate: ");
  92. scanf("%s",nume);
  93. if(strcmp(nume,"gata")==0)
  94. break;
  95. else
  96. {
  97. p=cautare(root,nume);
  98. if(p!=NULL)
  99. {
  100. fprintf(g,"\n%s %d %f %d",p->tip_p,p->tp,p->pret,p->pi);
  101. pret_total=pret_total+p->pret;
  102. }
  103. else
  104. printf("\nPlanta nu exista! ");
  105. }
  106. }
  107. fprintf(g, "\nPretul total este: %f", pret_total);
  108. fclose(f);
  109. fclose(g);
  110. }
  111.  
  112. nod *supred(nod *t, nod *p)
  113. {
  114. nod *q, *aux;
  115. q=t;
  116. if(q->dr!=NULL)
  117. q->dr=supred(q->dr,p);
  118. else
  119. {
  120. strcpy(p->tip_p,q->tip_p);
  121. p->tp=q->tp;
  122. p->pret=q->pret;
  123. p->pi=q->pi;
  124. aux=q;
  125. q=q->st;
  126. free(aux);
  127. }
  128. return q;
  129. }
  130.  
  131. nod *stergere(nod *t, char *num)
  132. {
  133. nod *aux;
  134. if(t==NULL)
  135. printf("\nNodul nu exista! ");
  136. else
  137. {
  138. if(strcmp(num,t->tip_p)<0)
  139. t->st=stergere(t->st,num);
  140. else
  141. if(strcmp(num,t->tip_p)>0)
  142. t->dr=stergere(t->dr,num);
  143. else
  144. {
  145. if(t->st==NULL)
  146. {
  147. aux=t;
  148. t=t->dr;
  149. free(aux);
  150. }
  151. else if(t->dr==NULL)
  152. {
  153. aux=t;
  154. t=t->st;
  155. free(aux);
  156. }
  157. else
  158. t->st=supred(t->st,t);
  159. }
  160. }
  161. return t;
  162. }
  163.  
  164. int main()
  165. {
  166. char n[50], planta[50];
  167. citire();
  168. afisare(root);
  169. getch();
  170. printf("\nZi planta: ");
  171. scanf("%s",n);
  172. stergere(root,n);
  173. printf("\n\n");
  174. afisare(root);
  175. printf("\nCe planta vrei sa salvezi in fisier: ");
  176. scanf("%s",planta);
  177. salvare_fisier(planta);
  178. system("pause");
  179. getch();
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement