All pastes #223244 Raw Edit

Untitled

public text v1 · immutable
#223244 ·published 2006-10-26 20:35 UTC
rendered paste body
/*
 * csort.c
 * PHCalc 1.7, Pewlett-Hackard Calculator Application
 * Copyright (C) 2004-2005 by Magden Marron
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include "csort.h"

int main(void)
{
	/* Variable Declaration/Initialization */
	FILE *infile;
//	char list[MAX_LINES][STRINGLENGTH + 1] = {"dog", "cat", "cap", "zapgun", "rooster", "not", "even", "doom", "music"};
	char *list[MAX_LINES];
	char tempstr[STRINGLENGTH + 1];
	int i;
	char inchar;
	int currentline;
	int currentchar;
	long begin;
	long end;
	long benchtime;
	begin = time(NULL);
	infile = fopen(INFILENAME, "r");
	if (infile == NULL)
	{
		printf("File error.\n");
		return FALSE;
	}
	currentline = 0;
//	while (fscanf(infile, "%16s", list[currentline]) && currentline < MAX_LINES && !feof(infile))
	while (fscanf(infile, "%16s", tempstr) && currentline < MAX_LINES && !feof(infile))
	{
	//	printf("%s\n", list[currentline]);
		list[currentline] = tempstr;
		currentline++;
	}
	printf("currentline: %d", currentline);

	for (i = 0; i < currentline; i++)
	{
		padzero(list[i], STRINGLENGTH);
	}
	bubblesort(list, currentline);
	for (i = 0; i < currentline; i++)
	{
		printf("%s\n", list[i]);
	}
	end = time(NULL);
	benchtime = end - begin;
		printf("currentline: %d", currentline);
	printf("Program took %ld seconds to run.\n", benchtime);
}

void padzero(char instr[STRINGLENGTH + 1], int length)
{
	int current = strlen(instr);
	for (; current < STRINGLENGTH; current++)
	{
		instr[current] = ' ';
	}
	instr[STRINGLENGTH] = '\0';
}

void bubblesort(char *inlist[MAX_LINES], int size)
{
	int i, j;
	char *temp;
	int flipped = FALSE;
	for (i = size; i >= 0; i--)
	{
		for (j = 0; j < i; j++)
		{
			if (strcmp(inlist[j],inlist[j+1]) < 0)
			{
				temp = inlist[j];
				inlist[j] = inlist[j+1];
				inlist[j+1] = temp;
				flipped = TRUE;
			}
		}
		if (flipped == FALSE)
		{
			return;
		}
	}
}