⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newwintc.c

📁 学生信息管理系统,用链表实现,包含删除,增加,查询,保存到文件等功能
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <string.h>
#define LEN sizeof(struct stu_info)



struct mark
{
char name[10];/* 科目名称 */
float fen;/* 科目分数 */
};
union stu_sign
{
char num[8];/* 学号 */
char name[10];/* 姓名 */
struct mark mark[4];/* 数学,外语,体育,平均分 */
};

struct stu_info 
{
char num[8];/* 学号 */
char name[10];/* 姓名 */
struct mark mark[4];/* 数学,外语,体育,平均分 */
struct stu_info *next;
}*head=NULL;

void Sort();/*排序函数*/
void Search(); /*查找函数*/
void Insert();/*插入函数*/
void Print(); /*显示学生信息*/
void Bye();/*结束函数*/
void Menu(); /*菜单函数*/
void Del(); /*删除函数*/


/* /* //////////////////// */ */
void main()
{
float m;
scanf("%f",&m);
Menu();
}
void Insert()
{
FILE *fp;
int x[5],y[5];
struct stu_info *p=head;
struct stu_info *stu=(struct stu_info *)malloc(LEN);
/* 完成显示效果  光标随输入 */
clrscr();
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆插入学生成绩◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n");
printf("\n");
printf("\t\t\t学号:");
x[0]=wherex();y[0]=wherey();
printf("\n\n\n");
printf("\t\t\t姓名:");
x[1]=wherex();y[1]=wherey();
printf("\n\n\n");
printf("\t\t\t数学:");
x[2]=wherex();y[2]=wherey();
printf("\n\n\n");
printf("\t\t\t外语:");
x[3]=wherex();y[3]=wherey();
printf("\n\n\n");
printf("\t\t\t体育:");
x[4]=wherex();y[4]=wherey();
printf("\n");
printf("◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆\n");
gotoxy(x[0],y[0]);
scanf("%s",stu->num);
gotoxy(x[1],y[1]);
scanf("%s",stu->name);
gotoxy(x[2],y[2]);
scanf("%f",&stu->mark[0].fen);
gotoxy(x[3],y[3]);
scanf("%f",&stu->mark[1].fen);
gotoxy(x[4],y[4]);
scanf("%f",&stu->mark[2].fen);
/* 将结点插入到链表中 */
while(p!=NULL)
{p++;}
p=stu;
stu->next=NULL;

stu->mark[3].fen=(stu->mark[0].fen+stu->mark[1].fen+stu->mark[2].fen)/3;
if((fp=fopen("record.txt","ab"))==NULL)
printf("文件打开失败!\n");
if(fwrite(stu,sizeof(struct stu_info),1,fp)!=1)
printf("文件写入失败!\n");
fwrite(NULL,NULL,1,fp);
fclose(fp);
free(stu);
clrscr();
printf("插入成功,程序正在返回主菜单...");
sleep(1);
}
/*删除数据*/

void Del()
{
FILE *fp;
int choose;
struct stu_info *s,*f,*head,*p,*p1,*p2;
union stu_sign *stu;
stu=(union stu_sign *)malloc(sizeof(union stu_sign));
s=(struct stu_info *)malloc(LEN);
loop:
clrscr();
printf("□□□□□□□□□□□□□□□□□删除数据□□□□□□□□□□□□□□□□□□□\n");
printf("\n");
printf("\n");
printf("\t1.删除已知学号的记录                          2.删除已知姓名的记录              \n");
printf("\n");
printf("\n");
printf("□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□\n");
printf("\n");
printf("请选择删除方式:");

  switch(getch())
  {
  case '1':   clrscr();
              printf("\n\n\n\n请输入要删除的学号:");
              scanf("%s",stu->num);
            if((fp=fopen("record.txt","rb"))==NULL)printf("数据文件不存在!");
            else
           { /*取表头!!*/
             head=s;
            while(fread(s,LEN,1,fp)==1)
            {
             f=s;
             p=(struct stu_info *)malloc(LEN);
             s->next=p;
             s=p;
             }
             f->next=NULL;

             fclose(fp);
            } /* 取到表头后关闭文件 */
            /*以下是删除结点函数*/
            if(head==NULL){printf("您还没有录入数据,文件为空!");}
            else
             {
              p1=head;


              while(strcmp(stu->num,p1->num)!=0&&p1->next!=NULL)
              {
               p2=p1;

               p1=p1->next;

              }

                  if(strcmp(stu->num,p1->num)==0)
                    {
                      if(p1==head)head=p1->next;
                      else p2->next=p1->next;
                       /*结点已删除*/


          /*—重新写入到文件中*/ if((fp=fopen("record.txt","wb"))==NULL)printf("数据文件不存在!,写入失败!");
                                 else
                                        {
                                          while(head!=NULL)
                                          { if(fwrite(head,sizeof(struct stu_info),1,fp)==1)


                                            head=head->next;
                                          }
                                         }
                    printf("\n\n\n所录入数据已删除!");getch();
                   }
                  else {clrscr();printf("\n\n\n\n\n\n\n没有找到要删除的信息,请确认输入是否正确!");getch();}

             }  break;
  case '2':   clrscr();
              printf("请输入要删除的姓名:");
              scanf("%s",stu->num);
            if((fp=fopen("record.txt","rb"))==NULL)printf("数据文件不存在!");
            else
           { /*取表头!!*/
             head=s;
            while(fread(s,LEN,1,fp)==1)
            {
             p=(struct stu_info *)malloc(LEN);
             s->next=p;
             s=p;
             }


             fclose(fp);
            } /* 取到表头后关闭文件 */
            /*以下是删除结点函数*/
            if(head==NULL){printf("您还没有录入数据,文件为空!");}
            else
             {
              p1=head;


              while(strcmp(stu->name,p1->name)!=0&&p1->next!=NULL)
              {
               p2=p1;

               p1=p1->next;

              }

                  if(strcmp(stu->name,p1->name)==0)
                    {
                      if(p1==head)head=p1->next;
                      else p2->next=p1->next;
                       /*结点已删除*/


          /*—重新写入到文件中*/ if((fp=fopen("record.txt","wb"))==NULL)printf("数据文件不存在!,写入失败!");
                                 else
                                        {
                                          while(head!=NULL)
                                          { if(fwrite(head,sizeof(struct stu_info),1,fp)==1)


                                            head=head->next;
                                          }
                                         }
                    printf("\n\n\n所录入数据已删除!");getch();
                   }
                  else {clrscr();printf("\n\n\n\n\n\n\n没有找到要删除的信息,请确认输入是否正确!");getch();}

             }  break;



  
  }
/* ///////////////////此处应有释放内存的语句 */
fclose(fp);
clrscr();
printf("\n\n程序正在返回主菜单...");
getch();
}

