C语言学生管理系统

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#define PRINT_ERROR_FILE(e)\
do\
{\
    if(e==NULL)\
    {\
        printf("打开文件失败!\n");\
        exit(-1);\
    }\
}while(0)

typedef struct _Grade
{
    int id; //课程编号
    char name[20]; //课程名称
    double score;//学分
}Grade;

typedef struct _Stu
{
    Grade gb;
    int xuehao;
    char name[20];
    char  sex[5];
    int Su_se;
   char  dian_hua[12];
    double normal_score;
    double lab_score;
    double shijuan_score;
    double sum_score;
    double result_xuefen;
}Stu;

typedef struct _Node
{
    Stu  s;
    struct _Node* next;
}Node;

Node* head=NULL;


void Input_Message()
{
    Node* cur=head;
    while(cur->next)
    {
        cur=cur->next;
    }

    if(head->next==NULL)
    {
        head->next=(Node*)malloc(sizeof(Node));
        head->next->next=NULL;
        cur=head->next;
    }
    else {
        Node* temp=(Node*)malloc(sizeof(Node));
        temp->next=NULL;
        cur->next=temp;
        cur=temp;
    }
    printf("请输入课程编号:");
    scanf("%d",&cur->s.gb.id);
    printf("请输入课程名称:");
    scanf("%s",cur->s.gb.name);
    printf("请输入课程学分:");
    scanf("%lf",&cur->s.gb.score);
    printf("请输入学生姓名:");
    scanf("%s",cur->s.name);
    printf("请输入学生学号:");
    scanf("%d",&cur->s.xuehao);
    printf("请输入学生性别:");
   scanf("%s",cur->s.sex);
   printf("请输入学生宿舍号码:");
   scanf("%d",&cur->s.Su_se);
   printf("请输入学生电话:");
   scanf("%s",cur->s.dian_hua);
   printf("请输入学生平时成绩:");
   scanf("%lf",&cur->s.normal_score);
   printf("请输入学生实验成绩:");
   scanf("%lf",&cur->s.lab_score);
   printf("请输入学生卷面成绩:");
   scanf("%lf",&cur->s.shijuan_score);

   if(cur->s.lab_score==-1)
   {
       cur->s.sum_score=(cur->s.normal_score)*0.30+(cur->s.shijuan_score)*0.70;
   }
   else {
        cur->s.sum_score=(cur->s.normal_score)*0.15+(cur->s.lab_score)*0.15+(cur->s.shijuan_score)*0.7;
   }
   double t=cur->s.sum_score;
   if((t>=90)&&(t<=100))
   {
       cur->s.result_xuefen=t;
   }
   else if ((t>=80)&&(t<90)) {
       cur->s.result_xuefen=t*0.8;
   }
   else if ((t>=70)&&(t<80)) {
       cur->s.result_xuefen=t*0.7;
   }
   else if ((t>=60)&&(t<70)) {
        cur->s.result_xuefen=t*0.6;
   }
   else if (t<60) {
         cur->s.result_xuefen=0.0;
   }

}

void Print_Info()
{
    printf("课程编号  课程名称  课程学分  学号\t姓名\t性别\t宿舍\t电话\t\t平时成绩  实验成绩  卷面成绩  综合成绩  最后学分\n");
    Node* cur=head->next;

    while(cur)
    {
        printf("%-8d  %-8s  %-8lf  %-4d\t%-4s\t%-4s\t%-4d\t%-4s\t\t%-8.1lf  %-8.1lf  %-8.1lf  %-8.1lf  %-8.1lf\n",cur->s.gb.id,cur->s.gb.name,
               cur->s.gb.score,
               cur->s.xuehao,cur->s.name,cur->s.sex,cur->s.Su_se,
               cur->s.dian_hua,cur->s.normal_score,cur->s.lab_score,cur->s.shijuan_score,
               cur->s.sum_score,cur->s.sum_score);

        cur=cur->next;
    }
}

void Print_Single(Node* cur)
{
     printf("课程编号  课程名称  课程学分  学号\t姓名\t性别\t宿舍\t电话\t\t平时成绩  实验成绩  卷面成绩  综合成绩  最后学分\n");
     printf("%-8d  %-8s  %-8lf  %-4d\t%-4s\t%-4s\t%-4d\t%-4s\t\t%-8.1lf  %-8.1lf  %-8.1lf  %-8.1lf  %-8.1lf\n",cur->s.gb.id,cur->s.gb.name,
            cur->s.gb.score,
            cur->s.xuehao,cur->s.name,cur->s.sex,cur->s.Su_se,
            cur->s.dian_hua,cur->s.normal_score,cur->s.lab_score,cur->s.shijuan_score,
            cur->s.sum_score,cur->s.sum_score);
}


void Find_Info()
{
    Node* cur=head->next;
    printf("请输入要查询学生的姓名或者学号\n首先请选择查询方式:1.姓名. 2.学号\n");
    int x;
    scanf("%d",&x);
    if(x==1)
    {
        printf("请输入要查询的学生姓名:");
        char name[20];
        scanf("%s",name);
         while(cur)
         {
             if(strcmp(name,cur->s.name)==0)
             {
                 printf("找到了该学生,接下来打印该学生信息\n");
                 Print_Single(cur);
                return;
             }
             cur=cur->next;
         }
        printf("没有找到该学生信息\n");
        return;
    }
    else {
        printf("请输入要查询的学生学号:");
        int id;
        scanf("%d",&id);
        while(cur)
        {
            if(id==cur->s.xuehao)
            {
                printf("找到了该学生,接下来打印该学生信息\n");
                Print_Single(cur);
               return;
            }
            cur=cur->next;
        }
       printf("没有找到该学生信息\n");
       return;
    }
}

Node* Find_By_Number(int id)
{
        Node* cur=head->next;
        while(cur)
        {
            if(cur->s.xuehao==id)
            {
               //Print_Single(cur);
               return cur;
            }
            cur=cur->next;
        }
        printf("找不到这个学号对应的学生信息\n");
        return NULL;
}

Node* Find_By_Name(char * s)
{
    Node* cur=head->next;
    while(cur)
    {
        if(strcmp(s,cur->s.name)==0)
        {
            //Print_Single(cur);
            return cur;
        }
        cur=cur->next;
    }
    printf("没有这位学生\n");
    return NULL;
}


void Del_Stu(int id)
{
    //删除的前提是你得必须先找到该学生.
    Node* cur=Find_By_Number(id);
    if(!cur)return;
   if(cur->next==NULL)
   {
       free(cur);
       printf("删除成功\n");
       return;
   }

   //找到所查找节点的前一个节点
   Node* temp=head;
   while(temp->next)
   {
       if(temp->next->s.xuehao==id)
       {
           temp->next=cur->next;
           cur->next=NULL;
           free(cur);
           printf("删除aaa成功\n");
           return;
       }
       temp=temp->next;
   }
}


void Result(Node* head,int n)
{
    Node* pre,*q,*p;

    for (int i=0;i<n-1;i++)
    {
        pre=head;
        p=head->next;
        q=p->next;
        for(int j=0;j<n-(i+1);j++)
        {
            if(p->s.result_xuefen>q->s.result_xuefen)
            {
             pre->next=q;
             p->next=q->next;
             q->next=p;
             pre=q;
             q=p->next;
             continue;
            }
            pre=pre->next;
            p=p->next;
            q=q->next;
        }
    }
}

int GetLength(Node* head)
{
    Node* cur=head->next;
    int count=0;
    while(cur)
    {
        count++;
        cur=cur->next;
    }
    return count;
}


void Write_File(char * filename)
{
    Node* cur=head->next;
    FILE* fs=fopen(filename,"w+");
    PRINT_ERROR_FILE(fs);
    while(cur)
    {
        fwrite((void*)&cur->s,sizeof(Stu),1,fs);
        cur=cur->next;
    }
    printf("写入成功\n");
    fclose(fs);
}

Node* Read_File(char* filename)
{
    head=(Node*)malloc(sizeof(Node));
    head->next=NULL;
    FILE* fs=fopen(filename,"r+");
    PRINT_ERROR_FILE(fs);
    Node* cur=(Node*)malloc(sizeof(Node));

    while(fread( ((void*)&cur->s),sizeof(Stu),1,fs )   )
    {
        cur->next=head->next;
        head->next=cur;
        cur=(Node*)malloc(sizeof(Node));
    }
    free(cur);
    return head;
}

int main()
{
    int a;
    head=(Node*)malloc(sizeof(Node));
    head->next=NULL;
    char filename[20];
    while(1)
    {
    printf("\t\t\t\t\t\t学生单科成绩查询系统\n");
    printf("请先选择功能:\n");
    printf("1.录入学生信息\n");
    printf("2.打印学生信息\n");
    printf("3.查询学生信息\n");
    printf("4.删除学生\n");
    printf("5.综合排序\n");
    printf("6.读数据到文件\n");
    printf("7.读文件到内存\n");
    char ch=getch();

    switch(ch) {
        case '1':
          Input_Message();
     break;
        case '2':
        Print_Info();
    break;
        case '3':
        printf("请选择查询方式:1.通过学号查询.2.通过姓名查询\n");
        int x;
        scanf("%x",&x);
        if(x==1)
        {
            int temp;
            printf("请输入学号:");
            scanf("%d",&temp);
           Node*t= Find_By_Number(temp);
           if(t==NULL)break;
           printf("该学生信息情况如下\n");
           Print_Single(t);
        }
        else {
            char name[20];
            scanf("%s",name);
           Node*t= Find_By_Name(name);
           if(t==NULL)break;
           Print_Single(t);
        }
    break;
        case '4':
            printf("请输入要删除的学生学号:");
            int id;
            scanf("%d",&id);
            Node *cur=Find_By_Number(id);
            if(cur==NULL)break;
            Del_Stu(id);
    break;
    case '5':
        a=GetLength(head);
        Result(head,a);
        break;
    case '6':
        printf("请输入文件的名字:");
        scanf("%s",filename);
         Write_File(filename);
        break;
      case '7':
        Read_File(filename);
        Print_Info();
        break;
    }
}

    return 0;
}

 

posted @ 2020-11-22 11:11  sunshine_gzw  阅读(544)  评论(0编辑  收藏  举报