第13周

1学生成绩管理系统V4.0

抄的

#include 
#include 
#define   MAX_LEN  10                       /* 字符串最大长度 */
#define   STU_NUM 30                       /* 最多的学生人数 */
#define   COURSE_NUM 6                     /* 最多的考试科目数 */
int   Menu(void);
void  ReadScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], int n, int m);
void AverSumofEveryCourse(float score[][COURSE_NUM], int n, int m);
void AverSumofEveryStudent(float score[][COURSE_NUM], int n, int m, float  sum[STU_NUM], float aver[STU_NUM]);
int   Descending(float a, float b);
int   Ascending(float a, float b);
void  SwapFloat(float* x, float* y);
void  SwapLong(long* x, long* y);
void  SwapChar(char x[], char y[]);
void  SortbyScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m, int (*compare)(float a, float b));
void  AsSortbyNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m);
void  SortbyName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m);
void  SearchbyNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m);
void  SearchbyName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m);
void  StatisticAnalysis(float score[][COURSE_NUM], int n, int m);
void  PrintScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m);
int main()
{
    int n, p, m;
    long num[STU_NUM];
    char name[STU_NUM][MAX_LEN];
    float score[STU_NUM][COURSE_NUM], sum[STU_NUM], aver[STU_NUM];
    printf("Input student number(n<30):\n");
    scanf("%d", &n);
    Menu();
    while (scanf("%d", &p) != EOF && p != 0)
    {
        switch (p)
        {
        case 1:printf("Input course number(m<=%d):\n", n);
            scanf("%d", &m);
            printf("Input student's ID, name and score:\n");
            ReadScore(num, name, score, n, m);
            break;
        case 2:AverSumofEveryCourse(score, n, m);
            break;
        case 3:AverSumofEveryStudent(score, n, m, sum, aver);
            break;
        case 4:printf("Sort in descending order by score:\n");
            SortbyScore(num, name, score, sum, aver, n, m, Descending);
            break;
        case 5:printf("Sort in ascending order by score:\n");
            SortbyScore(num, name, score, sum, aver, n, m, Ascending);
            break;
        case 6:printf("Sort in ascending order by number:\n");
            AsSortbyNum(num, name, score, sum, aver, n, m);
            break;
        case 7:printf("Sort in dictionary order by name:\n");
            SortbyName(num, name, score, sum, aver, n, m);
            break;
        case 8:printf("Input the number you want to search:\n");
            SearchbyNum(num, name, score, sum, aver, n, m);
            break;
        case 9:printf("Input the name you want to search:\n");
            SearchbyName(num, name, score, sum, aver, n, m);
            break;
        case 10:StatisticAnalysis(score, n, m);
            break;
        case 11:PrintScore(num, name, score, sum, aver, n, m);
            break;
        default:printf("Input error!\n");
        }
        Menu();
    }
    printf("End of program!");
    return 0;
}
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");
    return 0;
}
void  ReadScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], int n, int m)
{
    int i, r;
    for (i = 0; i < n; i++)
    {
        scanf("%ld%s", &num[i], name[i]);
        for (r = 0; r < m; r++)
        {
            scanf("%f", &score[i][r]);
        }
    }
    return;
}
void AverSumofEveryCourse(float score[][COURSE_NUM], int n, int m)
{
    int i, r;
    float sum = 0;
    for (i = 0; i < m; i++)
    {
        sum = 0;
        for (r = 0; r < n; r++)
        {
            sum = sum + score[r][i];
        }
        printf("course %d:sum=%.0f,aver=%.0f\n", i + 1, sum, sum / (float)n);
    }
    return;
}
void AverSumofEveryStudent(float score[][COURSE_NUM], int n, int m, float  sum[STU_NUM], float aver[STU_NUM])
{
    int i, r;
    memset(sum, 0, sizeof(float) * STU_NUM);
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < m; r++)
        {
            sum[i] = sum[i] + score[i][r];
        }
        aver[i] = sum[i] / (float)m;
    }
    for (i = 0; i < n; i++)
    {
        printf("student %d:sum=%.0f,aver=%.0f\n", i + 1, sum[i], aver[i]);
    }
    return;
}
void  SwapFloat(float* x, float* y)
{
    float term;
    term = *x;
    *x = *y;
    *y = term;
    return;
}
void  SwapLong(long* x, long* y)
{
    long term;
    term = *x;
    *x = *y;
    *y = term;
    return;
}
void  SwapChar(char x[], char y[])
{
    char c[MAX_LEN];
    strcpy(c, x);
    strcpy(x, y);
    strcpy(y, c);
    return;
}
int   Descending(float a, float b)
{
    if (a < b)
        return 1;
    return 0;
}
int   Ascending(float a, float b)
{
    if (a > b)
        return 1;
    return 0;
}
void  SortbyScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m, int (*compare)(float a, float b))
{
    int i, r, t;
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < n - 1; r++)
        {
            if ((*compare)(sum[r], sum[r + 1]))
            {
                SwapLong(&num[r], &num[r + 1]);
                SwapChar(name[r], name[r + 1]);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&score[r][t], &score[r + 1][t]);
                }
                SwapFloat(&sum[r], &sum[r + 1]);
                SwapFloat(&aver[r], &aver[r + 1]);
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", num[i], name[i]);
        for (r = 0; r < m; r++)
        {
            printf("%.0f\t", score[i][r]);
        }
        printf("%.0f\t%.0f\n", sum[i], aver[i]);
    }
    return;
}
void  AsSortbyNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m)
{
    int i, r, t;
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < n - 1; r++)
        {
            if (num[r] > num[r + 1])
            {
                SwapLong(&num[r], &num[r + 1]);
                SwapChar(name[r], name[r + 1]);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&score[r][t], &score[r + 1][t]);
                }
                SwapFloat(&sum[r], &sum[r + 1]);
                SwapFloat(&aver[r], &aver[r + 1]);
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", num[i], name[i]);
        for (r = 0; r < m; r++)
        {
            printf("%.0f\t", score[i][r]);
        }
        printf("%.0f\t%.0f\n", sum[i], aver[i]);
    }
    return;
}
void  SortbyName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m)
{
    int i, r, t;
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < n - 1; r++)
        {
            if (strcmp(name[r], name[r + 1]) > 0)
            {
                SwapLong(&num[r], &num[r + 1]);
                SwapChar(name[r], name[r + 1]);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&score[r][t], &score[r + 1][t]);
                }
                SwapFloat(&sum[r], &sum[r + 1]);
                SwapFloat(&aver[r], &aver[r + 1]);
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", num[i], name[i]);
        for (r = 0; r < m; r++)
        {
            printf("%.0f\t", score[i][r]);
        }
        printf("%.0f\t%.0f\n", sum[i], aver[i]);
    }
    return;
}
void  SearchbyNum(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m)
{
    int i, r;
    long number;
    scanf("%ld", &number);
    for (i = 0; i < n; i++)
    {
        if (number % 1000 == num[i] % 1000)
        {
            printf("%ld\t%s\t", num[i], name[i]);
            for (r = 0; r < m; r++)
            {
                printf("%.0f\t", score[i][r]);
            }
            printf("%.0f\t%.0f\n", sum[i], aver[i]);
            return;
        }
    }
    printf("Not found!\n");
    return;
}
void  SearchbyName(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m)
{
    int i, r;
    char ch[MAX_LEN];
    scanf("%s", ch);
    for (i = 0; i < n; i++)
    {
        if (strcmp(ch, name[i]) == 0)
        {
            printf("%ld\t%s\t", num[i], name[i]);
            for (r = 0; r < m; r++)
            {
                printf("%.0f\t", score[i][r]);
            }
            printf("%.0f\t%.0f\n", sum[i], aver[i]);
            return;
        }
    }
    printf("Not found!\n");
    return;
}
void  StatisticAnalysis(float score[][COURSE_NUM], int n, int m)
{
    int i, r;
    int x[COURSE_NUM][6];
    memset(x, 0, sizeof(x));
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < m; r++)
        {
            if (score[i][r] < 60)
            {
                x[r][0]++;
            }
            else if (score[i][r] < 70)
            {
                x[r][1]++;
            }
            else if (score[i][r] < 80)
            {
                x[r][2]++;
            }
            else if (score[i][r] < 90)
            {
                x[r][3]++;
            }
            else if (score[i][r] < 100)
            {
                x[r][4]++;
            }
            else
            {
                x[r][5]++;
            }
        }
    }
    for (r = 0; r < m; r++)
    {
        printf("For course %d:\n", r + 1);
        printf("<60\t%d\t%.2f%%\n", x[r][0], (float)x[r][0] * 100 / (float)n);
        printf("%d-%d\t%d\t%.2f%%\n", 60, 69, x[r][1], (float)x[r][1] * 100 / (float)n);
        printf("%d-%d\t%d\t%.2f%%\n", 70, 79, x[r][2], (float)x[r][2] * 100 / (float)n);
        printf("%d-%d\t%d\t%.2f%%\n", 80, 89, x[r][3], (float)x[r][3] * 100 / (float)n);
        printf("%d-%d\t%d\t%.2f%%\n", 90, 99, x[r][4], (float)x[r][4] * 100 / (float)n);
        printf("%d\t%d\t%.2f%%\n", 100, x[r][5], (float)x[r][5] * 100 / (float)n);
    }
    return;
}
void  PrintScore(long num[], char name[][MAX_LEN], float score[][COURSE_NUM], float  sum[], float aver[], int n, int m)
{
    int i, r, t;
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < n - 1; r++)
        {
            if (strcmp(name[r], name[r + 1]) > 0)
            {
                SwapLong(&num[r], &num[r + 1]);
                SwapChar(name[r], name[r + 1]);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&score[r][t], &score[r + 1][t]);
                }
                SwapFloat(&sum[r], &sum[r + 1]);
                SwapFloat(&aver[r], &aver[r + 1]);
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", num[i], name[i]);
        for (r = 0; r < m; r++)
        {
            printf("%.0f\t", score[i][r]);
        }
        printf("%.0f\t%.0f\n", sum[i], aver[i]);
    }
    return;
}

2寻找最高分成绩的学生

#include  
#include  
void InputScore(int* p, int m, int n);
int  FindMax(int* p, int m, int n, int* pRow, int* pCol);
int main()
{
    int* pScore, m, n, maxScore, row, col;
    printf("Input array size m,n:\n");
    scanf("%d,%d", &m, &n);
    pScore = (int *)malloc(m*n*sizeof(int)); /* 申请动态内存 */

    if (pScore == NULL)
    {
        printf("No enough memory!\n");
        exit(0);
    }
    InputScore(pScore, m, n);
    maxScore = FindMax(pScore, m, n, &row, &col);

    printf("maxScore = %d, class = %d, number = %d\n", maxScore, row + 1, col + 1);
    free(pScore);                                      /* 释放动态内存 */
    return 0;
}

/* 函数功能:输入m行n列二维数组的值 */
void InputScore(int *pScore, int m, int n)
{
    int i, j;
    printf("Input %d*%d array:\n", m, n);
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &pScore[i * n + j]);
        }
    }
}
/*  函数功能:计算任意m行n列二维数组中元素的最大值,并指出其所在行列下标值 */
int  FindMax(int* p, int m, int n, int* pRow, int* pCol)
{
    int  i, j, max = p[0];
    *pRow = 0;

    *pCol = 0;

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (p[i * n + j] > max)
            {
                max = p[i * n + j];
                *pRow = i;       /*记录行下标*/
                *pCol = j;             /*记录列下标*/
            }
        }
    }
    return max;
}

