#include <stdio.h>
#include <string.h>
#define MAXSTR 100
int income_lev, year; // to record which year and income level does user choice//
int country_num = 0, data_num = 0;
struct Country
{
char country_name[52];
char income_level[8];
char region[27];
struct Data
{
char year[6];
char percent[5];
} data[34];
} country[200];
void user_choice()// let user to choose the year and income level they want//
{
printf("Please enter a year which you want to search:\n");
scanf("%d", &year);
printf("Please enter an income level which you want to search\n\
1 corresponds to 'low income'\n\
2 corresponds to 'lower middle income'\n\
3 corresponds to 'upper middle income'\n\
4 corresponds to 'high income'\n");
scanf("%d", &income_lev);
}
void record_data(char line[100])
{
int i;
for(i=0; i<51; i++)
{
country[country_num].country_name[i] = line[i];
}
for(i=51; i<58; i++)
{
country[country_num].income_level[i - 51] = line[i];
}
for(i=59; i<62; i++)
{
country[country_num].data[data_num].percent[i - 59] = line[i];
}
for(i=62; i<87; i++)
{
country[country_num].region[i - 62] = line[i];
}
for(i=88; i<92; i++)
{
country[country_num].data[data_num].year[i-88] = line[i];
}
if (strcmp(country[country_num].country_name, country[country_num - 1].country_name) != 0 || country_num == 0)
{
country_num += 1;
data_num = 0;
//printf("C:%d\n",country_num);
}
else
{
data_num += 1;
//printf("D:%d\n",data_num);
}
}
int main ()
{
FILE *fp;
char str[MAXSTR];
int i=1;
fp = fopen("measles.txt" , "r");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}
while( fgets (str, MAXSTR, fp)!=NULL )
{
//printf("%s", str);
record_data(str);
}
fclose(fp);
//user_choice();
/*for(i=0; i<200; i++)
{
puts(country[i].country_name);
puts(country[i].region);
puts(country[i].income_level);
puts(country[i].data[1].year);
puts(country[i].data[1].percent);
}*/
for(i=0; i<33; i++)
{
printf("%s %s\n", country[0].data[2].year, country[0].data[2].percent);
}
return(0);
}
|