Code:
/*
* File: main.c
* Author: eef8
*
* Created on 06 December 2011, 15:53
*/
#include <stdio.h>
#include <stdlib.h>
#include "structs.h"
#include "navigation.h"
/*
*
*/
extern int errno;
/* Here, I need to create the linked list, so that the data for each ship
is stored in a new instance of the array. */
typedef struct shipInfo item;
int main(int argc, char** argv) {
char text[500]; /* Create a character array that will store all of the text in the file */
char line[100]; /* Create a character array to store each line individually */
dateTime *dates = (dateTime *) malloc (6 * sizeof (int));
FILE *file; /* Create a pointer to the file which will be loaded, to allow access to it */
char fileName[30]; /* Create a character array to store the name of the file the user want to load */
printf("Enter the name of the file containing ship information: ");
scanf("%s", fileName);
/*Try to open the file specified by the user. Use error handling if file cannot be found*/
file = fopen(fileName, "r"); /* Open the file specified by the user in 'read' mode*/
char lineBuffer[100];
if(file == NULL){
perror("The following error occurred: ");
printf("Value of errno: %d\n", errno);
}
else {
/* Read in the date and time of the ship information */
dates = calloc(1, sizeof(dateTime));
fscanf(file, "%d %d %d %d %d %d", &dates->day, &dates->month, &dates->year, &dates->hour, &dates->minute, &dates->second);
printf("File loaded for %d/%d/%d\n", dates->day, dates->month, dates->year);
/* Display a message to let the user know
* that the file has been loaded properly.
* Need to use fscanf to read the information in from the file */
item *current, *head;
int i;
head = NULL;
while(!feof(file)){ /* Will need to give i some proper value (not 10)
* that will be correct depending of the number of
* ships in a file- maybe read how many lines there are
* in the file, to find out how many ships there will be */
current = (item *)malloc(sizeof(item));
fscanf(file, "%s %f %f %f %f\n", ¤t->AISID, ¤t->latitude, ¤t->longitude, ¤t->direction, ¤t->speed);
current->nextShip = head;
head = current;
}
current = head;
/* Print out the contents of the array for each ship, to check that the
data is being stored correctly */
while(current!=NULL){
printf("%s\n%f\n%f\n%f\n%f\n", current->AISID, current->latitude, current->longitude, current->direction, current->speed);
current = current->nextShip;
}
/*Now ask the user to provide a duration, for which the program should simulate*/
float duration, timeStep;
printf("Please enter the duration in hours of time for which you would like to simulate: ");
scanf("%f", &duration);
printf("Please enter the 'time step' in minutes that the program should move forward in each step of the simulation: ");
scanf("%f", &timeStep);
fclose(file);
// return 0;
}
free(dates);
dates = NULL;
/*free(ships);
ships = NULL;*/
return (EXIT_SUCCESS);
}
/* Here, use the 'great_circle' method in navigation.h to calculate the distance between each of
* the ships in the file. If any of the ships are within 2 miles of each other AND are on courses
* that overlap at anytime in the future, a 'risk of collision' is deemed to exist*/
int riskOfCollision(location ship1, location ship2){
if((great_circle(ship1, ship2)<=2) &&(/*Need code here to determine whether or not ships'
* courses overlap at all here*/)){
}
}
I'm currently trying to implement a method to determine whether or not a 'risk of collision' occurs- this would happen if any two ships' courses overlap at all, but I'm not sure how I would work that out...