Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <ctype.h>
#include <sys/types.h>
#include <io.h>
#include <time.h>
typedef struct
{
char name[30];
int position;
int file_choice;
}CSV;
typedef struct List
{
char city[50];
char zipcode[8];
char state[2];
double latitude;
double longitude;
long population;
struct List *next;
struct List *previous;
}List_node;
typedef struct
{
struct List *psHead;
struct List *psTail;
struct List psCurrent;
int node_count;
}List_Header;
typedef struct heap
{
int ipriority;
char city[50];
char state[2];
char zipcode[7];
double longitude;
double latitude;
long population;
}heap_node;
typedef struct
{
struct List *psHead;
struct List *psTail;
int node_count;
}Header;
int main()
{
//pointers
CSV *file_info;
heap_node *node_ptr, *heaptemp;
List_node *psnew, *pscurrent;
Header *psList;
List_Header *List;
//declarations of functions and variables
int choice;
int Main_Menu(int);
int Select_File(CSV *file_info, heap_node *node_ptr, heap_node *heaptemp, Header *psList,List_node *psnew, List_node *pscurrent, List_Header *List);
int Create_List(CSV *file_info, heap_node *node_ptr, heap_node *heaptemp, Header *psList,List_node *psnew, List_node *pscurrent, List_Header *List, FILE *readfile);
int Create_Heap(CSV *file_info, heap_node *node_ptr, heap_node *heaptemp, Header *psList,List_node *psnew, List_node *pscurrent, List_Header *List);
int ReHeapUp(heap_node *node_ptr, heap_node *heaptemp, Header *psList, int heap_count);
int ReHeapDown(heap_node *node_ptr, heap_node *heaptemp, Header *psList, int heap_count);
//Memory Allocation
file_info = (CSV*) malloc(sizeof(CSV));
node_ptr = (heap_node*) malloc(sizeof(heap_node)*600);///changed this
psnew = (List_node*) malloc(sizeof(List_node));
pscurrent = (List_node*) malloc(sizeof(List_node));
heaptemp = (heap_node*) malloc(sizeof(heap_node));
psList = (Header*) malloc(sizeof(Header));
List = (List_Header*) malloc(sizeof(List_Header));
while(choice != 6)
{
choice = Main_Menu(choice);
switch(choice)
{
case 1:
//Select File
Select_File(file_info, node_ptr,heaptemp, psList,psnew, pscurrent, List);
break;
case 2:
//Remove largest pop
printf("%s",&node_ptr[0].city);//print test
break;
case 3:
//display summary data
break;
case 4:
//save summary data
break;
case 5:
//Reset
break;
case 6:
//Exit
break;
}
}
return 0;
}
//
//Main_Menu prompts the user to choose one of the 6 options
//
int Main_Menu(int choice)
{
printf(" Heap Sort \n");
printf("\n*******************************************************");
printf("\n Please choose an option from the list below\n");
printf("\n 1. Select and read a .CSV file ");
printf("\n 2. Remove largest Population node from the Heap");
printf("\n 3. Display summary data");
printf("\n 4. Save summary data");
printf("\n 5. Reset data");
printf("\n 6. Exit\n");
printf("\n Choose an option by number<1-6>");
scanf("%d",&choice);
system("cls");
return choice;
}/*end Main_menu()*/
int Select_File(CSV *file_info, heap_node *node_ptr, heap_node *heaptemp, Header *psList,List_node *psnew, List_node *pscurrent, List_Header *List)
{
struct _finddata_t c_file;
long hFile;
char path_name[256];
int counter = 2;
FILE *readfile;
/* Finds first file in current directory */
if( (hFile = _findfirst( "*.csv", &c_file )) == -1L )
{
printf( "No *.csv files in current directory!\n" );
printf("Enter full path and file name");
scanf("%s",&path_name);
}//end if
else
{
printf( "Listing of .csv files in current directory\n\n" );
printf( "******************************************");
printf( "\nFilename \n", ' ' );
printf( "------ \n", ' ' );
printf("1. %-12s\n", c_file.name);
file_info[1].position = 1;
strcpy(file_info[1].name, c_file.name);
/* Find the rest of the files */
while( _findnext( hFile, &c_file ) == 0 )
{
printf("%d. %-12s\n", counter, c_file.name);
file_info[counter].position = counter;
strcpy(file_info[counter].name, c_file.name);
counter++;
}//end while
printf("%d. Enter a full path and file name", counter);
_findclose( hFile );
}//end else
scanf("%d",&file_info->file_choice);
system("cls");
if(file_info->file_choice == counter)
{
printf("Enter full path and file name");
scanf("%s",&path_name);
}//end if
else
{
strcpy(path_name, file_info[file_info->file_choice].name);
}
readfile = fopen(path_name,"r");
if(readfile == NULL)
{
printf("\nfile could not be opened\n\n");
main();
}
else
{
printf("\nLoading\n");
Create_List(file_info, node_ptr, heaptemp, psList,psnew, pscurrent, List, readfile);
Create_Heap(file_info, node_ptr, heaptemp, psList,psnew, pscurrent, List);
system("cls");
}
return 0;
}
//
//
//Create_List creates a linked list
//that is then used to create a binary tree
int Create_List(CSV *file_info, heap_node *node_ptr, heap_node *heaptemp, Header *psList,List_node *psnew, List_node *pscurrent, List_Header *List, FILE *readfile)
{
char line[100], long_temp[25], lat_temp[25], pop_temp[25];
int z = 0;
printf("z = %d",z);
while ( fgets(line,100,readfile) != NULL )
{
psnew = malloc(sizeof(struct List));
strcpy(psnew->city, strtok(line,","));
strcpy(psnew->state, strtok(NULL,","));
strcpy(psnew->zipcode, strtok(NULL,","));
strcpy(long_temp, strtok(NULL,","));
psnew->longitude = atof(long_temp);
strcpy(lat_temp, strtok(NULL,","));
psnew->latitude = atof(lat_temp);
strcpy(pop_temp, strtok(NULL,","));
psnew->population = atol(pop_temp);
if (z == 0)
{
List->psHead = psnew;
psnew->previous = NULL;
psnew->next = pscurrent;
pscurrent->previous = psnew;
}
else
{
psnew->next = pscurrent;
psnew->previous = pscurrent->previous;
psnew->next->previous = psnew;
psnew->previous->next = psnew;
}
z++;//node_count was initialized to 0
//printf("%d",&node_count);
}
List->psTail = psnew;
List->node_count = z;
fclose(readfile);
//strcpy(data->time, asctime(localtime(&timer)));
//system("cls");
return 0;
}
//
int Create_Heap(CSV *file_info, heap_node *node_ptr, heap_node *heaptemp, Header *psList,List_node *psnew, List_node *pscurrent, List_Header *List)
{
int x, y = 0,counter, heap_count = 0;
pscurrent = List->psHead;
counter = List->node_count;
//heap_count is number of nodes currently in heap
printf("%d",&counter);
while(counter != 0)//goes until no nodes are left
{
//node_ptr = realloc(node_ptr, sizeof(heap_node)*16);//is this right
for(x = 0;x<16;x++)
{
if(psList->node_count == 0)//if no nodes exist
{
node_ptr->ipriority = 0;
strcpy(node_ptr[0].city, pscurrent->city);
strcpy(node_ptr[0].zipcode, pscurrent->zipcode);
strcpy(node_ptr[0].state, pscurrent->state);
node_ptr[0].latitude = pscurrent->latitude;
node_ptr[0].longitude = pscurrent->longitude;
node_ptr[0].population = pscurrent->population;
}
else
{
node_ptr->ipriority = heap_count;
strcpy(node_ptr[heap_count].city, pscurrent->city);
strcpy(node_ptr[heap_count].zipcode, pscurrent->zipcode);
strcpy(node_ptr[heap_count].state, pscurrent->state);
node_ptr[heap_count].latitude = pscurrent->latitude;
node_ptr[heap_count].longitude = pscurrent->longitude;
node_ptr[heap_count].population = pscurrent->population;
ReHeapUp(node_ptr,heaptemp, psList, heap_count);
}
pscurrent = pscurrent->next;
free(pscurrent->previous);
counter--;
heap_count++;
}
}
system("cls");
return 0;
}
//
//
//
int ReHeapUp(heap_node *node_ptr, heap_node *heaptemp, Header *psList, int heap_count)
{
int x;
for(x = heap_count;x > 0;x--)
{
if(node_ptr[x].population > node_ptr[(x-1)/2].population)
{
//if child is bigger than parent
strcpy(heaptemp->city, node_ptr[x].city);
strcpy(node_ptr[x].city, node_ptr[(x-1)/2].city);
strcpy(node_ptr[(x-1)/2].city, heaptemp->city);
strcpy(heaptemp->zipcode, node_ptr[x].zipcode);
strcpy(node_ptr[x].zipcode, node_ptr[(x-1)/2].zipcode);
strcpy(node_ptr[(x-1)/2].zipcode, heaptemp->zipcode);
strcpy(heaptemp->state, node_ptr[x].state);
strcpy(node_ptr[x].state, node_ptr[(x-1)/2].state);
strcpy(node_ptr[(x-1)/2].state, heaptemp->state);
heaptemp->latitude = node_ptr[(x-1)/2].latitude;
node_ptr[(x-1)/2].latitude = node_ptr[x].latitude;
node_ptr[x].latitude = heaptemp->latitude;
heaptemp->longitude = node_ptr[(x-1)/2].longitude;
node_ptr[(x-1)/2].longitude = node_ptr[x].longitude;
node_ptr[x].longitude = heaptemp->longitude;
heaptemp->population = node_ptr[(x-1)/2].population;
node_ptr[(x-1)/2].population = node_ptr[x].population;
node_ptr[x].population = heaptemp->population;
heaptemp->ipriority = node_ptr[(x-1)/2].ipriority;
node_ptr[(x-1)/2].ipriority = node_ptr[x].ipriority;
node_ptr[x].ipriority = heaptemp->ipriority;
}
}
return 0;
}
//
//
//
int ReHeapDown(heap_node *node_ptr, heap_node *heaptemp, Header *psList, int heap_count)
{
return 0;
}