FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Machine Problem No.2 [Pt. 1]




The hardest C code I ever did. Didn't get the perfect score, but it's working solidly.



[code]
/*
*************************************
* MACHINE PROBLEM NO.2 *
* The Exam Scheduling Problem (ESP) *
*************************************
*
* CAY, ACEZON RHAY E.
* 2011 - 25305
* BS Computer Science
* CS11 THEW
*
* Instructor : LESTER NACU
*
*/

/* Initialize libraries */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Create new data types */
typedef struct studList {
char studName[3];
struct studList *next;
} stud;

typedef struct courseList {
char courseName[3];
stud *toStud;
struct courseList *next;
} course;

typedef struct scheduling {
int color;
struct scheduling *next;
} sched;

int compareStuds(stud *toStud1, stud *toStud2) {
int count = 0;
stud *tempPtr;
while(toStud1 != NULL) {
tempPtr = toStud2;
while(tempPtr != NULL) {
if(strcmp(toStud1->studName, tempPtr->studName) == 0) {
count = 1;
}
tempPtr = tempPtr->next;
}
toStud1 = toStud1->next;
}
return count;
}

int main() {
char filename[50];
int roomNum, courseSize = 0;

scanf("%s%d", filename, &roomNum); //Input filename, number of rooms

/* Initialize main pointers */
course *head = NULL, *hover = NULL, *tail = NULL;
stud *studHead, *studHover, *compPtr1, *compPtr2;

/* Open file containing courses */
FILE *courseNames = fopen(filename, "r"), *studentNames;

/* -------------------------------------------------------- */
/* 1 - SAVING COURSE LIST AND STUDENT NAMES TO LINKED LISTS */
/* -------------------------------------------------------- */
char courseList2[500], studentName[3];
printf("\n");

while (!feof(courseNames)) {
fscanf(courseNames, "%s", courseList2);
courseList2[3] = '\0';
course *node = (course *) malloc(sizeof(course));

strcpy(node->courseName, courseList2);
printf("%s: ", courseList2);

studHead = NULL, studHover = NULL;
studentNames = fopen(courseList2, "r");
while (!feof(studentNames)) {
fscanf(studentNames, "%s", studentName);
studentName[3] = '\0';

stud *studNode = (stud *) malloc(sizeof(stud));
strcpy(studNode->studName, studentName);

if(node->toStud == NULL) {
node->toStud = studNode;
}

studNode->next = NULL;

if(studHead == NULL) {
studHead = studNode;
studHover = studNode;
}
else {
studHover->next = studNode;
studHover = studNode;
}

printf("%s ", studentName);
}
fclose(studentNames);

node->toStud = studHead;
node->next = NULL;

if(head == NULL) {
head = node;
hover = node;
}
else {
hover->next = node;
hover = node;
}
courseSize++;

printf("\n");
}
fclose(courseNames);
printf("\n");



0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.