| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1608 人关注过本帖
标题:求大神帮助修改,将文件改成单链表形式保存
只看楼主 加入收藏
看了就看了
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2019-12-31
收藏
 问题点数:0 回复次数:2 
求大神帮助修改,将文件改成单链表形式保存
把保存信息用保存进单链表的方式呈现出来   不是文件形式   查看的话可以不要

#include "stdio.h"
#include"stdlib.h"
#include"string.h"
//定义学生
  typedef struct student
  {
   char name[10];
   int number;
   char sex[2];
   int jump;
   int vatil;
   int up;
   int average;
   int nian;
  }student;

//定义接点
  typedef struct node
  {
   student date;
   struct node *next;
  }node;

//建立链表并返回指针
  node* build()
  {
   node *p;
   if((p=(node*)malloc(sizeof(node)))==NULL)
   {
       printf("=>初始化失败!");
       return 0;
   }
  else
  {
      p->next=NULL;
      p->date.number=0;//头结点存放学生人数
      printf("初始化成功!\n");
      return p;
  }
  }

//增加学生
  void add(node *head)
  {
    node *p,*q;
    int m,n=0;
    q=head->next;
    p=(node*)malloc(sizeof(node));
    printf("=>请输入新生姓名!\n");
    gets(p->date.name);
    fflush(stdin);
    printf("=>请输入学号!\n");
    while(n==0)
    {
      scanf("%d",&m);fflush(stdin);
      if(q==NULL) n=1;
      while(q)
      {
        if(q->date.number==m)
        {
          printf("=>你输入的学号与已有同学的学号相同,请重新输入!\n");
          q=head->next;
          break;
        }

      else
      {
        q=q->next;
       if(q==NULL) n=1;
      }
      }
    }
     p->date.number=m;

     printf("=>请输入性别!\n");
     gets(p->date.sex);
     fflush(stdin);
     
     printf("=>请输入年份!\n");
     scanf("%d",&m);
     fflush(stdin);
     p->date.nian=m;

     printf("=>请输入跳远成绩\n");
     scanf("%d",&m);
     fflush(stdin);
     p->date.jump=m;

     printf("=>请输入肺活量成绩\n");
     scanf("%d",&m);
     fflush(stdin);
     p->date.vatil=m;

     printf("=>请输入仰卧起坐或引体向上成绩\n");
     scanf("%d",&m);
     fflush(stdin);
     p->date.up=m;

     p->date.average=(p->date.jump+p->date.vatil+p->date.up);
     q=head->next;
     head->next=p;
     p->next=q;
     head->date.number++;
  }

//删除一名学生
  void deletion(node *head)
  {
    node *p=head->next,*q=head;
    char N[10];
    printf("=>请输入你想删除的学生姓名!\n");
    gets(N);
    fflush(stdin);
    if(p==NULL)
    {
    printf("=>系统无学生可删除!\n");
    }
    while(p)
    {
    if(strcmp(p->date.name,N)==0)
    {
       q->next=p->next;
       head->date.number--;
       printf("=>删除%s成功!\n",p->date.name);
       free(p);
       break;
    }
    else
    {
      p=p->next;
      q=q->next;
      if(p==NULL)
      {
          printf("=>你要删除的学生不存在,删除失败!\n");
      }
    }
    }
  }

//显示学生信息
  int display(node *head)
  {
    node *p=head->next;
    int m,n=0;
    char N[10];
    if(p==NULL)
    {
     printf("=>系统无学生!\n");
     return 0;
    }
    while(n==0)
    {
       printf("******************************************************************************\n");
       printf("=>请选择你的操作!\n");
       printf("=>\t\t\t1.显示所有\t2.按姓名查找\n\t\t\t3.按学号查找\t4.按年份查找\n\t\t\t5.返回主菜单\n");
       scanf("%d",&m);
       fflush(stdin);
       switch(m)
       {
      case 1:printf("\n=>该系统拥有%d名学生!\n\n",head->date.number);
      p=head->next;
      printf("\t姓名\t学号\t性别\t年份\t跳高\t肺活量\t仰(引)\t综合\n");
      while(p)
      {
        printf("\t%-8s%d\t%s\t%d\t%d\t%d\t%d\t%d\n",p->date.name,p->date.number,p->date.sex,p->date.nian,p->date.jump,p->date.vatil,
        p->date.up,p->date.average);
        p=p->next;
      }
        break;
      case 2:printf("=>请输入查找姓名!\n");
      gets(N);
      fflush(stdin);
      p=head->next;
      while(p)
      {
        if(strcmp(p->date.name,N)==0)
        {
          printf("\t姓名\t学号\t性别\t年份\t跳高\t肺活量\t仰(引)\t综合\n");
          printf("\t%-8s%d\t%s\t%d\t%d\t%d\t%d\t%d\n",p->date.name,p->date.number,p->date.sex,p->date.nian,p->date.jump,p->date.vatil,
          p->date.up,p->date.average);
          break;
        }
        else
        {
          p=p->next;
          if(p==NULL)
          {
          printf("=>你要查的学生不存在!");
          }
        }
      }
       break;
     case 3: printf("=>请输入查找学号!\n");
     scanf("%d",&m);
     fflush(stdin);
     p=head->next;
    while(p)
    {
      if(p->date.number==m)
      {
         printf("\t姓名\t学号\t性别\t年份\t跳高\t肺活量\t仰(引)\t综合\n");
         printf("\t%-8s%d\t%s\t%d\t%d\t%d\t%d\t%d\n",p->date.name,p->date.number,p->date.sex,p->date.nian,p->date.jump,p->date.vatil,
         p->date.up,p->date.average);
         break;
      }
      else
      {
        p=p->next;
        if(p==NULL)
        {
        printf("=>你要查的学生不存在!\n");
        }
      }
    }
     break;
     case 4: printf("=>请输入查找年份!\n");
     scanf("%d",&m);
     fflush(stdin);
     p=head->next;
    while(p)
    {
      if(p->date.nian==m)
      {
         printf("\t姓名\t学号\t性别\t年份\t跳高\t肺活量\t仰(引)\t综合\n");
         printf("\t%-8s%d\t%s\t%d\t%d\t%d\t%d\t%d\n",p->date.name,p->date.number,p->date.sex,p->date.nian,p->date.jump,p->date.vatil,
         p->date.up,p->date.average);
         break;
      }
      else
      {
        p=p->next;
        if(p==NULL)
        {
        printf("=>你要查的学生不存在!\n");
        }
      }
    }
     break;
     case 5:n=1;
         break;
       }
    }
    return 1;
  }

//排序
   int range(node *head)
   {
     node *p=head,*q=head->next;
     int n,i,m=head->date.number;
     //printf("*******************************************************************************\n");
     printf("\t\t\t1.按学号\t2.按成绩\t3.按年份\n\n");
     printf("=>请选择操作!\n》");
     scanf("%d",&n);
     fflush(stdin);
     if(q==NULL)
     {
         printf("=>无学生可排序!\n");
         return 0;
     }
     switch(n)
     {
         case 2:for(i=0;i<m;i++)//按总分排序
                {
                   p=head;
                   q=head->next;
                   while(q->next)
                   {
                      if((q->date.average)<(q->next->date.average))
                      {
                          p->next=q->next;
                          p=p->next;
                          q->next=p->next;
                          p->next=q;
                      }
                      else
                      {
                         p=p->next;
                         q=q->next;
                      }
                   }
                }
                 break;
         case 1:for(i=0;i<=m;i++)//按学号排序
                {
                  p=head;
                  q=head->next;
                  while(q->next)
                  {
                    if((q->date.number)>(q->next->date.number))
                    {
                      p->next=q->next;
                      p=p->next;
                      q->next=p->next;
                      p->next=q;
                    }
                    else
                    {
                      p=p->next;
                      q=q->next;
                    }
                  }
                }
                 break;
         case 3:for(i=0;i<=m;i++)//按年份排序
                {
                  p=head;
                  q=head->next;
                  while(q->next)
                  {
                    if((q->date.nian)>(q->next->date.nian))
                    {
                      p->next=q->next;
                      p=p->next;
                      q->next=p->next;
                      p->next=q;
                    }
                    else
                    {
                      p=p->next;
                      q=q->next;
                    }
                  }
                }
                 break;
     }
      printf("=>排序成功!\n");
      return 1;
   }
   void menu()
   {
      printf("\n********************************* 主菜单 ***************************************\n");
      printf("\t\t\t1.新建系统\t2.添加学生\n\t\t\t3.删除学生\t4.查看信息\n\t\t\t5.排列顺序\t6.读取信息\n\t\t\t7.保存信息\t8.退出系统\n");
      printf("=>请选择你的操作!\n");
   }

//保存
   int save(node *head)
   {
     FILE *fp;
     node *p;
     p=head;
     if((fp=fopen("d:\\学生","wb+"))==NULL)
     {
       printf("=>保存失败!");
       return 0;
     }
     while(p)
     {
         fwrite(&(p->date),sizeof(student),1,fp);
         p=p->next;
     }
     printf("=>保存成功!");
     fclose(fp);
     return 1;
   }

//读入系统学生信息
  node* read()
  {
   int i;
   node *p,*q,*head;
   FILE *fp;
   if((head=(node*)malloc(sizeof(node)))==NULL)
   {
       printf("=>开辟空间失败!");
       exit(1);
   }
   else
   {
     head->next=NULL;
     head->date.number=0;//头结点存放学生人数
   }
   q=head;
   if((fp=fopen("d:\\学生","rb"))==NULL)
   {
       printf("=>系统无学生!\n");
       return 0;
   }
   fread(&(q->date),sizeof(student),1,fp);
   for(i=0;i<head->date.number;i++)
   {
       if((p=(node*)malloc(sizeof(node)))==NULL)
       {
           printf("=>读值失败!");
           exit(1);
       }
       fread(&(p->date),sizeof(student),1,fp);
       p->next=q->next;
       q->next=p;
   }
    fclose(fp);
    return head;
  }


  void main()
  {
    int m=0,n,i;
    node *head;
    //printf("********************************************************************************\n\n");
    printf("\t\t\t* 欢迎使用学生成绩管理系统 * \n");
    printf("=>正在读取系统信息......\n");
    if((head=read())==0)
    {
       printf("=>是否新建?\n");
       printf("\t\t\t是(1)\t\t否(2)\n");
       scanf("%d",&i);
       fflush(stdin);
       if(i==1)
       head=build();
       else if(i==2)
       exit(1);
    }
    else
     printf("=>信息导入成功!");
    while(m==0)
    {
       menu();
       scanf("%d",&n);
       fflush(stdin);
   switch(n)
   {
     case 1: head=build();
         break;
     case 2:add(head);
         break;
     case 3:deletion(head);
         break;
     case 4:display(head);
         break;
     case 5: range(head);
         break;
     case 6:head=read();
         if(head!=0) printf("=>读取存盘成功!\n");break;
     case 7:save(head);
         break;
     case 8:m=1;
         break;
   }
    }
  }
搜索更多相关主题的帖子: printf date node next head 
2020-01-02 15:52
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10608
专家分:43190
注 册:2014-5-20
收藏
得分:0 
是要保存为文本文件格式文件吧
2020-01-02 17:12
看了就看了
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2019-12-31
收藏
得分:0 
回复 2楼 吹水佬
emmmm  就是改成不用文件的形式啦   我们改好啦已经  
2020-01-06 13:28
快速回复:求大神帮助修改,将文件改成单链表形式保存
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035548 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved