Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // esame 20-07-18
- // file200718.txt
- # include <math.h>
- # include <string.h>
- # include <stdlib.h>
- # include <stdio.h>
- struct telecomando {
- int tempo;
- 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
- char nomeComando[6];
- };
- struct telecomando *letturaFile(FILE *f,int *n);
- void stampaStructTelecomando(struct telecomando *t,int n);
- int pulsanteMax(struct telecomando *t,int n);
- int tempoLuce(struct telecomando *t,int n);
- int tempoVelocitaVentolaAlta(struct telecomando *t,int n);
- int tempoAccensioneVentola(struct telecomando *t,int n);
- void ordinamento(struct telecomando *t,int n);
- int main( int argc, char *argv[]){
- int n,pulsMax,tempoLux,tempoVelH,tempoVentola;
- struct telecomando *t;
- FILE *f;
- if(argc!=2){
- return 1;
- }
- f=fopen(argv[1],"r");
- if(f==NULL){
- printf("file non trovato\n");
- return 1;
- }
- // lettura e stampa struct
- t=letturaFile(f,&n);
- fclose(f);
- stampaStructTelecomando(t,n);
- //richiesta1
- pulsMax=pulsanteMax(t,n);
- printf("[MAX-PRESSIONI]\n%d\n",pulsMax);
- //richiesta 2
- tempoLux=tempoLuce(t,n);
- printf("[MEDIA-LUCE]\n%d\n",tempoLux);
- // richiesta 3
- tempoVelH=tempoVelocitaVentolaAlta(t,n);
- printf("[TOT-ALTA-VELOCITA]\n%d\n",tempoVelH);
- //richiesta 4
- tempoVentola=tempoAccensioneVentola(t,n);
- printf("[TOT-ACCENSIONE]\n%d\n",tempoVentola);
- //richiesta 5
- ordinamento(t,n);
- stampaStructTelecomando(t,n);
- return 0;
- }
- struct telecomando *letturaFile(FILE *f,int *n){
- int nConv;
- char buffer[100];
- int size=10;
- *n=0;
- struct telecomando *t1,*t2;
- t2=malloc(size*sizeof(struct telecomando));
- while(fgets(buffer,sizeof(buffer),f)){
- t1=t2 + *n;
- nConv = sscanf(buffer, "%d %s", &t1->tempo, t1->nomeComando);
- if(strcmp(t1->nomeComando,"OFF") == 0){
- t1->comando=0;
- }
- if(strcmp(t1->nomeComando,"LIGHT") == 0){
- t1->comando=1;
- }
- if(strcmp(t1->nomeComando,"L") == 0){
- t1->comando=2;
- }
- if(strcmp(t1->nomeComando,"M") == 0){
- t1->comando=3;
- }
- if(strcmp(t1->nomeComando,"H") == 0){
- t1->comando=4;
- }
- if(strcmp(t1->nomeComando,"1h") == 0){
- t1->comando=5;
- }
- if(strcmp(t1->nomeComando,"3h") == 0){
- t1->comando=6;
- }
- (*n)++;
- if(*n>=size){
- size=size*2;
- t2=realloc(t2,size*sizeof(struct telecomando));
- }
- }
- t2=realloc(t2,*n*sizeof(struct telecomando));
- return t2;
- }
- void stampaStructTelecomando(struct telecomando *t,int n){
- int i;
- for(i=0;i<n;i++){
- printf("%d %d %s\n",t[i].tempo,t[i].comando, t[i].nomeComando);
- }
- }
- int pulsanteMax(struct telecomando *t,int n){
- int i;
- int v[7];
- int max=0;
- int pulsante;
- for(i=0;i<7;i++){
- v[i]=0;
- }
- for(i=0;i<n;i++){
- pulsante=t[i].comando;
- v[pulsante]++;
- }
- for(i=0;i<7;i++){
- if(v[i]>max){
- max=v[i];
- pulsante=i;
- }
- }
- return pulsante;
- }
- int tempoLuce(struct telecomando *t,int n){
- int i,media,durataIn;
- int durataTot=0;
- int numAcc=0;
- int flag=0;
- for(i=0;i<n;i++){
- if(t[i].comando== 1){
- if(flag==0){
- flag=1;
- durataIn=t[i].tempo;
- }else{
- flag=0;
- durataTot+=t[i].tempo-durataIn;
- numAcc++;
- }
- }
- }
- media=durataTot/numAcc;
- return media;
- }
- int tempoVelocitaVentolaAlta(struct telecomando *t,int n){
- int i;
- int tempoVel;
- int somma=0;
- int flag=0;
- for(i=0;i<n;i++){
- if(t[i].comando== 4){
- if(flag==0){
- tempoVel=t[i].tempo;
- flag=1;
- }
- }else if(t[i].comando==0 ||t[i].comando==2 || t[i].comando==3 ){
- somma+= t[i].tempo-tempoVel;
- flag=0;
- }
- }
- return somma;
- }
- int tempoAccensioneVentola(struct telecomando *t,int n){
- int i;
- int tempoOff,tempoVentola;
- int sommaOff=0;
- int sommaVentola=0;
- for(i=0;i<n;i++){
- if(t[i].comando==0){
- tempoOff=t[i].tempo;
- sommaOff+=tempoOff;
- } //printf("SOMMA OFF:%d\n",sommaOff);
- }
- for(i=0;i<n;i++){
- if(t[i].comando==2 || t[i].comando==3 || t[i].comando==4){
- tempoVentola=t[i].tempo;
- sommaVentola+=tempoVentola;
- }
- }
- //printf("SOMMA VENTOLA:%d\n",sommaVentola);
- return sommaVentola-sommaOff ;
- }
- void ordinamento(struct telecomando *t,int n){
- int i,j;
- struct telecomando temp; // temporaneo=temp
- char *s1, *s2;
- for(i=0;i<n;i++){
- for(j=0;j<n;j++){
- s1 = t[i].nomeComando;
- s2 = t[j].nomeComando;
- if(strcmp(s1,s2)<0){
- temp=t[i];
- t[i]=t[j];
- t[j]=temp;
- }
- if(strcmp(t[i].nomeComando,t[j].nomeComando)==0){
- if(t[i].tempo>t[j].tempo){
- temp=t[i];
- t[i]=t[j];
- t[j]=temp;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement