Report card of headache homework

(who writes the report card in C? [doge])

Experiment content:

There are no more than 30 people in a class (the specific number of people is determined by the actual input) to take the final exam, and no more than 6 (the specific number of courses is determined by the actual input). Student achievement management system is a very practical program. It will be more useful if you can learn the character file reading and writing operation in advance, save the data entered by the user as a character file, and read it out the next time. That is, the following menu driven student achievement management system is programmed:

(1) Enter the student number, name and examination results of each subject;

(2) Calculate the total score and average score of each course;

(3) Calculate the total score and average score of each student;

(4) According to the total score of each student, the ranking table is arranged from high to low;

(5) According to the total score of each student, the ranking table is arranged from low to high;

(6) Discharge the score table from small to large according to the student number;

(7) Arrange the score sheet according to the dictionary order of names;

(8) Query student ranking and test scores by student number;

(9) Query student rankings and test scores by name;

(10) According to the five categories of excellent (90 ~ 100), good (80 ~ 89), medium (70 ~ 79), pass (60 ~ 69) and fail (0 ~ 59), count the number and percentage of each category for each course;

(11) Output each student's student number, name, examination results of each subject, as well as the total score and average score of each course;

(12) Write the recorded information of each student into the file (optional Implementation);

(13) Read out the record information of each student from the file and display it (optional Implementation).

After entering the number of courses and all course names, the program is required to display the following menu and prompt the user to enter options:

1.Input record

2.Calculate total and average score of every course

3.Calculate total and average score of every student

4.Sort in descending order by total score of every student

5.Sort in ascending order by total score of every student

6.Sort in ascending order by number

7.Sort in dictionary order by name

8.Search by number

9.Search by name

10.Statistic analysis for every course

11.List record

12.Write to a file

13.Read from a file

0.Exit

Please enter your choice:

Then, perform corresponding operations according to the options entered by the user.

Note: 12 13. Two are not required to be achieved.  

#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
 
#define   MAX_ Len 10 / * maximum string length*/
#define   STU_NUM 30 / * maximum number of students*/
#define   COURSE_NUM 6 / * maximum number of test subjects*/
 
typedef struct student
{
    long num;            /* Student number of each student */
    char name[MAX_LEN];                /* Name of each student */
    float score[COURSE_NUM];    /* Course per student_ Num course grades */
    float sum;                    /* Total score of each student */
    float aver;                /* Average grade per student */
 
} STU;
 
int Menu(void);

void ReadCourse(char course[COURSE_NUM][MAX_LEN], int m);
 
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);

void Writeto(STU stu[], int n, int m);

void Readfrom(STU stu[], int n, int m);
 
int main()
{
    char ch;
    int n = 0, m = 0;  /* The number of students is n and the number of courses is m */
    char course[COURSE_NUM][MAX_LEN];
    STU stu[STU_NUM];
    printf("Input student number(n<=30):\n", STU_NUM);
    scanf("%d", &n);
    printf("Input course number(m<=%d):\n", COURSE_NUM);
    scanf("%d", &m);
    ReadCourse(course, m);
    while (1)
    {
        ch = Menu();       /* Displays the menu and reads user input */
        switch (ch)
        {
            case 1:    
                ReadScore(stu, n, m);
                break;
            case 2:
                AverSumofEveryCourse(stu, n, m);
                break;
            case 3:
                AverSumofEveryStudent(stu, n, m);
                break;
            case 4:
                SortbyScore(stu, n, m, Descending);
                printf("Sort in descending order by score:\n");
                PrintScore(stu, n, m);
                break;
            case 5:
                SortbyScore(stu, n, m, Ascending);
                printf("Sort in ascending order by score:\n");
                PrintScore(stu, n, m);
                break;
            case 6:
                AsSortbyNum(stu, n, m);
                printf("Sort in ascending order by number:\n");
                PrintScore(stu, n, m);
                break;
            case 7:
                SortbyName(stu, n, m);
                printf("Sort in dictionary order by name:\n");
                PrintScore(stu, n, m);
                break;
 
            case 8:
                SearchbyNum(stu, n, m);
                break;
            case 9:
                SearchbyName(stu, n, m);
                break;
            case 10:
                StatisticAnalysis(stu, n, m);
                break;
            case 11:
                PrintScore(stu, n, m);
                break;
            case 12:
            	printf("NOT YET OPEN! PLEASE UNDERSTAND!\n");
            	break;
            case 13:
            	printf("NOT YET OPEN! PLEASE UNDERSTAND!\n");
            	break;
            case 0:
                printf("End of program!");
                exit(0);
            default:
                printf("Input error!\n");
        }
    }
    return 0;
}
 
 
/*  Function function: display the menu and get the options entered by the user's keyboard */
 
int Menu(void)
{
    int ch;
    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"
           "12.Write to a file\n"
           "13.Read from a file\n"
           "0.Exit\n"
           "Please Input your choice:\n");
    scanf("%d", &ch);
    return ch;
}
 
//Function function: enter the name of m courses 

void ReadCourse(char course[COURSE_NUM][10], int m)
{
	int i;
	for (i = 0; i < m; i++)
	{
	printf("Please input No.%d course:\n", i + 1);
	scanf("%s", &course[i]);
	}
}

// Function function: enter the scores of m courses of n students 
 
void ReadScore(STU stu[], int n, int m)
{
    printf("Input student's number, name and score:\n");
    for (int i = 0; i < n; ++i)
    {
        scanf("%ld%s", &stu[i].num, stu[i].name);
        for (int j = 0; j < m; ++j)
        {
            scanf("%f", &stu[i].score[j]);
        }
    }
}
 
 
// Function function: calculate the total score and average score of each student's course 
 
void AverSumofEveryStudent(STU stu[], int n, int m)
{
    float sum2;
    for (int i = 0; i < n; ++i)
    {
        sum2 = 0;
        for (int j = 0; j < m; ++j)
        {
            sum2 += stu[i].score[j];
        }
        stu[i].aver = sum2 / m;
        stu[i].sum = sum2;
        printf("student %d: sum=%.0f,aver=%.0f\n", i + 1, stu[i].sum, stu[i].aver);
    }
}
 
 
// Function function: calculate the total score and average score of each course 
 
void AverSumofEveryCourse(STU stu[], int n, int m)
{
    float sum;
    for (int i = 0; i < m; ++i)
    {
        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);
    }
}
 
// Function function: sort the element values of the array sum according to the selection method 
 
void SortbyScore(STU stu[], int n, int m, int (*compare)(float a, float b))
{
    int flag;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if ((*compare)(stu[j].sum, stu[j + 1].sum))
            {
                SwapFloat(&stu[j].sum, &stu[j + 1].sum);
                //Course score exchange
                for (int k = 0; k < m; ++k)
                {
                    SwapFloat(&stu[j].score[k], &stu[j + 1].score[k]);
                }
                SwapFloat(&stu[j].aver, &stu[j + 1].aver);
                SwapLong(&stu[j].num, &stu[j + 1].num);
                SwapChar(stu[j].name, stu[j + 1].name);
                flag = 1;
            }
        }
        if (!flag)
        {
            break;
        }
    }
 
}
 
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 temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
void SwapLong(long *x, long *y)
{
    long temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
void SwapChar(char x[], char y[])
{
    char temp[MAX_LEN];
    strcpy(temp, x);
    strcpy(x, y);
    strcpy(y, temp);
}
 
 
// Function function: sort the element values of array num from low to high according to the selection method 
 
void AsSortbyNum(STU stu[], int n, int m)
{
    int flag;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if (stu[j].num > stu[j + 1].num)
            {
                SwapFloat(&stu[j].sum, &stu[j + 1].sum);
                //Course score exchange
                for (int k = 0; k < m; ++k)
                {
                    SwapFloat(&stu[j].score[k], &stu[j + 1].score[k]);
                }
                SwapFloat(&stu[j].aver, &stu[j + 1].aver);
                SwapLong(&stu[j].num, &stu[j + 1].num);
                SwapChar(stu[j].name, stu[j + 1].name);
                flag = 1;
            }
        }
        if (!flag)
        {
            break;
        }
    }
}
 
 
// Function function: exchange method to sort strings in dictionary order 
 
