爱问知识人 爱问教育 医院库

求C语言几道题的答案~~拜托了~~

首页

求C语言几道题的答案~~拜托了~~

建立一个无序链表,每个结点包含:学号、姓名、年龄、C语言成绩。由一个函数完成建立链表的工作,另一个函数完成输出链表上各结点值,一个函数完成释放链表结点占用的动态存储空间。

提交回答

全部答案

    2018-04-02 07:29:59
  •   #include  
    #include
    #include
    #include
    #include
    #include

    void pause();



    typedef struct STUDENT
    {
    char studentNumber[20]; //学生学号
    char studentName[20]; //学生姓名
    char className[20]; //班级名称
    float chinese; //语文绩
    float maths; //数学成绩
    float english; //外语成绩
    float total;
    float average; //平均值
    struct STUDENT *next; //指向下个数据
    struct STUDENT *last; //指向上个
    }STUDENT;


    static STUDENT *first=NULL; //链表头
    static STUDENT *end=NULL;


    STUDENT *Malloc(void)
    {
    STUDENT *p;
    p=(STUDENT*)malloc(sizeof(STUDENT));
    if(p==NULL)
    return NULL;
    memset(p->studentNumber,'',20);
    memset(p->studentName,'',20);
    memset(p->className,'',20);
    p->chinese=0。
      0;
    p->maths=0。0;
    p->english=0。0;
    p->total=0。0;
    p->average=0。0;
    p->next=NULL;
    p->last=NULL;
    return p;
    }


    void Insert(STUDENT *t) //链表插入
    {
    STUDENT *p;
    if (first==NULL)
    first=t;
    else
    { p=first;
    while (p->next)
    p=p->next;
    p->next=t;
    t->last=p;
    end=t;
    }
    }

    void loaddatabase() //加载数据
    {
    FILE *fp;
    STUDENT *p;
    fp=fopen("student。
      txt","r");
    if(!fp)
    {
    printf("打开文件出错 ");
    pause();
    return;
    }
    p=Malloc();
    //first=p;
    while(fscanf(fp,"%s%s%s%f%f%f%f%f",
    p->studentNumber,p->studentName,p->className,
    &(p->chinese),&(p->maths),&(p->english),&(p->total),&(p->average))>0)
    {
    Insert(p);
    p=Malloc();
    }
    fclose(fp);
    printf(" 欢迎使用学生成绩管理系统 ");
    printf(" 数据加载成功 ");
    system("pause");
    system("cls");
    }

    void savedata() //保存数据
    {
    FILE *fp;
    STUDENT *p;

    if (first==NULL )
    {
    printf("没有学生记录 ");
    pause();
    //return;
    }
    fp=fopen("student。
      txt","w ");
    if(!fp)
    {
    printf("打开文件出错 ");
    pause();
    return;
    }
    p=first;
    while(p)
    {
    fprintf(fp,"%s %s %s %f %f %f %f %f ",p->studentNumber,p->studentName,p->className,
    p->chinese,p->maths,p->english,p->total,p->average);
    p=p->next;
    }
    fclose(fp);
    printf("数据保存成功 ");
    }

    void add() //添加记录
    {
    STUDENT *p;
    STUDENT *newstudent=Malloc();
    printf("请输入学号:");
    scanf("%s",newstudent->studentNumber);
    printf("请输入姓名:");
    scanf("%s",newstudent->studentName);
    printf("请输入班级名称:");
    scanf("%s",newstudent->className);
    printf("请输入语文成绩:");
    scanf("%f",&newstudent->chinese);
    printf("请输入数学成绩:");
    scanf("%f",&newstudent->maths);
    printf("请输入外语成绩:");
    scanf("%f",&newstudent->english);
    newstudent->total=newstudent->chinese newstudent->maths newstudent->english;
    newstudent->average=(newstudent->total)/3;
    Insert(newstudent);

    }

    void pause()
    {
    system("pause");
    system("cls");
    }

    void printall() //打印全部记录
    {
    STUDENT *p;
    p=first;
    printf(" ");
    printf("学号 姓名 班级 语文 数学 外语 总分 平均分 ");
    while (p)
    {
    printf("%s %s %s %3。
      1f %3。1f %3。1f %3。1f %3。1f ",p->studentNumber,p->studentName,p->className,
    p->chinese,p->maths,p->english,p->total,p->average);
    p=p->next;
    }
    printf(" ");
    }

    void findbyname() //按姓名查询
    { int i=0;
    STUDENT *p;
    char buf[20];
    p=first;
    printf("请输入学生姓名:");
    scanf("%s",buf);
    while (p)
    {
    if (strcmp(p->studentName,buf)==0)
    { printf("%s %s %s %3。
      1f %3。1f %3。1f %3。1f %3。1f ",
    p->studentNumber,p->studentName,p->className,
    p->chinese,p->maths,p->english,p->total,p->average);
    i ;
    break;
    }
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到改学生记录 ");
    }

    void findbynumber() //按学号查询
    {
    int i=0;
    STUDENT *p;
    char buf[20];
    p=first;
    printf("请输入学生学号:");
    scanf("%s",buf);
    while (p)
    {
    if (strcmp(p->studentNumber,buf)==0)
    { printf("%s %s %s %3。
      1f %3。1f %3。1f %3。1f %3。1f ",
    p->studentNumber,p->studentName,p->className,
    p->chinese,p->maths,p->english,p->total,p->average);
    i ;
    }
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到改学生记录 ");
    }

    void findbyclass() //按班级名查询
    { int i=0;
    STUDENT *p;
    char buf[20];
    p=first;
    printf("请输入班级名:");
    scanf("%s",buf);
    while (p)
    {
    if (strcmp(p->className,buf)==0)
    { printf("%s %s %s %3。
      1f %3。1f %3。1f %3。1f %3。1f ",
    p->studentNumber,p->studentName,p->className,
    p->chinese,p->maths,p->english,p->total,p->average);
    i ;
    }
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到改学生记录 ");
    }



    int collect(){ //统计分数
    int totalstudentnum=0;
    double totalch=0。
      0,totalmh=0。0,totalen=0。0;
    double totalscore=0。0;
    STUDENT *p;
    p=first;
    while (p)
    {
    totalscore=totalscore p->total;
    totalch=totalch p->chinese;
    totalmh=totalmh p->maths;
    totalen=totalen p->english;
    totalstudentnum ;
    p=p->next;
    }
    printf("人数:%d 整体总分:%。
      1f 整体平均分:%。1f ",
    totalstudentnum,totalscore,(totalscore/totalstudentnum));
    printf("语文:%。
      1f 数学:%。1f 外语:%。1f ",totalch,totalmh,totalen);
    printf("语文平均分:%。1f 数学平均分:%。1f 外语平均分:%。1f ",
    (totalch/totalstudentnum),(totalmh/totalstudentnum),(totalen/totalstudentnum));
    }

    int delbyname()
    { int i=0;
    STUDENT *p,*pnext,*pbuf,*plast;
    char buf[20];
    p=first;
    printf("请输入需删除人员的姓名:");
    scanf("%s",buf);

    while (p)
    {
    if (strcmp(p->studentName,buf)==0)
    {
    i ;
    if (first==p)
    {
    pbuf=first;
    if (first->next==NULL) end=NULL;

    first=first->next;
    if (first) first->last=NULL;
    free(pbuf);
    printf("删除成功 ");
    return 0;
    }
    plast->next=p->next;
    if (p->next=NULL) end=p->last;
    free(p);
    printf("删除成功 ");
    }
    plast=p;
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到该学生记录 ");
    }


    int delbynumber()
    { int i=0;
    STUDENT *p,*pnext,*pbuf,*plast;
    char buf[20];
    p=first;
    printf("请输入需删除人员的学号:");
    scanf("%s",buf);

    while (p)
    {
    if (strcmp(p->studentNumber,buf)==0)
    {
    i ;
    if (first==p)
    {
    pbuf=first;
    if (first->next==NULL) end=NULL;
    first=first->next;
    //first->last=NULL;
    if (first) first->last=NULL;
    free(pbuf);
    printf("删除成功 ");
    return 0;
    }
    plast->next=p->next;
    if (p->next=NULL) end=p->last;
    free(p);
    printf("删除成功 ");
    }
    plast=p;
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到该学生记录 ");
    }


    void changebynumber()
    {
    int i=0;
    STUDENT *p;
    char buf[20];
    p=first;
    printf("请输入需修改人员的学号:");
    scanf("%s",buf);
    while (p)
    {
    if (strcmp(p->studentNumber,buf)==0)
    {
    printf("请输入学号:");
    scanf("%s",p->studentNumber);
    printf("请输入姓名:");
    scanf("%s",p->studentName);
    printf("请输入班级名称:");
    scanf("%s",p->className);
    printf("请输入语文成绩:");
    scanf("%f",&p->chinese);
    printf("请输入数学成绩:");
    scanf("%f",&p->maths);
    printf("请输入外语成绩:");
    scanf("%f",&p->english);
    p->total=p->chinese p->maths p->english;
    p->average=(p->total)/3;
    printf("修改成功");
    i ;
    return;
    }
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到改学生记录 ");
    }

    void changebyname()
    {
    int i=0;
    STUDENT *p;
    char buf[20];
    p=first;
    printf("请输入需修改人员的姓名:");
    scanf("%s",buf);
    while (p)
    {
    if (strcmp(p->studentName,buf)==0)
    {
    printf("请输入学号:");
    scanf("%s",p->studentNumber);
    printf("请输入姓名:");
    scanf("%s",p->studentName);
    printf("请输入班级名称:");
    scanf("%s",p->className);
    printf("请输入语文成绩:");
    scanf("%f",&p->chinese);
    printf("请输入数学成绩:");
    scanf("%f",&p->maths);
    printf("请输入外语成绩:");
    scanf("%f",&p->english);
    p->total=p->chinese p->maths p->english;
    p->average=(p->total)/3;
    printf("修改成功 ");
    i ;
    return;
    }
    p=p->next;
    }
    if (i==0)
    printf("sorry,没找到改学生记录 ");
    }


    void sortbychinese() //排序
    {
    STUDENT *p,*p1,*p2,*plast=NULL,*pnext=NULL;
    for(p1=end->last;p1;p1=p1->last)
    {

    // printf("外层 %f ",p1->chinese);
    // printall();
    // if (p1->next==NULL) p1=p1->last;
    for(p2=first,p=first->next;p2!=p1->next;p=p->next,p2=p2->next) //p指向P2后面一个数据
    {
    // printf("调整之前p=%f p2=%f ",p->chinese,p2->chinese);
    if ((p->chinese)-(p2->chinese)>0)
    {
    //printf("1 ");
    plast=p2->last; //保存前节点
    pnext=p->next; //保存后节点

    if (plast) plast->next=p; //前节点接后面
    else
    { first=p;
    p->last=NULL;}
    if (pnext) pnext->last=p2; //后节点接前面
    else
    {end=p2;
    p2->next=NULL;
    }
    if (p1==p) p1=p2; //外层指针校正
    else if (p1==p2)
    p1=p;
    // printf("2 ");
    p->next=p2; //节点交换
    p2->last=p;

    p2->next=pnext;
    // printf("3 ");

    p->last=plast;
    // printf("4 ");
    // else
    //p->last=NULL;
    p2=p;
    p=p2->next;

    //if (p==NULL) break;
    //printf("3 ");
    //if (p->last==first) break;
    // printall();
    //printf ("5 ");
    // printf("调整之后p=%f p2=%f ",p->chinese,p2->chinese);
    }
    }


    }

    //printall();
    }


    void sortbymaths() //排序
    {
    STUDENT *p,*p1,*p2,*plast=NULL,*pnext=NULL;
    for(p1=end->last;p1;p1=p1->last)
    {
    // if (p1->next==NULL) p1=p1->last;
    for(p2=first,p=first->next;p2!=p1->next && p;p=p->next,p2=p2->next) //p指向P2后面一个数据
    {

    if ((p->maths)-(p2->maths)>0)
    {
    //printf("1 ");
    plast=p2->last; //保存前节点
    pnext=p->next; //保存后节点

    if (plast) plast->next=p; //前节点接后面
    else
    { first=p;
    p->last=NULL;}
    if (pnext) pnext->last=p2; //后节点接前面
    else
    {end=p2;
    p2->next=NULL;
    }
    if (p1==p) p1=p2; //外层指针校正
    else if (p1==p2)
    p1=p;
    // printf("2 ");
    p->next=p2; //节点交换
    p2->last=p;

    p2->next=pnext;
    // printf("3 ");

    p->last=plast;
    // printf("4 ");
    // else
    //p->last=NULL;
    p2=p;
    p=p2->next;
    //if (p==NULL) break;
    //printf("3 ");
    //if (p->last==first) break;
    // printall();
    // printf ("5 ");
    }
    }
    }
    //printall();
    }

    void sortbyenglish() //排序
    {
    STUDENT *p,*p1,*p2,*plast=NULL,*pnext=NULL;
    for(p1=end->last;p1;p1=p1->last)
    {
    // if (p1->next==NULL) p1=p1->last;
    for(p2=first,p=first->next;p2!=p1->next && p;p=p->next,p2=p2->next) //p指向P2后面一个数据
    {

    if ((p->english)-(p2->english)>0)
    {
    //printf("1 ");
    plast=p2->last; //保存前节点
    pnext=p->next; //保存后节点

    if (plast) plast->next=p; //前节点接后面
    else
    { first=p;
    p->last=NULL;}
    if (pnext) pnext->last=p2; //后节点接前面
    else
    {end=p2;
    p2->next=NULL;
    }
    if (p1==p) p1=p2; //外层指针校正
    else if (p1==p2)
    p1=p;
    // printf("2 ");
    p->next=p2; //节点交换
    p2->last=p;

    p2->next=pnext;
    // printf("3 ");

    p->last=plast;
    // printf("4 ");
    // else
    //p->last=NULL;
    p2=p;
    p=p2->next;
    //if (p==NULL) break;
    //printf("3 ");
    //if (p->last==first) break;
    // printall();
    // printf ("5 ");
    }
    }
    }

    }

    void sortbytotal() //排序
    {
    STUDENT *p,*p1,*p2,*plast=NULL,*pnext=NULL;
    for(p1=end->last;p1;p1=p1->last)
    {
    // if (p1->next==NULL) p1=p1->last;
    for(p2=first,p=first->next;p2!=p1->next && p;p=p->next,p2=p2->next) //p指向P2后面一个数据
    {

    if ((p->total)-(p2->total)>0)
    {
    // printf("1 ");
    plast=p2->last; //保存前节点
    pnext=p->next; //保存后节点

    if (plast) plast->next=p; //前节点接后面
    else
    { first=p;
    p->last=NULL;}
    if (pnext) pnext->last=p2; //后节点接前面
    else
    {end=p2;
    p2->next=NULL;
    }
    if (p1==p) p1=p2; //外层指针校正
    else if (p1==p2)
    p1=p;
    // printf("2 ");
    p->next=p2; //节点交换
    p2->last=p;

    p2->next=pnext;
    // printf("3 ");

    p->last=plast;
    // printf("4 ");
    // else
    //p->last=NULL;
    p2=p;
    p=p2->next;
    //if (p==NULL) break;
    //printf("3 ");
    //if (p->last==first) break;
    //printall();
    // printf ("5 ");
    }
    }
    }

    }








    void exit_free()
    {
    STUDENT *p,*p1;
    p=first;
    while (p)
    {
    p1=p;
    free(p1);
    p=p->next;
    }

    }

    void ver()
    {
    system("cls");
    printf(" ");
    printf(" 学生成绩管理系统 v1。
      0");
    printf(" ");
    printf(" 作者 风中的纸屑 ");

    }



    int menu(void)
    {
    int choose;
    //system("cls");

    printf(" ");
    printf(" 1: 显示所有学生的信息 |");
    printf(" 2: 按姓名查询 | ");
    printf(" 3: 按学号查询 |");
    printf(" 4: 按班级查询 | ");
    printf(" 5: 通过姓名删除学生 | ");
    printf(" 6:按学号删除学生 | ");
    printf(" 7: 按学号修改学生成绩 |");
    printf(" 8: 按姓名修改学生成绩 | ");
    printf(" 9: 添加 |");
    printf(" 10: 语文排序 | ");
    printf(" 11: 数学排序 |");
    printf(" 12: 外语排序 | ");
    printf(" 13: 总分排序 |");
    printf(" 14:汇总 | ");
    printf(" 15:保存所有学生信息 |");
    printf(" 16:清屏 | ");
    printf(" 17: 版本 |");
    printf(" 18: 退出 | ");
    printf(" ");
    printf(" ");
    printf(" 请输入对应数字:");
    scanf("%d",&choose);/*取得用户的选择*/

    switch(choose)
    {
    case 1:
    system("cls");
    printall();/*显示所有学生的信息*/
    printf(" ");
    collect();
    break;
    case 2:
    findbyname();
    break;
    case 3:
    findbynumber();/*根据用户输入的学号显示该学生的信息*/
    break;
    case 4:
    findbyclass();
    break;
    case 5:
    printall();
    delbyname();
    break;
    case 6:
    printall();
    delbynumber();
    break;
    case 7:
    printall();
    changebynumber();/*根据用户输入的学号修改学生成绩*/
    break;
    case 8:
    printall();
    changebyname();/*保存数据*/
    break;
    case 9:
    add();
    break;
    case 10:
    system("cls");
    sortbychinese();
    printall();
    break;
    case 11:
    system("cls");
    sortbymaths();
    printall();
    break;
    case 12:
    system("cls");
    sortbyenglish();
    printall();
    break;
    case 13:
    system("cls");
    sortbytotal();
    //sortbytotal();
    printall();
    break;
    case 14:
    collect();
    break;
    case 15:
    savedata();
    break;
    case 16:
    system("cls");
    break;
    case 17:
    system("cls");
    ver();
    break;
    case 18:
    exit_free();
    exit(1);
    break;
    default: system("cls"); break;
    }
    }





    int main()
    {
    loaddatabase();
    while (1)
    menu();
    }


    这是我以前写得,基本符合楼主要求,贴上来了。
      

    杨***

    2018-04-02 07:29:59

类似问题

换一换
  • C/C++ 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 174-193
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):