void Search()
{
FILE *fp;
int i=0;/*记录是否查到的变量*/
struct stu_info *p;

union stu_sign *stu;  /*共用体用来存储用户输入的信息*/
p=(struct stu_info *)malloc(LEN);

stu=(union stu_sign *)malloc(sizeof(union stu_sign));/* 存储查询项 */
clrscr();
printf("\n");
printf("\n");
printf("∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏查询数据∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏∏\n");
printf("\n");
printf("\n");
printf("\t\t\t  1.按学号查询\n");
printf("\t\t\t  2.按名字查询\n");
printf("\t\t\t  3.按平均分查询\n");
printf("\n");
printf("\n");
printf("∏∏∏∏∏∏∏∏∏----------------------------------------∏∏∏∏∏∏∏∏∏∏∏\n");
printf("\n");
printf("\n");
/* /////////////////////此处有响应操作 */

switch(getch())
{
case '1':
         clrscr();
         printf("\n\n\n\n\n\n\n\n\n\n请输入要查询的学号:");
         scanf("%s",stu->num);
         clrscr();
         printf("\n\n\n\n\n\n");
         if((fp=fopen("record.txt","rb"))==NULL)
            {printf("文件打开失败!");}
             else
               {
                  while(fread(p,LEN,1,fp)==1)
                    {
                      if(strcmp(p->num,stu->num)==0)
                     {
                      i++;
                      printf("\t学号\t姓名\t数学\t英语\t体育\t平均分\n");
                      printf("\t%s\t%s\t%3.1f\t%3.1f\t%3.1f\t%3.2f\n",p->num,p->name,p->mark[0].fen,p->mark[1].fen,p->mark[2].fen,p->mark[3].fen);
                      printf("\n\n\n");
                      }
                    }
                }
                if(i==0)printf("没有找到您要查找的信息,请检查输入是否正确!");
               fclose(fp);
               free(p);
               free(stu);getchar();getchar();clrscr(); break;

case '2':
         clrscr();
         printf("\n\n\n\n\n\n\n\n\n\n请输入要查询的姓名:");
         scanf("%s",stu->name);
         clrscr();
         printf("\n\n\n\n\n\n");
         if((fp=fopen("record.txt","rb"))==NULL)
            {printf("文件打开失败!");}
             else
               {
                  while(fread(p,LEN,1,fp)==1)
                    {
                      if(strcmp(p->name,stu->name)==0)
                     {
                      i++;
                      printf("\t学号\t姓名\t数学\t英语\t体育\t平均分\n");
                      printf("\t%s\t%s\t%3.1f\t%3.1f\t%3.1f\t%3.2f\n",p->num,p->name,p->mark[0].fen,p->mark[1].fen,p->mark[2].fen,p->mark[3].fen);
                      printf("\n\n\n");
                      }
                    }
                }
               if(i==0)printf("没有找到您要查找的信息,请检查输入是否正确!");
               fclose(fp);
               free(p);
               free(stu);getchar();getchar();clrscr(); break;
case '3':
         clrscr();
         printf("\n\n\n\n\n\n\n\n\n\n请输入要查询的平均分:");
         scanf("%f",&stu->mark[3].fen);
         clrscr();
         printf("\n\n\n\n\n\n");
         if((fp=fopen("record.txt","rb"))==NULL)
            {printf("文件打开失败!");}
             else
               {
                  while(fread(p,LEN,1,fp)==1)
                    {
                      if(p->mark[3].fen==stu->mark[3].fen)
                     { i++;
                      printf("\t学号\t姓名\t数学\t英语\t体育\t平均分\n");
                      printf("\t%s\t%s\t%3.1f\t%3.1f\t%3.1f\t%3.2f\n",p->num,p->name,p->mark[0].fen,p->mark[1].fen,p->mark[2].fen,p->mark[3].fen);
                      printf("\n\n\n");
                      }
                    }
                }
                if(i==0)printf("没有找到您要查找的信息,请检查输入是否正确!");               getch();
               fclose(fp);
               free(p);
               free(stu);getchar();getchar();clrscr(); break;


}


}
/*打印学生信息*/
void Print()
{
FILE *fp;
struct stu_info *p=(struct stu_info *)malloc(LEN);
if((fp=fopen("record.txt","rb"))==NULL)
printf("文件打开失败!");
else
  {
     /*打印学生信息*/
     clrscr();
     printf("\n");
     printf("\n");
     printf("\n");
      printf("≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮≮学生信息≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯≯");
      printf("\n");
      printf("\t学号\t姓名\t数学\t英语\t体育\t平均分\n");

      while(fread(p,sizeof(struct stu_info),1,fp)==1)
    {
       printf("\t%s\t%s\t%3.1f\t%3.1f\t%3.1f\t%3.2f\n",p->num,p->name,p->mark[0].fen,p->mark[1].fen,p->mark[2].fen,p->mark[3].fen);

     }
   }

fclose(fp);
getch();
}

void Sort()/*主要思想是把文件中的数据放到结构体数组中进行排序,排序完成后输出结果,并重新保存到文件中 */
{
 FILE *fp;
struct stu_info *head;
struct stu_info *s=(struct stu_info*)malloc(sizeof(struct stu_info));
struct stu_info *p;
struct stu_info *i,*t,*f,*front;
struct stu_info *newhead,*newnode;
int n=0;

/*排序界面*/
printf("\n\n\n〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -