Guest User

Untitled

a guest
Jun 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 3
  4. #define cog 16
  5. #define nom 12
  6.  
  7. typedef struct {
  8.  
  9. char cognome[cog];
  10. char nome[nom];
  11. int stip; } dipendente;
  12.  
  13. void letturastampa(FILE *fp); //mostra contenuto del file
  14.  
  15. void putvet(FILE *fp, dipendente *dipendenti1, dipendente *dipendenti2, int *n, int *m);
  16. void stampapari(dipendente *dipendenti1, int n);
  17. void stampadispari(dipendente *dipendenti2, int m);
  18. void fondi(dipendente *a, dipendente *b, dipendente *f, int n, int m, int *p);
  19. void stampafuso(dipendente *dipendenti3, int p);
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23. dipendente dipendenti1[SIZE];
  24. dipendente dipendenti2[SIZE];
  25. dipendente dipendenti3[SIZE];
  26. FILE *fp;
  27. int n, m, p;
  28.  
  29. //mostra il contenuto del file
  30. fp=fopen("anagrafe.txt", "r");
  31. letturastampa(fp);
  32. fclose(fp);
  33.  
  34. //1
  35. fp=fopen("anagrafe.txt", "r");
  36. putvet(fp, dipendenti1, dipendenti2, &n, &m);
  37. fclose(fp);
  38. //2
  39. printf("\n\nSTAMPA DIPENDENTI CON STIPENDIO PARI:\n\n");
  40. stampapari(dipendenti1, n);
  41. //3
  42. printf("\n\nSTAMPA DIPENDENTI CON STIPENDIO DISPARI:\n\n");
  43. stampadispari(dipendenti2, m);
  44. //4
  45. printf("\n\nFONDI:\n\n");
  46. fondi(dipendenti1, dipendenti2, dipendenti3, n, m, &p);
  47. //5
  48. stampafuso(dipendenti3, p);
  49.  
  50. printf("\n\n");
  51. system("PAUSE");
  52. return 0;
  53. }
  54.  
  55.  
  56.  
  57. void letturastampa(FILE *fp) {
  58.  
  59. char c;
  60. while(!feof(fp)) {
  61.  
  62. c = fgetc(fp);
  63. putchar(c); }
  64.  
  65. }
  66.  
  67.  
  68. void putvet(FILE *fp, dipendente *dipendenti1, dipendente *dipendenti2, int *n, int *m) {
  69.  
  70. char c;
  71. int i=0, j=0;
  72. dipendente TEMP;
  73.  
  74. while(!feof(fp) && i<SIZE && j<SIZE) {
  75.  
  76. fscanf(fp, "%s %s %d", TEMP.cognome, TEMP.nome, &TEMP.stip);
  77.  
  78. if(TEMP.stip%2==0) { strcpy(dipendenti1[i].cognome, TEMP.cognome);
  79. strcpy(dipendenti1[i].nome, TEMP.nome);
  80. dipendenti1[i].stip = TEMP.stip;
  81. i++; }
  82.  
  83. else if(TEMP.stip%2!=0) { strcpy(dipendenti2[j].cognome, TEMP.cognome);
  84. strcpy(dipendenti2[j].nome, TEMP.nome);
  85. dipendenti2[j].stip = TEMP.stip;
  86. j++; } }
  87.  
  88. *n = i;
  89. *m = j;
  90. }
  91.  
  92. void stampapari(dipendente *dipendenti1, int n) {
  93.  
  94. int i;
  95. for(i=0; i<n; i++) printf("%s %s %d\n", dipendenti1[i].cognome, dipendenti1[i].nome, dipendenti1[i].stip);
  96.  
  97.  
  98. }
  99.  
  100.  
  101. void stampadispari(dipendente *dipendenti2, int m) {
  102.  
  103. int i;
  104. for(i=0; i<m; i++) printf("%s %s %d\n", dipendenti2[i].cognome, dipendenti2[i].nome, dipendenti2[i].stip);
  105.  
  106. }
  107.  
  108.  
  109. void fondi(dipendente *a, dipendente *b, dipendente *f, int n, int m, int *p) {
  110.  
  111. int i=0, j=0, k=0;
  112.  
  113.  
  114. while (i<n && j<m) {
  115. if(a[i].stip < b[j].stip) {
  116.  
  117. strcpy(f[k].nome, a[i].nome);
  118. strcpy(f[k].cognome, a[i].cognome);
  119. f[k].stip = a[i].stip;
  120. i++; }
  121. else {
  122. strcpy(f[k].nome, b[j].nome);
  123. strcpy(f[k].cognome, b[j].cognome);
  124. f[k].stip=b[j].stip;
  125. j++; }
  126.  
  127. k++; }
  128.  
  129. while(i<n) {
  130. strcpy(f[k].nome, a[i].nome);
  131. strcpy(f[k].cognome, a[i].cognome);
  132. f[k].stip = a[i].stip;
  133. i++;
  134. k++; }
  135. while(j<m) {
  136. strcpy(f[k].nome, b[j].nome);
  137. strcpy(f[k].cognome, b[j].cognome);
  138. f[k].stip = b[j].stip;
  139. j++;
  140. k++; }
  141.  
  142. *p = k;
  143.  
  144. }
  145.  
  146.  
  147. void stampafuso(dipendente *dipendenti3, int p) {
  148.  
  149. int i;
  150. for(i=0; i<p; i++) printf("%s %s %d\n", dipendenti3[i].cognome, dipendenti3[i].nome, dipendenti3[i].stip);
  151.  
  152. }
Add Comment
Please, Sign In to add comment