Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4.  
  5. typedef enum { anuala, bianuala, perena }tip_planta;
  6. typedef enum {primavara,vara,toamna}perioada;
  7.  
  8. typedef struct nod {
  9. char nume_planta[20];
  10. tip_planta tip;
  11. float pret;
  12. perioada inflorire;
  13. struct nod *st, *dr;
  14. }nod;
  15.  
  16. nod *root = NULL;
  17.  
  18. nod *adaugare(nod *t, char nume[20], tip_planta planta, float p, perioada per)
  19. {
  20. if (t == NULL)
  21. {
  22. t = (nod*)malloc(sizeof(nod));
  23. strcpy(t->nume_planta, nume);
  24. t->tip = planta;
  25. t->pret = p;
  26. t->inflorire = per;
  27. t->st = t->dr = NULL;
  28. }
  29. else if (strcmp(nume, t->nume_planta) < 0)
  30. t->st = adaugare(t->st, nume, planta, p, per);
  31. else
  32. if (strcmp(nume, t->nume_planta) > 0)
  33. t->dr = adaugare(t->dr, nume, planta, p, per);
  34. else
  35. printf("Nodul deja exista.");
  36. return t;
  37. }
  38.  
  39. void afisare(nod *t)
  40. {
  41. if (t != NULL)
  42. {
  43. afisare(t->st);
  44. printf("\n%s %d %f %d", t->nume_planta, t->tip, t->pret, t->inflorire);
  45. afisare(t->dr);
  46. }
  47. }
  48.  
  49. void citire()
  50. {
  51. FILE *f;
  52.  
  53. char nume[20];
  54. tip_planta tp;
  55. float pre;
  56. perioada perin;
  57. f = fopen("plante.txt", "rt");
  58. if (f == NULL)
  59. printf("Fisirul nu se poate deschide.");
  60. else {
  61. while (!feof(f))
  62. {
  63. fscanf(f,"%s %d %f %d",nume, &tp, &pre, &perin);
  64. root = adaugare(root, nume, tp, pre, perin);
  65. }
  66. }
  67.  
  68. }
  69.  
  70. int main()
  71. {
  72. int opt;
  73. do {
  74. printf("\nMeniu:");
  75. printf("\n1.Citire");
  76. printf("\n2.Afisare");
  77. printf("Dati optiunea:");
  78. scanf("%d", &opt);
  79. switch (opt)
  80. {
  81. case 1:
  82. citire();
  83. break;
  84. case 2:
  85. afisare(root);
  86. break;
  87. case 0:
  88. break;
  89. }
  90. } while (opt != 0);
  91. getch();
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement