C语言实现简单学生信息管理系统

  #include

  #include

  #include

  #include

  typedef struct _student{

  char name[20];

  int age;

  int stuNum;

  int score;

  }student;

  typedef struct _Node{

  student stu1;

  struct _Node* pNext;

  }Node;

  Node *g_head=NULL;

  void menu(){

  printf("------------------

  ");

  printf("- 1录入信息-

  ");

  printf("- 2查看信息-

  ");

  printf("- 3保存信息-

  ");

  printf("- 4读取信息-

  ");

  printf("- 5统计人数-

  ");

  printf("- 6查找信息-

  ");

  printf("- 7修改信息-

  ");

  printf("- 8删除信息-

  ");

  printf("- 0退出-

  ");

  printf("退出不要直接点叉,请按0退出!

  查看文件之前请先读取文件!

  ");

  }

  void input(){

  printf("请输入学生信息

  ");

  Node *pNewNode=(Node*)malloc(sizeof(Node));//创建一个新节点。

  pNewNode->pNext=NULL;

  if(g_head==NULL){

  g_head=pNewNode;

  }

  else{

  pNewNode->pNext=g_head;

  g_head=pNewNode;

  }

  printf("请输入姓名: ");

  scanf("%s",pNewNode->stu1.name);

  printf("请输入年龄: ");

  scanf("%d",&pNewNode->stu1.age);

  printf("请输入学号 : ");

  scanf("%d",&pNewNode->stu1.stuNum);

  printf("请输入成绩 : ");

  scanf("%d",&pNewNode->stu1.score);

  printf("该学生信息输入完毕!

  ");

  }

  void printdate(){

  Node* p=g_head;

  printf(" 姓名 年龄 学号 成绩

  ");

  while(p!=NULL)

  {

  printf(" %s ,%d ,%d ,%d

  ",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);

  p=p->pNext;

  }

  }

  void save(){

  FILE *fp=fopen("E:stu.data","w");

  if(fp==NULL){

  printf("文件打开失败");

  return;

  }

  Node* p=g_head;

  while(p!=NULL){

  fwrite(&p->stu1,1,sizeof(student),fp);

  p=p->pNext;

  }

  fclose(fp);

  printf("文件保存成功!

  ");

  }

  void rs(){

  FILE* fp=fopen("E:stu.data","r");

  if(fp==NULL){

  printf("文件打开失败");

  }

  printf("文件读取成功!

  查看文件请按2

  ");

  student stu;

  while(fread(&stu,1,sizeof(student),fp)){

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

  pNewNode->pNext=NULL;

  memcpy(pNewNode,&stu,sizeof(student));

  if(g_head==NULL){

  g_head=pNewNode;

  }

  else{

  pNewNode->pNext=g_head;

  g_head=pNewNode;

  }

  }

  }

  void count(){

  int a=0;

  FILE* fp=fopen("E:stu.data","r");

  if(fp==NULL){

  printf("文件打开失败");

  return;

  }

  Node* p=g_head;

  while(p!=NULL){

  p=p->pNext;

  a++;

  }

  printf("总人数%d",a);

  }

  void find(){

  int num;

  printf("请输入要查找的学生学号:

  ");

  scanf("%d",&num);

  Node* p=g_head;

  while(p!=NULL){

  if(p->stu1.stuNum==num){

  printf(" %s ,%d ,%d ,%d

  ",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);

  }

  p=p->pNext;

  }

  printf("have not");

  }

  void change(){

  int num;

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

  scanf("%d",&num);

  Node* p=g_head;

  while(p!=NULL){

  if(p->stu1.stuNum==num){

  printf("请输入姓名:

  ");

  scanf("%s",p->stu1.name);

  printf("请输入年龄:

  ");

  scanf("%d",&p->stu1.age);

  printf("请输入学号:

  ");

  scanf("%d",&p->stu1.stuNum);

  printf("请输入成绩:

  ");

  scanf("%d",&p->stu1.score);

  printf("信息更改完毕!");

  }

  p=p->pNext;

  }

  if(p==NULL){

  printf("该学生不存在!

  ");

  }

  }

  void del(){

  int num;

  printf("请输入要删除的学号");

  scanf("%d",&num);

  Node* p=g_head;

  Node*p1,*p2;

  if(p->stu1.stuNum==num){

  p1=p->pNext;

  free(p1);

  }

  if(p->pNext!=NULL){

  p2=p->pNext;

  p->pNext=p->pNext->pNext;

  free(p2);

  }

  printf("学号为%d的信息删除成功!

  ",num);

  }

  int main()

  {

  menu();

  while(1)

  {

  char ch=getch();

  switch(ch){

  case '1':input();break;

  case '2':printdate();break;

  case '3':save();break;

  case '4':rs();break;

  case '5':count();break;

  case '6':find();break;

  case '7':change();break;

  case '8':del();break;

  case '0':exit(0);

  }

  }

  return 0;

  }