Guest User

Untitled

a guest
Jun 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define dim 30
  4.  
  5. typedef struct{char nomeEsame[20];
  6. int votoEsame;
  7. } esame;
  8.  
  9. void scrivi(esame a[],int m);
  10. void visualizza(esame a[],int m);
  11. void salvasufile(FILE*out,esame a[],int m);
  12. void leggifile(FILE*out);
  13. void ordina(esame a[],int m);
  14. void stampavettore(esame a[],int m);
  15.  
  16. int main()
  17. {
  18. esame esami[dim];
  19. int n;
  20. FILE*fp;
  21.  
  22. printf("quanti esami vuoi inserire? ");
  23. scanf("%d",&n);
  24.  
  25. scrivi(esami,n);
  26.  
  27. visualizza(esami,n);
  28.  
  29. fp=fopen("verbale.txt","w");
  30. salvasufile(fp,esami,n);
  31. fclose(fp);
  32.  
  33. fp=fopen("verbale.txt","r");
  34. leggifile(fp);
  35. fclose(fp);
  36.  
  37. ordina(esami,n);
  38.  
  39. stampavettore(esami,n);
  40.  
  41.  
  42. system("PAUSE");
  43. return 0;
  44. }
  45.  
  46. void scrivi(esame a[],int m)
  47. {
  48. printf("\ninserisci esami\n\n");
  49. int i;
  50. getchar();
  51. for(i=0;i<m;i++)
  52. {
  53. printf("insersci nome esame\n");
  54. gets(a[i].nomeEsame);
  55. printf("unserisci voto esame\n");
  56. scanf("%d",&a[i].votoEsame);
  57. printf("\n");
  58. }
  59. }
  60.  
  61.  
  62. void visualizza(esame a[],int m)
  63. {
  64. printf("gli esami inseriti sono\n\n");
  65. int i;
  66. for(i=0;i<m;i++)
  67. {
  68. printf("nome esame: %s\n",a[i].nomeEsame);
  69. printf("voto esame: %d\n",a[i].votoEsame);
  70. printf("\n");
  71. }
  72. }
  73.  
  74.  
  75. void salvasufile(FILE*out,esame a[],int m)
  76. {
  77. if(out==NULL)
  78. {
  79. printf("errore nell'apertura del file in lettura\n");
  80. }
  81. else
  82. {
  83. printf("\nSALVATAGGIO SU FILE IN CORSO....\n\n");
  84. int i;
  85. for(i=0;i<m;i++)
  86. {
  87. fprintf(out,"nome esame: %s\n",a[i].nomeEsame);
  88. fprintf(out,"voto esame: %d\n",a[i].votoEsame);
  89. fprintf(out,"\n");
  90. }
  91. }
  92. }
  93.  
  94.  
  95. void leggifile(FILE*out)
  96. {
  97. if(out==NULL)
  98. {
  99. printf("errore nell'apertura del file\n");
  100. }
  101. else
  102. {
  103. char c;
  104. printf("******CONTENUTO DEL FILE******\n\n");
  105. c=fgetc(out);
  106. while(!feof(out))
  107. {
  108. putchar(c);
  109. c=fgetc(out);
  110. }
  111. }
  112. }
  113.  
  114.  
  115. void ordina(esame a[],int m)
  116. {
  117. esame app;
  118. int i,j;
  119. for(i=1;i<m;i++)
  120. {
  121. app=a[i];
  122. j=i-1;
  123. while(j>=0 && a[j].votoEsame>app.votoEsame)
  124. {
  125. a[j+1]=a[j];
  126. j--;
  127. }
  128. a[j+1]=app;
  129. }
  130. }
  131.  
  132.  
  133. void stampavettore(esame a[],int m)
  134. {
  135. printf("\n******VETTORE ESAMI ORDINATO RISPETTO AL CAMPO VOTO******\n\n");
  136. int i;
  137. for(i=0;i<m;i++)
  138. {
  139. printf("nome esame: %s\n",a[i].nomeEsame);
  140. printf("voto esame: %d\n",a[i].votoEsame);
  141. printf("\n");
  142. }
  143. }
Add Comment
Please, Sign In to add comment