第14周

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

继续抄

#include  
#include  
#include  
#define   MAX_LEN  10                      /* 字符串最大长度 */
#define   STU_NUM 30                       /* 最多的学生人数 */
#define   COURSE_NUM 6                     /* 最多的考试科目数 */
typedef struct student
{
    long num;                       /* 每个学生的学号 */
    char name[MAX_LEN];             /* 每个学生的姓名 */
    float score[COURSE_NUM];        /* 每个学生COURSE_NUM门功课的成绩 */
    float sum;                      /* 每个学生的总成绩 */
    float aver;                     /* 每个学生的平均成绩 */
}STU;
int   Menu(void);
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);
int main()
{
    int  ch;
    int   n = 0, m = 0;  /* 学生人数为n,课程门数为m */
    STU   stu[STU_NUM];
    printf("Input student number(n<=30):\n", STU_NUM);
    scanf("%d", &n);
    while (1)
    {
        ch = Menu();      /* 显示菜单,并读取用户输入 */
        switch (ch)
        {
        case 1:
            printf("Input course number(m<=%d):\n", COURSE_NUM);
            scanf("%d", &m);
            printf("Input student's ID, name and score:\n");
            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 0:
            printf("End of program!");
            exit(0);
        default: printf("Input error!\n");
        }

    }

    return 0;

}



/*  函数功能:显示菜单并获得用户键盘输入的选项 */

int Menu(void)
{
    int ret;
    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");
    scanf("%d", &ret);
    return ret;
}



/* 函数功能:输入n个学生的m门课成绩 */

void ReadScore(STU stu[], int n, int m)

{
    int i, r;
    for (i = 0; i < n; i++)
    {
        scanf("%ld%s", &stu[i].num, stu[i].name);
        for (r = 0; r < m; r++)
        {
            scanf("%f", &stu[i].score[r]);
        }
    }
    return;
}



/* 函数功能:计算每个学生各门课程的总分和平均分 */

void AverSumofEveryStudent(STU stu[], int n, int m)

{
    int i, r;
    for (i = 0; i < n; i++)
    {
        stu[i].sum = 0;
        for (r = 0; r < m; r++)
        {
            stu[i].sum = stu[i].sum + stu[i].score[r];
        }
        stu[i].aver = stu[i].sum / m;
        printf("student %d: sum=%.0f,aver=%.0f\n", i + 1, stu[i].sum, stu[i].aver);
    }
    return;
}



/* 函数功能:计算每门课程的总分和平均分 */

void AverSumofEveryCourse(STU stu[], int n, int m)

{
    int i, r;
    float sum;
    for (i = 0; i < m; i++)
    {
        sum = 0;
        for (r = 0; r < n; r++)
        {
            sum = sum + stu[r].score[i];
        }
        printf("course %d:sum=%.0f,aver=%.0f\n", i + 1, sum, sum / n);
    }
    return;
}

/* 函数功能:按选择法将数组sum的元素值排序 */

void SortbyScore(STU stu[], 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)(stu[r].sum, stu[r + 1].sum))
            {
                SwapLong(&stu[r].num, &stu[r + 1].num);
                SwapChar(stu[r].name, stu[r + 1].name);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&stu[r].score[t], &stu[r + 1].score[t]);
                }
                SwapFloat(&stu[r].sum, &stu[r + 1].sum);
                SwapFloat(&stu[r].aver, &stu[r + 1].aver);
            }
        }
    }
    return;
}



/* 使数据按升序排序 */

int Ascending(float a, float b)

{
    if (a > b)
        return 1;
    return 0;
}



/* 使数据按降序排序 */

int Descending(float a, float b)

{
    if (a < b)
        return 1;
    return 0;
}



/* 交换两个单精度浮点型数据 */

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 term[MAX_LEN];
    strcpy(term, x);
    strcpy(x, y);
    strcpy(y, term);
    return;
}



/* 函数功能:按选择法将数组num的元素值按从低到高排序 */

void AsSortbyNum(STU stu[], int n, int m)

{
    int i, r, t;
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < n - 1; r++)
        {
            if (stu[r].num > stu[r + 1].num)
            {
                SwapLong(&stu[r].num, &stu[r + 1].num);
                SwapChar(stu[r].name, stu[r + 1].name);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&stu[r].score[t], &stu[r + 1].score[t]);
                }
                SwapFloat(&stu[r].sum, &stu[r + 1].sum);
                SwapFloat(&stu[r].aver, &stu[r + 1].aver);
            }
        }
    }
    return;
}



/* 函数功能:交换法实现字符串按字典顺序排序 */

void SortbyName(STU stu[], int n, int m)

{
    int i, r, t;
    for (i = 0; i < n; i++)
    {
        for (r = 0; r < n - 1; r++)
        {
            if (strcmp(stu[r].name, stu[r + 1].name) > 0)
            {
                SwapLong(&stu[r].num, &stu[r + 1].num);
                SwapChar(stu[r].name, stu[r + 1].name);
                for (t = 0; t < m; t++)
                {
                    SwapFloat(&stu[r].score[t], &stu[r + 1].score[t]);
                }
                SwapFloat(&stu[r].sum, &stu[r + 1].sum);
                SwapFloat(&stu[r].aver, &stu[r + 1].aver);
            }
        }
    }
    return;
}



