| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1771 人关注过本帖
标题:求大神帮忙,怎么用c语言顺序表做一个健康表
只看楼主 加入收藏
旧丨雨
Rank: 1
等 级:新手上路
帖 子:10
专家分:4
注 册:2016-4-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
求大神帮忙,怎么用c语言顺序表做一个健康表
健康表可以插入、删除姓名学号身体状况
搜索更多相关主题的帖子: 身体状况 c语言 健康 姓名 
2016-04-18 08:01
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:10 
给个例子,请自行根据实际情况修改。
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NAMElEN        20
#define STATUELEN   10

typedef struct tag_student
{
    char name[NAMElEN];
    int num;
    char status[STATUELEN];
    struct tag_student *next;
} STUDENT, *student;

student head = NULL;

void Build()//建立一个带头结点的单链表
{    
    head = (student)malloc(sizeof(STUDENT));
    memset(head, 0, sizeof(STUDENT));
}

void Print(student L)//
{
    printf("学生姓名:%20s 学号%4d, 健康状况:%10s\n", L->name, L->num, L->status);
}

void FindWithName(char *name)//
{
    student p;
    if (head == NULL)
    {
        Build();
        printf("没找到!!\n\n");
        return;
    }
    p = head;
    while (p && strcmp(p->name, name))
        p = p->next;
    if (p)
        Print(p);
    else
        printf("没找到!!\n\n");
}

void FindWithNum(int num)//
{
    student p;
    if (head == NULL)
    {
        Build();
        printf("没找到!!\n\n");
        return;
    }
    p = head;
    while (p && p->num != num)
        p = p->next;
    if (p)
        Print(p);
    else
        printf("没找到!!\n\n");
}

void ShowAll()//
{
    student p;
    if (head == NULL)
    {
        Build();
        printf("没找到!!\n");
        return;
    }
    p = head;
    while (p)
    {
        Print(p);
        p = p->next;
    }    
}

void DeleteWithName(char *name)//
{
    student p, q;
    if (head == NULL)
    {
        Build();
        printf("没有找到该生%s信息\n", name);
        return;
    }
    p = head;
    if (0 == strcmp(p->name, name))
    {
        head = head->next;
        free(p);
        printf("删除成功!!\n\n");
        return;
    }
    while (p->next &&strcmp(p->next->name, name))
        p = p->next;
    if (p->next)
    {
        q = p->next;
        p->next = q->next;
        free(q);
        printf("删除成功!!\n\n");        
    }
    else
        printf("没有找到该生%s信息\n", name);
}

void DeleteWithNum(int num)//
{
    student p, q;
    if (head == NULL)
    {
        Build();
        printf("没有找到该生%d信息\n", num);
        return;
    }
    p = head;
    if (p->num == num)
    {
        head = head->next;
        free(p);
        printf("删除成功!!\n\n");
        return;
    }
    while (p->next &&p->next->num != num)
        p = p->next;
    if (p->next)
    {
        q = p->next;
        p->next = q->next;
        free(q);
        printf("删除成功!!\n");        
    }
    else
        printf("没有找到该学号%d对应信息\n", num);
}

void Insert(student p)//
{
    student s,q;
    
    if (head == NULL)
    {
        Build();
    }
    s = head;
    if (head->name[0] == 0)
    {
        strcpy(head->name, p->name);
        head->num = p->num;
        strcpy(head->status, p->status);
        return;
    }
    while (s->next)
        s = s->next;
    
    q = (student)malloc(sizeof(STUDENT));
    memset(q, 0, sizeof(STUDENT));
    strcpy(q->name, p->name);
    q->num = p->num;
    strcpy(q->status, p->status);
    s->next = q;
}

int main()
{
    int op, x;
    STUDENT st;
    Build();
    
    while (1)
    {    
        printf("请选择你要做的操作\n");
        printf(" 1.插入一个学生健康表\n");
        printf(" 2.删除一个学生健康表\n");
        printf(" 3.查找一个学生健康表\n");
        printf(" 4.退出系统\n");
        scanf("%d", &op);
        switch (op)
        {
            case 1:
                printf("请输入学生信息,用空格隔开:姓名 学号 健康状况\n");
                scanf("%s %d %s", st.name, &st.num, st.status);
                Insert(&st);
                printf("操作完成!\n\n");
                break;
            case 2:
                printf("请输入删除方式\n");
                printf(" 1.按姓名删除\n");
                printf(" 2.按学号删除\n");
                scanf("%d", &x);
                switch (x)
                {
                    case 1:
                        printf("请输入学生姓名\n");
                        scanf("%s", st.name);
                        DeleteWithName(st.name);
                        break;
                    case 2:
                        printf("请输入学生学号\n");
                        scanf("%d", &st.num);
                        DeleteWithNum(st.num);
                        break;
                    default:
                        printf("输入错误!\n\n");
                        break;
                }                
                break;
            case 3:
                printf("请输入查找方式\n");
                printf(" 1.按姓名查找\n");
                printf(" 2.按学号查找\n");
                printf(" 3.显示所有学生信息\n");
                scanf("%d", &x);
                switch (x)
                {
                case 1:
                    printf("请输入学生姓名\n");
                    scanf("%s", st.name);
                    FindWithName(st.name);                
                    break;
                case 2:
                    printf("请输入学生学号\n");
                    scanf("%d", &st.num);
                    FindWithNum(st.num);                    
                    break;
                case 3:
                    ShowAll();
                    break;
                default:
                    printf("输入错误!\n\n");
                    break;
                }
                break;
            case 4:
                return;
            default:            
                    printf("输入错误!\n\n");
                break;
        }
    }
    return 0;
}
2016-04-18 14:09
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:10 
送上我的作业代码:学生成绩管理系统V5.0(前面还有V1.0 2.0 3.0 4.0,,5.0包含前面的所有功能)
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define   MAX_LEN  10                        /* 字符串最大长度 */
#define   STU_NUM 30                       /* 最多的学生人数 */
#define   COURSE_NUM 6                     /* 最多的考试科目数 */
typedef struct student
{
    long num;                        /* 每个学生的学号 */
    char name[MAX_LEN];                /* 每个学生的姓名 */
    float score[COURSE_NUM];        /* 每个学生COURSE_NUM门功课的成绩 */
    float sum;                            /* 每个学生的总成绩 */
    float aver;                        /* 每个学生的平均成绩 */
}STU;
int   Menu(void);
void  ReadScore(STU stu[], int n, int m);
void  AverSumofEveryStudent(STU stu[], int n, int m);
void  AverSumofEveryCourse(STU stu[], int n, int m);
void  SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b));
int   Ascending(float a, float b);
int   Descending(float a, float b);
void  SwapFloat(float *x, float *y);
void  SwapLong(long *x, long *y);
void  SwapChar(char x[], char y[]);
void  AsSortbyNum(STU stu[], int n, int m);
void  SortbyName(STU stu[], int n, int m);
void  SearchbyNum(STU stu[], int n, int m);
void  SearchbyName(STU stu[], int n, int m);
void  StatisticAnalysis(STU stu[], int n, int m);
void  PrintScore(STU stu[], int n, int m);
int main() {
    int n;//学生人数
    int m;
    printf("Input student number(n<30):\n");
    scanf("%d",&n);
    STU*stu=(STU*)calloc(n,sizeof(STU));
    int choice;
u1:
    choice=Menu();
    switch(choice) {
        case 1:
            printf("Input course number(m<=%d):\n",COURSE_NUM);
            scanf("%d",&m);
            printf("Input student's ID, name and score:\n");
            ReadScore(stu,n,m);
            goto u1;
            break;
        case 2:
            AverSumofEveryCourse(stu,n,m);
            goto u1;
            break;
        case 3:
            AverSumofEveryStudent(stu,n,m);
            goto u1;
            break;
        case 4:
            SortbyScore(stu,n,m,Descending);
            printf("Sort in descending order by score:\n");
            PrintScore(stu,n,m);
            goto u1;
            break;
        case 5:
            SortbyScore(stu,n,m,Ascending);
            printf("Sort in ascending order by score:\n");
            PrintScore(stu,n,m);
            goto u1;
            break;
        case 6:
            AsSortbyNum(stu,n,m);
            printf("Sort in ascending order by number:\n");
            PrintScore(stu,n,m);
            goto u1;
            break;
        case 7:
            SortbyName(stu,n,m);
            printf("Sort in dictionary order by name:\n");
            PrintScore(stu,n,m);
            goto u1;
            break;
        case 8:
            SearchbyNum(stu,n,m);
            goto u1;
            break;
        case 9:
            SearchbyName(stu,n,m);
            goto u1;
            break;
        case 10:
            StatisticAnalysis(stu,n,m);
            goto u1;
            break;
        case 11:
            PrintScore(stu,n,m);
            goto u1;
            break;
        case 0:
            printf("End of program!");
            break;
        default:
            printf("Input error!\n");
            goto u1;
            break;
    }

    return 0;
}
void  PrintScore(STU stu[], int n, int m){
        for(int i=0; i<n; i++) {
        printf("%ld\t%s\t",stu[i].num,stu[i].name);
        for(int j=0; j<m; j++) {
            printf("%.0f\t",stu[i].score[j]);
        }
        printf("%.0f\t%.0f\n",stu[i].sum,stu[i].aver);
    }
}
void  StatisticAnalysis(STU stu[], int n, int m){
        for(int j=0; j<m; j++) {
        int array[11]= {0};
        for(int i=0; i<n; i++) {
            if(stu[i].score[j]<60)array[5]++;
            else if(stu[i].score[j]<70)array[6]++;
            else if(stu[i].score[j]<80)array[7]++;
            else if(stu[i].score[j]<90)array[8]++;
            else if(stu[i].score[j]<100)array[9]++;
            else if(stu[i].score[j]==100)array[10]++;
        }
        printf("For course %d:\n",j+1);
        printf("<60\t%d\t%.2f%%\n",array[5],array[5]*100.0/n);
        printf("%d-%d\t%d\t%.2f%%\n",60,69,array[6],array[6]*100.0/n);
        printf("%d-%d\t%d\t%.2f%%\n",70,79,array[7],array[7]*100.0/n);
        printf("%d-%d\t%d\t%.2f%%\n",80,89,array[8],array[8]*100.0/n);
        printf("%d-%d\t%d\t%.2f%%\n",90,99,array[9],array[9]*100.0/n);
        printf("%d\t%d\t%.2f%%\n",100,array[10],array[10]*100.0/n);
    }
}
void  SearchbyName(STU stu[],int n, int m) {
    char key[MAX_LEN];
    printf("Input the name you want to search:\n");
    scanf("%s",key);
    while(getchar()!='\n');
    int flag=0;
    for(int i=0; i<n; i++) {
        if(strcmp(stu[i].name,key)==0) {
            flag=1;
            printf("%ld\t%s\t",stu[i].num,stu[i].name);
            for(int j=0; j<m; j++)printf("%.0f\t",stu[i].score[j]);
            printf("%.0f\t%.0f\n",stu[i].sum,stu[i].aver);
            break;
        }
    }
    if(!flag)printf("Not found!\n");
}
void  SearchbyNum(STU stu[], int n, int m){
    long key;
    int flag=0;
    printf("Input the number you want to search:\n");
    scanf("%ld",&key);
    for(int i=0; i<n; i++) {
        if(stu[i].num==key) {
            flag=1;
            printf("%ld\t%s\t",stu[i].num,stu[i].name);
            for(int j=0; j<m; j++)printf("%.0f\t",stu[i].score[j]);
            printf("%.0f\t%.0f\n",stu[i].sum,stu[i].aver);
            break;
        }
    }
    if(!flag)printf("Not found!\n");
}
void  SortbyName(STU stu[], int n, int m){
    for(int i=0; i<n; i++) {
        for(int j=i+1; j<n; j++) {
            if(strcmp(stu[i].name,stu[j].name)>0) {
            for(int f=0;f<m; f++)SwapFloat(&stu[i].score[f],&stu[j].score[f]);
                SwapLong(&stu[i].num,&stu[j].num);
                SwapChar(stu[i].name,stu[j].name);
                SwapFloat(&stu[i].sum,&stu[j].sum);
                SwapFloat(&stu[i].aver,&stu[j].aver);
            }
        }
    }
}
void  AsSortbyNum(STU stu[], int n, int m){
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(stu[i].num>stu[j].num){
                for(int f=0;f<m; f++)SwapFloat(&stu[i].score[f],&stu[j].score[f]);
                SwapLong(&stu[i].num,&stu[j].num);
                SwapChar(stu[i].name,stu[j].name);
                SwapFloat(&stu[i].sum,&stu[j].sum);
                SwapFloat(&stu[i].aver,&stu[j].aver);
            }
        }
    }
}
void  SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b)){
    for(int i=0; i<n; i++) {
        for(int j=i+1; j<n; j++) {
            if((*compare)(stu[i].sum,stu[j].sum)) {
            for(int f=0;f<m; f++)SwapFloat(&stu[i].score[f],&stu[j].score[f]);
                SwapLong(&stu[i].num,&stu[j].num);
                SwapChar(stu[i].name,stu[j].name);
                SwapFloat(&stu[i].sum,&stu[j].sum);
                SwapFloat(&stu[i].aver,&stu[j].aver);
            }
        }
    }
}

void  AverSumofEveryStudent(STU stu[], int n, int m) {
    for(int i=0; i<n; i++) {
        float sum=0;
        for(int j=0; j<m; j++) {
            sum+=stu[i].score[j];
        }
        stu[i].sum=sum;
        stu[i].aver=sum/m;
        printf("student %d: sum=%.0f,aver=%.0f\n",i+1,stu[i].sum,stu[i].aver);
    }
}
void  AverSumofEveryCourse(STU stu[], int n, int m) {
    for(int i=0; i<m; i++) {
        float sum=0;
        for(int j=0; j<n; j++) {
            sum+=stu[j].score[i];
        }
        printf("course %d:sum=%.0f,aver=%.0f\n",i+1,sum,sum/n);
    }
}
void  ReadScore(STU stu[], int n, int m) {
    for(int i=0; i<n; i++) {

        scanf("%ld",&stu[i].num);
        scanf("%s",stu[i].name);
        for(int j=0; j<m; j++)
            scanf("%f",&stu[i].score[j]);

    }
}
int Menu(void) {
    printf("Management for Students' scores\n\
1.Input record\n\
2.Caculate total and average score of every course\n\
3.Caculate total and average score of every student\n\
4.Sort in descending order by score\n\
5.Sort in ascending order by score\n\
6.Sort in ascending order by number\n\
7.Sort in dictionary order by name\n\
8.Search by number\n\
9.Search by name\n\
10.Statistic analysis\n\
11.List record\n\
0.Exit\n\
Please Input your choice:\n");
    int n;
    scanf("%d",&n);
    return n;
}
int Ascending(float a,float b) {
    return a>b;
}
int   Descending(float a, float b) {
    return a<b;
}
void  SwapFloat(float *x, float *y) {
    float u;
    u=*x;
    *x=*y;
    *y=u;
}
void  SwapLong(long *x, long *y) {
    long u;
    u=*x;
    *x=*y;
    *y=u;
}
void  SwapChar(char x[], char y[]) {
    char u[MAX_LEN];
    strcpy(u,x);
    strcpy(x,y);
    strcpy(y,u);
}


φ(゜▽゜*)♪
2016-04-18 14:15
旧丨雨
Rank: 1
等 级:新手上路
帖 子:10
专家分:4
注 册:2016-4-18
收藏
得分:0 
回复 2楼 grmmylbs
多谢
2016-04-18 23:02
旧丨雨
Rank: 1
等 级:新手上路
帖 子:10
专家分:4
注 册:2016-4-18
收藏
得分:0 
回复 3楼 书生牛犊
多谢了哈
2016-04-18 23:03
快速回复:求大神帮忙,怎么用c语言顺序表做一个健康表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038953 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved