Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. //setul 1 problema 1
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6.  
  7. //Se citesc datele despre elevii unei clase, respectiv numele şi data naşterii. Să se ordoneze elevii în ordinea crescătoare după data naşterii şi să se afişeze această situaţie
  8.  
  9. typedef struct data
  10. {
  11. int zi, luna, an;
  12. } Data;
  13.  
  14. typedef struct elev
  15. {
  16. char nume[20];
  17. Data datan;
  18. } Elev;
  19.  
  20. typedef struct vector{
  21. int current_dim;
  22. int total_dim;
  23. Elev* elevi;
  24. } Vector;
  25.  
  26. void init_vector(Vector* vector, int dim)
  27. {
  28. vector->current_dim = 0;
  29. vector->total_dim = dim;
  30. vector->elevi = (Elev*) malloc(sizeof(Elev) * dim);
  31. }
  32.  
  33. void add_vector(Vector* v_elevi, Elev elev)
  34. {
  35. if(v_elevi->current_dim >= v_elevi->total_dim) {
  36. v_elevi->total_dim = v_elevi->total_dim * 2;
  37. v_elevi->elevi =
  38. (Elev*) realloc(v_elevi->elevi, sizeof(Elev) * v_elevi->total_dim);
  39. }
  40.  
  41. strcpy(v_elevi->elevi[v_elevi->current_dim].nume, elev.nume);
  42. v_elevi->elevi[v_elevi->current_dim].datan.zi = elev.datan.zi;
  43. v_elevi->elevi[v_elevi->current_dim].datan.luna = elev.datan.luna;
  44. v_elevi->elevi[v_elevi->current_dim].datan.an = elev.datan.an;
  45. v_elevi->current_dim++;
  46. }
  47.  
  48. void print_elevi(Vector v_elevi)
  49. {
  50. for(int i = 0; i < v_elevi.current_dim; i++) {
  51. printf("Nume: %s\tAn: %d\tZi: %d\n", v_elevi.elevi[i].nume, v_elevi.elevi[i].datan.an,
  52. v_elevi.elevi[i].datan.zi);
  53. }
  54. }
  55.  
  56. void sort_elevi(Elev* e, int n)
  57. {
  58. int i,j;
  59. Elev aux;
  60.  
  61. for (i = 0; i < n; i++)
  62.  
  63. for (j = i + 1; j <= n; j++)
  64.  
  65. {
  66.  
  67. if (e[i].datan.an > e[j].datan.an)
  68. {
  69. aux = e[i];
  70. e[i] = e[j];
  71. e[j] = aux;
  72. }
  73. else if (e[i].datan.an == e[j].datan.an)
  74. {
  75. if (e[i].datan.luna > e[j].datan.luna)
  76.  
  77. {
  78.  
  79. aux = e[i];
  80. e[i] = e[j];
  81.  
  82. e[j] = aux;
  83.  
  84. }
  85. else if (e[i].datan.luna == e[j].datan.luna)
  86. if (e[i].datan.zi > e[j].datan.zi)
  87. {
  88.  
  89. aux = e[i];
  90. e[i] = e[j];
  91.  
  92. e[j] = aux;
  93. }
  94. }
  95. }
  96. }
  97. int
  98. main ()
  99. {
  100. int n, i;
  101.  
  102. Vector v_elevi;
  103. Elev aux;
  104. printf ("Numarul de elevi:");
  105. scanf ("%d", &n);
  106. init_vector(&v_elevi, 1);
  107.  
  108. for (i = 0; i < n; i++)
  109. {
  110. scanf("%s %d %d %d", aux.nume, &aux.datan.an, &aux.datan.luna, &aux.datan.zi);
  111. add_vector(&v_elevi, aux);
  112. }
  113.  
  114. //sort_elevi(v_elevi.elevi, v_elevi.current_dim);
  115. print_elevi(v_elevi);
  116.  
  117. return 0;
  118. }
  119.  
  120. // //setul 9 problema 2
  121. // #include<stdio.h>
  122. // int main ()
  123. // {
  124. // struct firma
  125. //
  126. // {
  127. //
  128. // int cod_firma;
  129. //
  130. // int cod_produs;
  131. //
  132. // float cant;
  133. // }v[4], x;
  134. //
  135. //
  136. //
  137. //
  138. // int i,n,j ;
  139. //
  140. // printf("Dati numarul n:");
  141. //
  142. // scanf("%d", &n);
  143. //
  144. // for(i=1;i<n;i++)
  145. //
  146. // {
  147. //
  148. // printf("Firma %d\n", i);
  149. //
  150. // scanf("%d", v[i].cod_firma);
  151. //
  152. // printf("produsul \n ");
  153. //
  154. // scanf("%d",&v[i].cod_produs);
  155. //
  156. // printf("cantitate \n");
  157. //
  158. // scanf("%f",&v[i].cant);
  159. //
  160. //
  161. // }
  162. // for(i=1;i<n;i++)
  163. //
  164. // for (j=i+1;j<=n;j++)
  165. //
  166. // if(v[i].cant>v[j].cant)
  167. //
  168. // {
  169. //
  170. // x=v[i];
  171. //
  172. // v[i]=v[j];
  173. //
  174. // v[j]=x;
  175. //
  176. // }
  177. //
  178. // for(i=1;i<n;i++)
  179. //
  180. // printf(" %d %f", v[i].cod_firma,v[i].cant);
  181. //
  182. // return 0;
  183. //
  184. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement