Teste

скачати

Teste Essay, Research Paper

#include

#include

#include

#include

typedef struct {

char className[8];

float units;

char grade;

} CLASSRECS;

typedef struct nodeTag {

CLASSRECS data;

struct nodeTag *next;

} NODE;

typedef struct {

int id;

char stuName[20];

int numOfClasses;

NODE *pClassRecs; /* Pointer to head of list */

} STUINFO;

STUINFO * buildArray(FILE *fp, STUINFO *stuAry, NODE *pList, NODE *pPre);

void displayMenu(int *choice);

NODE *addNode(NODE *pList, NODE *pPre, CLASSRECS *data);

NODE *deleteNode(NODE *pList, NODE *pPre, NODE *pCur, STUINFO *stuAry, int stuID);

NODE *searchNode(NODE *pList, NODE **pPre, NODE **pCur, char className[8]);

int seekNode (NODE *pList, NODE **pPre, CLASSRECS *data);

void addClass(STUINFO *stuAry, NODE *pList, NODE *pPre, int stuID);

void printList (STUINFO *pList);

void main () {

FILE *fp;

STUINFO *stuAry;

NODE *pList, *pPre, *pCur;

int stuID, choice;

if(!(fp = fopen(”C:\\Documents and Settings\\Administrator\\Desktop\\test.txt”, “rw”))) {

printf(”Error opening file stream!\n”);

exit(1432);

}

stuAry = buildArray(fp, stuAry, pList, pPre);

printf(”Welcome to the De Anza Student Records Program!”);

printf(”\n***********************************************”);

printf(”\nPlease enter your student ID -> “);

scanf(”%d”, &stuID);

displayMenu(&choice);

if(choice == 1)

addClass(stuAry, pList, pPre, stuID);

printList(stuAry);

}

STUINFO * buildArray(FILE *fp, STUINFO *stuAry, NODE *pList, NODE *pPre) {

char tmp[20];

int count = 0;

int countClass, countStu;

int numStudents = 0;

CLASSRECS *data = (CLASSRECS *)malloc(sizeof(CLASSRECS));

fscanf(fp, “%d”, &numStudents);

stuAry = (STUINFO *)calloc(numStudents + 1, sizeof(STUINFO));

stuAry[0].id = numStudents;

for(countStu = 1; countStu fscanf(fp, “%d”, &(stuAry[countStu].id));

fgetc(fp);

fgets(stuAry[countStu].stuName, 20, fp);

fgetc(fp);

fscanf(fp, “%d”, &(stuAry[countStu].numOfClasses));

fgetc(fp);

pList = pPre = NULL; /* Set all to NULL before creating nodes */

for(countClass = 0; countClass fgets(data->className, 8, fp);

fgetc(fp);

fscanf(fp, “%s”, tmp);

data->units = atof(tmp);

fgetc(fp);

data->grade = fgetc(fp);

fgetc(fp);

seekNode(pList, &pPre, data);

pList = addNode(pList, pPre, data);

}

stuAry[countStu].pClassRecs = pList;

}

return stuAry;

}

void addClass(STUINFO *stuAry, NODE *pList, NODE *pPre, int stuID) {

CLASSRECS *data = (CLASSRECS *)malloc(sizeof(CLASSRECS));

int dupClass, tmpUnits;

char grade, units[5], tmpClass[8];

NODE *pCur;

for(int i = 1; i if(stuAry[i].id == stuID)

break;

}

pList = stuAry[i].pClassRecs;

getchar();

do {

printf(”\nPlease enter class name -> “);

gets(tmpClass);

strncpy(data->className, tmpClass, 8);

dupClass = seekNode(pList, &pPre, data);

if(dupClass == 0)

printf(”Class already exists in your records!\n”);

} while(dupClass == 0);

do {

printf(”Please enter the # of units -> “);

gets(units);

data->units = atof(units);

tmpUnits = data->units * 10;

if((tmpUnits % 5 != 0) || (data->units units > 6.0))

printf(”Invalid units!\n”);

} while((tmpUnits % 5 != 0) || (data->units units > 6.0));

do {

printf(”Please enter the grade you earned -> “);

scanf(”%c”, &grade);

grade = toupper(grade);

data->grade = toupper(grade);

if((grade ‘F’) && ((grade != ‘W’ ) || (grade != ‘I’) || (grade != ‘ ‘))))

printf(”Invalid grade!\n”);

} while((grade ‘F’) && ((grade != ‘W’ ) || (grade != ‘I’) || (grade != ‘ ‘))));

