代码改变世界

学生信息录入(学号 姓名 成绩),并按学号查找。

2016-06-01 15:58  Purples  阅读(2905)  评论(0编辑  收藏  举报
#include<stdio.h>
#include<stdlib.h>
#define N 100
struct student
{
        char num[9];
        char name[20];
        int score;
};
void imput(struct student stu[],int n)
{
        int i;
        printf("依次输入学生学号、姓名、成绩:\n");
        for(i=0;i<n;i++)
        {
                scanf("%s",stu[i].num);
                scanf("%s",stu[i].name);
                scanf("%d",&stu[i].score);
        }
}
void sort(struct student st[],int n)
{
        struct student t;
        int i,j,k;
        for(i=0;i<n-1;i++)
        {
                k=i;
                for(j=i+1;j<n;j++)
                {
                        if(st[k].score<st[j].score)
                        k=j;
                }
                if(k!=i)
                {
                        t=st[i];st[i]=st[k];st[k]=t;
                }
        }

}
void output(struct student stu[],int n)
{
        printf("排序后的信息为:\n");
        int i;
        for(i=0;i<n;i++)
        {
                printf("%s ",stu[i].num);
                printf("%s ",stu[i].name);
                printf("%d ",stu[i].score);
                printf("\n");
        }

}
void serch(struct student st[],int n)
{
        printf("请输入学生8位学号进行查找:\n");
        int i,k=-1;
        char c[9];
        scanf("%s",c);
        for(i=0;i<n;i++)
        {
                if(strcmp(st[i].num,c)==0)
                {
                         k=i;
                         printf("%s ",st[i].num);
                         printf("%s ",st[i].name);
                         printf("%d ",st[i].score);
                         printf("\n");

                }
        }
        if(k==-1)printf("该学号不存在。");

}
void main()
{
        struct student st[N];
        int n;
        printf("请输入学生数量: ");
        scanf("%d",&n);
        imput(st,n);
        sort(st,n);
        output(st,n);
        serch(st,n);
}