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

📄 chengjiguanli.c

📁 一个用链表来做的c语言的课程设计的源代码。
💻 C
字号:
/*版本1.1    经过一下午的修改,成绩管理模块还是存在问题.
对单链表的操作把握不住.出现一些不明白的错误.其他模块功能正常.
*/
/* 系统要求: */
/* 成绩管理功能(添加 删除 排序) */
/* 成绩统计功能(最高分 最低分 平均分 及格率) */
/* 实现按学号 姓名 课程名查询成绩功能 */
/* 学生成绩纪录包括(学号 姓名 院系 年级 班级 课程名 成绩) */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* 为减少数据冗余,用两个文件分别保存学生信息(学号 院 */
/* 系 年级 班级)和(学号 姓名 课程名 成绩) */
/* 设计两个结构体用于读写信息 */
typedef struct student1{
    char no[10];
    char college[10];
    int grade;
    int clas;
    struct student1 * next;
}stu1;

typedef struct student2{
    char no[10];
    char name[10];
    char course[20];
    int result;
    struct student2 * next;
}stu2;

/* 全局变量 */
FILE * fp1 = NULL, * fp2 = NULL;
stu1 * h1 = NULL;
stu2 * h2 = NULL;

void menu();      /* 显示菜单 */
void manage();    /* 成绩管理 */
void statistics();/* 统计成绩 */
void query();     /* 查询 */
void re_info1();
void re_info2();
void saveinfo1();
void saveinfo2();
void add();
void del();
void sort();
void q_no();
void q_na();
void q_c();
void clr1();
void clr2();
void JianCe();


/* 主函数 */
void main()
{
    char flag = '1';
    h1 = (stu1 *) malloc (sizeof(stu1));
    h2 = (stu2 *) malloc (sizeof(stu2));
    if(!h1 || !h2){
        printf("\nmalloc error!\n");
        exit(0);
    }
    h1->next = NULL;
    h2->next = NULL;

    do{
        clrscr();
        menu();
        flag = getch();
        switch(flag){
            case '1': manage();     break;
            case '2': statistics(); break;
            case '3': query();      break;
            default: ;
            }
        }while(flag != '0');
    printf("\nThank you for using,Goodbye!\n");
    getch();
}


void menu()
{
    printf("\n\n\n\n\n***MENU:******************************\n");
    printf("\n  0:exit\n");
    printf("\n  1:manage\n");
    printf("\n  2:statistics\n");
    printf("\n  3:query\n");
    printf("\nPlease enter the nummber witch you want\n");
    printf("\n***************************************");
}


void JianCe()
{
    stu1 * p = NULL;
    stu2 * r = NULL;
    for(p=h1->next;p!=NULL;)
    {
        printf("\n%s %s %d %d\nJianCe\n",p->no,p->college,p->grade,p->clas);
        p=p->next;
    }
    for(r=h2->next;r!=NULL;)
    {
        printf("\n%s %s %s %d\nJianCe\n",r->no,r->name,r->course,r->result);
        r=r->next;
    }

}
/* 清理单链表1上的信息 */
void clr1()
{
    stu1 * w = NULL, * a = NULL;
    w = h1->next;
    a = w->next;
    h1->next = NULL;
    while(w!=NULL){
        free(w);
        w = a;
        a = a->next;
    }
}
/* 清理单链表2上的信息 */
void clr2()
{
    stu2 * w = NULL, * a = NULL;
    w = h2->next;
    a = w->next;
    h2->next = NULL;
    while(w!=NULL){
        free(w);
        w = a;
        a = a->next;
    }
}

/* 读文件信息: */
/* 把文件stu1.txt里的信息读到h1单链表中去 */
/* 不适合读大型文件 */
void re_info1()
{
/**/stu1 * p = NULL;
    fp1 = fopen("stu1.txt","r");
    if(!fp1){
        printf("\nOpen file stu1.txt error!\n");
        exit(0);
    }
    while(!feof(fp1)){
        p = (stu1 *) malloc (sizeof(stu1));
/**/    fscanf(fp1,"%s%s%d%d",&(p->no),&(p->college),&(p->grade),&(p->clas));
        p->next = h1->next;
        h1->next = p;
    }
    fclose(fp1);
}

/* 读文件信息: */
/* 把文件stu2.txt里的信息读到h2单链表中去 */
/* 不适合读大型文件 */
void re_info2()
{
    stu2 * p = NULL;
    fp2 = fopen("stu2.txt","r");
    if(!fp2){
        printf("\nOpen file stu2.txt error!\n");
        exit(0);
    }
    while(!feof(fp2)){
        p = (stu2 *) malloc (sizeof(stu2));
        fscanf(fp2,"%s%s%s%d",&(p->no),&(p->name),&(p->course),&(p->result));
        p->next = h2->next;
        h2->next = p;
    }
    fclose(fp2);
}

/* 保存单链表信息 */
/* 把h1单链表的信息保存到文件stu1.txt中 */
void saveinfo1()
{
    stu1 * p = NULL;
    fp1 = fopen("stu1.txt","w");
    if(!fp1){
        printf("\nSave file stu1.txt error!\n");
        exit(0);
    }
    p = h1->next;
    while(p!=NULL){
        fprintf(fp1,"%s%c%s%c%d%c%d%c",p->no,'\n',p->college,'\n',p->grade,'\n',p->clas,'\n');
        p = p->next;
    }
    clr1();
    fclose(fp1);
}

/* 保存单链表信息 */
/* 把h2单链表的信息保存到文件stu2.txt中 */
void saveinfo2()
{
    stu2 * p = NULL;
    fp2 = fopen("stu2.txt","w");
    if(!fp2){
        printf("\nSave file stu2.txt error!\n");
        exit(0);
    }
    p = h2->next;
    while(p!=NULL){
        fprintf(fp2,"%s%c%s%c%s%c%d%c",p->no,'\n',p->name,'\n',p->course,'\n',p->result,'\n');
        p = p->next;
    }
    clr2();
    fclose(fp2);
}

/* 添加信息 */
void add()
{
/**/stu1 * p = NULL;
    stu2 * r = NULL, * s = NULL;
    re_info1();    /* 读信息到链表 */
    re_info2();
    JianCe();
    r = h2->next;
    p = (stu1 *) malloc (sizeof(stu1));
    s = (stu2 *) malloc (sizeof(stu2));
    printf("\nPlease enter the no_college_grade_clas\n");
/**/scanf("%s%s%d%d",&(p->no),&(p->college),&(p->grade),&(p->clas));
    printf("\n%s %s %d %d",p->no,p->college,p->grade,p->clas);
    p->next = NULL;
    printf("\nPlease enter the no_name_course_result\n");
/**/scanf("%s%s%s%d",&(s->no),&(s->name),&(s->course),&(s->result));
    printf("\n%s %s %s %d",s->no,s->name,s->course,s->result);
    s->next = NULL;
        
    while(r!=NULL)
    {
        if(strcmp(r->no,s->no)==0)
        {     /* 如果有相同学号 */
            printf("\nno. is in the same\n");
            if(strcmp(r->course,s->course)==0){   /* 如果有相同课程名 */
                r->result = s->result;
                break;
            }
            if(r->next == NULL){                  /* 如果没有相同课程名 */
                r->next = s;
                break;
            }
        }
        else
        {                          /* 如果没相同学号 */
            if(r->next==NULL)
            {
                printf("\nno. is diffrent\n");
                r->next = s;
                p->next = h1->next;
                h1->next = p;
                break;
            }
        }
        r = r->next;
    }
    getch();
    JianCe();
    saveinfo1();
    saveinfo2();
    printf("\nadd Ok!\n");
    sleep(3);
    return;
}

/* 删除信息 */
void del()
{
    char c = '0';
    char no[10];
    char course[20];
    stu1 * p = h1, * q = NULL;
    stu2 * r = h2, * s = NULL;
    re_info1();
    re_info2();
    printf("\nIf you want to delete a student enter 1\n");
    printf("\nIf you want to delete a course enter 2\n");
    c = getch();
    if(c=='1'){      /* 删除满足学号的学生 */
        printf("\nPlease enter the no.\n");
        gets(no);
        /* 在h1上删除 */
        while(p!=NULL){
            q = p;
            if(strcmp(p->next->no,no)==0){
                q->next = q->next->next;
                p = p->next;
                free(p);
                break;
            }
            p = p->next;
         }
        /* 在h2上删除 */
        while(r!=NULL){
            s = r;
            if(strcmp(r->next->no,no)==0){
                s->next = s->next->next;
                r = r->next;
                free(r);
                r = s;
            }
            else{
            r = r->next;
            }
        }
        printf("\ndel student ok!\n");
    }

    else if(c=='2'){   /* 删除某学生的某课程(只在h2上删除) */
        printf("\nPlease enter the no.\n");
        gets(no);
        printf("\nPlease enter the course\n");
        gets(course);
        while(r!=NULL){
            s = r;
            if(strcmp(r->next->no,no)==0 && strcmp(r->course,course)==0){
                s->next = s->next->next;
                r = r->next;
                free(r);
                break;
            }
            r = r->next;
        }
        printf("\nDel course ok!\n");
    }

    else{                /* 不删除 */
        ;
    }
    saveinfo1();
    saveinfo2();
    return;
}

/* 排序(不保存,只做一下按学号排序,其他类似) */
void sort()
{
    stu2 * p = NULL, * q = h2, * s = NULL;
    re_info2();
    p = h2->next;
    while(q->next!=NULL)
        q = q->next;
    h2 = h2->next->next;
    p->next = NULL;
    while(p!=q){
        s = q;
        if(strcmp(p->no,s->no)>0 && s->next!=NULL){
            s = s->next;
        }
        else{
            p->next = s->next;
            s->next = p;
        }
        p = h2->next;
    }
    s = h2->next;
    while(s!=NULL){
        printf("\nNo.is %s\tName is %s\tcourse is %s\tresult is %d\n",s->no,s->name,s->course,s->result);
        s = s->next;
    }
    clr2();
}

/* 按学号查询 */
void q_no()
{
    char no[10];
    stu2 * p = h2->next;
    printf("\nPlease enter the no.\n");
    gets(no);
    while(p!=NULL){
        if(strcmp(p->no,no)==0){
        printf("\nNo.is %s\tName is %s\tcourse is %s\tresult is %d\n",p->no,p->name,p->course,p->result);
        }
        else{
            if(p->next==NULL)
                printf("\nno one!\n");
        }
/**/    p = p->next;
    }
}

/* 按姓名查询 */
void q_na()
{
    char na[10];
    stu2 * p = h2->next;
    printf("\nPlease enter the name\n");
    gets(na);
    while(p!=NULL){
        if(strcmp(p->name,na)==0){
        printf("\nNo.is %s\tName is %s\tcourse is %s\tresult is %d\n",p->no,p->name,p->course,p->result);
        }
        else{
            if(p->next==NULL)
                printf("\nno one!\n");
        }
        p = p->next;
    }
}

/* 按课程名查询 */
void q_c()
{
    char course[20];
    stu2 * p = h2->next;
    printf("\nPlease enter the course\n");
    gets(course);
    while(p!=NULL){
        if(strcmp(p->course,course)==0){
        printf("\nNo.is %s\tName is %s\tcourse is %s\tresult is %d\n",p->no,p->name,p->course,p->result);
        }
        else{
            if(p->next==NULL)
                printf("\nno one!\n");
        }
        p = p->next;
    }
}

/* manage */
void manage()
{
    char ch1 = '1';
    do{
        clrscr();
        printf("\n\n\n\n\n***manage:*****************************\n");
        printf("\n  0:return\n");
        printf("\n  1:add\n");
        printf("\n  2:del\n");
        printf("\n  3:sort\n");
        printf("\nPlease enter the nummber witch you want\n");
        printf("\n***************************************");
        ch1 = getch();
        switch(ch1){
            case '1': clrscr();add();        break;
            case '2': clrscr();del();        break;
            case '3': clrscr();sort();       break;
            default: ;
            }
    }while(ch1 != '0');
    printf("\nmanage Ok!\n");
}

/* 统计 */
void statistics(){
    int max = 0, min =100;
    double ave = 0, pa = 0, count = 0;
    stu2 * p = NULL;
    clrscr();
    re_info2();
    p = h2->next;
    while(p!=NULL){
        printf("\nstatistics start!\n");
        if(p->result > max)
            max = p->result;
        if(p->result < min)
            min = p->result;
        if(p->result > 60)
            pa++;
        count++;
        ave += p->result;
/**/    p = p->next;
    }
    fp1 = fopen("tongji.txt","w");
    if(!fp1){
        printf("\nopen file tongji.txt error!\n");
        exit(0);
    }
    printf("\nmax=%d\nmin=%d\nave=%g\npass=%g\n",max,min,ave/count,pa/count);
    fprintf(fp1,"%s%d%c%s%d%c","max is:",max,'\n',"min is:",min,'\n');
    fprintf(fp1,"%s%g%c%s%g%c","ave is:",ave/count,'\n',"pass is:",pa/count,'\n');
    printf("\n statistics ok!");
    sleep(5);
    clr2();
    fclose(fp1);
}

/* 查询 */
void query(){
    char ch2 = '1';
    re_info2();
    do{
        clrscr();
        printf("\n\n\n\n\n***query:*****************************\n");
        printf("\n  0:return\n");
        printf("\n  1:query by no.\n");
        printf("\n  2:query by name\n");
        printf("\n  3:query by course\n");
        printf("\nPlease enter the nummber witch you want\n");
        printf("\n***************************************");

        ch2 = getch();
        switch(ch2){
            case '1': clrscr();q_no(); getch();       break;
            case '2': clrscr();q_na(); getch();       break;
            case '3': clrscr();q_c();  getch();       break;
            default: ;
            }
    }while(ch2 != '0');
    clr2();
    printf("\nquery over!\n");
}

⌨️ 快捷键说明

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