3程序改错

#include  
#define STUD   30      /* 最多可能的学生人数 */
#define COURSE 5       /* 最多可能的考试科目数 */
void  Total(int* pScore, int sum[], float aver[], int m, int n);
void  Print(int* pScore, int sum[], float aver[], int m, int n);
int main()
{
    int     i, j, m, n, score[STUD][COURSE], sum[STUD];
    float   aver[STUD];
    printf("How many students?\n");
    scanf("%d", &m);
    printf("How many courses?\n");
    scanf("%d", &n);
    printf("Input scores:\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &score[i][j]);
        }
    }

    Total(score, sum, aver, m, n);
    Print(score, sum, aver, m, n);
    return 0;
}
void  Total(int* pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    for (i = 0; i < m; i++)
    {
        sum[i] = 0;
        for (j = 0; j < n; j++)
        {
            sum[i] +=  pScore[i * COURSE + j];
        }
        aver[i] = (float)sum[i] / n;
    }
}
void  Print(int* pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    printf("Result:\n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%4d", pScore[i * COURSE + j]);
        }
        printf("%5d%6.1f\n", sum[i], aver[i]);
    }
}

4矩阵转置

#include 
#define M 10
#define N 10
void Transpose(int* a, int* at, int m, int n);
void InputMatrix(int* a, int m, int n);
void PrintMatrix(int* at, int n, int m);
int main()
{
    int s[M][N], st[N][M], m, n;
    printf("Input m, n:\n");
    scanf("%d,%d", &m, &n);
    InputMatrix(s, m, n);
    Transpose(s, st, m, n);
    printf("The transposed matrix is:\n");
    PrintMatrix(*st, n, m);
    return 0;
}
/* 函数功能:计算m*n矩阵a的转置矩阵at */
void Transpose(int* a, int* at, int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            *(at+j*M+i) = *(a+i*N+j);
        }
    }
}
/* 函数功能:输入m*n矩阵a的值 */
void InputMatrix(int* a, int m, int n)
{
    int i, j;
    printf("Input %d*%d matrix:\n", m, n);
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", a+i*N+j);
        }
    }
}
/* 函数功能:输出n*m矩阵at的值 */
void PrintMatrix(int* at, int n, int m)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            printf("%-5d", *(at+i*M+j));
        }
        printf("\n");
    }
}

5在升序排序的数组中插入一个元素

#include 
#include 
#include 

int main()
{
    int n;
    int insert;
    int* p;
    printf("Input array size:\n");
    scanf("%d", &n);
    p = (int*)malloc((n+1) * sizeof(int));
    if (p != NULL)
    {
        printf("Input array:\n");
        for (int i = 0; i < n; i++)
            scanf("%d", p + i);
    }
    else
        return 0;
    printf("Input x:\n");
    scanf("%d", &insert);
    for (int i = n-1; i >= 0; i--)
    {
        if (*(p + i) > insert)
            *(p + i + 1) = *(p + i);
        else
        {
            *(p + i + 1) = insert;
            break;
        }   
    }
    printf("After insert %d:\n", insert);
    for (int i = 0; i < n + 1; i++)
        printf("%4d", *(p+i));
    free(p);
    return 0;
}

6计算平均数、中位数和众数

#include

int main()
{
    int datas[40];
    int temp, sum=0;
    int maxmode = 1;
    int count=0;
    int mode_i;
    printf("Input the feedbacks of 40 students:\n");
    for (int i = 0; i < 40; i++)
    {
        scanf("%d", datas + i);
    }
    for (int i = 0; i < 39; i++)
    {
        for (int j = 0; j < 39 - i; j++)
        {
            if (datas[j] > datas[j + 1])
            {
                temp = datas[j]; datas[j] = datas[j + 1]; datas[j + 1] = temp;
            }
        }
    }
    sum = datas[0];
    for (int i = 1; i < 40; i++)
    {
        sum += datas[i];
        if (datas[i] == datas[i - 1])
        {
            count++;
            if (count > maxmode)
            {
                mode_i = i;
                maxmode = count;
            }
        }
        else
            count = 0;
    }
    printf("Mean value=%d\n", sum / 40);
    printf("Median value=%d\n", (datas[19] + datas[20]) / 2);
    printf("Mode value=%d\n", datas[mode_i]);

    return 0;
}

你可能感兴趣的:(第13周)