Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define MAX_CIT 32
- #define MAX_LEN 32
- int get_data(FILE *, char (*)[MAX_LEN], int (*)[2]);
- int give_data(int, char (*)[MAX_LEN], int (*)[2], char *, int);
- /* WARNING : this program is designed to be case INsensitive */
- main(){
- int temp[MAX_CIT][2], n_cities, asked_temp;
- char cities[MAX_CIT][MAX_LEN], selection[MAX_LEN];
- FILE * fp;
- ((fp=fopen("file.txt", "r"))!=NULL)?:(exit(1));
- n_cities=get_data(fp, cities, temp);
- puts("Hi user, gimme a city, I'll give you min temp!");
- scanf("%s", selection);
- if((asked_temp=give_data(n_cities, cities, temp, selection, 0))!=-98)
- 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 */
- else{
- puts("Error getting data!");
- exit(1);
- }
- fclose(fp);
- return 0;
- }
- int get_data(FILE * fpPtr, char (*citiesPtr)[MAX_LEN], int (*tempPtr)[2]){
- int ci=0, i;
- while(feof(fpPtr)==0){
- fscanf(fpPtr, "%s %d %d", citiesPtr[ci], &tempPtr[ci][0], &tempPtr[ci][1]);
- for( i=0; i<strlen(citiesPtr[ci]); i++ )
- citiesPtr[ci][i]=tolower(citiesPtr[ci][i]);
- ci++;
- }
- return ci; /* return number of cities */
- }
- int give_data(int n_cities, char (*citiesPtr)[MAX_LEN], int (*tempPtr)[2], char *selectionPtr, int whattemp){
- int get_city_index(char *, char (*)[MAX_LEN], int);
- int city_index, i;
- for( i=0; i<strlen(selectionPtr); i++ )
- selectionPtr[i]=tolower(selectionPtr[i]);
- if((city_index=get_city_index(selectionPtr, citiesPtr, n_cities))!=-99)
- return tempPtr[city_index][whattemp];
- else
- return -98;
- }
- int get_city_index(char *selectionPtr, char (*citiesPtr)[MAX_LEN], int n_cities){
- int i;
- for(i=0;i<n_cities;i++){
- if((strcmp(selectionPtr, citiesPtr[i]))==0)
- break;
- }
- (i<n_cities)?:(i=-99);
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement