Linux高级编程——学生成绩管理系统(每个班级放入一个文件夹内)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
typedef struct student  //结构体
{
    //char grade[30];
    char num[30];
    char name[20];
    char place[50];
    char sex[4];
    int age;
    struct student *next; 
}stu;
void create_mkdir()  //创建目录
{
    if(mkdir("./m1",0666)<0)
         perror("mkdir m1 error:");
    if(mkdir("./m2",0666)<0)
     perror("mkdir m2 error:");
    if(mkdir("./m3",0666)<0)
     perror("mkdir m3 error:");
    if(mkdir("./m4",0666)<0)
     perror("mkdir m4 error:");        
}
void save(stu *head)  //保存文件
{
    int i,c;
    FILE *fp[4];
    stu* p1=head->next;
    fp[0]=fopen("./m1/m1s","w");
    fp[1]=fopen("./m2/m2s","w");
    fp[2]=fopen("./m3/m3s","w");
    fp[3]=fopen("./m4/m4s","w");
    for(i=0;i<4;i++)
    {
        if(fp[i]==NULL)
        {
            printf("cannot fopen file:%d\n",i+1);
            return;
        }
    }
    printf("\t\t@[1] grade is m1\n");
    printf("\t\t@[2] grade is m2\n");
    printf("\t\t@[3] grade is m3\n");
    printf("\t\t@[4] grade is m4\n");
    printf("\t\t@[0] quit\n");
    printf("please choose\n");
    scanf("%d",&c);
    switch(c)
    {
        case 1:printf("choose is grade:m1\n");break;
        case 2:printf("choose is grade:m2\n");break;
        case 3:printf("choose is grade:m3\n");break;
        case 4:printf("choose is grade:m4\n");break;
        case 0:for(i=0;i<4;i++)
            {
             fclose(fp[i]);
            }
            return;
            default:printf("choose error!\n");return;    
    } 
    while(p1!=NULL)
    {
        //fwrite(p1,sizeof(stu),1,fp[c-1]);
        if(fwrite(p1,sizeof(stu),1,fp[c-1])!=1)
        {
            printf("file write error\n");
        }
        p1=p1->next;
    }
    for(i=0;i<4;i++)
    {
        fclose(fp[i]);
    }
}
void reads()  //文件读取
{
    int i,c;
    FILE *fp[4];
    stu* p1;
    fp[0]=fopen("./m1/m1s","r+");
    fp[1]=fopen("./m2/m2s","r+");
    fp[2]=fopen("./m3/m3s","r+");
    fp[3]=fopen("./m4/m4s","r+");
    for(i=0;i<4;i++)
    {
        if(fp[i]==NULL)
        {
            printf("cannot fopen file:%d\n",i+1);
            return;
        }
    }
    printf("\t\t@[1] grade is m1\n");
    printf("\t\t@[2] grade is m2\n");
    printf("\t\t@[3] grade is m3\n");
    printf("\t\t@[4] grade is m4\n");
    printf("\t\t@[0] quit\n");
    printf("please choose\n");
    scanf("%d",&c);
    switch(c)
    {
        case 1:printf("choose is grade:m1\n");break;
        case 2:printf("choose is grade:m2\n");break;
        case 3:printf("choose is grade:m3\n");break;
        case 4:printf("choose is grade:m4\n");break;
        case 0:for(i=0;i<4;i++)
            {
             fclose(fp[i]);
            }
            return;
            default:printf("choose error!\n");return;    
    } 
    printf("\tnum\tname\tplace\tsex\tage\n");
    p1=(stu*)malloc(sizeof(stu));
    fread(p1,sizeof(stu),1,fp[c-1]);
    while(!feof(fp[c-1]))
    {
        printf("\t%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
        p1=(stu*)malloc(sizeof(stu));
        fread(p1,sizeof(stu),1,fp[c-1]);
        //{
        //    printf("file read error\n");
        //}
        //p1=p1->next;
    }
    for(i=0;i<4;i++)
    {
        fclose(fp[i]);
    }
}
void create_head()  //创建头指针
{
    stu *head;
    head=(struct student *)malloc(sizeof(struct student));
    head->next=NULL;
}
void input(stu *head)  //输入
{
    int i,j,c,n,m,fd[10];
    struct student *p1=head,*p2;
    printf("please input the grade's student number\n");
    scanf("%d",&m);
    for(j=0;j<m;j++)
     {
        p2=(struct student *)malloc(sizeof(struct student));
        printf("please input student's num\n");
        scanf("%s",&p2->num);
        printf("please input student's name\n");
        scanf("%s",&p2->name);
        printf("please input student's place\n");
        scanf("%s",&p2->place);
        printf("please input student's sex\n");
        scanf("%s",&p2->sex);
        printf("please input student's age\n");
        scanf("%d",&p2->age);
        p1->next=p2;
        p1=p2;
      }
       p2->next=NULL;
}
void output(stu *head)  //输出
{
    stu *p1=head->next;
    if(p1==NULL)
    {
        printf("data is NULL\n");
        return;
    }
    printf("\tnum\tname\tplace\tsex\tage\n");
    while(p1!=NULL)
    {
         printf("\t%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
         p1=p1->next;
    }    
}
void insert(stu* head)  //插入
{
    int i,n;
    stu* p1=head->next,*p2;
    if(p1==NULL)
    {
        printf("data NULL,please input data\n");
        return;
    }
    while(p1->next!=NULL)
    p1=p1->next;
    
    printf("please input the grade's student number\n");
    scanf("%d",&n);
    while(n--)
    {
        p2=(struct student *)malloc(sizeof(struct student));
        printf("please input student's num\n");
        scanf("%s",&p2->num);
        printf("please input student's name\n");
        scanf("%s",&p2->name);
        printf("please input student's place\n");
        scanf("%s",&p2->place);
        printf("please input student's sex\n");
        scanf("%s",&p2->sex);
        printf("please input student's age\n");
        scanf("%d",&p2->age);
        p1->next=p2;
        p1=p2;
    }
    p2=NULL;
}
void alter_num(stu* head)  //修改学号
{
    int x=0;
    stu *p1=head->next;
    char num[30];
    printf("please input alter num\n");
    scanf("%s",&num);
     while(p1!=NULL)
     {
        if(strcmp(p1->num,num)==0)
        {    x=1;
            printf("alter before,this data\tnum\tname\tsplace\tsex\tage\n");
            printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
            printf("please input student's new num\n");
            scanf("%s",&p1->num);
            printf("please input student's new name\n");
            scanf("%s",&p1->name);
            printf("please input student's new place\n");
            scanf("%s",&p1->place);
            printf("please input student's new sex\n");
            scanf("%s",&p1->sex);
            printf("please input student's new age\n");
            scanf("%d",&p1->age);
            printf("alter after,this data\tnum\tname\tsplace\tsex\tage\n");
            printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
            break;
        }
        p1=p1->next;
     }
     if(x==0)
      printf("cann't select this data\n");
}
void alter_name(stu* head)  //修改姓名
{
    int x=0;
    stu *p1=head->next;
    char name[20];
    printf("please input alter name\n");
    scanf("%s",&name);
    while(p1!=NULL)
    {
        if(strcmp(p1->name,name)==0)
        {    x=1;
            printf("alter before,this data\tnum\tname\tsplace\tsex\tage\n");
            printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
            printf("please input student's new num\n");
            scanf("%s",&p1->num);
            printf("please input student's new name\n");
            scanf("%s",&p1->name);
            printf("please input student's new place\n");
            scanf("%s",&p1->place);
            printf("please input student's new sex\n");
            scanf("%s",&p1->sex);
            printf("please input student's new age\n");
            scanf("%d",&p1->age);
            printf("alter after,this data\tnum\tname\tsplace\tsex\tage\n");
            printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
        }
        p1=p1->next;
    }
    if(x==0)
     printf("cann't select this data\n");
}
void alter_place(stu* head)  //修改地址
{
    int x=0;
    char place[50];
    stu* p1=head->next;
    printf("please input alter place\n");
    scanf("%s",&place);
    while(p1!=NULL)
    {
            if(strcmp(p1->place,place)==0)
            {    x=1;
                printf("alter before,this data\tnum\tname\tsplace\tsex\tage\n");
                printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
                printf("please input student's new num\n");
                scanf("%s",&p1->num);
                printf("please input student's new name\n");
                scanf("%s",&p1->name);
                printf("please input student's new place\n");
                scanf("%s",&p1->place);
                printf("please input student's new sex\n");
                scanf("%s",&p1->sex);
                printf("please input student's new age\n");
                scanf("%d",&p1->age);
                printf("alter after,this data\tnum\tname\tsplace\tsex\tage\n");
                printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
             }
              p1=p1->next;
     }    
     if(x==0)
     printf("cann't select this data\n");
}
void alter(stu* head)  //修改总函数
{
    int i,c,age,x;
    stu* p1;
    char num[30],name[20],place[50],sex[4];
    while(1)
    {
        printf("[1]basis of:num to alter\n");
        printf("[2]basis of:name to alter\n");
        printf("[3]basis of:place to alter\n");
        printf("[0]quit\n");
        printf("please choose\n");
        scanf("%d",&c);
        switch(c)
        {
           case 1:alter_num(head);break;  
           case 2:alter_name(head);break;    
           case 3:alter_place(head);break;
           case 0:return;
           default:printf("choose error\n");    
        }
        }    
}
void selects(stu* head)  //查询函数
{
    int c,x;
    stu* p1;
    char num[30],name[20],place[50],sex[4];
    while(1)
    {
        printf("[1]basis of:num to select\n");
        printf("[2]basis of:name to select\n");
        printf("[3]basis of:place to select\n");
        printf("[0]quit\n");
        printf("please choose\n");
        scanf("%d",&c);
        switch(c)
        {
           case 1:x=0;
               p1=head->next;
               printf("please input select to num");
               scanf("%s",&num);
               printf("\tnum\tname\tplace\tsex\tage\n");
            while(p1!=NULL)
            {
                if(strcmp(p1->num,num)==0)
                {
                    printf("this data\tnum\tname\tsplace\tsex\tage\n");
                    printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
                    x=1;
                    break;
                }
                p1=p1->next;
            }    
            if(x==0)
              printf("cann't select this data\n");
              break;
           case 2:x=0;
              p1=head->next;
               printf("please input select to name");
               scanf("%s",&name);
               printf("\tnum\tname\tplace\tsex\tage\n");
            while(p1!=NULL)
            {
                if(strcmp(p1->name,name)==0)
                {    
                    printf("this data\tnum\tname\tsplace\tsex\tage\n");
                    printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
                    x=1;
                    break;
                }
                p1=p1->next;
            }    
            if(x==0)
              printf("cann't select this data\n");
              break;           
           case 3:x=0;
               p1=head->next;
               printf("please input select to place");
               scanf("%s",&place);
               printf("\tnum\tname\tplace\tsex\tage\n");
            while(p1!=NULL)
            {
                if(strcmp(p1->place,place)==0)
                {
                    printf("this data\tnum\tname\tsplace\tsex\tage\n");
                    printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
                    x=1;
                    break;
                }
                p1=p1->next;
            }    
            if(x==0)
              printf("cann't select this data\n");
              break;
               case 0:return;
           default:printf("choose error\n");           
        }
    }
    
}
void deletes(stu *head)  //删除
{
    int c,x;
    stu* p1,*p2;
    char num[30],name[20];
    while(1)
    {
        printf("[1]basis of:num to delete\n");
        printf("[2]basis of:name to delete\n");
        printf("[0]quit\n");
        printf("please choose\n");
        scanf("%d",&c);
        switch(c)
        {
            case 1:x=0;
                  p1=head->next,p2=head;
                  printf("please input delete num\n");
                  scanf("%s",&num);
                  while(p1!=NULL)
                  {
                      if(strcmp(p1->num,num)==0)
                      {
                          printf("delete this data\tnum\tname\tsplace\tsex\tage\n");
                    printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
                          p2->next=p1->next;
                          free(p1);
                          x=1;
                          break;
                      }
                      p1=p1->next;
                      p2=p2->next;
                  }
                  if(x==0)
                  printf("cann't select this data\n");
                  break;
             case 2:x=0;
                  p1=head->next,p2=head;
                  printf("please input delete name\n");
                  scanf("%s",&name);
                  while(p1!=NULL)
                  {
                      if(strcmp(p1->name,name)==0)
                      {
                          printf("delete this data\tnum\tname\tsplace\tsex\tage\n");
                    printf("%s\t%s\t%s\t%s\t%d\n",p1->num,p1->name,p1->place,p1->sex,p1->age);
                          p2->next=p1->next;
                          free(p1);
                          x=1;
                          break;
                      }
                      p1=p1->next;
                      p2=p2->next;
                  }
                  if(x==0)
                  printf("cann't select this data\n");
                  break;
            case 0:return;
            default:printf("choose error\n");
        }
    }    
}
void menu(stu *head)  //菜单
{
      int c;
      while(1)
      {
    printf("[1]input\n");
    printf("[2]output\n");
    printf("[3]insert\n");
    printf("[4]alter\n");
    printf("[5]select\n");
    printf("[6]delete\n");
    printf("[7]save\n");
    printf("[8]read\n");
    printf("[0]quit\n");
    printf("please choose\n");
    scanf("%d",&c);
    switch(c)
    {
        case 1:input(head);break;
        case 2:output(head);break;
        case 3:insert(head);break;
        case 4:alter(head);break;
        case 5:selects(head);break;
        case 6:deletes(head);break;
        case 7:save(head);break;
        case 8:reads();break;
        case 0:printf("thank your use!!\n");return;
        default:printf("choose error\n");
    }
      }
}
int main()
{
    create_mkdir();
    stu *head=(stu *)malloc(sizeof(stu));
    head->next=NULL;
    menu(head);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值