Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Derek Churchill
- // Assignment 5
- // 2017-03-26
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <ctype.h>
- struct courseInfo {
- int courseID;
- char courseName[30];
- };
- typedef struct courseInfo CI;
- struct studentInfo {
- char studentID[20];
- char fName[20];
- char lName[25];
- int n;
- CI data[10];
- struct studentInfo *next;
- };
- typedef struct studentInfo SI;
- SI *head=NULL,*tail=NULL;
- void addStudent(char id[],char fName[],char lName[],int n,char courses[][100],char code[][100]);
- void deleteStudent(int studentID);
- SI *searchStudentID(int id);
- SI *searchStudentlName(char *lName);
- void displayStudentInfo();
- void saveStudentInfo();
- void loadStudentInfo();
- void exit();
- int main(void) {
- loadStudentInfo();
- displayStudentInfo();
- }
- void loadStudentInfo() {
- FILE *input = fopen("studentList.txt","r");
- bool scanning = true;
- char tempFields[4][100];
- char tempCourses[10][100];
- char tempCodes[10][100];
- char *line;
- int i = 0,ppl = 0;
- while(scanning == true) {
- line = fgets(line,25,input);
- strtok(line,"\n");
- if(strcmp("***",line) != 0) {
- if(i <= 3) {
- strcpy(tempFields[i],line);
- i++;
- }
- if(i > 3) {
- for(int j = 0; j < atoi(tempFields[3]); j++) {
- int w = 0;
- line = fgets(line,25,input);
- strtok(line,"\n");
- char *token = strtok(line, " ");
- while(token != NULL) {
- if(w == 0) { strcpy(tempCourses[j],token);}
- else if(w == 1) { strcpy(tempCodes[j],token);}
- w++;
- token = strtok(NULL, " ");
- }
- }
- addStudent(tempFields[0],tempFields[1],tempFields[2],atoi(tempFields[3]),tempCourses, tempCodes);
- i = 0;
- }
- }
- else {scanning = false;}
- }
- }
- void addStudent(char id[],char fName[],char lName[],int n,char course[][100],char code[][100]) {
- SI *newNode = (SI*)malloc(sizeof(SI));
- newNode->next = NULL;
- if(!head) {
- head = newNode;
- head->next = NULL;
- }
- else {
- SI *node = head;
- if(atoi(id) < atoi(node->studentID)) {
- SI *temp = node;
- head = newNode;
- head->next = node;
- }
- else {
- while(node->next != NULL && atoi(id) > atoi(node->next->studentID)) {
- node = node->next;
- }
- if(node->next == NULL) {
- node->next = newNode;
- }
- else {
- SI *tempBigger;
- tempBigger = node->next;
- node->next = newNode;
- newNode->next = tempBigger;
- }
- }
- }
- strcpy(newNode->studentID,id);
- strcpy(newNode->fName,fName);
- strcpy(newNode->lName,lName);
- newNode->n = n;
- for(int i = 0; i < n; i++) {
- strcpy(newNode->data[i].courseName,course[i]);
- newNode->data[i].courseID = atoi(code[0]);
- }
- }
- void displayStudentInfo() {
- SI *node = head;
- int n = 1;
- while(node != NULL) {
- printf("Student: %d\n",n);
- puts(node->studentID);
- puts(node->fName);
- puts(node->lName);
- printf("%d\n",node->n);
- for(int i = 0; i < node->n; i++) {
- printf("%s %d\n",node->data[i].courseName,node->data[i].courseID);
- }
- node = node->next;
- n++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement