Advertisement
Mazamin

Incompleto Esercizio Temperature

Dec 17th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #define MAX_CIT 32
  5. #define MAX_LEN 32
  6.  
  7. int get_data(FILE *, char (*)[MAX_LEN], int (*)[2]);
  8. int give_data(int, char (*)[MAX_LEN], int (*)[2], char *, int);
  9. /* WARNING : this program is designed to be case INsensitive */
  10. main(){
  11.     int temp[MAX_CIT][2], n_cities, asked_temp;
  12.     char cities[MAX_CIT][MAX_LEN], selection[MAX_LEN];
  13.     FILE * fp;
  14.     ((fp=fopen("file.txt", "r"))!=NULL)?:(exit(1));
  15.     n_cities=get_data(fp, cities, temp);
  16.     puts("Hi user, gimme a city, I'll give you min temp!");
  17.     scanf("%s", selection);
  18.     if((asked_temp=give_data(n_cities, cities, temp, selection, 0))!=-98)
  19.         printf("%d", asked_temp); /* the function give_data will return an int, and the 4th value passed is 0 or 1, representing min and max */
  20.     else{
  21.         puts("Error getting data!");
  22.         exit(1);
  23.     }
  24.     fclose(fp);
  25.     return 0;
  26. }
  27.  
  28. int get_data(FILE * fpPtr, char (*citiesPtr)[MAX_LEN], int (*tempPtr)[2]){
  29.     int ci=0, i;
  30.     while(feof(fpPtr)==0){
  31.         fscanf(fpPtr, "%s %d %d", citiesPtr[ci], &tempPtr[ci][0], &tempPtr[ci][1]);
  32.         for( i=0; i<strlen(citiesPtr[ci]); i++ )
  33.             citiesPtr[ci][i]=tolower(citiesPtr[ci][i]);
  34.         ci++;
  35.     }
  36.     return ci; /* return number of cities */
  37. }
  38.  
  39. int give_data(int n_cities, char (*citiesPtr)[MAX_LEN], int (*tempPtr)[2], char *selectionPtr, int whattemp){
  40.     int get_city_index(char *, char (*)[MAX_LEN], int);
  41.     int city_index, i;
  42.     for( i=0; i<strlen(selectionPtr); i++ )
  43.         selectionPtr[i]=tolower(selectionPtr[i]);
  44.     if((city_index=get_city_index(selectionPtr, citiesPtr, n_cities))!=-99)
  45.         return tempPtr[city_index][whattemp];
  46.     else
  47.         return -98;
  48. }
  49.  
  50. int get_city_index(char *selectionPtr, char (*citiesPtr)[MAX_LEN], int n_cities){
  51.     int i;
  52.     for(i=0;i<n_cities;i++){
  53.         if((strcmp(selectionPtr, citiesPtr[i]))==0)
  54.             break;
  55.     }
  56.     (i<n_cities)?:(i=-99);
  57.     return i;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement