Skip to content

Instantly share code, notes, and snippets.

@foysalit
Created July 4, 2013 12:49
Show Gist options
  • Save foysalit/5927430 to your computer and use it in GitHub Desktop.
Save foysalit/5927430 to your computer and use it in GitHub Desktop.
finding latest destination script in c.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i, distance, cost = 0;
char destination[20], type, line[100], max_line[100];
char date[10];
int match = 0;
char day[10];
char month[10];
char year[10];
int max_year = 0;
int max_month = 0;
int max_day = 0;
int max_line_marker = 0;
FILE *f= fopen("bin/Debug/test.txt", "r");
if(f!=0){
//printf("reading file\n");
while(fgets(line, 100, f)!=NULL){
//printf("reading line %d\n", i);
sscanf(line, "%s %s %d %c", destination, date, &distance, &type);
//strcpy(max_line, line);
//printf("reading line %s\n\n", max_line);
match = strcmp("torino", destination);
if(match == 0){
//printf("%s", date);
strncpy(year, date+6, 4);
year[4] = '\0';
strncpy(month, date+3, 2);
month[2] = '\0';
strncpy(day, date, 2);
day[2] = '\0';
if(atoi(year) > max_year){
max_year = atoi(year);
max_month = atoi(month);
max_day = atoi(day);
max_line_marker = i;
strcpy(max_line, line);
}else if(atoi(year) == max_year){
if(atoi(month) > max_month){
max_month = atoi(month);
max_day = atoi(day);
max_line_marker = i;
strcpy(max_line, line);
}else if(atoi(month) == max_month){
if(atoi(day) > max_day){
max_day = atoi(day);
max_line_marker = i;
strcpy(max_line, line);
}
}
}
}
//increasing the counter value to keep track of the lines read
i++;
}
}
printf("MAX LINE REFERENCE: %d\n\n", max_line_marker);
//printf("MAX DATE: %s\n", date[max_line_marker]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment