Jump to content

List exercise

- - - - -

  • Please log in to reply
4 replies to this topic

#1
francesco

francesco

    Newbie

  • Members
  • Pip
  • 2 posts
Hello everyone, I am new to the forum, I joined because I have a problem, that is:
Given a text file containing data on N weather stations, I have to load the file into a list, and up to here, okay. Can not create a function able to calculate what the weather station that has recorded the largest change in temperature. I enter both the code and the text file:


/*inclusione delle librerie*/

#include <stdio.h>

#include <malloc.h>


/*definizione del tipo di dato per memorizzare i dati della stazione meteo*/

typedef struct dati_meteo

{

  int id;

  int giorno;

  double umidita;

  int temperatura;

} dati_meteo_t;


/*definizione del tipo per l'elemento della lista*/

typedef struct elem_lista

{

  dati_meteo_t dati;

  struct elem_lista *succ_p;

} elem_lista_t;


/*definizione lista*/

// typedef elem_lista_t * listaDati;


/*dichiarazioni delle funzioni*/

void caricaDati (char nomefile[]);

void inserisciDati (elem_lista_t **, dati_meteo_t);

void visualizzaDati (elem_lista_t *);



int main(void)

{

char nomefile[20];

char scelta;


printf("_______________________\n");

printf("|       METEO STATS    |\n");

printf("|           menu':     |\n");

printf("|______________________|\n");

/*ciclo per la ripetizione del menu'*/

do

{

printf("digita:\n");

printf("\t1> per caricare il file di testo\n");

printf("\t2> quale stazione meteo ha registrato la piu' grande variazione di temperatura nel mese corrente\n");

printf("\t3> calcola quale stazione meteo ha registrato la piu' alta umidita' media nel mese corrente\n");

printf("\t0> uscita\n\n");

printf("\t> ");

scanf(" %c",&scelta);

switch(scelta) /*applicare la scelta fatta*/

	{

	case '1': /*carica file di testo*/

	printf("\n\tinserisci il nome del file di testo (.txt) da caricare: ");

	scanf ("%s", nomefile);

	caricaDati (nomefile);

	

	break;

	

	case '2':

	printf("\n\tla stazione....");

	break;

	

	case '3':

	printf("\n\tla stazione....");

	break;

	

	case '0':

	printf("\n\tuscita\n");

	break;

	

	default:

	printf("\n\terrore! scelta non consentita\n");

	}

} while (scelta != '0');

return(0);

}


/*definizione della funzione caricaDati*/

void caricaDati (char nomefile[20])

{

FILE *file_dati;

dati_meteo_t temp;

int contatore = 0;

elem_lista_t *testa_p = NULL;


file_dati = fopen (nomefile, "r");


if ((file_dati = fopen(nomefile, "r")) == NULL)

{

  printf("\nNon posso aprire il file %s", nomefile);

}

else{

while( fscanf (file_dati,"%d%d%lf%d",&temp.id,&temp.giorno,&temp.umidita,&temp.temperatura) != EOF )

    { inserisciDati (&testa_p, temp);

      contatore ++;  

    }


	printf ("\n%d registrazioni stazioni meteo caricate", contatore);

	visualizzaDati(testa_p);

	}

fclose (file_dati);

}


/*definizione funzione inserisciDati*/

void inserisciDati (elem_lista_t **testa_p, dati_meteo_t dati)

{

elem_lista_t *corr_p,

             *prec_p,

             *nuovo_p;

             

for (corr_p = prec_p = *testa_p;

    ((corr_p != NULL) && (corr_p->dati.id == dati.id) && (corr_p->dati.temperatura > dati.temperatura));

    prec_p = corr_p, corr_p = corr_p->succ_p);

    {

       nuovo_p = (elem_lista_t *)malloc(sizeof(elem_lista_t));

       nuovo_p->dati = dati;

       nuovo_p->succ_p = corr_p;

       if (corr_p == *testa_p)

       *testa_p = nuovo_p;

       else

       prec_p->succ_p = nuovo_p; 

       }            

} 


void visualizzaDati (elem_lista_t *testa_p) {

elem_lista_t * temp;

temp = testa_p;


if (temp == NULL) printf ("\nla lista e' vuota!");

    else printf ("\n *** VISUALIZZO dati *** \n\n");

while (temp != NULL) {

       printf ("%d %d %lf %d\n",

                       temp->dati.id,

                       temp->dati.giorno,

                       temp->dati.umidita,

                       temp->dati.temperatura);

       temp = temp->succ_p;

       }

}


Text file is like this:

ID Day Hygr Temp
1435 09 0.48 30
1435 21 0.76 24
1435 28 0.32 22
2307 03 0.54 19
2307 14 0.82 14
0081 18 0.46 30
0081 21 0.86 34


Bella!

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts

file_dati = fopen (nomefile, "r");


if ((file_dati = fopen(nomefile, "r")) == NULL)

 
That is attempting to open the same file twice. Delete the first call to fopen().

Other than that, I don't know. You shuld translate into English so that people can make useful suggestions and help you better.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
francesco

francesco

    Newbie

  • Members
  • Pip
  • 2 posts
here is the code with function names and all "translated" into English (Sorry, i don't speak/write English very well)

/* Nome programma: weather_station.c                    */

/* Reads a text file where each line is associated with the following information: */
/* ID:   id of the  station                     */
/* DAY:  day of the month                       */
/* HYGR: real number that stores the moisture   */
/* TEMP: an integer that stores the temperature */


#include<stdio.h>
#include<malloc.h>


/************************************************
   define the types
*************************************************/

typedef struct station{
  int id;
  int day;
  double hygr;
  int temp;
}station_t;

typedef struct list_item {
  station_t data;
  struct list_item *next_p;
}list_item_t;


void insert_data (list_item_t **, station_t);
void load_data (list_item_t **, char filename[]);
int display_menu(void);
int display_report(list_item_t *head_p);



int main(void)
{
  char filename[20];
  list_item_t *head_p = NULL;
do
{
  switch( display_menu() )
    {
    case '1': /* Load data file */
      printf("\nInsert text file name: ");
      scanf("%s", filename);
      load_data(&head_p,filename);
      display_report(head_p);
      break;
    case '2':
      printf("\nStation that recorded the largest temperature change in the current month:\n");
      break;
    case'3':
      printf("\nStation that recorded the highest average humidity:\n");
      break;
    case '0':
      printf("\nCiao e grazie per aver usato questo programma!!\n");
      break;
    default:
      printf("\nChoice is not allowed!");
      }
} while (display_menu() != '0');
  return(0);
}


/*********************************************************************/
/* Funzione: insert_data()                                           */
/* Scopo: Insert the data file read from files in the data structure */
/*********************************************************************/


void insert_data(list_item_t **head_p, station_t data)
{
  list_item_t *current_p,
    *prev_p,
    *new_p;
             
  for (current_p = prev_p = *head_p;
       ((current_p != NULL) && (current_p->data.id == data.id) && (current_p->data.temp > data.temp));
       prev_p = current_p, current_p = current_p->next_p);
  {
    new_p = (list_item_t *)malloc(sizeof(list_item_t));
    new_p->data = data;
    new_p->next_p = current_p;
    if (current_p == *head_p)
      *head_p = new_p;
    else
      prev_p->next_p = new_p; 
  }            
} 

/*************************************************************************/
/* Funzione: load_data()                                                 */
/* Scopo: Read data from file and passes them to function: insert_data() */
/*************************************************************************/

void load_data (list_item_t **head_p, char filename[20])
{
  FILE *datafile;
  station_t a;
  int ctr = 0;
  
  datafile = fopen (filename, "r");
 
  if (datafile == NULL)
    {
      printf("\nCannot open file %s", filename);
    }
  else{
    while( fscanf (datafile,"%d%d%lf%d",&a.id,&a.day,&a.hygr,&a.temp) != EOF )
      { insert_data (head_p, a);
	ctr ++;  
      }
    
    printf ("\n%d weather station recorded", ctr);
  }
  fclose (datafile);
}


int display_menu(void)
{
  char ch, buf[20];
  
  printf("\n");
  printf("\n1. Enter the name of the file");
  printf("\n2. Show the station that recorded the largest temperature change in the current month");
  printf("\n3. Show the station that recorded the highest average humidity:");
  printf("\n0. Exit");
  gets(buf);
  ch = *buf;
  return(ch);
}

/******************************/
/* Function: display_report() */
/******************************/

int display_report(list_item_t *head_p)
{
  list_item_t * a;
  a = head_p;
  
  if (a == NULL) printf ("\nEmpty list!");
  else printf ("\n *** Show report *** \n\n");
  while (a != NULL) {
    printf ("%d %d %lf %d\n",
	    a->data.id,
	    a->data.day,
	    a->data.hygr,
	    a->data.temp);
    a = a->next_p;
  }
}

Thank you Ancient Dragon for fopen function. The problem is the same as the first post.

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Thanks for the translation -- you did a great job :)

If you wrote all that good code, why can't you write the function that calculates what the weather station that has recorded the largest change in temperature ? Its not all that difficult. Search the linked list for the high and low temps for each station.

struct temp_range

{

   int id;

   int highTemp;

   int lowTemp;

};


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
This is all there is to it. The code below is not complete (on purpose) because it doesn't tell you which station had the greatest range of temperatures. All it does is calculate the low and high temp for each station.

void GetTempRange(list_item_t *head_p)

{

#define BLOCKSIZE 10

    list_item_t* curr;

    int i = 0;

    int nSize = BLOCKSIZE;

    int nItems = 0;

    temp_range_t* items = malloc(sizeof(temp_range_t) * nSize);

    for(curr = head_p; curr != NULL; curr = curr->next_p)

    {

        // search array items for the station id

        int found = 0;

        for(i = 0; i < nItems; i++)

        {

            if( items[i].id == curr->data.id )

            {

                found = 1;

                break;

            }

        }

        if( found == 0) // if not found

        {

            if( nItems == nSize)

            {

                nSize += BLOCKSIZE;

                items = realloc(items, sizeof(temp_range_t) * nSize);

            }

            items[nItems].id = curr->data.id;

            items[nItems].highTemp = curr->data.temp;

            items[nItems].lowTemp = curr->data.temp;

            ++nItems;

        }

        else

        {

            if( curr->data.temp > items[i].highTemp )

                items[i].highTemp = curr->data.temp;

            if( curr->data.temp < items[i].lowTemp )

                items[i].lowTemp = curr->data.temp;

        }


    }

    for(i = 0; i < nItems; i++)

        printf("%d %d %d\n", items[i].id, items[i].highTemp, items[i].lowTemp);

}


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users