基于C语言的学生成绩处理

来源:互联网 发布:设计牌匾的软件 编辑:程序博客网 时间:2024/04/26 05:02

学生成绩处理

#include<stdio.h> #include<string.h>/*定义结构头类型*/typedef struct {    char sno[10];    char name[16];    int score[4];    int total;    int mc;}student;/*信息录入程序*/void sr(student stud[], int n){    int i;    for (i = 1; i <= n; i++)    {        printf("输入学号 姓名(中间留一个空格):\n");        scanf("%s %s", stud[i].sno, stud[i].name);        printf("成绩1,成绩2,成绩3,成绩4\n");        scanf("%d,%d,%d,%d", &stud[i].score[0], &stud[i].score[1], &stud[i].score[2], &stud[i].score[3]);        stud[i].total = stud[i].score[0] + stud[i].score[1] + stud[i].score[2] + stud[i].score[3];        stud[i].mc = 1;    }}/*信息查询程序*/void cx(student stud[], int n){    int xz, i;    char no[10], nm[16];    printf("    1.按学号查询 2.按姓名查询\n");    printf("    请输入 1 或 2 并按回车键:");    scanf("%d", &xz);    getchar();    if (xz == 1) {        printf("请输入学号:");        gets(no);    }    else        if (xz == 2) {            printf("请输入姓名:");            gets(nm);        }    for (i = 1; i <= n; i++)    {        switch(xz){            case 1:                if (strcmp(stud[i].sno, no) == 0) {                    printf("学号      姓名      成绩一 成绩二 成绩三 成绩四 总分  名次\n");                    printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",                        stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],                        stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);                    return;                }            case 2:                if (strcmp(stud[i].name, nm) == 0) {                    printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",                        stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],                        stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);                    return;                }        }    }    if (i>n) printf("你要查找的学生没查到!\n");}/*信息修改程序*/void xg(student stud[], int n){    char no[10];    int i;    printf("请输入要修改成绩的学生学号:");    getchar();    gets(no);    for (i = 1; i <= n; i++) {        if (strcmp(stud[i].sno, no) == 0) {            printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",                stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],                stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);            printf("输入最新的成绩:成绩一,成绩二,成绩三,成绩四\n");            scanf("%d,%d,%d,%d",&stud[i].score[0], &stud[i].score[1], &stud[i].score[2], &stud[i].score[3]);            stud[i].total = stud[i].score[0] + stud[i].score[1] + stud[i].score[2] + stud[i].score[3];            break;        }    }    if (i>n) printf("你要查找的学生没查到!\n");}/*排名次程序*/void qdmc(student stud[], int n){    int i;    for (i = 1; i <= n; i++)        stud[i].mc = i;//按排列顺序给出每个学生的名次     for (i = 2; i <= n; i++)        if (stud[i].total == stud[i - 1].total)            stud[i].mc = stud[i - 1].mc;//修改当前学生的名次与前一学生并列 }/*信息输出程序*/void sc(student stud[], int n){    int i;    printf("学号  姓名  成绩一 成绩二 成绩三 成绩四 总分  名次\n");    for (i = 1; i<-n; i++)        printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",            stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],            stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);}/*排序选则程序*/void px(student stud[], int n){    int xz, i;    int d[5], t;    void DbubbleSort(student stud[], int n), ShellSort(student stud[], int d[], int n, int t),        QuickSort(student stud[], int, int), HeapSort(student            stud[], int), qdmc(student stud[], int);    //排序函数说明,在后面订义    printf("***排序方法选择***\n");    printf("==================\n");    printf("  1.双向冒泡排序  \n");    printf("  2.希 尔 排 序   \n");    printf("  3.块 速 排 序   \n");    printf("  4.堆   排   序  \n");    printf("==================\n");    printf("  请选择:1,2,3,4:\n");    scanf("%d", &xz);    switch (xz) {    case 1:        DbubbleSort(stud, n);        break;    case 2:        printf("输入增量的个数:");        scanf("%d", &t);        printf("输入增量");        for (i = 0; i<t; i++)            scanf("%d", &d[i]);        ShellSort(stud,d, n, t);        break;    case 3:        QuickSort(stud,1,n);        break;    case 4:        HeapSort(stud, n);        break;    }    qdmc(stud, n);}/*双向冒泡排序算法*/void DbubbleSort(student R[], int n){    //R[1...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;            }        i = i + 1;    }}/*希尔排序算法(降序)*/void ShellInsert(student R[], int n, int dk){    //希尔排序中的一趟插入排序,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){    //按增量序列d[0...t-1]对顺序表R做希尔排序    int k;    for (k = 0; k<t; k++)        ShellInsert(R,n, d[k]);}/*快速排序的一次划分双发*/int Partition(student R[], int i, int j){    //对R[i] ...R[j]区间内的记录进行一次划分排序    student x = R[i];    while (i<j) {        while (i<j && R[j].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 Sift(student R[], int i, int h){    //将R[i...h]调整为最小根堆,除r[i]外,其余结点均满足堆性质。    int j;    student x = R[i];    j = 2 * i;    while (j <= h) {        if (j<h && R[j].total>R[j + 1].total)            j++;        if (x.total<R[j + 1].total) break;        R[i] = R[j];        i = j;        j = 2 * i;    }    R[i] = x;}/*堆排序算法*/void HeapSort(student R[], int n){    //对R[1...n]进行堆排序,设R[0]为暂存单元    int i;    for (i = n / 2; i>0; i--)        Sift(R, i, n);    for (i = n; i>1; i--)    {        R[0] = R[1];        R[1] = R[i];        R[i] = R[i] = R[0];        Sift(R, 1, i - 1);    }}/*主程序*/int main(void){    student stud[41];    int xz = 1, 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(stud, n);            break;        case 2:            cx(stud, n);            break;        case 3:            xg(stud, n);            break;        case 4:            px(stud, n);            break;        case 5:            sc(stud, n);        }    }}
原创粉丝点击