初学者:C语言简易的学生成绩管理系统

    在大一学习了C语言,老师让我利用所学知识写了一个简易的学生成绩管理系统。这个系统没有使用结构体或文件、链表等难度较高的编程框架,编写难度低,适合初学者练手,巩固基础知识。
    该学生成绩管理系统用数组储存学生的各种信息,调用不同的函数实现其功能,对于指针的应用也较为简易。这个系统也有一些不当之处,可自行简单修改。如有错误,请指正,非常感谢!
    在此我分享代码给大家,希望对于大家有关C语言系统的编写有所帮助。如有疑问,请在评论区提出。
#include 
#include 

#define N 100

void enter(int *q, int *g)
{
    int found = 0, a;
    do
    {
        found = 0;
        for (a = 0; a < N; a++)
        {
            printf("请输入任意学号: \n");
            scanf("%d", (q + a));
            printf("请输入任意密码: \n");
            scanf("%d", (g + a));
            if (*q == *(q + a) && *g == *(g + a))
            {
                found = 1;
                break;
            }
        }
        if (!found)
        {
            printf("学号或密码输入错误,请重新输入!\n");
        }
    } while (!found);
    printf("已进入系统,登陆成功!\n");
    printf("\t\t欢迎使用学生成绩管理系统!\n");
    printf("====================================================\n");
    printf("\t |1.添加学生信息                            |\n");
    printf("\t |2.查找学生信息                            |\n");
    printf("\t |3.显示所有成绩                            |\n");
    printf("\t |4.修改学生信息                            |\n");
    printf("\t |5.删除学生信息                            |\n");
    printf("\t |6.对学生成绩进行升序排序                  |\n");
    printf("\t |7.对学生成绩进行降序排序                  |\n");
    printf("\t |8.显示学生成绩的最高分                    |\n");
    printf("\t |9.显示学生成绩的最低分                    |\n");
    printf("\t |10.显示学生成绩的平均分                   |\n");
    printf("\t |11.插入学生信息                           |\n");
    printf("\t |12.统计学生的不及格率 (60及60以上及格)    |\n");
    printf("\t |13.退出系统                               |\n");
    printf("==|=================================================|\n");
}

void fun1(char student[][20], int id[], int M[], int E[], int C[], int Z[], int *n)
{
	char name[100];
	int ids,x;
	int scoreM, scoreE, scoreC;
	int i, j;
	int flag=0;
	
	
	printf("-------\n\t请输入学生姓名(仅支持两到三个汉字!): ");
	scanf("%s", name);
	// 验证学生学号是否已存在
	printf("\t请输入学生学号: ");
	scanf("%d", &ids);
	flag = 1;
	for (i = 0; i < *n; i++) 
	{
	    if (id[i] == ids) 
		{
	        flag = 0;
	        break;
	    }
	}
	while (flag==0) 
	{
	    printf("\t该学号已存在,请重新输入:");
	    scanf("%d", &ids);
	    flag = 1;
	    for (i = 0; i < *n; i++) 
		{
	        if (id[i] == ids) 
			{
	            flag = 0;
	            break;
	        }
	    }
	}
	// 输入学生成绩并验证合法性
	printf("\t请输入学生的成绩 (高数 英语 程序设计,用空格分隔):");
	scanf("%d %d %d", &scoreM, &scoreE, &scoreC);
	while (scoreM < 0 || scoreM > 100 || scoreE < 0 || scoreE > 100 || scoreC < 0 || scoreC > 100)
	{
	    printf("\t学生成绩输入错误,请重新输入 (高数 英语 程序设计,用空格分隔):");
	    scanf("%d %d %d", &scoreM, &scoreE, &scoreC);
	}
	// 添加学生信息
	strcpy(student[*n], name);
	id[*n] = ids;
	M[*n] = scoreM;
	E[*n] = scoreE;
	C[*n] = scoreC;
	Z[*n] = M[*n] + E[*n] + C[*n];
	printf("\t该学生的总成绩为%d\n", Z[*n]);
	(*n)++; 
	}

void fun2(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int x, found = 0, i;
    printf("输入要查找的学生学号:");
    scanf("%d", &x);
    printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
    for (i = 0; i < n; i++)
    {
        if (id[i] == x)
        {
            printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
            found = 1;
            break;
        }
    }
    if(!found)
    {
        printf("未找到该学生!\n");
    }
}

void fun3(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int i;
    printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
    for (i = 0; i < n; i++)
    {
        printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
        printf("===========================================================\n");
    }
}

