...

View Full Version : help writing a race results program in C



reneeccski
01-13-2005, 08:23 PM
I'm a high school Nordic ski coach and I'm trying to write a program in C that will read results from a file, sort them, and print the results to another file. I'm having trouble understanding how to deal with times. I have looked up a bunch of stuff on the time.h library but can't make sense of how to declare the time variables. The input file will look like this:

Johnny Skier 08:00 20:00 Schoolname

With the first number being start time and the second number being fiinsh time.

The results should look like this:

Johnny Skier 12:00 Schoolname

With the 12:00 being elapsed time.

I think I also want the sorted list to be numbered, but that's a minor detail. The main problem I am having is with the time variables.

So far, this is my work in progress:


#include <stdio.h>
#include <time.h>

int main()
{

time_t start, finish;
char a[100], fname[15], lname[15], school[15];
double start, finish, elapse;

FILE*infile;
FILE*outfile;

infile = (fopen "prelim.dat", "r");
outfile = (fopen "final.dat", "w");

while (NULL != fgets(a, 100, infile));

elapse = difftime (finish, start);

/*insert sorting algorithm here*/

fprintf ("%s %s %f %s\n", fname, lname, &elapse, school);

return 0;
} /*end main*/

Can anyone help with the time stuff? Thanks!

Jason
01-13-2005, 09:27 PM
small note, in your print statement your trying to print strings when your variables are character arrays, you might have a small problem with that.

since this sounds like an HW assignment Im only going to offer advice here. You should focus on pulling out the start time and finnish time from the array a. Anothering you might look at is if a is only 100 chars long what happens if the firstname is the full 115 chars you allow it to be? some things for you to look at and work on.


Jason

reneeccski
01-14-2005, 05:57 AM
Thanks. I will play around with those suggestions. Actually, this is not a homework assignment. It is purely to help my conference with scoring ski meets. I took an intro cs class last fall but now I'm taking a Linux course, MS Server 2003 1, and Project Management. I'm a networking student, not a programming student.

Dr. Evil
01-14-2005, 07:48 PM
Well, I don't want to be a spoil sport, but considering it's not a homework assignment and I'm not posting it all for you, here's what I came up with. You still have to write the sorting and output parts, and it's not the most streamlined code in the world, but it works.

Jason: Strings in C are, essentially, character arrays.



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <float.h>

#define MAX_SKIERS 10

typedef struct
{
char name[50];
float time;
char school[20];
} SKIER_INFO;

int main(int argc, char *argv[])
{
FILE *in;
SKIER_INFO skiers[MAX_SKIERS];
char tmp[100], tmp2[50], *pt1, *pt2;
int i, j;
float time1, time2;

if(argc != 2) {puts("Invalid argument number"); return 1;}

in = fopen(argv[1], "rb");
if(in == NULL) {puts("Error whilst opening file"); return 1;}

for(i=0; i<MAX_SKIERS; i++)
{
strcpy(skiers[i].name, "");
skiers[i].time = 0;
strcpy(skiers[i].school, "");
}
for(i=0; !feof(in); i++)
{
fgets(tmp, 100, in);

pt1 = &tmp[0];
pt2 = &(skiers[i].name[0]);
for(; ((*pt1 < 48) || (*pt1 > 57)); pt1++, pt2++) *pt2 = *pt1;
pt2--;
*pt2 = 0;

pt2 = &tmp2[0];
for(; ((*pt1 != ':') && (*pt1 != '.')); pt1++, pt2++) *pt2 = *pt1;
pt1++;
*pt2 = '.';
pt2++;
for(; *pt1 != ' '; pt1++, pt2++) *pt2 = *pt1;
*pt2 = 0;
time1 = atof(tmp2);

pt1++;
pt2 = &tmp2[0];
for(; ((*pt1 != ':') && (*pt1 != '.')); pt1++, pt2++) *pt2 = *pt1;
pt1++;
*pt2 = '.';
pt2++;
for(; *pt1 != ' '; pt1++, pt2++) *pt2 = *pt1;
*pt2 = 0;
time2 = atof(tmp2);
skiers[i].time = time2 - time1;

pt1++;
pt2 = &(skiers[i].school[0]);
for(; ((*pt1 != 0) && (*pt1 != '\r') && (*pt1 != '\n')); pt1++, pt2++) *pt2 = *pt1;
*pt2 = 0;
}

fclose(in);

for(j=0; j<i; j++)
{
printf("%s %f %s\n", skiers[j].name, skiers[j].time, skiers[j].school);
}

return 0;
}

reneeccski
01-14-2005, 07:52 PM
Wow. I would have never come up with all that! I'm still learning. Thanks so much. I'm going to have to break this down for myself to understand the what and why of what you've written. So don't worry, I'm not just going to blindly copy and paste!

I do have a question for you. Since there is going to be a space between the skiers' first and last names, does that need to be two separate strings? We did an example in class last semester with batting averages of famous baseball players and we used a string for first and last name separately.

Dr. Evil
01-14-2005, 08:08 PM
Not with this. It basically adds everything to the name string until it comes to a number, so as long as you don't have any numbers in the name, it should work fine.

reneeccski
01-15-2005, 09:02 PM
Cool. Thanks.

aman
01-15-2005, 11:07 PM
That code is really hard for someone else to figure out what's going on. I'd suggest writing it easier to understand (and debug if necessary).


#include <stdlib.h>

#define MAX_SKIERS 100

typedef struct
{
char first_name[32];
char last_name[32];
char start_time[8];
char end_time[8];
char school[64];
float total_time;
} SKIER_INFO;


// sort order = fastest times first
int compare_time( const void *arg1, const void *arg2 )
{
return ((SKIER_INFO*)arg1)->total_time > ((SKIER_INFO*)arg2)->total_time;
}

// convert string to float, replace ':' with '.' if necessary
float cstf( char *string )
{
char *ptr;

if( (ptr =strchr(string, ':')) != NULL )
*ptr = '.';

return (float)atof(string);
}

int main()
{
FILE *in, *out;
SKIER_INFO skiers[MAX_SKIERS];
int i=0, j=0;

if( (in = fopen("prelim.txt", "r")) == NULL ) {
perror("prelim.txt");
return 1;
}
if( (out = fopen("final.txt", "w")) == NULL ) {
perror("final.txt");
return 1;
}


memset(skiers, 0, MAX_SKIERS*sizeof(SKIER_INFO));

while (i < MAX_SKIERS && fscanf(in, "%s %s %s %s %s \n", skiers[i].first_name, skiers[i].last_name,
skiers[i].start_time, skiers[i].end_time, skiers[i].school) != EOF)
{
skiers[i].total_time = cstf(skiers[i].end_time) - cstf(skiers[i].start_time);
i++;
}
fclose(in);

qsort(skiers, i, sizeof(SKIER_INFO), compare_time);

for(j=0; j<i; j++)
{
fprintf(out, "%s %s %.02f %s\n", skiers[j].first_name, skiers[j].last_name, skiers[j].total_time, skiers[j].school);
}
fclose(out);

return 0;
}

reneeccski
01-16-2005, 08:54 AM
Yes, this is much easier to understand and looks more similar to other stuff I've done. Thank you. I am just working on debugging the program now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum