/* * 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 #include #include #include #include #include "csort.h" int main(void) { /* Variable Declaration/Initialization */ FILE *infile; char *list[MAX_LINES]; char strings[MAX_LINES][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", strings[currentline]) && currentline < MAX_LINES && !feof(infile)) { list[currentline] = strings[currentline]; 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("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; } } }