Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-13-2005, 07:23 PM   PM User | #1
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
help writing a race results program in C

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:

Code:
#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!

Last edited by reneeccski; 01-14-2005 at 06:20 AM..
reneeccski is offline   Reply With Quote
Old 01-13-2005, 08:27 PM   PM User | #2
Jason
Regular Coder

 
Join Date: Feb 2003
Location: California
Posts: 925
Thanks: 0
Thanked 0 Times in 0 Posts
Jason is an unknown quantity at this point
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
Jason is offline   Reply With Quote
Old 01-14-2005, 04:57 AM   PM User | #3
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
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.

Last edited by reneeccski; 01-14-2005 at 05:14 AM..
reneeccski is offline   Reply With Quote
Old 01-14-2005, 06:48 PM   PM User | #4
Dr. Evil
New Coder

 
Join Date: Nov 2004
Location: Netherlands
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Dr. Evil is an unknown quantity at this point
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.

Code:
#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;
}
Dr. Evil is offline   Reply With Quote
Old 01-14-2005, 06:52 PM   PM User | #5
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
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.

Last edited by reneeccski; 01-14-2005 at 06:55 PM..
reneeccski is offline   Reply With Quote
Old 01-14-2005, 07:08 PM   PM User | #6
Dr. Evil
New Coder

 
Join Date: Nov 2004
Location: Netherlands
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Dr. Evil is an unknown quantity at this point
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.
Dr. Evil is offline   Reply With Quote
Old 01-15-2005, 08:02 PM   PM User | #7
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
Cool. Thanks.
reneeccski is offline   Reply With Quote
Old 01-15-2005, 10:07 PM   PM User | #8
aman
Regular Coder

 
Join Date: Oct 2004
Posts: 230
Thanks: 0
Thanked 0 Times in 0 Posts
aman is an unknown quantity at this point
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).
Code:
#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;
}
aman is offline   Reply With Quote
Old 01-16-2005, 07:54 AM   PM User | #9
reneeccski
New Coder

 
Join Date: Sep 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
reneeccski is an unknown quantity at this point
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.
reneeccski is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:47 AM.


Advertisement
Log in to turn off these ads.