版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上精选优质文档-倾情为你奉上专心-专注-专业专心-专注-专业精选优质文档-倾情为你奉上专心-专注-专业编程实习报告(2009届)题目:用字符串指针实现学生成绩管理系统姓名:杨宇超学号:学院:自动化学院班级:指导教师:席旭刚高云园Timeyyyy年M月d日2011年7月5日2011年7月5日摘要:本文介绍运用C语言中单用字符串指针实现一个学生信息管理工具软件,主要功能有:add、show、sort、Delete。程序由七个子函数(creat、edit、extra、imdelete、print、save、)和一个主函数(main)组成。关键字:C语言、用字符串指针、学生信息
2、一、任务要求(用字符串指针实现学生成绩管理系统,具体完成的功能见可执行程序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);插入学生姓名基本想法:先
3、读取学生姓名,判断指针是否为空,为空则建立内存。否则再开拓新的内存空间,然后将读取的学生与名单一一比较,若相同,则显示已存在该学生,若没有,则开拓内存给新到的学生,并对名单进行排序,最后释放内存。voidDeleteStudent(char*students,int*marks);删除学生信息基本想法:先读取要删除的学生姓名,将读入的学生姓名与已存在的学生进行比较,如相同,记住该学生所在位置,判断该学生下一个是否为空,若为空,就可以直接把该学生内存释放掉。若不是,则用一个while将后面的所有学生向前移一位直到NULL,再释放最后的内存,最后再释放学生姓名的那个内存voidSortClass(
4、char*students,int*marks);排序基本想法:采用冒泡法来进行排序。N次排序先进行n-1次比大小,找到最小的,与第一个交换,再进行n-2次。voidShowClass(char*students,int*marks);显示所有学生信息基本想法:直接用printf输出,voidEditMarks(char*students,int*marks);编辑学生成绩基本想法:先读取要编辑的学生姓名,然后与所有的学生姓名进行比较,判断是否在名单内,若不在,就输出不在,否则就再读取该学生的5个成绩到marks三、编码实现#include#include#includevoidInsertS
5、tudent(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%dn,a);exit(1);/*functionmain-*/intmain()char*st
6、udents=NULL;int*marks=NULL;charline100;intmenu;while(1)printf(Enter(1)toAddaStudenttoClassListn(2)toDeleteaStudentfromClassListn(3)toshowClassListn(4)toEditmarksofaStudentn(5)toQuitn);fflush(stdout);gets(line);if(sscanf(line,%d,&menu)!=1)printf(incorrectentry);continue;if(menu5)printf(incorrectselec
7、tion);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-*/*Thisfunctionpromptstheusertoenteranewstud
8、entname.ThepreciseformofthepromptisEnterStudentNametobeaddedtoClassList:Thenthisfunctionreadsthenamefromstandardinput.Butitmustbeabletoreadanynameofanysize!Thusyouhavetoallocatememory(usingmalloc()toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan1
9、0bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememorybeforeexitingthefunction!Oncethefunctionhasthenametobeentered,ittraversesthearraystudenttofindoutifthenameisalreadythere.Ifso,thefunctiondisplaysawarningmessagestudentxxxalreadyintheclasslis
10、tandterminates(wherexxxisthenameofthestudent).Ifthenameisnotinthearray,thearraystudentsisextendedbyoneitem(usingrealloc()andacopyofthestringwiththenameisplacedthere.Alsothearraymarksisextendedbyonerow(usingrealloc()andallfivemarksinthatrowaresetto-1.ThenupdatedarraysstudentsandmarksarepassedtoSortCl
11、ass()sotheycanbesortedalphabetically,asyouarerequiredtokeeptheclasslistandthelistofmarksinalphabeticalorder.Notethatbothstudentsandmarksarepassedtothisfunctionbyreference,forthisfunctiononoccasionsmustbeabletomodifythevaluestoredinthepointerstudent(inmain()-whenputtingtheretheveryfirststudentorwhenr
12、ealloc()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(
13、students1=(char*)malloc(2*sizeof(char*)=NULL)merror(2);if(students10=(char*)malloc(strlen(name)+1)=NULL)merror(3);strcpy(students10,name);students11=NULL;if(marks1=(int*)malloc(2*sizeof(int*)=NULL)merror(4);if(marks10=(int*)malloc(5*sizeof(int)=NULL)merror(5);marks100=marks101=marks102=marks103=mark
14、s104=0;marks11=NULL;*students=students1;*marks=marks1;return;/*dowehavethestudentyet*/for(found=i=0;students1i!=NULL;i+)if(strcmp(students1i,name)=0)found=1;break;if(found)printf(student%salreadyintheclasslistn,name);return;/*soitisnotintheclasslistyet*/students1=(char*)realloc(void*)students1,(i+2)
15、*sizeof(char*);if(students1=NULL)merror(6);if(students1i=(char*)malloc(strlen(name)+1)=NULL)merror(7);strcpy(students1i,name);students1i+1=NULL;free(void*)name);if(marks1=(int*)realloc(void*)marks1,sizeof(int*)*(i+2)=NULL)merror(8);if(marks1i=(int*)malloc(5*sizeof(int)=NULL)merror(9);marks1i0=marks1
16、i1=marks1i2=marks1i3=marks1i4=0;marks1i+1=NULL;SortClass(students1,marks1);*students=students1;*marks=marks1;/*functionDeleteStudent-*/*Thisfunctionpromptstheusertoenterastudentnametobedeleted.ThepreciseformofthepromptisEnterStudentNametobedeletedfromClassList:Thenthisfunctionreadsthenamefromstandar
17、dinput.Butitmustbeabletoreadanynameofanysize!Thusyouhavetoallocatememory(usingmalloc()toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan10bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememoryb
18、eforeexitingthefunction!NotethatthispartofthecodeisthesameasinInsertStudent(),soyoucaneithercopyitorwriteafunctionthatwoulddoitforboth.Oncethefunctionhasthenametobedeleted,ittraversesthearraystudenttofindoutifitisthere.Ifnot,awarningmessageisdisplayedstudentxxxnotintheclasslistandthefunctionterminat
19、es.Ifitisfound(sayitsisstudentsd),adynamicone-dimensionalarrayoneitemshorterthanstudentsiscreatedandthestringsfromthearraystudentsareunhookedfromthearraysstudentsandhookedtothenewarraywiththeexceptionofthestringtobedeleted(dontforgetthatthedeletedstringmustbedeallocated).Nowdeallocatethearraystudent
20、sandmakethepointerstudentpointtothenewarray.Asyoucanseewehavethesamearrayofstringasbefore,butwithonestringdeleted.Youmustdothesamewiththearraymarks,foryoumustdeletecompletelyd-throw(soitdidcorrespondtostudentdwhichwedeleted).Notethatbothstudentsandmarksarepassedtothisfunctionbyreference,forthisfunct
21、iononmustbeabletomodifythevaluestoredinthepointerstudent(inmain()-whenastudentnameisdeleted.Thesameapliestomarks.*/voidDeleteStudent(char*students,int*marks)intfound,i,j,a,b;char*studentsx;int*marksx;char*students1;int*marks1;char*namex;char*name;printf(EnterStudentNametobedeletedfromClassList:n);ff
22、lush(stdout);name=ReadLine();namex=name;studentsx=*students;marksx=*marks;/*dowehavethestudentyet*/for(found=i=0;studentsxi!=NULL;i+)if(strcmp(studentsxi,name)=0)found=1;j=i;b=i;/*givegroopspace*/students1=(char*)malloc(b+1)*sizeof(char*);marks1=(int*)malloc(b+1)*sizeof(int*);if(found)for(i=0;ij;i+)
23、students1i=studentsxi;marks1i=marksxi;for(i=j;i=0)temp=studentsi;studentsi=studentsi+1;studentsi+1=temp;temp1=marksi;marksi=marksi+1;marksi+1=temp1;/*functionShowClass-*/*Inthisfunctionyoudisplayonthescreeneachstudentintheclassandhis/her5marksonaline.Ifthemarkis-1displayaspaceinstead.Afterthenameofs
24、tudentdisplay:andseparatethemarksby,Notethatbothstudentsandmarksarepassedtothisfunctionbyvalue,forthisfunctiondoesnotneedtomodifythevaluestoredinthepointerstudent.Thesameappliestomarks.*/voidShowClass(char*students,int*marks)inti;for(i=0;studentsi!=NULL;i+)printf(%s,%d,%d,%d,%d,%dn,studentsi,marksi0
25、,marksi1,marksi2,marksi3,marksi4);/*functionEditMarks-*/*Thisfunctionpromptstheusertoenterastudentnamewhosemarksaretobeedited.ThepreciseformofthepromptisEnterStudentNamewhosemarksaretobeedited:Thenthisfunctionreadsthenamefromstandardinput.Butitmustbeabletoreadanynameofanysize!Thusyouhavetoallocateme
26、mory(usingmalloc()toreadtheinputinto,andifitisnotbigenough,mustuserealloc()toextendit.Youarenotallowedtoallocatemorethan10bytesinoneallocationorextendyoursegmentbymorethan10bytesinonecalltorealloc().Donotforgettodeallocatethememorybeforeexitingthefunction!NotethatthispartofthecodeisthesameasinInsert
27、Student()andDeleteStudent(),soyoucaneithercopyitorwriteafunctionthatwoulddoitforallthree.Ifthestudentisnotfoundinthearraystudents,awarningmessagestudentxxxnotintheClassListisdisplayedonthescreenandthefunctionterminates.Ifitisfound,thestudentsnamefollowedby:andhis/herfivemarksaredisplayed(again,-1mus
28、tbeshownasaspace,asinShowClass().Letusassumethatthestudentinquestionhasmarks10,-1,20,15,5Thenthefunctiondisplaysaprompttotheuserforthefirstmarktoleavethe1stmark10unchangedpressotherwisetypethenewmarkandpressandreadstheusersreplyusinggets()intoacharacterarraysofsize80.Thenitusessscanf()onthatarraytoe
29、xtracttheresponse.Thenthefunctiondisplaysaprompttotheuserforthesecondmarktoleavethe2ndmarkunchangedpressotherwisetypethenewmarkandpressandreadstheusersreplyusinggets()intoacharacterarraysofsize80.Thenitusessscanf()onthatarraytoextracttheresponse.Etc.forthethird,fourth,andfifth.Notethatbothstudentsan
30、dmarksarepassedtothisfunctionbyvalue,forthisfunctiondoesnotneedtomodifythevaluestoredinthepointerstudent.Thesameappliestomarks.*/voidEditMarks(char*students,int*marks)intfound,i,j;char*name;printf(EnterStudentNametobeaddedmarksin:n);fflush(stdout);name=ReadLine();/*dowehavethestudentyet*/for(found=i
31、=0;studentsi!=NULL;i+)if(strcmp(studentsi,name)=0)found=1;j=i;break;if(found)printf(pleaseinput%ssmarksoffiveclassesdividedwithan,n,name);scanf(%d,%d,%d,%d,%d,&marksj0,&marksj1,&marksj2,&marksj3,&marksj4);return;elseprintf(wrongstudentnamen);return;/*fuctionreadline*/char*ReadLine()char*buf;inti,j,size;if(buf=(char*)malloc(10)=NULL)merror(0);size=10;j=-1;while(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度外派工程师专项劳动合同精要3篇
- 2025年度特许经营权授予与行使合同3篇
- 海南外国语职业学院《交通信息系统》2023-2024学年第一学期期末试卷
- 二零二五年度旅游度假村合作协议范本
- 2025年度绿色蔬菜直销合作协议范本6篇
- 二零二五年度大型超市连锁加盟经营合同2篇
- 二零二五年度教育机构教师兼职工作合同2篇
- 课程设计批阅意见
- 二零二五年度季度销售奖杯采购与市场调研与竞争分析合同3篇
- 二零二五年度ROHS认证服务及产品保证合同模板2篇
- (人教版新目标)八年级英语上册全册各单元知识点期末总复习讲解教学课件
- 国家开放大学2023年7月期末统一试《11141工程经济与管理》试题及答案-开放本科
- 海康威视枪机摄像机检测报告.文档
- 体检中心组织架构
- 森林抚育投标方案
- 电工作业岗位风险告知卡
- 肿瘤科工作制度
- GB/T 4795-2023船用舱底水处理装置
- 特种设备作业人员考核申请表(样表)
- 融合心理健康教育的教学设计(八年级数学下册苏科版教案)
- 企业实际控制人的协议书
评论
0/150
提交评论