Code:
#include <stdio.h>
#include <string.h>
typedef struct GeocentricCelestialReferenceFrame {
int x;
int y;
int z;
} GCRF;
typedef struct DiscoveredPlanet {
int usersConfirmed;
int type;
GCRF*coordinates;
struct DiscoveredPlanet* next;
} discPlanet;
typedef struct VerifiedPlanet {
int type;
GCRF*coordinates;
struct VerifiedPlanet* next;
} verPlanet;
typedef struct userWorkCompleted {
char firstName[20];
char lastName[20];
int gbAnalyzed;
int planetsDiscovered;
struct userWorkCompleted* next;
} userWork;
typedef enum {
NONE = 0,
GAS = 1,
ICE = 2,
STORM = 3,
BARREN = 4,
TEMPERATE = 5,
LAVA = 6,
OCEANIC = 7,
PLASMA = 8,
UNKNOWN = 9
} Planets;
//Declare userLists
userWork *userList = NULL;
discPlanet *discList = NULL;
verPlanet *verList = NULL;
userWork *currentUser = NULL;
discPlanet *currentDisc = NULL;
verPlanet *currentVer = NULL;
userWork *insertUser(userWork *userList, char *first, char *last);
discPlanet *confirmedPlanets(discPlanet* listDisc, verPlanet* listVer, int typePlanet, int x, int y, int z);
int main(){
int entries, i, analyzed, discovered, x, y, z;
char first, last;
//Scan in input file and number of entries
FILE *fp;
fp = fopen("GridComputing.in","r");
fscanf(fp, "%d" , &entries);
//Initialize for loop for number of entries
for(i=0; i<entries; i++){
fscanf(fp, "%s %s", first, last);
fscanf(fp, "%d", &analyzed);
fscanf(fp, "%d", &discovered);
currentUser = insertUser(userList, &first, &last);
currentUser->gbAnalyzed = currentUser->gbAnalyzed + analyzed;
if(discovered != 0){
currentUser->planetsDiscovered++;
fscanf(fp, "%d %d %d", &x, &y, &z);
currentDisc = confirmedPlanets(discList, verList, discovered, x, y, z);
}
}
fclose(fp);
fp = fopen("GridComputing.out", "w");
fprintf(fp, "Users:\n");
currentUser = userList;
userWork* userTemp;
while(currentUser != NULL){
fprintf(fp, "\t%s, %s - Analyzed:%d\n", currentUser->lastName, currentUser->firstName, currentUser->planetsDiscovered);
userTemp = currentUser;
currentUser = currentUser->next;
free(userTemp);
}
fprintf(fp, "\nDiscovered Planets:\n");
currentDisc = discList;
discPlanet* discTemp;
while(currentDisc != NULL){
fprintf(fp, "\t(%d,%d,%d) - Confirmed:%d Type:%d\n", currentDisc->coordinates->x, currentDisc->coordinates->y, currentDisc->coordinates->z, currentDisc->usersConfirmed, currentDisc->type);
discTemp = currentDisc;
currentDisc = currentDisc->next;
free(discTemp->coordinates);
free(discTemp);
}
fprintf(fp, "\nVerified Planets:\n");
currentVer = verList;
verPlanet* verTemp;
while(currentVer != NULL){
fprintf(fp, "\t(%d,%d,%d) - Type:%d\n", currentVer->coordinates->x, currentVer->coordinates->y, currentVer->coordinates->z, currentVer->type);
verTemp = currentVer;
currentVer = currentVer->next;
free(verTemp->coordinates);
free(verTemp);
}
fclose(fp);
return 0;
}
userWork *insertUser(userWork *userList, char *first, char *last) {
userWork *temp;
userWork *current;
// Create the new node.
temp = (userWork*)malloc(sizeof(userWork));
temp->next = NULL;
// Take care of case inserting into an empty userList.
if (userList == NULL){
temp=(userWork*)malloc(sizeof(userWork));
temp->next = NULL;
strcpy(temp->firstName, first);
strcpy(temp->lastName, last);
temp->gbAnalyzed = 0;
temp->planetsDiscovered = 0;
userList=temp;
return temp;
}
if(stricmp(last, userList->lastName)== 0){
if(stricmp(first, userList->firstName) == 0){
return userList;
}
else if(stricmp(first, userList->firstName)< 0){
temp=(userWork*)malloc(sizeof(userWork));
strcmp(temp->firstName, first);
strcmp(temp->lastName, last);
temp->gbAnalyzed = 0;
temp->planetsDiscovered = 0;
temp->next = userList;
userList = temp;
return temp;
}
}
else if(stricmp(last, userList->lastName) < 0){
temp=(userWork*)malloc(sizeof(userWork));
strcmp(temp->firstName, first);
strcmp(temp->lastName, last);
temp->gbAnalyzed = 0;
temp->planetsDiscovered = 0;
temp->next = userList;
userList = temp;
return temp;
}
while(current->next != NULL) {
if(stricmp(first, current->next->firstName) == 0){
if(stricmp(last, current->next->lastName) == 0){
return current->next;
}
else if(stricmp(first, current->next->firstName) < 0){
temp=(userWork*)malloc(sizeof(userWork));
strcmp(temp->firstName, first);
strcmp(temp->lastName, last);
temp->gbAnalyzed = 0;
temp->planetsDiscovered = 0;
temp->next = current->next;
current->next = temp;
return temp;
}
}
else if(stricmp(last, current->next->lastName) < 0){
temp=(userWork*)malloc(sizeof(userWork));
strcpy(temp->firstName, first);
strcpy(temp->lastName, last);
temp->gbAnalyzed = 0;
temp->planetsDiscovered = 0;
temp->next = current->next;
current->next = temp;
return temp;
}
current = current->next;
}
temp=(userWork*)malloc(sizeof(userWork));
strcpy(temp->firstName, first);
strcpy(temp->lastName, last);
temp->gbAnalyzed = 0;
temp->planetsDiscovered = 0;
temp->next = NULL;
current->next = temp;
return temp;
}
discPlanet* confirmedPlanets(discPlanet *listDisc, verPlanet *listVer, int typePlanet, int x, int y, int z){
discPlanet *dpNew = NULL;
if(listDisc == NULL){
listDisc=(discPlanet*)malloc(sizeof(discPlanet));
listDisc->usersConfirmed = 1;
listDisc->type = typePlanet;
listDisc->coordinates = (GCRF*)malloc(sizeof(GCRF));
listDisc->coordinates->x = x;
listDisc->coordinates->y = y;
listDisc->coordinates->z = z;
listDisc->next = NULL;
return listDisc;
}
discPlanet* temp = (listDisc);
discPlanet* temp2 = (listDisc);
while(temp != NULL){
if((temp->coordinates->x == x) && (temp->coordinates->y == y) && (temp->coordinates->z == z)){
temp->usersConfirmed++;
if(temp->usersConfirmed >= 3){
return NULL;
}
return temp;
}
if(temp->next == NULL){
break;
}
temp = temp->next;
}
temp2 = (discPlanet*)malloc(sizeof(discPlanet));
temp2->usersConfirmed = 1;
temp2->type = typePlanet;
temp2->coordinates = (GCRF*)malloc(sizeof(GCRF));
temp2->coordinates->x = x;
temp2->coordinates->y = y;
temp2->coordinates->z = z;
temp2->next = NULL;
temp->next = temp2;
return temp2;
}