5链表的应用--学生管理系统

前面介绍过链表,本人觉得链表在运用方面比较重要,下面给出一个实例。

 

学生管理系统

1.实现了链表的创建,以及一些增、删、改、查的基本操作

2.对于信息的存储采用的是文件的读写。将输入的信息保存在.txt中。

 

 

// Student.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include<stdio.h>

#include<time.h>

#include<string.h>

#include<stdlib.h>

#include<conio.h>

 

//学生基本信息

typedef struct Student

{

         int  id;                //学号

         char name[10];                  //姓名

         char sex[3];               //性别

         int chinese;                //语文

         int math;           //数学

         int english         ;        //英语

}Student;

 

//节点类型

typedef struct Node

{

         Student data;

         Node *next;

}Node;

 

Node *head=NULL;

Node *cur=NULL;

 

 

void Loading();                     //加载

void MyTime(int mTime);                   //计时器

void Menu();                         //菜单

void AddStu();                       //增加学生信息

Node* Find(int pos);            //遍历学生信息

void DelStu();                    //删除学生信息

void ChaStu();                       //修改学生信息

void FindStu();                      //查询学生信息

void AllStu();                         //显示全部学生信息

void LoadStu();                          //加载学生信息

void KeepStu();                              //保存学生信息

 

 

 

int _tmain(int argc, _TCHAR* argv[])

{

         system("color 2E");

         Loading();

         Menu();

         system("pause");

         getchar();

         return 0;

}

//计时器

void MyTime(int mTime)

{

         clock_t start,end;

         start=clock();

         end=clock();

         while((end-start)<mTime)

         {

                   end=clock();

         };

 

};

 

//界面加载

void Loading()

{

         printf("\n\n\n\n\n\n\n\n\n\n\n");

         printf("\t\t\t    欢迎进行学生信息系统\n");

         printf("\n\n\n\n\n\n\n\n\n\n\n\n");

         printf("正在加载数据,请稍等。。。");

         MyTime(3000);

};

 

//菜单

void Menu()

{

         system("cls");

         fflush(stdin);

         printf("\t\t********************************************\n");

         printf("\t\t*                                          *\n");

         printf("\t\t*          欢迎使用学生信息系统            *\n");

         printf("\t\t*                                          *\n");

         printf("\t\t********************************************\n");

         printf("\t\t\t1.增加学生信息\n");

         printf("\t\t\t2.删除学生信息\n");

         printf("\t\t\t3.修改学生信息\n");

         printf("\t\t\t4.查询学生信息\n");

         printf("\t\t\t5.显示全部学生信息\n");

         printf("\t\t\t6.加载学生信息\n");

         printf("\t\t\t7.保存学生信息\n");

         printf("\t\t\t8.退出系统\n");

         printf("请输入要操作的编号:(1-8)");

         int ch;

         scanf("%d",&ch);

         switch(ch)

         {

         case 1:

                            AddStu();break;

         case 2:

                            DelStu();break;                             

         case 3:

                            ChaStu();break;                          

         case 4:

                            FindStu();break;                         

         case 5:

                            AllStu();break;

         case 6:

                            LoadStu();break;                       

         case 7:

                            KeepStu();break;

         case 8:    exit(1);

         default:

                   printf("编号不存在,请输入正确的编号\n");

                   MyTime(1000);

         };

         Menu();

};

 

int k=0;

//增加学生信息

void AddStu()