void fun4(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int y, found = 0, i;
    printf("输入要修改的学生学号:");
    scanf("%d", &y);
    for (i = 0; i < n; i++)
    {
        if (id[i] == y)
        {
            printf("请输入该学生的新姓名: ");
            scanf("%s", student[i]);
            printf("请输入该学生的新学号: ");
            scanf("%d", &id[i]);
            printf("请输入该学生的新成绩 (高数 英语 程序设计,用空格分隔): ");
            scanf("%d%d%d", &M[i], &E[i], &C[i]);
            Z[i] = M[i] + E[i] + C[i];
            printf("该学生新的总成绩为%d\n", Z[i]);
            found = 1;
            break;
        }
    }
    if(!found)
    {
        printf("未找到该学生!\n");
    }
}

void fun5(char student[][20], int id[], int M[], int E[], int C[], int Z[], int *n)
{
    int z, found = 0, i;
    printf("输入要删除的学生学号:");
    scanf("%d", &z);
    for (i = 0; i < *n; i++)
    {
        if (id[i] == z)
        {
            (*n)--;
            while(i < *n)
            {
                strcpy(student[i], student[i+1]);
                id[i] = id[i+1];
                M[i] = M[i+1];
                E[i] = E[i+1];
                C[i] = C[i+1];
                Z[i] = Z[i+1];
                i++;
            }
            found = 1;
            printf("学生信息已删除!\n");
            break;
        }
    }
    if(!found)
    {
        printf("未找到该学生!\n");
    }
}

void fun6(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (Z[j] > Z[j + 1])
            {
                int tz,ti,tm,te,tc;
				tz = Z[j];   Z[j] = Z[j + 1];   Z[j + 1] = tz;

                ti = id[j];  id[j] = id[j + 1];   id[j + 1] = ti;

                tm = M[j];  M[j] = M[j + 1];    M[j + 1] = tm;

                te = E[j];  E[j] = E[j + 1];   E[j + 1] = te;

                tc = C[j];   C[j] = C[j + 1];    C[j + 1] = tc;

                char tmp_str[20];
                strcpy(tmp_str, student[j]);
                strcpy(student[j], student[j + 1]);
                strcpy(student[j + 1], tmp_str);
            }
        }
    }
    printf("已对学生成绩进行升序排列,可通过操作“3”查看。。\n");
}

void fun7(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int i, j;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (Z[j] < Z[j + 1])
            {
                int tz,ti,tm,te,tc;
				tz = Z[j];   Z[j] = Z[j + 1];   Z[j + 1] = tz;

                ti = id[j];  id[j] = id[j + 1];   id[j + 1] = ti;

                tm = M[j];  M[j] = M[j + 1];    M[j + 1] = tm;

                te = E[j];  E[j] = E[j + 1];   E[j + 1] = te;

                tc = C[j];   C[j] = C[j + 1];    C[j + 1] = tc;

                char tmp_str[20];
                strcpy(tmp_str, student[j]);
                strcpy(student[j], student[j + 1]);
                strcpy(student[j + 1], tmp_str);
            }
        }
    }
    printf("已对学生成绩进行降序排列,可通过操作“3”查看。\n");
}
void fun8(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int max = 0, i;
    for (i = 1; i < n; i++)
    {
        if (Z[i] > Z[max])
        {
            max = i;
        }
    }
    printf("学生成绩最高的是%s,他的学号为%d,总成绩为%d分。\n", student[max], id[max], Z[max]);
}

void fun9(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int min = 0, i;
    for (i = 1; i < n; i++)
    {
        if (Z[i] < Z[min])
        {
            min = i;
        }
    }
    printf("学生成绩最低的为%s,学号为%d,总成绩为%d分。\n", student[min], id[min], Z[min]);
}

void avg(char student[][20], int id[], int M[], int E[], int C[], int Z[], int n)
{
    int i;
    double sum = 0;
    for (i = 0; i < n; i++)
    {
        sum += Z[i];
    }
    double avg = sum / n;
    printf("学生成绩的平均分为%.2f分。\n", avg);
}

