学生成绩处理

作者在 2011-06-21 12:53:26 发布以下内容
#include <stdio.h>
#include <string.h>
typedef struct
{
    char number[10];
    char name[16];
    int score[4];
    int total;
    int position;
}student;

void sr(student st[],int n)  //录入信息函数
{
    int i;
    for(i = 1;i <= n;i ++)
    {
        printf("输入学号 姓名: \n");
        scanf("%s %s",st[i].number,st[i].name);
        printf("成绩1,成绩2,成绩3,成绩4\n");
        scanf("%d,%d,%d,%d",&st[i].score[0],&st[i].score[1],&st[i].score[2],&st[i].score[3]);
        
        st[i].total = st[i].score[0] + st[i].score[1] + st[i].score[2] + st[i].score[3];
        st[i].position = 1;    //为确定名次之前全赋值为1
    }
}

void cx(student st[],int n)  //信息查询函数
{
    int xz,i;
    char number[10];
    char name[16];
    
    printf("1.按照学号查询    2.按照姓名查询 \n");
    printf("  请输入选择 1 或者 2 并按回车: ");
    scanf("%d",&xz);
    getchar();
    if(xz == 1)
    {
        printf("请输入学号: ");
        gets(number);
    }
    else if(xz == 2)
    {
        printf("请输入姓名: ");
        gets(name);
    }
    
    for(i= 1;i <= n;i ++)
    {
        switch(xz)
        {
            case 1:if(strcmp(st[i].number,number) == 0)
            {
                printf("学号        姓名    成绩1 成绩2 成绩3 成绩4  总分 名次\n");
                printf("%s,%11s,%7d,%5d,%5d,%5d,%7d,%3d\n",
                st[i].number,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],
                st[i].score[3],st[i].total,st[i].position);
                break;
            }
            case 2:if(strcmp(st[i].name,name) == 0)
            {
                printf("学号        姓名    成绩1 成绩2 成绩3 成绩4  总分 名次\n");
                printf("%s,%11s,%7d,%5d,%5d,%5d,%7d,%3d\n",
                st[i].number,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],
                st[i].score[3],st[i].total,st[i].position);
                break;
            }
        }
        if(i > n)
        printf("查找失败!请检查!\n");
    }
}

void xg(student st[],int n)     //修改信息函数
{
    char number[10];
    int i;
    printf("请输入要修改的学生学号: ");
    getchar();
    gets(number);
    
    for(i = 1;i <= n;i ++)
        if(strcmp(st[i].number,number) == 0)
        {
            printf("学号        姓名    成绩1 成绩2 成绩3 成绩4  总分 名次\n");
                printf("%s,%11s,%7d,%5d,%5d,%5d,%7d,%3d\n",
                st[i].number,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],
                st[i].score[3],st[i].total,st[i].position);
            printf("输入新的成绩: 成绩1, 成绩2, 成绩3, 成绩4\n");
            scanf("%d,%d,%d,%d",&st[i].score[0],&st[i].score[1],&st[i].score[2],&st[i].score[3]);
            st[i].total = st[i].score[0] + st[i].score[1] + st[i].score[2] + st[i].score[3];
            break;
        }
        if(i > n)
        printf("查找失败!请检查!\n");
}

void position(student st[],int n)   //排名次函数
{
    int i;
    for(i = 1;i <= n;i ++)
    st[i].position = i;
    for(i = 2;i <= n;i ++)
    if(st[i].total == st[i - 1].total)
    st[i].position = st[i - 1].position;
}


void sc(student st[],int n)     //信息输出函数
{
    int i;
    printf("学号        姓名    成绩1 成绩2 成绩3 成绩4  总分 名次\n");
    for(i = 1;i <= n;i ++)
    printf("%s,%11s,%7d,%5d,%5d,%5d,%7d,%3d\n",
            st[i].number,st[i].name,st[i].score[0],st[i].score[1],st[i].score[2],
            st[i].score[3],st[i].total,st[i].position);    
}

void px(student st[],int n)
{
    void bubbleSort(student r[],int n);
    void shellSort(student r[],int d[],int n,int t);
    void quickSort(student r[],int low,int high);
    
    int xz,i;
    int d[5],t;
    printf("*********排序方法选择*********\n");
    printf("==============================\n");
    printf("        1.双向冒泡排序   \n");
    printf("        2.希 尔 排 序    \n");
    printf("        3.快 速 排 序    \n");
    printf("==============================\n");
    printf("请选择1, 2, 3, 4 :");
    scanf("%d",&xz);
    switch(xz)
    {
        case 1:bubbleSort(st,n);break;
        
        case 2:printf("输入增量个数: \n");
        scanf("%d",&t);
        for(i = 0;i < t;i ++)
        scanf("%d",&d[i]);
        shellSort(st,d,n,t);break;
        
        case 3:quickSort(st,1,n);break;
    }
    position(st,n);
}

void bubbleSort(student r[],int n)   //双向冒泡排序算法
{
    int i,j;
    student t;
    int noswap;
    noswap = 1;
    i = 1;
    while(noswap)
    {
        noswap = 0;
        for(j = n - i + 1;j >= i + 1;j --)
        if(r[j].total > r[j - 1].total)
        {
        t = r[j];
        r[j] = r[j - 1];
        r[j - 1] = t;
        noswap = 1;
        }
        
        for(j = i + 1;j <= n - i + 1;j ++)
        if(r[j].total < r[j + 1].total)
        {
            t = r[j];
            r[j] = r[j + 1];
            r[j + 1] = t;
            noswap = 1;
        }
        i = i +1;
    }
}

void shellInsert(student r[],int n,int dk)
{//做一趟排序插入
    int i,j;
    for(i = dk + 1;i <= n;i ++)
    if(r[i].total > r[i - dk].total)
    {
        r[0] = r[i];
        j = i - dk;
        while(j > 0 && r[0].total > r[j].total)
        {
            r[j + dk] = r[j];
            j = j - dk;
        }
        r[j + dk] = r[0];
    }
}

void shellSort(student r[],int d[],int n,int t)//希尔排序
{
    int k;
    for(k = 0;k < t;k ++)
    shellInsert(r,n,d[k]);
}


int partition(student r[],int i,int j)  //快速排序的一次划分函数
{
    student x = r[i];
    while(i < j)
    {
        while(i < j && r[j].total <= x.total)
        j --;
        if(i < j)
        {
            r[i] = r[j];
            i ++;
        }
        
        while(i < j && r[i].total >= x.total)
        i ++;
        if(i < j)
        {
            r[j] = r[i];
            j --;
        }
    }
    r[i] = x;
    return i;
}


void quickSort(student r[],int low,int high)
{
    int p;
    if(low < high)
    {
        p = partition(r,low,high);
        quickSort(r,low,p - 1);
        quickSort(r,p + 1,high);
    }
}

void main()
{
    student st[41];
    int xz = 1;
    int n;
    printf("请输入学生数: ");
    scanf("%d",&n);
    while(xz)
    {
        printf("****学生成绩管理***\n");
        printf("===================\n");
        printf("   1.学生信息输入  \n");
        printf("   2.学生信息查询  \n");
        printf("   3.学生信息修改  \n");
        printf("   4.学生信息排序  \n");
        printf("   5.学生信息输出  \n");
        printf("   0.退出系统  \n");
        printf("===================\n");
        printf("请选择 1, 2, 3, 4, 5, 0 :");
        scanf("%d",&xz);
        switch(xz)
        {
            case 1:sr(st,n);break;
            case 2:cx(st,n);break;
            case 3:xg(st,n);break;
            case 4:px(st,n);break;
            case 5:sc(st,n);break;
        }
    }
}

课程设计 | 阅读 1002 次
文章评论,共0条
游客请输入验证码
浏览11078次