void SortbyName(STU stu[], int n, int m)
{
    int flag;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if (strcmp(stu[j].name, stu[j + 1].name) > 0)
            {
                SwapFloat(&stu[j].sum, &stu[j + 1].sum);
                //Course score exchange
                for (int k = 0; k < m; ++k)
                {
                    SwapFloat(&stu[j].score[k], &stu[j + 1].score[k]);
                }
                SwapFloat(&stu[j].aver, &stu[j + 1].aver);
                SwapLong(&stu[j].num, &stu[j + 1].num);
                SwapChar(stu[j].name, stu[j + 1].name);
                flag = 1;
            }
        }
        if (!flag)
        {
            break;
        }
    }
}
 
 
// Function function: find student grades by student number and display the search results 
 
void SearchbyNum(STU stu[], int n, int m)
{
    int number, flag = 0, i;
    printf("Input the number you want to search:\n");
    scanf("%d", &number);
    for (i = 0; i < n; ++i)
    {
        if (stu[i].num == number)
        {
            flag = 1;
            break;
        }
    }
    if (flag)
    {
        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);
    } else
    {
        printf("Not found!\n");
    }
}
 
 
// Function function: arrange the score table according to the dictionary order of names 
 
void SearchbyName(STU stu[], int n, int m)
{
    char name2[MAX_LEN];
    int flag = 0, i;
    printf("Input the name you want to search:\n");
    scanf("%s", name2);
    for (i = 0; i < n; ++i)
    {
        if (strcmp(name2, stu[i].name) == 0)
        {
            flag = 1;
            break;
        }
    }
    if (flag)
    {
        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);
    } else
    {
        printf("Not found!\n");
    }
}
 
//Function function: count the number and percentage of students in each score segment 
 
void StatisticAnalysis(STU stu[], int n, int m)
{
    int a, b, c, d, e, f;
    float score;
    for (int i = 0; i < m; ++i)
    {
        a = b = c = d = e = f = 0;
        for (int j = 0; j < n; ++j)
        {
            score = stu[j].score[i];
            if (score == 100)
            {
                a++;
            } 
			else if (score >= 90 && score < 100)
            {
                b++;
            } 
			else if (score >= 80)
            {
                c++;
            } 
			else if (score >= 70)
            {
                d++;
            } 
			else if (score >= 60)
            {
                e++;
            } 
			else
            {
                f++;
            }
        }
        printf("For course %d:\n", i + 1);
        printf("<60\t%d\t%.2f%%\n", f, (float) (100 * f) / n);
        printf("%d-%d\t%d\t%.2f%%\n", 60, 69, e, (float) (100 * e) / n);
        printf("%d-%d\t%d\t%.2f%%\n", 70, 79, d, (float) (100 * d) / n);
        printf("%d-%d\t%d\t%.2f%%\n", 80, 89, c, (float) (100 * c) / n);
        printf("%d-%d\t%d\t%.2f%%\n", 90, 99, b, (float) (100 * b) / n);
        printf("%d\t%d\t%.2f%%\n", 100, a, (float) (100 * a) / n);
    }
}
 
 
// Function: print student grades 
 
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);
    }
}


There are still imperfections. Please criticize and correct them. Thank you very much:)

Keywords: C++

Added by mushroom on Sun, 12 Dec 2021 12:29:40 +0200