#include <stdlib.h> #include <stdio.h> /*#include <windows.h> */
typedef struct stuscorestruct { char stuname[20]; int score; struct stuscorestruct *prev; struct stuscorestruct *next; }STUSCORESTRUCT; typedef STUSCORESTRUCT *stu_score_dlink; stu_score_dlink stu_score_dlist; stu_score_dlink head; char *stu_name[4] = {"beijing","shanghai","shenzhen","abc"}; int score_array[4] = {100,95,90,55}; int initial_stat = 0; void main(); void DrawModifyScore();
void Print_stu_score_info_dlist(stu_score_dlink head) { do { head = head->next; printf("%s's score:%d/n",head->stuname,head->score); }while(head->next!= NULL); }
void Free_stu_score_info_dlist(stu_score_dlink head) { stu_score_dlink pointer; do { pointer = head; head = head->next; free(pointer); }while(head->next != NULL); }
stu_score_dlink create_stu_score_info_dlist(stu_score_dlink head,char **stu_name,int *stu_score,int n) { int i = 0; stu_score_dlink pointer; stu_score_dlink new; if((head = (STUSCORESTRUCT *)malloc(sizeof(STUSCORESTRUCT))) == NULL) { printf("Memory allocated failure!/n"); exit(1); } head->score = 0; memset(head->stuname,0,sizeof(head->stuname)); head->next = NULL; head->prev = NULL; pointer = head; for(i = 0;i<n;i++) { if((new = (STUSCORESTRUCT *)malloc(sizeof(STUSCORESTRUCT))) == NULL) { printf("Memory allocated failure!/n"); exit(1); } pointer->next = new; pointer->next->prev = pointer; new->next = NULL; new->score = stu_score[i]; memset(new->stuname,0x00,sizeof(new->stuname)); strcpy(new->stuname,stu_name[i]); pointer = new; new = new->next; } return head; }
/******************************************************* 把学生的姓名和分数的数据信息插入到双向链表 的尾端进行管理,如果只有姓名或者只有分数则 不能完成插入 *******************************************************/ int Insert_stu_info(stu_score_dlink head,char *stu_name,int score) { stu_score_dlink tailnode; if(stu_name == NULL || (score < 0 && score > 100)) { printf("/nData is Error!/n"); return 0; } if((tailnode = (STUSCORESTRUCT *)malloc(sizeof(STUSCORESTRUCT))) == NULL) { printf("/nMemory allocated failured!"); return 0; } memset(tailnode->stuname,0x00,sizeof(tailnode->stuname)); strcpy(tailnode->stuname,stu_name); tailnode->score = score; do { head = head->next; }while(head->next != NULL);
head->next = tailnode; head->next->prev = head; tailnode->next = NULL; do { head = head->prev; }while(head->prev != NULL); Print_stu_score_info_dlist(head); return 1; }
void DrawErrorScr() { initial_stat = 1; printf("input error!/n"); printf("Press any goback mainmenu./n"); getch(); main(); }
void PrintStuScore() { initial_stat = 1; system("cls"); Print_stu_score_info_dlist(head); /*Free_stu_score_info_dlist(head); */ printf("Press any key to go back to main menu."); getch(); main(); }
/************************************************ **函数说明: 遍历链表,若查找到与stu_name ** 相匹配的节点,则返回1,否则 ** 返回0. **************************************************/ int search_stu_info(stu_score_dlink head,char *stu_name) { do { head = head->next; if(strncmp(head->stuname,stu_name,strlen((char *)stu_name)) == 0) { printf("/nThe student's name is %s,score:%d/n",head->stuname,head->score); if(head->score >= 60) { printf("/nThe student's score is not failure,please check your input,press any key to continue. "); getch(); DrawModifyScore(); } return 1; } else continue; }while(head->next->stuname != NULL); return 0; }
/*在修改不及格学生成绩时,需要事先查找到该不及格学生的节点信息, 程序不支持节点信息搜索的模糊匹配*/ void DrawModifyScore() { char stu_name[20]; int stu_score; int found = 0; system("cls"); initial_stat = 1; memset(stu_name,0x00,sizeof(stu_name)); printf("/nplease input the failure student's name:"); scanf("%s",&stu_name); printf("searching...please wait.../n"); /*Sleep(2000);*/ if ((found = search_stu_info(head,stu_name)) == 0) { printf("This name is not exist,please check your input and try again./nPress any key to continue."); getch(); DrawModifyScore(); } else { printf("/nPlease input the student's score you want to modify:"); scanf("%d",&stu_score); if(stu_score < 60 || stu_score > 100) { printf("/nInput error!press any key to go back to mainmenu."); getch(); main(); } else { do { head = head->next; if(strncmp(head->stuname,stu_name,strlen((char *)stu_name)) == 0) { head->score = stu_score; break; } }while(head->next->stuname != NULL); printf("/nModify succeed!the student 's info:%s's score:%d,press any key to go mainmenu.",head->stuname,head->score); /*指针回到链首*/ do { head = head->prev; }while(head ->prev != NULL); getch(); main(); } } getch(); }
/**************************************** 用户 输入学生姓名和成绩后,如果 选择保存,则保存到程序中,否则 则退出该界面至主菜单界面,程序未 对用户输入的分数段和数据类型进 行检测,在输入分数时请不要输入 非数字的字符 ****************************************/ void DrawInputScore() { char stu_name[20]; int stu_score; char choice; system("cls"); printf("/nPlease input student's name:"); scanf("%s",&stu_name); printf("Please input student's score:"); scanf("%d",&stu_score); printf("/nDo you want to save this data?please select Y/N"); scanf("/n%c",&choice); if(choice == 'Y' || choice == 'y') { initial_stat = 1; if (1 == Insert_stu_info(head,stu_name,stu_score));/*save data to dlist*/ printf("/nSave succeed!press any key to continue."); getch(); DrawInputScore(); } else if(choice == 'N'|| choice == 'n') main(); else { printf("/ninput error,this student's data is not saved!press any key to continue:"); getch(); DrawInputScore(); } }
void Drawmainmenu() { system("cls");
printf("/n/n/n/n/n/n/n/n/n/n"); printf(" please select menus/n"); printf(" 1:input students name and score/n"); printf(" 2:modify failure student's score/n"); printf(" 3:print student's score/n"); printf(" 4:exit/n");
}
void main() { int choice_menu;
Drawmainmenu();
if(0 == initial_stat) head = create_stu_score_info_dlist(head, stu_name, score_array, 4); scanf("%d",&choice_menu); switch(choice_menu) { case 1: DrawInputScore(); break; case 2: DrawModifyScore(); break; case 3: PrintStuScore(); break; case 4: { Free_stu_score_info_dlist(head); exit(1); } break; default: DrawErrorScr(); }
}