/* 函数功能:按学号查找学生成绩并显示查找结果 */

void SearchbyNum(STU stu[], int n, int m)

{
    long term;
    int i, r;
    printf("Input the number you want to search:\n");
    scanf("%ld", &term);
    for (i = 0; i < n; i++)
    {
        if (term % 1000 == stu[i].num % 1000)
        {
            printf("%ld\t%s\t", stu[i].num, stu[i].name);
            for (r = 0; r < m; r++)
            {
                printf("%.0f\t", stu[i].score[r]);
            }
            printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
            return;
        }
    }
    printf("Not found!\n");
    return;
}



/* 函数功能:按姓名查找学生成绩并显示查找结果 */

void SearchbyName(STU stu[], int n, int m)

{
    int i, r;
    char ch[MAX_LEN];
    printf("Input the name you want to search:\n");
    scanf("%s", ch);
    for (i = 0; i < n; i++)
    {
        if (strcmp(ch, stu[i].name) == 0)
        {
            printf("%ld\t%s\t", stu[i].num, stu[i].name);
            for (r = 0; r < m; r++)
            {
                printf("%.0f\t", stu[i].score[r]);
            }
            printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
            return;
        }
    }
    printf("Not found!\n");
    return;
}

/* 函数功能:统计各分数段的学生人数及所占的百分比 */

void StatisticAnalysis(STU stu[], int n, int m)

{
    int i, r;
    int x[COURSE_NUM][COURSE_NUM];
    memset(x, 0, sizeof(int) * COURSE_NUM * COURSE_NUM);
    for (i = 0; i < m; i++)
    {
        for (r = 0; r < n; r++)
        {
            if (stu[r].score[i] < 60)
            {
                x[i][0]++;
            }
            else if (stu[r].score[i] < 70)
            {
                x[i][1]++;
            }
            else if (stu[r].score[i] < 80)
            {
                x[i][2]++;
            }
            else if (stu[r].score[i] < 90)
            {
                x[i][3]++;
            }
            else if (stu[r].score[i] < 100)
            {
                x[i][4]++;
            }
            else
            {
                x[i][5]++;
            }
        }
    }
    for (i = 0; i < m; i++)
    {
        printf("For course %d:\n", i + 1);
        printf("<60\t%d\t%.2f%%\n", x[i][0], (float)x[i][0] * 100 / n);
        printf("%d-%d\t%d\t%.2f%%\n", 60, 69, x[i][1], (float)x[i][1] * 100 / n);
        printf("%d-%d\t%d\t%.2f%%\n", 70, 79, x[i][2], (float)x[i][2] * 100 / n);
        printf("%d-%d\t%d\t%.2f%%\n", 80, 89, x[i][3], (float)x[i][3] * 100 / n);
        printf("%d-%d\t%d\t%.2f%%\n", 90, 99, x[i][4], (float)x[i][4] * 100 / n);
        printf("%d\t%d\t%.2f%%\n", 100, x[i][5], (float)x[i][5] * 100 / n);
    }
    return;
}



/* 函数功能: 打印学生成绩 */

void PrintScore(STU stu[], int n, int m)

{
    int i, r;
    for (i = 0; i < n; i++)
    {
        printf("%ld\t%s\t", stu[i].num, stu[i].name);
        for (r = 0; r < m; r++)
        {
            printf("%.0f\t", stu[i].score[r]);
        }
        printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
    }
    return;
}

2字符串中的字符排序

#include
#include

int main()
{
    char a[20];
    char temp;
    printf("Input a string:\n");
    gets(a);
    for (int i=0; i 0)
            {
                temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
            }
        }
    puts(a);
    return 0;
}

3纯数字字符串检验

#include
#include

int main()
{
    char a[20];
    char temp;
    printf("Please input a string:\n");
    gets(a);
    for (int i=0; i'9')
        {
            printf("The string is not digit string.\n");
            return 0;
        }
    printf("The string is digit string.\n");
    return 0;
}

4孪生素数

#include
#include
#include

int Isprime(int a);

int main()
{
    int c, d;
    int count = 0;
    printf("please input c,d(c>2):\n");
    scanf("%d,%d", &c, &d);
    for (int i=c; i<=d; i++)
        if (IsPrime(i) && IsPrime(i + 2))
        {
            printf("(%d,%d)\n", i, i + 2);
            count++;
        }
    printf("total=%d\n", count);
    return 0;
}

int IsPrime(int a)
{
    if (a < 2)
        return 0;
    for (int i = 2; i <= sqrt(a); i++)
        if (a % i == 0)
            return 0;
    return 1;
}

5求解不等式

#include
#include
#include



int main()
{
    double s=0;
    double n;
    int i;
    printf("Input n:\n");
    scanf("%lf", &n);
    for (i = 1; s < n; i++)
    {
        s = 0;
        for (int j = i; j <= 2 * i; j++)
            s += sqrt(j);
    }
    printf("Result:m>=%d\ns=%.2lf\n", i-1, s);

    return 0;
}

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