Advertisement
Perlamado

Untitled

Feb 24th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. // esame 20-07-18
  2. // file200718.txt
  3.  
  4. # include <math.h>
  5. # include <string.h>
  6. # include <stdlib.h>
  7. # include <stdio.h>
  8.  
  9. struct telecomando {
  10. int tempo;
  11. int comando;// 0 OFF VENTOLA, 1 LIGHT, 2 VEL BASSA "L", 3 VEL MEDIA "M", 4 VEL ALTA "H", 5 TIMER 1H, 6 TIMER 3H
  12. char nomeComando[6];
  13. };
  14.  
  15. struct telecomando *letturaFile(FILE *f,int *n);
  16. void stampaStructTelecomando(struct telecomando *t,int n);
  17. int pulsanteMax(struct telecomando *t,int n);
  18. int tempoLuce(struct telecomando *t,int n);
  19. int tempoVelocitaVentolaAlta(struct telecomando *t,int n);
  20. int tempoAccensioneVentola(struct telecomando *t,int n);
  21. void ordinamento(struct telecomando *t,int n);
  22.  
  23. int main( int argc, char *argv[]){
  24.  
  25. int n,pulsMax,tempoLux,tempoVelH,tempoVentola;
  26. struct telecomando *t;
  27. FILE *f;
  28. if(argc!=2){
  29. return 1;
  30. }
  31. f=fopen(argv[1],"r");
  32. if(f==NULL){
  33. printf("file non trovato\n");
  34. return 1;
  35. }
  36. // lettura e stampa struct
  37. t=letturaFile(f,&n);
  38. fclose(f);
  39. stampaStructTelecomando(t,n);
  40. //richiesta1
  41. pulsMax=pulsanteMax(t,n);
  42. printf("[MAX-PRESSIONI]\n%d\n",pulsMax);
  43. //richiesta 2
  44. tempoLux=tempoLuce(t,n);
  45. printf("[MEDIA-LUCE]\n%d\n",tempoLux);
  46. // richiesta 3
  47. tempoVelH=tempoVelocitaVentolaAlta(t,n);
  48. printf("[TOT-ALTA-VELOCITA]\n%d\n",tempoVelH);
  49. //richiesta 4
  50. tempoVentola=tempoAccensioneVentola(t,n);
  51. printf("[TOT-ACCENSIONE]\n%d\n",tempoVentola);
  52. //richiesta 5
  53. ordinamento(t,n);
  54. stampaStructTelecomando(t,n);
  55. return 0;
  56. }
  57. struct telecomando *letturaFile(FILE *f,int *n){
  58. int nConv;
  59. char buffer[100];
  60. int size=10;
  61. *n=0;
  62. struct telecomando *t1,*t2;
  63. t2=malloc(size*sizeof(struct telecomando));
  64.  
  65. while(fgets(buffer,sizeof(buffer),f)){
  66. t1=t2 + *n;
  67. nConv = sscanf(buffer, "%d %s", &t1->tempo, t1->nomeComando);
  68.  
  69. if(strcmp(t1->nomeComando,"OFF") == 0){
  70. t1->comando=0;
  71. }
  72. if(strcmp(t1->nomeComando,"LIGHT") == 0){
  73. t1->comando=1;
  74. }
  75. if(strcmp(t1->nomeComando,"L") == 0){
  76. t1->comando=2;
  77. }
  78. if(strcmp(t1->nomeComando,"M") == 0){
  79. t1->comando=3;
  80. }
  81. if(strcmp(t1->nomeComando,"H") == 0){
  82. t1->comando=4;
  83. }
  84. if(strcmp(t1->nomeComando,"1h") == 0){
  85. t1->comando=5;
  86. }
  87. if(strcmp(t1->nomeComando,"3h") == 0){
  88. t1->comando=6;
  89. }
  90. (*n)++;
  91. if(*n>=size){
  92. size=size*2;
  93. t2=realloc(t2,size*sizeof(struct telecomando));
  94. }
  95. }
  96. t2=realloc(t2,*n*sizeof(struct telecomando));
  97. return t2;
  98. }
  99. void stampaStructTelecomando(struct telecomando *t,int n){
  100. int i;
  101. for(i=0;i<n;i++){
  102. printf("%d %d %s\n",t[i].tempo,t[i].comando, t[i].nomeComando);
  103. }
  104.  
  105. }
  106. int pulsanteMax(struct telecomando *t,int n){
  107. int i;
  108. int v[7];
  109. int max=0;
  110. int pulsante;
  111. for(i=0;i<7;i++){
  112. v[i]=0;
  113. }
  114.  
  115. for(i=0;i<n;i++){
  116. pulsante=t[i].comando;
  117. v[pulsante]++;
  118. }
  119. for(i=0;i<7;i++){
  120. if(v[i]>max){
  121. max=v[i];
  122. pulsante=i;
  123. }
  124. }
  125. return pulsante;
  126. }
  127.  
  128. int tempoLuce(struct telecomando *t,int n){
  129.  
  130. int i,media,durataIn;
  131. int durataTot=0;
  132. int numAcc=0;
  133. int flag=0;
  134. for(i=0;i<n;i++){
  135. if(t[i].comando== 1){
  136. if(flag==0){
  137. flag=1;
  138. durataIn=t[i].tempo;
  139. }else{
  140. flag=0;
  141. durataTot+=t[i].tempo-durataIn;
  142. numAcc++;
  143. }
  144. }
  145.  
  146. }
  147. media=durataTot/numAcc;
  148. return media;
  149. }
  150. int tempoVelocitaVentolaAlta(struct telecomando *t,int n){
  151.  
  152. int i;
  153. int tempoVel;
  154. int somma=0;
  155. int flag=0;
  156.  
  157. for(i=0;i<n;i++){
  158. if(t[i].comando== 4){
  159. if(flag==0){
  160. tempoVel=t[i].tempo;
  161. flag=1;
  162. }
  163. }else if(t[i].comando==0 ||t[i].comando==2 || t[i].comando==3 ){
  164. somma+= t[i].tempo-tempoVel;
  165. flag=0;
  166. }
  167. }
  168. return somma;
  169. }
  170. int tempoAccensioneVentola(struct telecomando *t,int n){
  171.  
  172. int i;
  173. int tempoOff,tempoVentola;
  174. int sommaOff=0;
  175. int sommaVentola=0;
  176. for(i=0;i<n;i++){
  177. if(t[i].comando==0){
  178. tempoOff=t[i].tempo;
  179. sommaOff+=tempoOff;
  180. } //printf("SOMMA OFF:%d\n",sommaOff);
  181. }
  182.  
  183. for(i=0;i<n;i++){
  184. if(t[i].comando==2 || t[i].comando==3 || t[i].comando==4){
  185. tempoVentola=t[i].tempo;
  186. sommaVentola+=tempoVentola;
  187. }
  188. }
  189. //printf("SOMMA VENTOLA:%d\n",sommaVentola);
  190. return sommaVentola-sommaOff ;
  191. }
  192. void ordinamento(struct telecomando *t,int n){
  193.  
  194. int i,j;
  195. struct telecomando temp; // temporaneo=temp
  196. char *s1, *s2;
  197. for(i=0;i<n;i++){
  198. for(j=0;j<n;j++){
  199. s1 = t[i].nomeComando;
  200. s2 = t[j].nomeComando;
  201. if(strcmp(s1,s2)<0){
  202. temp=t[i];
  203. t[i]=t[j];
  204. t[j]=temp;
  205. }
  206. if(strcmp(t[i].nomeComando,t[j].nomeComando)==0){
  207. if(t[i].tempo>t[j].tempo){
  208. temp=t[i];
  209. t[i]=t[j];
  210. t[j]=temp;
  211. }
  212. }
  213. }
  214.  
  215. }
  216.  
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement