利用C语言版本的数据库制作一个学生成绩管理系统

                                                                  本人代做课程设计(qq:1905673854)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TRUE 1
#define FLASE -1
typedef struct student
{
 char name[10];
 char sex[10];
 char Class[10];
 float mark;
 int age;
 struct student *prev;
 struct student *next;
}node;/*定义结构体*/
char strbuf[40];
char strmark[10];
FILE *fp;/*定义只想文件的指针*/
//将链表中的学生信息导入到文本当中
void save(node *h)
{
 char strbuf[40];//定义一个字符串来储存链表学生信息中的姓名,性别,班级。
    char strmark[10];//用来储存链表中的学生的年龄,成绩。
 if((fp=fopen("D:\student.txt","wb"))==NULL)//判断是否打开文本。!!!!注意你要储存信息的文本最好添加在除C盘之外的地方
 {
  printf("不能打开该文件");
 }
 node *p=h;
 while(p)//通过判断p是否为空
 {
  strcpy(strbuf,p->name);//将链表中的名字赋值给字符串。
  strcat(strbuf,".");
  strcat(strbuf,p->sex );
  strcat(strbuf,".");
  strcat(strbuf,p->Class );
  strcat(strbuf,".");
  itoa(p->age ,strmark,10);//将int类型装换为字符串类型
  strcat(strbuf,strmark);
  strcat(strbuf,".");
  itoa(p->mark ,strmark,10);
  strcat(strbuf,strmark);
  fwrite(strbuf,1,strlen(strbuf),fp);//写入到文本当中
  fwrite(" ",1,strlen(" "),fp);//换行
  p=p->next ;
 }
  fclose(fp); //一定要关闭文本否则将保存失败
  printf("保存成功 ");
}
//打印链表中的信息
void print(node *h)
{
 if (h == NULL)
 {
  return ;
 }
 else
 {
  node *p = h;
  while (p)
  {
   printf("姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0f ", p->name, p->age,p->sex,p->Class ,p->mark );
   p = p->next;
  }
  printf(" ");
 }
}
//用来将文本中的信息重新插入到链表当中
node *creat3(node *h,char strstuname[10],char sex[10],char class1[10] ,int age,float mark)
{
 node *p = (node *)malloc(sizeof(node));
 memset(p, 0, sizeof(node));
 strcpy(p->name ,strstuname);
 strcpy(p->sex ,sex);
 strcpy(p->Class ,class1);
 p->age=age;
 p->mark =mark;
 p->next = NULL;
 p->prev = NULL;
 if (h == NULL)
  {
   h = p;
  }
   else
  {
   p->next = h;
   h->prev = p;
   h = p;
  }
  /*printf("姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%f ", p->name, p->age,p->sex,p->Class ,p->mark );*/
 /* return h; */
}
//将文本中的信息添加到链表当中
node* load(node *h)
{
 char strbuf[50]={0};
    char strstuname[10]={0};
    char strstusex[10]={0};//为了来储存文本中的信息。
    char strstuage[10]={0};
    char strstuclass[10]={0};
    char strstumark[10]={0};
if((fp=fopen("D:\student.txt","rb"))==NULL)
 {
  printf("不能打开该文件");
  exit(0);
 } 
 node *p=h;
 int ncount=0;
 int j=0;
 while(NULL !=fgets(strbuf,50,fp))
 {
  int i=0;
  ncount=0;
  j=0;
  
  for(i=0;strbuf[i]!=' ';i++)
  {
   if(0==ncount)
   {
    strstuname[i]=strbuf[i];
    if(strbuf[i]=='.')
    {
     strstuname[i]='';
     ncount++;
    }
   }
   else if(1==ncount)
   { 
    strstusex[j]=strbuf[i];
    j++;
    if(strbuf[i]=='.')
    {
     strstusex[j-1]='';
     ncount++;
     j=0;     
    }
    
   }
   else if(2==ncount)
   {
    strstuclass[j]=strbuf[i];
    j++;
    if(strbuf[i]=='.')
    {
     strstuclass[j-1]='';
     ncount++;
     j=0;
    }
   }
   else if(3==ncount)
   {
    
    strstuage[j]=strbuf[i];
    j++;
    if(strbuf[i]=='.')
    {
     strstuage[j-1]='';
     ncount++;
     j=0;
    }    
   }
   else
   {
    strstumark[j]=strbuf[i];
    j++;
   }
  }
  h=creat3(h,strstuname,strstusex,strstuclass,atoi(strstuage),atoi(strstumark));//调用前面的插入函数来插入到链表当中
 }
  printf("导入成功 ");
 fclose(fp);//!!!!一定要记得关闭文本
return h;
}
//从头部插入
node  *creat(node *h)

 while (1)
 {
   node *p = (node *)malloc(sizeof(node));
   if (p == NULL)
    return h;
    memset(p, 0, sizeof(node)); 
    printf("请输入学生的年龄:");
    int age;
    scanf("%d",&age );
    if (age==0)
   {
    break;
   }
   p->age =age;
    printf("请输入学生的名字:");
    scanf("%s",&p->name);
    printf("请输入学生的性别:");
    scanf("%s",p->sex );
    printf("请输入学生的班级:");
    scanf("%s",&p->Class );
    printf("请输入学生的成绩");
    scanf("%f",&p->mark ) ;
   p->next = NULL;
   p->prev = NULL;
   if (h == NULL)
   {
    h = p;
   }
   else
   {
    p->next = h;
    h->prev = p;
    h = p;
   }  
 }
 return h;
}
//从尾部插入
node *creat1(node *h)
{
 while (1)
 {
   node *p = (node *)malloc(sizeof(node));
   if (p == NULL)
    return h;
   memset(p, 0, sizeof(node));
    printf("请输入学生的年龄:");
    int age;
    scanf("%d",&age );
    if (age==0)
   {
    break;
   }
   p->age =age;
    printf("请输入学生的名字:");
    scanf("%s",&p->name);
    printf("请输入学生的性别:");
    scanf("%s",p->sex );
    printf("请输入学生的班级:");
    scanf("%s",&p->Class );
    printf("请输入学生的成绩");
    scanf("%f",&p->mark ) ;
   p->next = NULL;
   p->prev = NULL;
   if (h == NULL)
   {
    h = p;
   }
   else
   {
    node *q = h;
    while (q->next)
    {
     q = q->next;
    }
    q->next = p;
    p->prev = q;
   }
 }
 return h;
}
node* sort(node *h)
{
  int temp;
 if(h==NULL)
 {
  return h;
 }
  else if(h->next==NULL)
 {
  return h;
 }
 else
 {
  node *p=h;
  node *i;
  node *q;
  /*node *p;*/
  while(p)
  {
   for(i=p;i->next!=NULL;i=i->next)
   {
    q=i->next ;
    if(i->age<q->age )
    {
     temp=q->age ;
     q->age =i->age ;
        i->age=temp;  
    }
   }
   p=p->next;
  }
  return h;
 }
}
//从尾部打印
void Lastprint(node *h)
{
 if(h==NULL)
 {
     return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
  printf("姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0f ", p->name, p->age,p->sex,p->Class ,p->mark );
  p=p->prev ; 
  }
  printf(" ");
 }
}
//通过成绩查询
void select1(node *h,float mark)
{
 if(h==NULL)
 {
  return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
   if(p->mark==mark)
   {
       printf("姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:.0%f ", p->name, p->age,p->sex,p->Class ,p->mark );
      }
   p=p->next ;
  }
  printf(" ");
 }
}
//通过姓名修改
void update1(node *h,char name[10])
{
 float mark;
 if(h==NULL)
 {
  return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
   if(strcmp(p->name ,name)==0)
   {
    printf("请输入要修改的成绩");
    scanf("%f",&mark);
       p->mark =mark;
    printf("姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0f ", p->name, p->age,p->sex,p->Class ,p->mark );
   }
   p=p->next;
  }
 }
}
node *Sort(node *h)
{
 float temp;
 if(h==NULL)
 {
  return h;
 }
  else if(h->next==NULL)
 {
  return h;
 }
 else
 {
  node *p=h;
  node *i;
  node *q;
  /*node *p;*/
  while(p)
  {
   for(i=p;i->next!=NULL;i=i->next)
   {
    q=i->next ;
    if(i->mark<q->mark )
    {
     temp=q->mark ;
     q->mark  =i->mark;
        i->mark=temp;  
    }
   }
   p=p->next;
       }
 }
}
//排序之后的成绩打印
void SortMark(node *h)
{
  if (h == NULL)
 {
  return ;
 }
 else
 {
  node *p = h;
  while (p)
  {
   printf("%f ",p->mark );
   p = p->next;
  }
  printf(" ");
 }
}
//通过姓名查找
void select2(node *h,char name[10])
{
 if(h==NULL)
 {
  return ;
 }
 else
 {
  node *p=h;
  while(p)
  {
   if(strcmp(p->name ,name)==0)
   {
     printf("姓名:%s    年龄:%d   性别:%s   班级:%s   成绩:%.0f ", p->name, p->age,p->sex,p->Class ,p->mark );
   }
   p=p->next ;
  }
  printf(" ");
   printf("查找无此人");
 }
}
//销毁链表
void destroy(node *h)
{
 if (h == NULL)
 {
  return;
 }
 else
 {
  node *p=h;
  while (p)
  {
   free(p);
   p = p->next;
  }
 }
}
//通过姓名来删除链表中的信息
int delete1(node **h, char name[10])
{
 if (*h == NULL)
 {
  return -1 ;
 }
 else
 {
  node *p = *h;
  while (p)
  {
   if (strcmp(p->name,name)==0)
   {
    if (p ==*h)
    {
     *h =(*h)->next;
     (*h)->prev = NULL;
     free(p);
    }
    else if (p->next == NULL)
    {
     p->prev->next = NULL;
     free(p);
    }
    else
    {
     p->prev->next = p->next;
     p->next->prev = p->prev;
     free(p);
    }
   }
   p = p->next;
  }
  return -1;
 }
}
int main(int argc , char* argv[])
{
node *h=NULL; 
 int temp;
 int da=1;
 printf("                       **************************  开始 学生成绩管理系统   *********************** ");
 printf("                       ************************** (1) 从链表的头部插入   *********************** ");
 printf("                       ************************** (2) 从链表的尾部插入   *********************** ");
 printf("                       ************************** (3) 打印链表中的信息   *********************** ");
 printf("                       ************************** (4) 通过姓名查找学生的信息并打印********************** ");
 printf("                       ************************** (5) 通过姓名修改学生的成绩:*********************** ");
 printf("                       ************************** (6) 通过姓名删除学生的信息:*********************** ");
 printf("                       ************************** (7) 成绩从大到小的排序:*********************** ");
 printf("                       ************************** (8)       写入到文本       :*********************** ");
 printf("                       ************************** (9)       从文本中读出       :*********************** ");
 printf("                       ************************** (10)       退出程序       :*********************** ");
 while(da)
 {
 printf("请输入数字来选择功能 ");
 scanf("%d",&temp);
 switch(temp)
 {
  case 1: h=creat(h);
  break; 
  case 2: creat1(h);
  break;  
  case 3:  Print(h);
  break;
  case 4: char name [10];
 printf("请输入要查询的名字");
 scanf("%s",&name);
 select2(h,name);
 break;
 case 5:
  printf("请输入要修改的学生成绩的名字");
 char name1 [10];
 scanf("%s",&name1);
 update1(h,name1);
 Print(h);
 break ;
 case 6:
 printf("请输入要删除学生的信息");
  char name2[10];
  scanf("%s", &name2);
  delete1(&h, name2);
  Print(h);
  break ;
  case 7:
  Sort(h);
  SortMark(h);
  break;
  case 8:
   save(h);
   break;
   case 9:
    h=load(h);
    break;
  case 10:
   da=0;
   break;
 }
}
}

原文地址:https://www.cnblogs.com/Ljq12333/p/8004960.html