设为首页 加入收藏

TOP

数据结构程序设计――C语言学生管理(动态链表)(一)
2014-11-23 20:15:58 来源: 作者: 【 】 浏览:2
Tags:数据结构 程序设计 语言 学生管理 动态
【前言】 寒假回家也没闲着呀,花了4天晚上的时间完成了数据结构的课程设计。以我的观点来看,这个东西大一就应该做了,无非就是结构体,链表,增,删,查,改。坑爹是的,有三个不同的txt文件存放数据,有一种 数据库外键的感觉。总体来说仿佛回到了大一写黑框框程序的日子了,又巩固了C语言。不能说是浪费时间,收获也不小,隐藏BUG还在探索当中。代码写的不算漂亮,肯定可以优化。 ~ ~!。
【运行截图】 \ \ \ \

【需求】 1.有三个文件Student.txt,Course.txt,SC.txt分别存放学生基本,课程,学生成绩,程序运行时从这三个文件中读取数据,程序关闭时更新这三个文件。 2.对学生进行查找,删除,增添,修改等操作。 3.查询时采用多样查询。 4.添加时数据的检测。
【设计图】 \ \

【代码】
/*
* 课程设计:C语言版学生管理
* 作者:王小康
* 时间:2014-1-11
* 平台:windows
*/

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #define LEN_COURSE sizeof(Course) #define LEN_STUDENT sizeof(Student) typedef struct Course { int id; /*课程编号*/ char name[20]; /*课程名称*/ int credit; /*课程学分*/ int grade; /*课程成绩*/ Course *next; /*课程指针,1.记录课程时所用,2.用于学生结构中*/ }Course; typedef struct Student { char num[10]; /*学号*/ char name[16]; /*姓名*/ char sex[10]; /*姓名*/ int age; /*年龄*/ char dept[20]; /*专业*/ Course *headCourse; /*学生所修的课程信息*/ Student *next; /*学生指针,指向下一个学生*/ }Student; /*全局变量,程序启动时加载三个文件夹,这两个变量作为储存数据的实体*/ Student *student = NULL; Course *course = NULL; /*读取Student.txt文件中内容创建学生链表*/ Student* Creat_Student() { Student *head; Student *p1,*p2; p2 = (Student*)malloc(LEN_STUDENT); head = p2; FILE *fp; if((fp = fopen("Student.txt","r")) == NULL) { printf("◎学生文件打开错误,请将Student.txt放置与程序同一目录下!!!\n"); exit(0); } for(int i = 0; i<1; i++) fscanf(fp,"%*[^\n]%*c"); //跳过第一行开始读取 while (!feof(fp)) { p1 = (Student*)malloc(LEN_STUDENT); fscanf(fp,"%s%s%s%d%s",p1->num,p1->name,p1->sex,&p1->age,p1->dept); p1->headCourse = (Course*)malloc(LEN_COURSE); p1->headCourse->next = NULL; p1->next = NULL; p2->next = p1; p2 = p1; } fclose(fp); return head; } /*读取Couse.txt文件内容创建课程链表*/ Course* Creat_Course() { Course *head; Course *p1,*p2; p2 = (Course*)malloc(LEN_COURSE); head = p2; FILE *fp; if((fp = fopen("Course.txt","r")) == NULL) { printf("◎课程信息文件打开错误,请将Course.txt放置与程序同一目录下!!!\n"); exit(0); } for(int i = 0; i<1; i++) fscanf(fp,"%*[^\n]%*c"); //跳过第一行开始读取 while (!feof(fp)) { p1 = (Course*)malloc(LEN_COURSE); fscanf(fp, "%d%s%d",&p1->id,p1->name,&p1->credit); p1->next = NULL; p2->next = p1; p2 = p1; } fclose(fp); return head; } /*读取SC.txt完善学生信息*/ void Read_SC() { Student* FindStudentByNum(char s[]); Course* FindCourseById(int n); Student* stu =NULL; Course* cou = NULL; char s_num[10]; //储存Snum int c_id; //储存Cid int grade; //储存Grade FILE *fp; if((fp = fopen("SC.txt","r")) == NULL) { printf("◎课程信息文件打开错误,请将Course.txt放置与程序同一目录下!!!\n"); exit(0); } for(int i = 0; i<1; i++) fscanf(fp,"%*[^\n]%*c"); //跳过第一行开始读取 while (!feof(fp)) { fscanf(fp,"%s%d%d",s_num,&c_id,&grade); if((stu=FindStudentByNum(s_num)) != NULL && (cou=FindCourseById(c_id)) != NULL) { Course *p = stu->headCourse; //指针移动到最后一个结点 while (p->next != NULL) { p = p->next; } p->next = (Course*)malloc(LEN_COURSE); Course *q = p->next; q->id = cou->id; strcpy(q->name,cou->name); q->credit = cou->credit; q->grade = grade; q->next = NULL; } } fclose(fp); } /*根据学号查找学生,返回指向该学生的指针*/ Student* FindStudentByNum(char s[]) { Student* p = student; Student* temp = NULL; while (p!=NULL) { if(strcmp(p->num,s) == 0) { temp = p; break; } else { p = p->next; } } return temp; } /*根据ID查找课程,返回指向该课程的指针*/ Course* FindCourseById(int n) { Course* p = course; Course* temp = NULL; while (p!=NULL) { if(n == p->id) { temp = p; break; } else { p = p->next; } } return temp; } /*根据Name查找课程,返回指向该课程的指针*/ Course* FindCourseByName(char name[]) { Course* p = course; Course* temp = NULL; while (p!=NULL) { if(strcmp(name,p->name) == 0) { temp = p; break; } else { p = p->next; } } return temp; } /*辅助功能判断这个学生是否存在*/ bool IsStudent(char num[]) { Student *p = student->next; bool flag = false; while (p != NULL) { if(strcmp(p->num,num) == 0) { flag = true; } p = p->next; } return flag; } /*【1-1】添加基本数据*/ int Add_Inform() { Student *newStu = NULL; Student *p = student; char stu_num[10]; char stu_name[16]; char stu_sex[10]; int stu_age; char stu_dept[20]; printf("\n---------◎请输入学生基本信息--------\n"); printf("学号 姓名 性别 年龄 专业\n")
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C指针原理(54)-Ncurses-文本终端.. 下一篇C指针原理(55)-C语言-pvm并行计算

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: