C语言 学生成绩管理系统

前言

这几天我们上了一个叫做计算机实习的小学期课程,我被分到了写学生成绩管理系统,感觉比之前刚刚开始学的时候写扫雷要好上不少,也大概设计了一下ui。感觉数组位置变换可以使用指针,但是我那里学的不怎么好,以后研究一下。

程序要求

设计一个学生成绩管理系统,学生基本信息包括:学号、姓名、数学、英语、平均成绩。
主要功能:
①以菜单界面形式实现,内容包括输入成绩、、查询成绩、修改成绩、删除成绩、成绩排序、保存记录、显示所有成绩、退出系统等;
②能对学生的成绩进行输入、输出、查询、修改、删除、排序、统计等 。

程序运行

程序源码

数据库data.txt文件格式

1080201 aas 11 32
1080202 b3e 66 77
1080103 cqw 56 78
1080206 fui 99 88
1080205 ed8 68 95
1080666 xxc 90 99
1080667 1d9 90 99

程序源码

#define _CRT_SECURE_NO_WARNINGS  1
#include <stdio.h>
#include <stdlib.h>
#define num_max 100//可输入的最大学生数量

void input_score();
void query_grade();
void aver(int num);
void change();
void delete_score();
void sort_score();
void read_score();
void write_score();
void statistics();

struct stu_information {
	char name[4];
	long int id;
	int math, english;
	float aver_score;
};

struct stu_information stu[num_max];
int total = 0;//当前已经录入学生数

int main() {
	int a;//选择
	read_score();//读取数据库
	while (1) {
		printf("请选择:\n");
		printf("1.输入成绩\n");
		printf("2.查询成绩\n");
		printf("3.修改成绩\n");
		printf("4.删除成绩\n");
		printf("5.成绩排序\n");
		printf("6.成绩统计\n");
		printf("7.退出程序(不保存更改记录)\n");
		printf("8.退出程序并保存更改记录\n");
		scanf("%d", &a);
		switch (a) {
		case 1:
			input_score();
			break;
		case 2:
			query_grade();
			break;
		case 3:
			change();
			break;
		case 4:
			delete_score();
			break;
		case 5:
			sort_score();
			break;
		case 6:
			statistics();
			break;
		case 7:
			goto end;
		case 8:
			write_score();
			goto end;
		}
	}
end:
	return 0;
}

//读入数据
void read_score() {
	FILE* data_score;
	// 打开文件
	data_score = fopen("data.txt", "r");
	// 检查文件是否存在
	if (data_score == NULL) {
		printf("无法数据文件 \n");
	}
	// 读取文件内容
	int a, c, d;
	char b[4];
	while (fscanf(data_score, "%ld %s %d %d\n", &a, &b, &c, &d) == 4)
	{
		stu[total].id = a;
		stu[total].name[0] = b[0];
		stu[total].name[1] = b[1];
		stu[total].name[2] = b[2];
		stu[total].name[3] = b[3];
		stu[total].math = c;
		stu[total].english = d;
		aver(total);
		total += 1;
		//printf("%ld %s %d %d\n", a, b, c, d);//测试用
	}
	//关闭文件
	fclose(data_score);
}
//写入数据
void write_score() {
	FILE* data_score;
	// 打开文件
	data_score = fopen("data.txt", "w");
	// 检查文件是否存在
	if (data_score == NULL) {
		printf("无法数据文件 \n");
	}
	for (int i = 0; i < total; i++) {
		fprintf(data_score, "%ld %c %d %d\n", stu[i].id, stu[i].name, stu[i].math, stu[i].english);
	}
	//关闭文件
	fclose(data_score);
}
//输入成绩
void input_score() {
	int num;
	printf("请输入需要添加的学生数量\n");
	scanf("%d", &num);
	printf("输入顺序:\n学号 姓名 数学 英语\n");
	for (int i = 0; i < num; i++) {
		scanf("%ld %c %d %d", &stu[total].id, &stu[total].name, &stu[total].math, &stu[total].english);
		aver(total);
		total += 1;
	}
	system("cls");//清屏
}
//查询成绩
void query_grade() {
	system("cls");//清屏
	printf("成绩汇总:\n学号     姓名  数学 英语  平均\n");
	for (int i = 0; i < total; i++) {
		printf("%ld  %s    %d   %d   %.1f\n", stu[i].id, stu[i].name, stu[i].math, stu[i].english, stu[i].aver_score);
	}
	printf("\n");
}
//计算平均成绩
void aver(int num) {
	stu[num].aver_score = (stu[num].math + stu[num].english) / 2.0;

}
//修改成绩
void change() {
	int id, sbj, score;
	printf("请输入需要修改成绩的学号:");
	scanf("%d", &id);
	printf("请输入想要修改的科目,1:数学  2:英语\n");
	scanf("%d", &sbj);
	printf("请输入新的成绩");
	scanf("%d", &score);
	for (int i = 0; i < total; i++)
		if (stu[i].id == id) {
			if (sbj == 1) {
				stu[i].math = score;
				aver(i);
				break;
			}
			else if (sbj == 2) {
				stu[i].english = score;
				aver(i);
				break;
			}
		}
	query_grade();
}
//删除成绩
void delete_score() {
	int id;
	printf("请输入需要修改成绩的学号:");
	scanf("%d", &id);
	for (int i = 0; i < total; i++) {
		if (stu[i].id == id) {
			for (int j = i; j < total; j++) {
				stu[j].id = stu[j + 1].id;
				stu[j].name[0] = stu[j + 1].name[0];
				stu[j].name[1] = stu[j + 1].name[1];
				stu[j].name[2] = stu[j + 1].name[2];
				stu[j].name[3] = stu[j + 1].name[3];
				stu[j].math = stu[j + 1].math;
				stu[j].english = stu[j + 1].english;
				stu[j].aver_score = stu[j + 1].aver_score;
			}
			break;
		}
	}
	total -= 1;
	system("cls");//清屏
	query_grade();
}
//成绩排序
void sort_score() {
	int opt;
	printf("请输入需要排序的科目(升序):1.数学 2.英语 3.平均\n");
	scanf("%d", &opt);
	if (opt == 1) {
		for (int i = 0; i < total - 1; i++) {
			for (int j = 0; j < total - i - 1; j++)
				if (stu[j].math > stu[j + 1].math) {
					char temp[4];

					temp[0] = stu[j].name[0];
					temp[1] = stu[j].name[1];
					temp[2] = stu[j].name[2];
					temp[3] = stu[j].name[3];


					stu[j].name[0] = stu[j + 1].name[0];
					stu[j].name[1] = stu[j + 1].name[1];
					stu[j].name[2] = stu[j + 1].name[2];
					stu[j].name[3] = stu[j + 1].name[3];

					stu[j + 1].name[0] = temp[0];
					stu[j + 1].name[1] = temp[1];
					stu[j + 1].name[2] = temp[2];
					stu[j + 1].name[3] = temp[3];
					
					long int temp2 = stu[j].id;
					stu[j].id = stu[j + 1].id;
					stu[j + 1].id = temp2;
					int temp3 = stu[j].math;
					stu[j].math = stu[j + 1].math;
					stu[j + 1].math = temp3;
					int temp4 = stu[j].english;
					stu[j].english = stu[j + 1].english;
					stu[j + 1].english = temp4;
					float temp5 = stu[j].aver_score;
					stu[j].aver_score = stu[j + 1].aver_score;
					stu[j + 1].aver_score = temp5;
				}
		}
	}
	else if (opt == 2) {
		for (int i = 0; i < total-1; i++) {
			for (int j = 0; j < total - i-1; j++)
				if (stu[j].english > stu[j + 1].english) {
					char temp[4];
					temp[0] = stu[j].name[0];
					temp[1] = stu[j].name[1];
					temp[2] = stu[j].name[2];
					temp[3] = stu[j].name[3];

					stu[j].name[0] = stu[j + 1].name[0];
					stu[j].name[1] = stu[j + 1].name[1];
					stu[j].name[2] = stu[j + 1].name[2];
					stu[j].name[3] = stu[j + 1].name[3];

					stu[j + 1].name[0] = temp[0];
					stu[j + 1].name[1] = temp[1];
					stu[j + 1].name[2] = temp[2];
					stu[j + 1].name[3] = temp[3];

					long int temp2 = stu[j].id;
					stu[j].id = stu[j + 1].id;
					stu[j + 1].id = temp2;
					int temp3 = stu[j].math;
					stu[j].math = stu[j + 1].math;
					stu[j + 1].math = temp3;
					int temp4 = stu[j].english;
					stu[j].english = stu[j + 1].english;
					stu[j + 1].english = temp4;
					float temp5 = stu[j].aver_score;
					stu[j].aver_score = stu[j + 1].aver_score;
					stu[j + 1].aver_score = temp5;
				}
		}
	}
	else if (opt == 3) {
		for (int i = 0; i < total-1; i++) {
			for (int j = 0; j < total - i-1; j++)
				if (stu[j].aver_score > stu[j + 1].aver_score) {
					char temp[4];
					temp[0] = stu[j].name[0];
					temp[1] = stu[j].name[1];
					temp[2] = stu[j].name[2];
					temp[3] = stu[j].name[3];

					stu[j].name[0] = stu[j + 1].name[0];
					stu[j].name[1] = stu[j + 1].name[1];
					stu[j].name[2] = stu[j + 1].name[2];
					stu[j].name[3] = stu[j + 1].name[3];

					stu[j + 1].name[0] = temp[0];
					stu[j + 1].name[1] = temp[1];
					stu[j + 1].name[2] = temp[2];
					stu[j + 1].name[3] = temp[3];
					long int temp2 = stu[j].id;
					stu[j].id = stu[j + 1].id;
					stu[j + 1].id = temp2;
					int temp3 = stu[j].math;
					stu[j].math = stu[j + 1].math;
					stu[j + 1].math = temp3;
					int temp4 = stu[j].english;
					stu[j].english = stu[j + 1].english;
					stu[j + 1].english = temp4;
					float temp5 = stu[j].aver_score;
					stu[j].aver_score = stu[j + 1].aver_score;
					stu[j + 1].aver_score = temp5;
				}

		}
	}
	system("cls");//清屏
	query_grade();
}
//成绩统计
void statistics() {
	int opt, a = 0, b = 0, c = 0, d = 0, e = 0;
	printf("请选择想要统计的科目:1.数学 2.英语");
	scanf("%d", &opt);
	for (int i = 0; i < total; i++) {
		if (opt == 1) {
			if (stu[i].math >= 90)
				a += 1;
			else if (stu[i].math >= 80 && stu[i].math < 90)
				b += 1;
			else if (stu[i].math >= 70 && stu[i].math < 80)
				c += 1;
			else if (stu[i].math >= 60 && stu[i].math < 70)
				d += 1;
			else if (stu[i].math < 60)
				e += 1;
		}
		else if (opt == 2) {
			if (stu[i].english >= 90)
				a += 1;
			else if (stu[i].english >= 80 && stu[i].english < 90)
				b += 1;
			else if (stu[i].english >= 70 && stu[i].english < 80)
				c += 1;
			else if (stu[i].english >= 60 && stu[i].english < 70)
				d += 1;
			else if (stu[i].english < 60)
				e += 1;
		}
	}
	system("cls");//清屏
	if (opt == 1)
		printf("数学成绩统计:\n");
	else if(opt == 2)
		printf("英语成绩统计:\n");
	printf("低于60      :");
	for (int i = 0; i < e; i++) {
		printf("*");
	}
	printf("\n");
	printf("大于60小于70:");
	for (int i = 0; i < d; i++) {
		printf("*");
	}
	printf("\n");
	printf("大于70小于80:");
	for (int i = 0; i < c; i++) {
		printf("*");
	}
	printf("\n");
	printf("大于80小于90:");
	for (int i = 0; i < b; i++) {
		printf("*");
	}
	printf("\n");
	printf("大于90      :");
	for (int i = 0; i < a; i++) {
		printf("*");
	}
	printf("\n");
	printf("\n");
}

评论

  1. Bruce
    Android Chrome
    3 月前
    2024-1-24 13:40:06

    非常赞,路过顶一下😁

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