分享
分享赚钱 收藏 举报 版权申诉 / 16

类型编程实习报告09061109-杨宇超.doc

  • 上传人:精品资料
  • 文档编号:9787332
  • 上传时间:2019-09-04
  • 格式:DOC
  • 页数:16
  • 大小:188KB
  • 配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    编程实习报告09061109-杨宇超.doc
    资源描述:

    1、(2009 届)2011 年 7 月 5 日2019 年 9 月 4 日编 程 实 习 报 告 题 目: 用字符串指针实现学生成绩管理系统姓 名: 杨宇超 学 号: 09061109 学 院: 自动化学院 班 级: 09063011 指导教师: 席旭刚 高云园摘要:本文介绍运用 C 语言中单用字符串指针实现一个学生信息管理工具软件,主要功能有:add、show、sort、Delete。程序由七个子函数(creat、edit、extra、imdelete、print、save、)和一个主函数(main)组成。关键字:C 语言、用字符串指针、学生信息一、任务要求(用字符串指针实现学生成绩管理系统,

    2、具体完成的功能见可执行程序 Student.exe。完成函数void DeleteStudent(char* students,int* marks);void SortClass(char* students,int* marks);void ShowClass(char* students,int* marks);void EditMarks(char* students,int* marks);二、详细设计(分析各函数的功能,设计各函数的处理过程及其流程图)void InsertStudent(char* students,int* marks);插入学生姓名 基本想法:先读取学生姓名,

    3、判断指针是否为空,为空则建立内存。否则再开拓新的内存空间,然后将读取的学生与名单一一比较,若相同,则显示已存在该学生,若没有,则开拓内存给新到的学生,并对名单进行排序,最后释放内存。void DeleteStudent(char* students,int* marks);删除学生信息基本想法:先读取要删除的学生姓名,将读入的学生姓名与已存在的学生进行比较,如相同,记住该学生所在位置,判断该学生下一个是否为空,若为空,就可以直接把该学生内存释放掉。若不是,则用一个 while 将后面的所有学生向前移一位 直到 NULL,再释放最后的内存,最后再释放学生姓名的那个内存void SortClass

    4、(char* students,int* marks);排序 基本想法:采用冒泡法来进行排序。N 次排序 先进行 n-1 次比大小,找到最小的,与第一个交换,再进行 n-2 次。 。 。 。void ShowClass(char* students,int* marks);显示所有学生信息基本想法:直接用 printf 输出,void EditMarks(char* students,int* marks);编辑学生成绩基本想法:先读取要编辑的学生姓名,然后与所有的学生姓名进行比较,判断是否在名单内,若不在,就输出不在,否则 就再读取该学生的 5 个成绩到 marks 三、编码实现#inclu

    5、de #include #include void InsertStudent(char* students,int* marks);void DeleteStudent(char* students,int* marks);void SortClass(char* students,int* marks);void ShowClass(char* students,int* marks);void EditMarks(char* students,int* marks);char* ReadLine();#define merror(a) printf(“memory allocation

    6、error %dn“,a);exit(1);/* function main- */int main()char* students = NULL;int* marks = NULL;char line100;int menu;while(1) printf(“Enter (1) to Add a Student to Class Listn“ (2) to Delete a Student from Class Listn“ (3) to show Class Listn“ (4) to Edit marks of a Studentn“ (5) to Quitn“);fflush(stdo

    7、ut);gets(line);if (sscanf(line,“%d“,continue;if (menu 5)printf(“incorrect selection“);continue;if (menu = 1)InsertStudent(else if (menu = 2)DeleteStudent(else if (menu = 3)ShowClass(students,marks);else if (menu = 4)EditMarks(students,marks);elsebreak;/*endwhile*/return 0;/read here/*end main*/* fun

    8、ction InsertStudent- */*This function prompts the user to enter a new student name.The precise form of the prompt isEnter Student Name to be added to Class List:Then this function reads the name from standard input.But it must be able to read any name of any size! Thus you haveto allocate memory (us

    9、ing malloc() to read the input into, and ifit is not big enough, must use realloc() to extend it. You are notallowed to allocate more than 10 bytes in one allocation orextend your segment by more than 10 bytes in one call to realloc().Do not forget to deallocate the memory before exiting the functio

    10、n!Once the function has the name to be entered, it traverses the array studentto find out if the name is already there. If so, the function displaysa warning message “student xxx already in the class list“ and terminates (where xxx is the name of the student). If the name is not in the array, thearr

    11、ay students is extended by one item (using realloc() and a copyof the string with the name is “placed“ there. Also the array marks isextended by one row (using realloc() and all five marks in that row are set to-1. Then updated arrays students and marks are passed to SortClass()so they can be sorted

    12、 alphabetically, as you are required to keep the classlist and the list of marks in alphabetical order.Note that both students and marks are passed to this function “by reference“,for this function on occasions must be able to modify the value stored in thepointer student (in main() - when putting t

    13、here the very first student or whenrealloc() possibly “moves“ the array in the memory somewhere else. The same appliesto marks.*/void InsertStudent(char* students,int* marks)int found, i;char* students1;int* marks1;students1 = *students;marks1 = *marks;char *name;printf(“Enter Student Name to be add

    14、ed to Class List:n“);fflush(stdout);name = ReadLine();if (students1 = NULL) if (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)mer

    15、ror(4); if (marks10 = (int*) malloc(5*sizeof(int) = NULL)merror(5); marks100=marks101=marks102=marks103=marks104=0;marks11 = NULL;*students = students1;*marks = marks1;return;/* do we have the student yet ? */for(found = i = 0; students1i != NULL; i+) if (strcmp(students1i,name) = 0) found = 1;break

    16、;if (found) printf(“student %s already in the class listn“,name);return;/* so it is not in the class list yet */students1 = (char*) realloc(void*)students1,(i+2)*sizeof(char*);if (students1 = NULL)merror(6);if (students1i = (char*) malloc(strlen(name)+1) = NULL)merror(7);strcpy(students1i,name);stud

    17、ents1i+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=marks1i1=marks1i2=marks1i3=marks1i4=0;marks1i+1 = NULL;SortClass(students1,marks1);*students = students1;*marks = marks1;

    18、/* function DeleteStudent- */*This function prompts the user to enter a student nameto be deleted.The precise form of the prompt isEnter Student Name to be deleted from Class List:Then this function reads the name from standard input.But it must be able to read any name of any size! Thus you haveto

    19、allocate memory (using malloc() to read the input into, and ifit is not big enough, must use realloc() to extend it. You are notallowed to allocate more than 10 bytes in one allocation or extendyour segment by more than 10 bytes in one call to realloc().Do not forget to deallocate the memory before

    20、exiting the function!Note that this part of the code is the same as in InsertStudent(),so you can either copy it or write a function that would do it for both.Once the function has the name to be deleted, it traverses the arraystudent to find out if it is there. If not, a warning message isdisplayed

    21、 “student xxx not in the class list“ and the function terminates.If it is found (say its is studentsd), a dynamic one-dimensional array oneitem shorter than students is created and the strings from the array studentsare “unhooked“ from the arrays students and “hooked“ to the new array with theexcept

    22、ion of the string to be deleted (dont forget thatthe “deleted“ string must be deallocated). Now deallocate the array studentsand make the pointer student point to the new array. As you can see we havethe “same“ array of string as before, but with one string deleted. You mustdo the same with the arra

    23、y marks, for you must delete completely d-th row(so it did correspond to studentd which we deleted).Note that both students and marks are passed to this function “by reference“,for this function on must be able to modify the value stored in the pointerstudent (in main() - when a student name is dele

    24、ted. The same aplies to marks.*/void DeleteStudent(char* students,int* marks)int found, i, j,a,b;char* studentsx;int* marksx; char* students1;int* marks1;char* namex;char* name;printf(“Enter Student Name to be deleted from Class List:n“);fflush(stdout);name = ReadLine();namex=name;studentsx=*student

    25、s; marksx=*marks;/* do we have the student yet ? */for(found = i = 0; studentsxi != NULL; i+) if (strcmp(studentsxi,name) = 0) found = 1;j=i;b=i;/*give groop space*/students1 = (char*) malloc(b+1)*sizeof(char*); marks1 = (int*) malloc(b+1)*sizeof(int*); if (found) for(i=0;i=0)temp=studentsi;students

    26、i=studentsi+1;studentsi+1=temp;temp1=marksi;marksi=marksi+1;marksi+1=temp1; /* function ShowClass- */*In this function you display on the screen eachstudent in the class and his/her 5 marks on a line.If the mark is -1 display a space instead. After thename of student display : and separate the marks

    27、 by ,Note that both students and marks are passed to thisfunction “by value“, for this function does not need to modify thevalue stored in the pointer student. The same applies to marks.*/void ShowClass(char* students,int* marks)int i;for(i=0;studentsi!=NULL;i+)printf(“%s,%d,%d,%d,%d,%dn“,studentsi,

    28、marksi0,marksi1,marksi2,marksi3,marksi4);/* function EditMarks- */*This function prompts the user to enter a student namewhose marks are to be edited. The precise form of the promptis Enter Student Name whose marks are to be edited:Then this function reads the name from standard input.But it must be

    29、 able to read any name of any size! Thus you haveto allocate memory (using malloc() to read the input into, and ifit is not big enough, must use realloc() to extend it. You are notallowed to allocate more than 10 bytes in one allocation or extendyour segment by more than 10 bytes in one call to real

    30、loc().Do not forget to deallocate the memory before exiting the function!Note that this part of the code is the same as in InsertStudent()and DeleteStudent(), so you can either copy it or write a functionthat would do it for all three.If the student is not found in the array students, a warning mess

    31、age“student xxx not in the Class List“ is displayed on the screen andthe function terminates. If it is found, the students name followedby : and his/her five marks are displayed (again, -1 must be shownas a space, as in ShowClass(). Let us assume that the student inquestion has marks 10,-1,20,15,5Th

    32、en the function displays a prompt to the user forthe first mark to leave the 1st mark 10 unchanged pressotherwise type the new mark and press and reads the usersreply using gets() into a character arrays of size 80. Then it usessscanf() on that array to “extract“ the response. Then the functiondispl

    33、ays a prompt to the user for the second mark to leave the 2nd markunchanged pressotherwise type the new mark and pressand reads the users reply using gets() into a character arrays of size 80.Then it uses sscanf() on that array to “extract“ the response. Etc.for the third, fourth, and fifth.Note tha

    34、t both students and marks are passed to thisfunction “by value“,for this function does not need to modify the value stored in the pointer student.The same applies to marks.*/void EditMarks(char* students,int* marks)int found, i, j;char *name; printf(“Enter Student Name to be added marks in:n“);fflus

    35、h(stdout);name = ReadLine();/* do we have the student yet ? */for(found = i = 0; studentsi != NULL; i+) if (strcmp(studentsi,name) = 0) found = 1;j=i;break;if (found) printf(“please input %ss marks of five classes divided with an ,n“,name);scanf(“%d,%d,%d,%d,%d“,return;else printf(“wrong student nam

    36、en“);return;/*fuction readline*/char* ReadLine()char *buf;int i, j, size;if (buf = (char*) malloc(10) = NULL)merror(0);size = 10;j = -1;while(1) for(i = 0; i 10; i+) buf+j = getchar();if (bufj = n) bufj = 0;break;/*endfor*/if (i = 10 if (buf = (char*) realloc(void*)buf,size) = NULL)merror(1);continu

    37、e;break;/*endwhile*/return buf;/*end ReadLine*/四、程序调试(说明调试过程、问题及其解决方法)1.在 editmarks 这个函数中 我采用 scanf 这个函数来读取成绩,在读取第五个成绩的时候发现读完后会在函数主菜单里出现一次的错误 这时我上网查了资料 发现应该在第五个输入成绩的时候在%s 后面再加一个%c 来吃掉一个回车键 这样主菜单就不会出现菜单错误的情况 2.在调试的时候,出现了很多失误小错误,不过后来在老师和同学的帮助下。看了书后,最后都一一解决了。 五、总结1)从刚开始的概念模糊、知识点不清楚、无从下手开始到后来基本能实现功能,从本次

    38、编程实习中学会了很多知识,编程过程中需要一个清晰的思路,一步一步慢慢实现功能,在写代码过程中要养成良好习惯,输入法、代码对齐、漏写符号等一系列的问题都是很值得注意的,另外要彻底知道各条语句的作用及函数的使用规则。2)编程过程中出现的最多问题就是运行时总是跳出警告框提示内存什么的,出现这个问题主要是写代码时候不严谨,多处地方不能统一,导致机器运行是容易出错,避免这种问题要仔细查看代码任何一处地方,所有代码都要遵守规则;3)在编写过程中出现了很多问题,现挑主要问题说明如下:调用函数是,子函数没有形参,调用时却添加了形参;定义变量时没有注意类型,在输入时错误使用了输入格式;一个子函数包含多一些语句时

    39、,容易漏掉大括号导致程序检查时主函数出现错误;输入的方法错误导致运行出错;调用查找、修改函数不能进入函数中,链表地址指向不对,语句放置位子不对;以上问题在老师的指导下,一个一个解决了,同时也给自己加深了编写程序要注意的地方。4)界面的美观设计也是一个值得注意的问题,对使用者是否能顺利使用软件至关重要,所以界面的设计很重要,好的提示以及规则能避免很多不必要的错误。总结:本次编程学习到了很多知识,特别感受到了链表在 C 语言中比数组来的更加方便,虽然完成了本次实习目的,但是设计出的软件还有很大缺陷,不是很完善,在几个地方不是很人性化,需要改进,比如在保存文件有累赘部分,有待完善。本次实习收获很多,受益匪浅。

    展开阅读全文
    提示  道客多多所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:编程实习报告09061109-杨宇超.doc
    链接地址:https://www.docduoduo.com/p-9787332.html
    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    道客多多用户QQ群:832276834  微博官方号:道客多多官方   知乎号:道客多多

    Copyright© 2025 道客多多 docduoduo.com 网站版权所有世界地图

    经营许可证编号:粤ICP备2021046453号    营业执照商标

    1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png



    收起
    展开