首页    新闻    小组    威客    人才    下载    博客    代码贴    在线编程    论坛
代码贴夏、芷的代码贴全部
#include"stdio.h"#include"stdlib.h"#include"string.h"#define N 3typedef struct z1{    char no[11];    char name[15];    int score[N];    float sum;    float average;    int order;    struct z1 *next;}STUDENT;STUDENT *init();STUDENT *create();STUDENT *del(STUDENT *h);void print(STUDENT *h);void search1(STUDENT *h);void search2(STUDENT *h);STUDENT *insert(STUDENT *h);void sort(STUDENT *h);void save(STUDENT *h);void tongji(STUDENT *h);int menu_select();STUDENT *load();void inputs(char *prompt,char *s,int count);STUDENT *load();main(){    int i;    STUDENT *head;    head=init();    for(;;)    {        switch(menu_select())        {            case 0:head=init();break;            case 1:head=create();break;            case 2:head=insert(head);break;            case 3:save(head);break;            case 4:print(head);break;            case 5:search1(head);break;            case 6:head=del(head);break;            case 7:sort(head);break;            case 8:tongji(head);break;            case 9:search2(head);break;            case 10:exit(0);        }    }}int menu_select(){    char *menu[]={"************菜单************",    "0. 初始化链表",    "1. 输入学生成绩",    "2. 插入学生成绩",    "3. 保存学生记录",    "4. 显示学生记录",    "5. 按学号查找学生信息",    "6. 删除指定学号的学生信息",    "7. 按某一门课对学生成绩排序",    "8. 统计某门课程的学生成绩",    "9. 按姓名查找学生信息",    "10. 退出系统"};    char s[3];    int c,i;    for(i=0;i<=11;i++)    printf("        %s\n",menu[i]);    do    {        printf("\n请选择0~10中的某一个选项\n");        scanf("%s",s);        c=atoi(s);    }while(c<0||c>10);    return c;}STUDENT *init(){    return NULL;}STUDENT *create(){    int i;int s;    STUDENT *h=NULL,*info;    for(;;)    {        info=(STUDENT *)malloc(sizeof(STUDENT));        if(!info)        {            printf("\n内存不足");            return NULL;        }        inputs("输入学号:",info->no,11);        if(info->no[0]=='@')break;        inputs("输入姓名:",info->name,15);        printf("开始输入%d门课的成绩\n",N);        s=0;        for(i=0;i<N;i++)        {            do{                printf("第%d门分数:",i+1);                scanf("%d",&info->score[i]);                if(info->score[i]>100||info->score[i]<0)                printf("输入成绩错误,请重新输入:\n");            }while(info->score[i]>100||info->score[i]<0);            s=s+info->score[i];        }        info->sum=s;        info->average=(float)s/N;        info->order=0;        info->next=h;        h=info;    }    return h;}void inputs(char *prompt,char *s,int count){    char p[255];    do    {        printf(prompt);        scanf("%s",p);        if(strlen(p)>count)        printf("\n太长了!\n");    }while(strlen(p)>count);    strcpy(s,p);}void print(STUDENT *h){    int i=0;    STUDENT *p;    p=h;    printf("\n\n\n***********************学生***********************\n");    printf("|序号|学号  |  姓名   | 语文 | 英语 |数学 | 总分 |平均分 |名次 |\n");    printf("|---|-------|--------|----|----|----|------|------|---|\n");    while(p!=NULL)    {        i++;        printf("|%3d |%-10s|%-8s|%4d|%4d|%4d|%4.2f|%4.2f|%3d|\n",i,p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);        p=p->next;    }    printf("***********************end***********************\n");}STUDENT *del(STUDENT *h){    STUDENT *p,*q;    char s[11];    printf("请输入要删除的学生的学号\n");    scanf("%s",s);    q=p=h;    while(strcmp(p->no,s)&&p!=NULL)    {        q=p;        p=p->next;    }    if(p==NULL)    printf("\n链表中没有学号为%s的学生\n",s);    else    {        printf("\n\n\n***********************找到了***********************\n");    printf("|学号    |    姓名  | 语文  |  英语  | 数学 |  总分  | 平均分 | 名次 |\n");    printf("|----------|----------|----|----|----|------|------|---|\n");    printf("|%-10s|%-8s|%4d|%4d|%4d|%4.2f|%4.2f|%3d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);    printf("***********************end***********************\n");    printf("请按任意键删除\n");    getchar();    if(p==h)    h=p->next;    else q->next=p->next;    free(p);    printf("\n已经删除学号为%s的学生\n",s);    printf("不要忘了保存数据\n");    }    return h;}void search1(STUDENT *h){    STUDENT *p;    char s[11];    printf("请输入你要查找的同学的学号\n");    scanf("%s",s);    p=h;    while(strcmp(p->no,s)&&p!=NULL)    p=p->next;    if(p==NULL)    printf("'n没有学号为%s的学生\n",s);    else    {        printf("\n\n\n***********************找到了***********************\n");        printf("|学号      |    姓名   | 语文  |  英语  | 数学 |  总分  | 平均分 | 名次 |\n");        printf("|----------|-----------|----|----|----|------|------|---|\n");        printf("|%-10s|%-8s|%4d|%4d|%4d|%4.2f|%4.2f|%3d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);        printf("***********************end***********************\n");    }}void search2(STUDENT *h){    STUDENT *p;    char s[11];    printf("请输入你要查找的同学的姓名\n");    scanf("%s",s);    p=h;    while(strcmp(p->name,s)&&p!=NULL)    p=p->next;    if(p==NULL)    printf("\n没有姓名为%s的学生\n",s);    else    {        printf("\n\n\n***********************找到了***********************\n");        printf("|学号      |   姓名  | 语文  |  英语  | 数学 |  总分  | 平均分 | 名次 |\n");        printf("|----------|-----------|----|----|----|------|------|---|\n");        printf("|%-10s|%-8s|%4d|%4d|%4d|%4.2f|%4.2f|%3d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);        printf("***********************end***********************\n");    }}STUDENT *insert(STUDENT *h){    STUDENT *p,*q,*info;    char s[11];    int s1,i;    printf("请输入插入点的学生学号\n");    scanf("%s",s);    printf("\n请输入新的学生信息\n");    info=(STUDENT *)malloc(sizeof(STUDENT));    if(!info)    {        printf("\n内存不足!");        return NULL;    }    inputs("输入学号:",info->no,11);    inputs("输入姓名:",info->name,15);    printf("请输入%d门课的分数\n",N);    s1=0;    for(i=0;i<N;i++)    {        do{            printf("分数%d",i+1);            scanf("%d",&info->score[i]);            if(info->score[i]>100||info->score[i]<0)            printf("输入数据有误,请重新输入\n");        }while(info->score[i]>100||info->score[i]<0);        s1=s1+info->score[i];    }    info->sum=s1;    info->average=(float)s1/N;    info->order=0;    info->next=NULL;    p=h;    q=h;    while(strcmp(p->no,s)&&p!=NULL)    {q=p;p=p->next;}    if(p==NULL)    if(p==h)    h=info;    else q->next=info;    else    if(p==h)    {        info->next=p;        h=info;    }    else    {        info->next=p;        q->next=info;    }    printf("\n已经插入了%s这个学生\n",info->name);    printf("----不要忘了存盘啊--\n");    return(h);}void save(STUDENT *h){    FILE *fp;    STUDENT *p;    char outfile[10];    printf("请输入保存文件的文件名,例如 c:\\f1\\te.txt:\n");    scanf("%s",outfile);    if((fp=fopen(outfile,"wb"))==NULL)    {        printf("不能打开文件\n");        exit(1);    }    printf("\n正在保存......\n");    p=h;    while(p!=NULL)    {        fwrite(p,sizeof(STUDENT),1,fp);        p=p->next;    }    fclose(fp);    printf("------保存成功!!!------\n");}STUDENT *load(){    STUDENT *p,*q,*h=NULL;    FILE *fp;    char infile[10];    printf("请输入要读取数据的文件名,例如c:\\f1\\te.txt:\n");    scanf("%s",infile);     if((fp=fopen(infile,"wb"))==NULL)    {        printf("不能打开文件\n");        exit(1);    }    printf("正在读取数据....\n");    p=(STUDENT *)malloc(sizeof(STUDENT));    if(!p)    {        printf("内存不足!\n");        exit(1);    }    h=p;    while(!feof(fp))    {        if(1!=fread(p,sizeof(STUDENT),1,fp))        break;        p->next=(STUDENT *)malloc(sizeof(STUDENT));        if(!p->next)        {            printf("内存不足!\n");            return h;        }        q=p;        p=p->next;    }    q->next=NULL;    fclose(fp);    printf("---你已经成功地从文件中读取了数据!!!---\n");    return h;}void sort(STUDENT *h){    int i=0,j;    STUDENT *p,*q,*t,*h1;    printf("请输入要按哪门课程的编号来排序:(0.语文 1.数学 2.英语)\n");    scanf("%d",&j);    h1=h->next;    h->next=NULL;    while(h1!=NULL)    {        t=h1;        h1=h1->next;        p=h;        q=h;        while(t->score[j]<p->score[j]&&p!=NULL)        {            q=p;            p=p->next;        }        if(p==q)        {            t->next=p;            h=t;        }        else        {            t->next=p;            q->next=t;        }    }    p=h;    while(p!=NULL)    {        i++;        p->order=i;        p=p->next;    }    print(h);   printf("排序成功!!!\n");}void tongji(STUDENT *h){    STUDENT *p;    int a,b,i;    printf("请输入课程编号\n");    scanf("%d",&i);    printf("请输入分数段:\n");    scanf("%d,%d",&a,&b);    p=h;    while(p!=NULL)    {       printf("\n\n\n***********************找到了***********************\n");       if(p->score[i]>=a&&p->score[i]<=b)        {          printf("|学号    |   姓名  | 语文  |  英语  | 数学 |  总分  | 平均分 | 名次 |\n");          printf("|--------|---------|----|----|----|------|------|---|\n");          printf("|%-10s|%-8s|%4d|%4d|%4d|%4.2f|%4.2f|%3d|\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);        }        p=p->next;    }    printf("***********************end***********************\n");}
阅读全部 | 2012年12月12日 21:02
1
夏、芷