StudentManage.h:
#include <stdio.h> #include <string.h> #include <stdlib.h> //#include <curses.h> #define N 100 //#define CHIN typedef struct student { int studentID; char name[N]; int age; int sex; }STUDENT; typedef struct score { int chinese; int math; int english; int average; int total; }SCORE; typedef struct node { STUDENT stu; SCORE sc; struct node *next; struct node *pione; }NODE; extern NODE * mycreate(); extern NODE * myinsert(NODE *,STUDENT *,SCORE *); extern void mycopy(NODE *,STUDENT *,SCORE *); extern void myprint(NODE *); extern NODE * myremove(NODE **,int); extern int Doublelinklistlen(NODE *); //extern NODE * mysearch(NODE *); //extern NODE * mysort(NODE *); extern void display(NODE *); extern void addstudent(NODE *); extern void lookover(NODE *); extern void deletestudent(NODE *); extern NODE * mysearch(NODE *,int); extern void searchinfo(NODE *); //extern NODE *mysort(NODE *); //extern void sortinfo(NODE *); extern void printnode(NODE *);
StudentManage.c:
#include "StudentManage.h" void mycopy(NODE *node,STUDENT *student,SCORE *score) { node->stu.studentID = student->studentID; strcpy(node->stu.name,student->name); node->stu.age = student->age; node->stu.sex = student->sex; node->sc.chinese = score->chinese; node->sc.math = score->math; node->sc.english = score->english; node->sc.average = score->average; node->sc.total = score->total; } NODE * mycreate() { NODE *head = (NODE *)malloc(sizeof(NODE)); head->stu.studentID = 0; head->next = head; head->pione = head; return head; } NODE * myinsert(NODE *head,STUDENT *student,SCORE *score) { if(head->stu.studentID == 0) { mycopy(head,student,score); } else { NODE *last = head; while(last->next != head) { last = last->next; } NODE *p = (NODE *)malloc(sizeof(NODE)); memset(p,0,sizeof(p)); mycopy(p,student,score); head->pione = p; last->next = p; p->next = head; p->pione = last; } return head; } void myprint(NODE *head) { NODE *q = (NODE *)malloc(sizeof(NODE)); q = head; int i = 1; while(q->next != head) //while(i < Doublelinklistlen(head)) { printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex); printf(" Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total); i++; q = q->next; printf("\n"); } printf("No.%d:ID:%d\tName:%s\tAge:%d\tSex:%d\n",i,q->stu.studentID,q->stu.name,q->stu.age,q->stu.sex); printf(" Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",q->sc.chinese,q->sc.math,q->sc.english,q->sc.average,q->sc.total); } void printnode(NODE *node) { if(node != NULL) { printf("ID:%d\tName:%s\tAge:%d\tSex:%d\n",node->stu.studentID,node->stu.name,node->stu.age,node->stu.sex); printf("Chinese:%d\tMath:%d\tEnglish:%d\tAverage:%d\tTotal:%d\n",node->sc.chinese,node->sc.math,node->sc.english,node->sc.average,node->sc.total); } else { printf(" "); } printf("\n"); } int Doublelinklistlen(NODE *head) { int len = 0; NODE *q = head; while(q->next != head) { len++; q = q->next; } return (len + 1); } NODE * myremove(NODE **head,int studentID) { //#define __DDD__ NODE *q = (NODE *)malloc(sizeof(NODE)); q = *head; int j = 0; while(q->stu.studentID != studentID) { j++; q = q->next; if(j >= Doublelinklistlen(*head)) { #ifdef __DDD__ printf("No Such Student has The studentID!\n"); printf("Delete Student Failure!\n"); #endif return *head; } } NODE *pre = q->pione; NODE *ne = q->next; //pre->next = ne; //ne->pione = pre; if(q == *head) { (*head) = (*head)->next; pre->next = (*head); (*head)->pione = pre; free(q); q = NULL; } else { pre->next = ne; ne->pione = pre; free(q); q = NULL; } #ifdef __DDD__ printf("Delete Student Success!\n"); #endif return (*head); } NODE * mysearch(NODE *head,int info) { NODE *q = head; int j = 0; #if 0 int count = 0; for(i = 0; i < Doublelinklistlen(head); i++) { if((num == 1) && (q->stu.studentID == (int)info)) { printnode(q); count++; continue; } else if((num == 2) && (strcpy(q->stu.name,(char *)info) == 0)) { printnode(q); count++; continue; } else if((num == 3) && (q->stu.age == (int)info)) { printnode(q); count++; continue; } else if((num == 4) && (q->stu.sex == (int)info)) { printnode(q); count++; continue; } else { ; } q = q->next; } if(count == 0) { printf("No infomation about you input!\n"); } #endif #if 1 while(q->stu.studentID != info) { j++; q = q->next; if(j > Doublelinklistlen(head)) { printf("No This Student!\n"); return NULL; } } //printnode(q); return q; #endif } #if 0 NODE * mysort(NODE *head) { NODE *newhead = mycreate(); NODE *newlast = newhead; NODE *q = head; NODE *max = head; while(q != NULL) { printf("%d\n",__LINE__); for(; q->next != head; q = q->next) { printf("%d\n",__LINE__); //sleep(2); if(q->next->sc.total > max->sc.total) { printf("%d\n",__LINE__); max = q->next; } } printf("%d\n",__LINE__); if(max == head) { printf("%d\n",__LINE__); NODE * ne = head->next; NODE * pre = head->pione; head = head->next; ne->pione = pre; pre->next = ne; printf("%d\n",__LINE__); } else { printf("%d\n",__LINE__); NODE *ne = q->next; NODE *pre = q->pione; ne->pione = pre; pre->next = ne; printf("%d\n",__LINE__); } if(newhead->stu.studentID == 0) { printf("%d\n",__LINE__); newhead = max; newlast = max; printf("%d\n",__LINE__); } else { newlast->next = max; max->pione = newlast; max->next = newhead; newhead->pione = max; newlast = max; } q = q->next; } return newhead; } #endif
Display.c:
#include "StudentManage.h" void addstudent(NODE *head) { STUDENT stu; SCORE sc; printf("Please Input Student's StudentID(12XXX):"); scanf("%d",&stu.studentID); printf("Please Input Student's Name:"); scanf("%s",stu.name); printf("Please Input Student's Age:"); scanf("%d",&stu.age); printf("Please Input Student's Sex(1:男 0:女):"); scanf("%d",&stu.sex); printf("Please Input Student's Chinese Score:"); scanf("%d",&sc.chinese); printf("Please Input Student's Math Score:"); scanf("%d",&sc.math); printf("Please Input Student's English Score:"); scanf("%d",&sc.english); sc.average = (sc.chinese + sc.math + sc.english) / 3; sc.total = sc.chinese + sc.math + sc.english; system("clear"); printf("The Infomation New Student:\n"); printf("ID:%d\tName:%s\tAge:%d\tSex:%d\n",stu.studentID,stu.name,stu.age,stu.sex); printf("Chinese:%6d\tMath:%6d\tEnglish:%6d\n",sc.chinese,sc.math,sc.english); printf("Are You Sure To Add %d:%s ??(Y / N)",stu.studentID,stu.name); char ch; scanf("\n%c",&ch); if(ch == 'Y') { head = myinsert(head,&stu,&sc); printf("Add Student Success!\n"); } printf("Press Enter To Continue...\n"); getchar(); getchar(); } void lookover(NODE *head) { printf("The Infomation Of Student :\n"); myprint(head); printf("Press Enter To Continue..."); getchar(); getchar(); } void deletestudent(NODE *head) { int id; #define __DDD__ printf("Please Input The StudentID Of Student You Want To Delete(12XXX):"); scanf("%d",&id); NODE *p = mysearch(head,id); printf("The Infomation New Student:\n"); printnode(p); printf("Are You Sure To Add %d:%s ??(Y / N)",p->stu.studentID,p->stu.name); char ch; scanf("\n%c",&ch); if(ch == 'Y') { //head = myinsert(head,&stu,&sc); //printf("Add Student Success!\n"); //char ch; head = myremove(&head,id); } printf("Press Enter To Continue..."); getchar(); getchar(); } void searchinfo(NODE *head) { int n; //#define __DDD__ printf("Please Input StudentID You Want To Search(12XXX):"); scanf("%d",&n); NODE *p = mysearch(head,n); printnode(p); #if 0 int n; int info; char name[N]; printf("/*****************************************************/\n"); printf("/******************* 1.StudentID *********************/\n"); printf("/******************* 2.StudentName *******************/\n"); printf("/******************* 3.StudentAge ********************/\n"); printf("/******************* 4.StudentSex ********************/\n"); printf("/*****************************************************/\n"); printf("Please input what you want to search:"); scanf("%d",&n); while((n > 4) || (n < 1)) { printf("Your input must be 1-4!\n"); printf("Please input again:"); scanf("%d",&n); } if(n == 1) { printf("Please input StudeentID you want to search:"); scanf("%d",&info); mysearch(head,1,info); } if(n == 2) { printf("Please input StudeentName you want to search:"); scanf("%s",name); mysearch(head,2,name); } if(n == 3) { printf("Please input StudeentAge you want to search:"); scanf("%d",&info); mysearch(head,3,info); } if(n == 4) { printf("Please input StudeentSex you want to search:"); scanf("%d",&info); mysearch(head,4,info); } #endif printf("Press Enter To Continue..."); getchar(); getchar(); } void display(NODE *head) { int choose; while(1) { system("clear"); printf("/****************Student Manage System****************/\n"); printf("/******************* 1.Look Over *********************/\n"); printf("/******************* 2.Add Student *******************/\n"); printf("/******************* 3.Delete Student ****************/\n"); //printf("/******************* 4.Sort **************************/\n"); printf("/******************* 4.Search ************************/\n"); printf("/******************* 5.Exit **************************/\n"); printf("/*****************************************************/\n"); printf("Please Input Your Choose:"); scanf("%d",&choose); while((choose > 5) || (choose < 1)) { printf("Your Choose Must Be 1 To 5!\n"); printf("Please Input Your Choose Again:"); scanf("%d",&choose); } switch(choose) { case 1: { system("clear"); lookover(head); break; } case 2: { system("clear"); addstudent(head); break; } case 3: { system("clear"); deletestudent(head); break; } case 4: { system("clear"); searchinfo(head); break; } case 5: { system("clear"); exit(-1); } default: { break; } } } }
main.c:
#include "StudentManage.h" int main() { NODE *head = mycreate(); STUDENT stu1,stu2,stu3,stu4; SCORE sc1,sc2,sc3,sc4; stu1.studentID = 12001; strcpy(stu1.name,"王明"); stu1.age = 18; stu1.sex = 1; sc1.chinese = 90; sc1.math = 91; sc1.english = 92; sc1.average = 91; sc1.total = 273; stu2.studentID = 12002; strcpy(stu2.name,"张芳"); stu2.age = 19; stu2.sex = 0; sc2.chinese = 80; sc2.math = 81; sc2.english = 82; sc2.average = 81; sc2.total = 243; stu3.studentID = 12003; strcpy(stu3.name,"李强"); stu3.age = 19; stu3.sex = 1; sc3.chinese = 70; sc3.math = 71; sc3.english = 72; sc3.average = 71; sc3.total = 213; stu4.studentID = 12004; strcpy(stu4.name,"张小雨"); stu4.age = 20; stu4.sex = 0; sc4.chinese = 60; sc4.math = 61; sc4.english = 62; sc4.average = 61; sc4.total = 183; head = myinsert(head,&stu1,&sc1); head = myinsert(head,&stu2,&sc2); head = myinsert(head,&stu3,&sc3); head = myinsert(head,&stu4,&sc4); display(head); }
有一个bug,删除里第一个学生之后,myprint会无限不停输出剩下的学生~~~~~~