void fun11(char student[][20], int id[], int M[], int E[], int C[], int Z[], int *n)
{
    int insert_id, i, j, found = 0;
    printf("输入要插入后面的学生学号:");
    scanf("%d", &insert_id);
    for (i = 0; i < *n; i++)
    {
        if (id[i] == insert_id)
        {
            found = 1;
            break;
        }
    }
    if(!found)
    {
        printf("未找到该学生!\n");
        return;
    }
    (*n)++;
    for (j = *n - 1; j > i + 1; j--)
    {
        strcpy(student[j], student[j - 1]);
        id[j] = id[j - 1];
        M[j] = M[j - 1];
        E[j] = E[j - 1];
        C[j] = C[j - 1];
        Z[j] = Z[j - 1];
    }
    printf("请输入该学生的姓名: ");
    scanf("%s", student[i + 1]);
    printf("请输入该学生的学号: ");
    scanf("%d", &id[i + 1]);
    printf("请输入该学生的成绩 (高数 英语 程序设计,用空格分隔): ");
    scanf("%d%d%d", &M[i + 1], &E[i + 1], &C[i + 1]);
    Z[i + 1] = M[i + 1] + E[i + 1] + C[i + 1];
    printf("该学生的总成绩为%d\n", Z[i + 1]);
    printf("学生信息已插入!\n");
}

void fun12(char student[][20], int id[], int M[], int E[], int C[], int Z[], int *n)
{
    int i;
	int a=0,b=0,c=0,d=0;
    float rate_1,rate_2,rate_3,rate_4;
    for (i = 0; i < *n; i++)
    {
        if (Z[i] < 180)
        {
            a++;
            printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
            printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
            printf("--------------------------------------------------------\n");
        }
        if (E[i] < 60)
        {
            b++;
            printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
            printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
            printf("--------------------------------------------------------\n");
        }
        if (M[i] < 60)
        {
            c++;
            printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
            printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
            printf("--------------------------------------------------------\n");
        }
        if (C[i] < 60)
        {
            d++;
            printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
            printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
            printf("--------------------------------------------------------\n");
        }
    }
    rate_1 = (float)a / *n * 100;
    printf("\t不及格人数为%d,不及格率为%.2f%%\n", a, rate_1);
    printf("\t不及格学生如下:\n");
   for (i = 0; i < *n; i++)
    {
        if (Z[i] < 180)
        {
            a++;
            printf("姓名\t学号\t高数\t英语\t程序设计\t总成绩\n");
            printf("%s\t%d\t%d\t%d\t%d\t\t%d\n", student[i], id[i], M[i], E[i], C[i], Z[i]);
            printf("--------------------------------------------------------\n");
        }
	}
    printf("--------------------------------------------------------\n");
    printf("单科不及格如下:\n\n") ;
    rate_2 = (float)b / *n * 100;
    printf("\t英语不及格人数为%d,英语不及格率为%.2f%%\n", b, rate_2);
    printf("--------------------------------------------------------\n");
    rate_3 = (float)c / *n * 100;
    printf("\t高数不及格人数为%d,高数不及格率为%.2f%%\n", c, rate_3);
    printf("--------------------------------------------------------\n");
    rate_4 = (float)d / *n * 100;
    printf("\t程序设计不及格人数为%d,程序设计不及格率为%.2f%%\n", d, rate_4);
    printf("--------------------------------------------------------\n");
}

int main()
{
    int id[N], password[N], M[N], E[N], C[N], Z[N], n = 0;
    char student[N][20];                             // 存储学生姓名,长度不超过20
    int find_id, found = 0, i;

    enter(&id[0], &password[0]);

    int find;
    printf("请输入要操作的选项:\n");
    scanf("%d", &find);
    while (find != 13)
    {
        switch (find)
        {
        case 1:
            fun1(student, id, M, E, C, Z, &n);
            break;

        case 2:
            fun2(student, id, M, E, C, Z, n);
            break;

        case 3:
            fun3(student, id, M, E, C, Z, n);
            break;

        case 4:
            fun4(student, id, M, E, C, Z, n);
            break;

        case 5:
            fun5(student, id, M, E, C, Z, &n);
            break;

        case 6:
            fun6(student, id, M, E, C, Z, n);
            break;

        case 7:
            fun7(student, id, M, E, C, Z, n);
            break;

        case 8:
            fun8(student, id, M, E, C, Z, n);
            break;

        case 9:
            fun9(student, id, M, E, C, Z, n);
            break;

        case 10:
            avg(student, id, M, E, C, Z, n);
            break;
        
		case 11:
            fun11(student, id, M, E, C, Z, &n);
            break;

        case 12:
            fun12(student, id, M, E, C, Z, &n);
            break;
            
        default:
            printf("输入无效选项!请重新输入(1-13):\n");
            break;
        }
        printf("请输入要操作的选项:\n");
        scanf("%d", &find);
    }
    printf("---------------------------感谢您的使用---------------------------\n");
	printf("<已退出系统>\n");
    return 0;
}

你可能感兴趣的:(c语言,开发语言)