/*printf(”%s”, data->className);

printf(”\n%f”, data->units);

printf(”\n%c”, data->grade);

printf(”%s”, pPre->data.className);

*/

//printf(”%s”, data->className);

pList = addNode(pList, pPre, data);

stuAry[i].pClassRecs = pList;

}

void displayMenu(int *choice) {

printf(”\n\n\tWhat would you like to do?”);

printf(”\n\n\t1. Add a new class”);

printf(”\n\t2. Update a class’s info”);

printf(”\n\t3. Delete a class”);

printf(”\n\t4. List your student info”);

printf(”\n\t5. List all student’s info”);

printf(”\n\t6. Quit this program\n\n”);

printf(”\tPlease enter your choice -> “);

scanf(”%d”, choice);

}

NODE *addNode(NODE *pList, NODE *pPre, CLASSRECS *data) {

NODE *pNew;

if(!(pNew = (NODE *)malloc(sizeof(NODE))))

printf(”Error allocating memory for node(s)\n”), exit(3223);

pNew->data = *data;

if(pPre == NULL) { /* Add to front of list or empty list */

pNew->next = pList;

pList = pNew;

}

else { /* adding to middle or end */

pNew->next = pPre->next;

pPre->next = pNew;

}

return pList;

}

NODE *deleteNode(NODE *pList, NODE *pPre, NODE *pCur, STUINFO *stuAry, int stuID) {

if(pPre == NULL) /* deleting first node */

pList = pCur->next;

else /* deleting some other node */

pPre->next = pCur->next;

free(pCur);

for(int i = 1; i if(stuAry[i].id == stuID) {

stuAry[i].numOfClasses–;

stuAry[i].pClassRecs = pList;

}

}

return pList;

}

NODE *searchNode(NODE *pList, NODE **pPre, NODE **pCur, char className[8]) {

NODE *pTmp = pList;

*pPre = NULL;

*pCur = NULL;

while(pTmp) {

if((strcmp(className, pTmp->data.className) == 0)) {

*pCur = pTmp;

return pList;

}

*pPre = pTmp;

pTmp = pTmp->next;

}

return pList;

}

int seekNode (NODE *pList, NODE **pPre, CLASSRECS *data) {

/* Return 0 if duplicate name, else 1 */

int cmp = 0, int firstPass = 1;;

NODE *pTmp;

*pPre = NULL;

pTmp = pList;

if(pList = NULL) { /* empty list */

*pPre = NULL;

return 1;

}

else {

while(pTmp) {

cmp = strcmp(data->className, (*pTmp).data.className);

if(cmp == 0)

return 0; /* Duplicate class name */

else if(cmp > 0) {

*pPre = pTmp;

pTmp = pTmp->next;

}

else if(cmp return 1;

}

}

return 1;

}

}

void printList (STUINFO *pList) {

NODE *pWalker;

for(int i = 1; i printf(”%d\t%s\t”, pList[i].id, pList[i].stuName);

pWalker = pList[i].pClassRecs;

while(pWalker) {

printf(”%s %1.1f %c “, pWalker->data.className, pWalker->data.units, pWalker->data.grade);

pWalker = pWalker->next;

}

printf(”\n”);

}

}

Додати в блог або на сайт

Цей текст може містити помилки.

A Free essays | Essay
8.8кб. | download | скачати

© Усі права захищені
написати до нас