(2009届)2011年7月5日编程实习报告题目:用字符串指针实现学生成绩管理系统姓名:杨宇超学号:09061109学院:自动化学院班级:09063011指导教师:席旭刚高云园摘要:本文介绍运用C语言中单用字符串指针实现一个学生信息管理工具软件,主要功能有:add、show、sort、Delete。程序由七个子函数(creat、edit、extra、imdelete、print、save、)和一个主函数(main)组成。关键字:C语言、用字符串指针、学生信息一、任务要求(用字符串指针实现学生成绩管理系统,具体完成的功能见可执行程序Student.exe。完成函数voidDeleteStudent(char***students,int***marks);voidSortClass(char**students,int**marks);voidShowClass(char**students,int**marks);voidEditMarks(char**students,int**marks);二、详细设计(分析各函数的功能,设计各函数的处理过程及其流程图)voidInsertStudent(char***students,int***marks);插入学生姓名基本想法:先读取学生姓名,判断指针是否为空,为空则建立内存。否则再开拓新的内存空间,然后将读取的学生与名单一一比较,若相同,则显示已存在该学生,若没有,则开拓内存给新到的学生,并对名单进行排序,最后释放内存。voidDeleteStudent(char***students,int***marks);删除学生信息基本想法:先读取要删除的学生姓名,将读入的学生姓名与已存在的学生进行比较,如相同,记住该学生所在位置,判断该学生下一个是否为空,若为空,就可以直接把该学生内存释放掉。若不是,则用一个while将后面的所有学生向前移一位直到NULL,再释放最后的内存,最后再释放学生姓名的那个内存voidSortClass(char**students,int**marks);排序根据学生姓名基本想法:采用冒泡法来进行排序。N次排序先进行n-1次比大小,找到最小的,与第一个交换,再进行n-2次。。。。voidShowClass(char**students,int**marks);显示所有学生信息基本想法:直接用printf输出,voidEditMarks(char**students,int**marks);编辑学生成绩基本想法:先读取要编辑的学生姓名,然后与所有的学生姓名进行比较,判断是否在名单内,若不在,就输出不在,否则就再读取该学生的5个成绩到marks三、编码实现#includestdio.h#includestdlib.h#includestring.hvoidInsertStudent(char***students,int***marks);voidDeleteStudent(char***students,int***marks);voidSortClass(char**students,int**marks);voidShowClass(char**students,int**marks);voidEditMarks(char**students,int**marks);char*ReadLine();#definemerror(a){printf(memoryallocationerror%d\n,a);exit(1);}/*functionmain-----------------------------------------------*/intmain(){char**students=NULL;int**marks=NULL;charline[100];intmenu;while(1){printf(Enter(1)toAddaStudenttoClassList\n(2)toDeleteaStudentfromClassList\n(3)toshowClassList\n(4)toEditmarksofaStudent\n(5)toQuit\n);fflush(stdout);gets(line);if(sscanf(line,%d,&menu)!=1){printf(incorrectentry);continue;}if(menu1||menu5){printf(incorrectselection);continue;}if(menu==1)InsertStudent(&students,&marks);elseif(menu==2)DeleteStudent(&students,&marks);elseif(menu==3)ShowClass(students,marks);elseif(menu==4)EditMarks(students,marks);elsebreak;}/*endwhile*/return0;//readhere}/*endmain*//*functionInsertStudent------------------------------------*//*Thisfunctionpromptstheusertoenteranewstudentname.ThepreciseformofthepromptisEnterStudentNametobeaddedtoClassList:Thenthisfunctionreadsthenamefromstandardinput.Butitmustbeabletoreadanynameofanysize!!Thusyouhavetoallocatememory(usingmalloc())toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan10bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememorybeforeexitingthefunction!Oncethefunctionhasthenametobeentered,ittraversesthearraystudenttofindoutifthenameisalreadythere.Ifso,thefunctiondisplaysawarningmessagestudentxxxalreadyintheclasslistandterminates(wherexxxisthenameofthestudent).Ifthenameisnotinthearray,thearraystudentsisextendedbyoneitem(usingrealloc())andacopyofthestringwiththenameisplacedthere.Alsothearraymarksisextendedbyonerow(usingrealloc())andallfivemarksinthatrowaresetto-1.ThenupdatedarraysstudentsandmarksarepassedtoSortClass()sotheycanbesortedalphabetically,asyouarerequiredtokeeptheclasslistandthelistofmarksinalphabeticalorder.Notethatbothstudentsandmarksarepassedtothisfunctionbyreference,forthisfunctiononoccasionsmustbeabletomodifythevaluestoredinthepointerstudent(inmain())-whenputtingtheretheveryfirststudentorwhenrealloc()possiblymovesthearrayinthememorysomewhereelse.Thesameappliestomarks.*/voidInsertStudent(char***students,int***marks){intfound,i;char**students1;int**marks1;students1=*students;marks1=*marks;char*name;printf(EnterStudentNametobeaddedtoClassList:\n);fflush(stdout);name=ReadLine();if(students1==NULL){if((students1=(char**)malloc(2*sizeof(char*)))==NULL)merror(2);if((students1[0]=(char*)malloc(strlen(name)+1))==NULL)merror(3);strcpy(students1[0],name);students1[1]=NULL;if((marks1=(int**)malloc(2*sizeof(int*)))==NULL)merror(4);if((marks1[0]=(int*)malloc(5*sizeof(int)))==NULL)merror(5);marks1[0][0]=marks1[0][1]=marks1[0][2]=marks1[0][3]=marks1[0][4]=0;marks1[1]=NULL;*students=students1;*marks=marks1;return;}/*dowehavethestudentyet*/for(found=i=0;students1[i]!=NULL;i++){if(strcmp(students1[i],name)==0){found=1;break;}}if(found){printf(student%salreadyintheclasslist\n,name);return;}/*soitisnotintheclasslistyet*/students1=(char**)realloc((void*)students1,(i+2)*sizeof(char*));if(students1==NULL)merror(6);if((students1[i]=(char*)malloc(strlen(name)+1))==NULL)merror(7);strcpy(students1[i],name);students1[i+1]=NULL;free((void*)name);if((marks1=(int**)realloc((void*)marks1,sizeof(int*)*(i+2)))==NULL)merror(8);if((marks1[i]=(int*)malloc(5*sizeof(int)))==NULL)merror(9);marks1[i][0]=marks1[i][1]=marks1[i][2]=marks1[i][3]=marks1[i][4]=0;marks1[i+1]=NULL;SortClass(students1,marks1);*students=students1;*marks=marks1;}/*functionDeleteStudent------------------------------------*//*Thisfunctionpromptstheusertoenterastudentnametobedeleted.Thepreciseformofthep