{

         Node *pt=head;

         system("cls");

         fflush(stdin);   

         int ch,count=0;

         Student stu;`

         if(head==NULL)

         {

                   stu.id=1000;

                   pt=head;

                   k++;

         }

         else

                   {

                            stu.id=1000+k;

                            k++;

                   };

         printf("学生姓名:");

         scanf("%s",&stu.name);

         printf("学生性别(1.男|2.女):");

         scanf("%d",&ch);

         if(ch==1)

                   strcpy(stu.sex,"男");

         else

                   strcpy(stu.sex,"女");

         printf("语文:");

         scanf("%d",&stu.chinese);

         printf("数学:");

         scanf("%d",&stu.math);

         printf("英语:");

         scanf("%d",&stu.english);

         Node *p=(Node *)malloc(sizeof(Node));

         p->data=stu;

         if(head==NULL)

         {

                   head=p;

                   cur=p;

                   p->next=NULL;

         }

         else

         {

                   cur->next=p;

                   p->next=NULL;

                   cur=p;

         };

         printf("增加成功\n");

         printf("是否继续增加:(Y/N)");

         ch=getch();

         if(ch=='Y'||ch=='y')

                   AddStu();

         else

         system("cls");

         Menu();

 

};

 

//查询链表

Node* Find(int pos)//传入要查找的位置

{

         Node* tempTtr=head;

         int iCurPos=0;//标记

         while(tempTtr->next!=NULL&&iCurPos<pos)

         {

                   tempTtr=tempTtr->next;

                   iCurPos++;

         };

         if(tempTtr!=NULL&&pos==iCurPos)

         {

                   return tempTtr;

         }

         else

                   {

                            printf("你要查找的位置超过了范围!\n");

                            MyTime(2000);

                            return NULL ;

                   };

};

//删除学生信息                

void DelStu()

{

         LOOP:

         int num;

         system("cls");

         printf("请输入删除学生信息的序号:");

         scanf("%d",&num);

         Node *p=NULL,*temp=NULL;

         int curPos=0,count=0;

 

         //一个节点

         if(head->next==NULL&&num==1)

                   head=NULL;

 

         else if(!head)

         {

                   system("cls");

                   printf("不存在要数据\n");

                   Menu();

         }

         //多个节点

         else if(head->next!=NULL&&num==1)//删除头节点

         {

                   temp=head;

                   head=head->next;

                   delete temp;

                   count++;

         }

         else

         //删除中间节点

         {

                   p=Find(num-1);            //取到前一位节点

                   if(p==NULL)

                            goto LOOP;

                   Node *tempNew=NULL;

                   tempNew=p->next;

                   p->next=tempNew->next;

                   delete tempNew;

                   count++;

         };

         if(count>0)

                   printf("删除成功\n");

         MyTime(2000);

         system("cls");

         Menu();

};               

 

//修改学生信息

void ChaStu()

{

         int id=0,count=0;                                 //要修改的学生学号

         Node* p=NULL;

         system("cls");

         printf("请输入要修改的学生学号:");

         scanf("%d",&id);

         printf("\n");

         p=head;

         while(p!=NULL)

         {

                   if(p->data.id==id)

                   {

                            printf("语文:");

                            scanf("%d",&p->data.chinese);

                            printf("数学:");

                            scanf("%d",&p->data.math);

                            printf("英语:");

                            scanf("%d",&p->data.english);

                            ++count;

                            break;

                   }

         p=p->next;

         };

         if(count>0)

                   printf("修改成功\n");

         else

                            printf("不存在该学号");

         MyTime(2000);

         system("cls");

         Menu();

};               

 

//

int AllScore(Node *p)

{

         while(p!=NULL)

         {

                    return (p->data.chinese)+(p->data.math)+(p->data.english);

                    p=p->next;

         }

};

int  Adver(Node *p)

{

         while(p!=NULL)

         {

                   return ((p->data.chinese)+(p->data.math)+(p->data.english))/3;

                    p=p->next;

         }

};

//查询学生信息

void FindStu()

{

         int count=0;    

         char cname[10];               //用姓名查询

         Node* p=NULL;

         system("cls");

         printf("请输入要查询的学生姓名:");

         scanf("%s",cname);

         printf("\n");

         p=head;

         while(p!=NULL)

         {

                   if(strcmp(cname,p->data.name)==0)

                   {

                            if(count==0)

                            {

                                     printf("学号\t姓名\t性别\t语文\t数学\t英语\t总分\t平均分\n");

                            };

                            printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t",p->data.id,p->data.name,p->data.sex,

                                               p->data.chinese,p->data.math,p->data.english,AllScore(p),Adver(p));

                            count++;

                            break;

                   };

         p=p->next;

         };

         if(count==0)

                   {

                            system("cls");

                            printf("不存在该学生\n");

                            MyTime(2000);

                            system("cls");

                            Menu();

         };

         printf("\n按任意键返回\n");

         int ch=getch();

         Menu();

};                        

 

//显示全部学生信息

void AllStu()

{

         system("cls");

         Node* pp=NULL;

         int count=0;

         if(head==NULL||!head)

         {

                   printf("没有数据");

                   MyTime(2000);

                   system("cls");

                   Menu();

         };

         pp=head;

         while(pp!=NULL)

         {

                   if(count==0)

                            printf("学号\t姓名\t性别\t语文\t数学\t英语\t总分\t平均分\n");

                   printf("%d\t%s\t%s\t%d\t%d\t%d\t%d\t%d\n",pp->data.id,pp->data.name,

                                pp->data.sex,pp->data.chinese,pp->data.math,pp->data.english,AllScore(pp),Adver(pp));

                   pp=pp->next;

                   count++;

                   if(pp==NULL)

                            break;

         };

         printf("\n按任意键返回");

         int ch=getch();

         system("cls");

         Menu();

};               

 

//加载学生信息       

void LoadStu()

{

         system("cls");

         FILE *fp=fopen("Student1.txt","rb");

         if(fp==NULL)

         {

                   printf("加载数据失败");

                   MyTime(2000);

                   Menu();

         };

         Student stu;

         while(fread(&stu,sizeof(Student),1,fp));

         {

                   Node *p=(Node *)malloc(sizeof(Node));

                   p->data=stu;

                   p->next=NULL;

                   if(head==NULL)

                   {

                            head=p;

                            cur=p;

                   }

                   else

                   {

                            cur->next=p;

                            cur=p;

                   };

 

         };

 

         printf("加载成功");

         MyTime(2000);

         system("cls");

         Menu();

};                        

 

//保存学生信息

void KeepStu()

{

         system("cls");

         if(head==NULL)

         {

                   printf("没有数据可保存");

                   MyTime(2000);

                   Menu();

         };

         FILE *fp=NULL;

         fp=fopen("Student1.txt","wb");

         Node *p=head;

         while(p!=NULL)

         {

                   fwrite(&(p->data),sizeof(Student),1,fp);

                   p=p->next;

         };

         printf("保存成功");

         fclose(fp);

         MyTime(2000);

         Menu();

};                        

 

posted @ 2018-01-09 18:01  gd_沐辰  阅读(185)  评论(0编辑  收藏  举报