编程语言
首页 > 编程语言> > C代码和Python代码:学生成绩管理程序

C代码和Python代码:学生成绩管理程序

作者:互联网

目录

一、总结

没写代码久了,重新看了一遍python的列表和字典

关于C的链表:
链表的节点可以用列表和字典嵌套来实现

关于排序:
sorted()是python自带的函数,
用sorted()来排序是一个比较简便的方法
可参考:《python中的lambda函数与sorted函数》
https://blog.csdn.net/qq_25041667/article/details/102258338
博主写得很详细

二、C代码

运行环境:VS2017
以下是代码:

/*
学生成绩管理程序
编制一个统计学生考试分数的管理程序。
设学生成绩已以一个学生一个记录的形式存储在文件中,
每位学生记录包含的信息有:姓名,学号和各门功课的成绩。
程序具有以下几项功能:求出各门课程的总分,平均分,按姓名,
按学号寻找其记录并显示,浏览全部学生成绩和按总分由高到低显示学生信息等。
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define	SWN		3	/* 课程数 */
#define NAMELEN		20	/* 姓名最大字符数 */
#define CODELEN		10	/* 学号最大字符数 */
#define FNAMELEN	80	/* 文件名最大字符数 */
#define BUFLEN		80	/* 缓冲区最大字符数 */
/* 课程名称表 */
char schoolwork[SWN][NAMELEN + 1] = { "Chinese","Mathematic","English" };
struct record
{
	char	name[NAMELEN + 1];	/* 姓名 */
	char 	code[CODELEN + 1];	/* 学号 */
	int 	marks[SWN];		/* 各课程成绩 */
	int total;			/* 总分 */
}stu;

struct node
{
	char	name[NAMELEN + 1];	/* 姓名 */
	char 	code[CODELEN + 1];	/* 学号 */
	int 	marks[SWN];		/* 各课程成绩 */
	int 	total;			/* 总分 */
	struct	node *next;		/* 后续表元指针 */
}*head;	/* 链表首指针 */

int total[SWN];		/* 各课程总分 */
FILE *stfpt;		/* 文件指针 */
char stuf[FNAMELEN];	/* 文件名 */

/* 从指定文件读入一个记录 */
int readrecord( FILE *fpt, struct record *rpt)
{
	char buf[BUFLEN];
	int i;
	if ( fscanf_s(fpt, "%s", buf, BUFLEN) != 1)
		return 0;	/* 文件结束 */
	strncpy_s(rpt->name, buf, NAMELEN);

	fscanf_s(fpt, "%s", buf, BUFLEN);
	strncpy_s(rpt->code, buf, CODELEN);

	for (i = 0; i < SWN; i++)
		fscanf_s(fpt, "%d", &rpt->marks[i]);


	for (rpt->total = 0, i = 0; i < SWN; i++)
		rpt->total += rpt->marks[i];
	return 1;
}

/* 对指定文件写入一个记录 */
void writerecord( FILE *fpt, struct record *rpt)
{
	int i;
	fprintf(fpt, "%s\n", rpt->name);
	fprintf(fpt, "%s\n", rpt->code);
	for (i = 0; i < SWN; i++)
		fprintf(fpt, "%d\n", rpt->marks[i]);
	return;
}

/* 显示学生记录 */
void displaystu( struct record *rpt)
{
	int i;
	printf("\nName   : %s\n", rpt->name);
	printf("Code   : %s\n", rpt->code);
	printf("Marks  :\n");

	for (i = 0; i < SWN; i++)
		printf("       %-15s : %4d\n", schoolwork[i], rpt->marks[i]);
	printf("Total  : %4d\n", rpt->total);
}

/* 计算各单科总分 */
int totalmark( char *fname)
{
	FILE *fp;
	struct record s;
	int count, i;
	errno_t err;
	err = fopen_s(&fp, fname, "r");
	if ( err != 0)
	{
		printf("Can't open file %s.\n", fname);
		return 0;
	}
	for (i = 0; i < SWN; i++)
		total[i] = 0;
	count = 0;
	while (readrecord(fp, &s) != 0)
	{
		for (i = 0; i < SWN; i++)
			total[i] += s.marks[i];
		count++;
	}
	fclose(fp);
	return count;	/* 返回记录数 */
}

/* 列表显示学生信息 */
void liststu( char *fname)
{
	FILE *fp;
	struct record s;
	errno_t err;
	err = fopen_s(&fp, fname, "r");
	if (err != 0)
	{
		printf("Can't open file %s.\n", fname);
		return;
	}

	while (readrecord(fp, &s) != 0)
	{
		displaystu(&s);
		printf("\n      Press ENTER to continue...\n");
		while ( getchar() != '\n'); //getchar() != '\n'
	}
	fclose(fp);
	return;
}

/* 构造链表 */
struct node *makelist( char *fname)
{
	struct node *h, *p, *v, *u;
	FILE *fp;
	errno_t err;
	err = fopen_s(&fp, fname, "r");
	if (err != 0)
	{
		printf("Can't open file %s.\n", fname);
		return NULL;
	}

	h = p = v = u = NULL;
	p = (struct node *)malloc(sizeof(struct node));
	while (readrecord(fp, (struct record *)p) != 0)
	{
		v = h;
		while ( v &&  p->total <= v->total)		// 所以这个链表是总分从大到小的
		{
			u = v;
			v = v->next;
		}
		if (v == h)
			h = p;
		else
			u->next = p;
		p->next = v;
		p = (struct node *)malloc(sizeof(struct node));
	}
	free(p);
	fclose(fp);
	return h;
}

/* 顺序显示链表各表元 */
void displaylist(struct node *h)
{
	while (h != NULL)
	{
		displaystu((struct record *)h);
		printf("\n      Press ENTER to continue...\n");
		while (getchar() != '\n');
		h = h->next;
	}
	return;
}

/* 按学生姓名查找学生记录 */
int retrievebyn(char *fname, char *key)
{
	FILE *fp;
	int c;
	struct record s;
	errno_t err;
	err = fopen_s(&fp, fname, "r");
	if (err != 0)
	{
		printf("Can't open file %s.\n", fname);
		return 0;
	}
	c = 0;
	while (readrecord(fp, &s) != 0)
	{
		if (strcmp(s.name, key) == 0)
		{
			displaystu(&s);
			c++;
		}
	}
	fclose(fp);
	if (c == 0)
		printf("The student %s is not in the file %s.\n", key, fname);
	return 1;
}

/* 按学生学号查找学生记录 */
int retrievebyc(char *fname, char *key)
{
	FILE *fp;
	int c;
	struct record s;
	errno_t err;
	err = fopen_s(&fp, fname, "r");
	if (err != 0)
	{
		printf("Can't open file %s.\n", fname);
		return 0;
	}
	c = 0;
	while (readrecord(fp, &s) != 0)
	{
		if (strcmp(s.code, key) == 0)
		{
			displaystu(&s);
			c++;
			break;
		}
	}
	fclose(fp);
	if (c == 0)
		printf("The student %s is not in the file %s.\n", key, fname);
	return 1;
}

def main():
	#int i, j, n;
	#char c;
	char buf[BUFLEN];
	FILE *fp;
	struct record s;
	

	print("Please input the students marks record file's name: ", end = '')
	stuf = input()
	errno_t err;
	err = fopen_s(&fp, stuf, "r");
	if( err != 0 )
	{
		printf("The file %s doesn't exit, do you want to creat it? (Y/N) ", stuf);
		getchar();
		c = getchar();
		if c == 'Y' or c == 'y':
			err = fopen_s(&fp, stuf, "w");
			printf("Please input the record number you want to write to the file: ");
			scanf_s("%d", &n);
			for i in rang( 0, n):
				print("Input the student's name: ", end = '')
				s.name = intput()
				
				print("Input the student's code: ", end = '')
				s.code = intput()

				for j in range( 0, SWN):
					printf("Input the %s mark: ", schoolwork[j]);
					scanf_s("%d", &s.marks[j]);
			
				print("\n");
				writerecord(fp, &s)
			fclose(fp);
		}
	}
	fclose(fp);
	i = input()
	system("cls");
	puts("Now you can input a command to manage the records.");
	puts("m : mean of the marks.");
	puts("t : total of the marks.");
	puts("n : search record by student's name.");
	puts("c : search record by student's code.");
	puts("l : list all the records.");
	puts("s : sort and list the records by the total.");
	puts("q : quit!");
	while (1)
	{
		puts("Please input command:");
		scanf_s("%c", &c, 1);		/* 输入选择命令 */
		if (c == 'q' || c == 'Q')
		{
			puts("\n Thank you for your using.");
			break;		/* q,结束程序运行 */
		}
		switch (c)
		{
		case 'm': /* 计算平均分 */
		case 'M':
			if ((n = totalmark(stuf)) == 0)
			{
				puts("Error!");
				break;
			}
			printf("\n");
			for (i = 0; i < SWN; i++)
				printf("%-15s's average is: %.2f.\n", schoolwork[i], (float)total[i] / n);
			break;
		case 't': /* 计算总分 */
		case 'T':
			if ((n = totalmark(stuf)) == 0)
			{
				puts("Error!");
				break;
			}
			printf("\n");
			for (i = 0; i < SWN; i++)
				printf("%-15s's total mark is: %d.\n", schoolwork[i], total[i]);
			break;
		case 'n': /* 按学生的姓名寻找记录 */
		case 'N':
			printf("Please input the student's name you want to search: ");
			scanf_s("%s", buf, 80);
			retrievebyn(stuf, buf);
			break;
		case 'c': /* 按学生的学号寻找记录 */
		case 'C':
			printf("Please input the student's code you want to search: ");
			scanf_s("%s", buf, 80);
			retrievebyc(stuf, buf);
			break;
		case 'l': /* 列出所有学生记录 */
		case 'L':
			liststu(stuf);
			break;
		case 's': /* 按总分从高到低排列显示 */
		case 'S':
			if ((head = makelist(stuf)) != NULL)
				displaylist(head);
			break;
		default: break;
		}
	}
	system("pause");
	return 0;
}

三、Python代码

运行环境:Pycharm,python3.74
以下是代码:

"""学生成绩管理程序
编制一个统计学生考试分数的管理程序。
设学生成绩已以一个学生一个 记录 的形式存储在文件中,
每位学生记录包含的信息有:姓名,学号和各门功课的成绩。
程序具有以下几项功能:求出各门课程的总分,平均分,按姓名,
按学号寻找其记录并显示,浏览全部学生成绩和
按总分由高到低显示学生信息等。
"""

import os

swn = 3		# 课程数
#课程名称表
schoolwork = [ "Chinese", "Mathematic",
			   "English"]
# 各课程总分
total = [ 0 for i in range(swn)]

# 主要通过向 student_list列表 里存放 student字典,
# 向 student字典 里存放学生信息,相当于链表的一个节点,
#  student = {"name": ,"code": ,"marks":{ schoolwork[i]: ,}},
#  "marks"的值也是字典
student_list = []

def add_student():
	"""想要对文件录入的新的学生的记录"""
	new_students = []

	# n是我们需要录入文件的记录的“个数”
	n = int(input("Please input the record number "
				  "you want to write to the file: "))

	for i in range( 0, n):
		name = input("Input the student's name: ")
		code = input("Input the student's code: ")
		marks = {}
		for j in range( 0, swn):
			print("Input the %s mark: " % schoolwork[j], end = "")
			marks[ schoolwork[j]] = int( input())
		print("\n")

		student_record = {"name":name.title(), "code":code, "marks":marks}
		new_students.append(student_record)
	return new_students

def create_file( filename):
	"""
	创建日志文件夹和日志文件
	:param filename:
	:return:
	"""
	path = filename[0:filename.rfind("/")]
	if not os.path.isdir(path):  # 无文件夹时创建
		os.makedirs(path)
	if not os.path.isfile(filename):  # 无文件时创建
		fd = open(filename, mode="w", encoding="utf-8")
		fd.close()
	else:
		pass

def write_file( filename, new_students):
	"""将新输入的学生记录,录入文件"""
	fp = open(filename, "w", encoding='utf-8')
	fp.write(str(new_students))
	fp.close()
	print("Data saved to %s succeeded!" % filename)
	fp.close()

def read_file( filename):
	"""从文件中将数据读取至变量中"""
	global student_list

	if os.path.exists( filename):
		f = open( filename, 'r', encoding='utf-8')
		ret = f.read()
		student_list = eval(ret)
		f.close()

	for s in student_list:
		s["total"] = 0
		for value in s["marks"].values():
			s["total"] = s["total"] + value

	return

def total_mark():
	""" 计算各单科总分 """
	for i in range( 0, swn):
		total[i] = 0
	count = 0

	for s in student_list:
		for i in range( 0, swn):
			total[i] = total[i] + s["marks"][schoolwork[i]]
		count = count + 1

	return count	# 返回记录数

def display_stu( student):
	""" 显示学生记录 """
	print("Name   : ", student["name"])
	print("Code   : ", student["code"])
	print("Marks  :")
	for key,value in sorted( student["marks"].items()):
		print("       %-15s : %4d" % ( key, value))
	print("Total  : %4d" % student["total"])

def list_stu():
	"""列表显示学生信息 """
	for s in student_list:
		display_stu(s)
		print("\n\t Press Blank "
			  "space to continue...")
		e = input()
		while (e != ' '):
			e = input()
	print("\n\t END")

def display_list():
	""" 总分从大到小顺序显示链表各表元"""
	#列表中镶嵌着字典用这种sorted()
	for s in sorted(student_list, key=lambda
			k: k["total"], reverse=True):
		display_stu(s)
		print("\n\t Press Blank "
			  "space to continue...")
		e = input()
		while (e != ' '):
			e = input()
	print("\n\t END")

def show_list():
	""" 菜单"""
	print("\nNow you can input a command to manage the records.")
	print("m : mean of the marks.") #平均分数
	print("t : total of the marks.") #计算总分
	print("n : search record by student's name.") #按学生姓名搜索记录
	print("c : search record by student's code.") #按学生学号搜索记录
	print("l : list all the records.") #列出所有记录
	print("s : sort and list the records by the total.") #按总数排序和列出记录
	print("q : quit!\n") #退出

def retrieve_n( key):
	"""按学生姓名查找学生记录"""
	c = 0
	for s in student_list:
		if key == s["name"]:
			display_stu(s)
			c = c + 1
	if c == 0:
		print("The student %s is not "
			  "in the file." % key)
	return 1

def retrieve_c( key):
	"""按学生学号查找学生记录"""
	c = 0
	for s in student_list:
		if key == s["code"]:
			display_stu(s)
			c = c + 1
			break
	if c == 0:
		print("The student %s is not "
			  "in the file." % key)
	return 1

def main():
	os.system("cls")
	stuf = input("Please input the students "
				 "marks record file's name: ") #文件名

	#调用函数“检查学生成绩记录文件是否创建”
	if not os.path.exists(stuf):
		c = input("The file %s doesn't exit,"
				  "do you want to creat it? (Y/N) ")
		if c == 'Y' or c == 'y':
			create_file( stuf)
			write_file( stuf, add_student())
		else:
			print("\n\tThank you for your using.")
			return

	#学生的信息都将存入student_list中
	read_file( stuf)

	if not student_list:
		print("\n\tStudent information is not stored in the file.")
		print("\t\t\tThank you for your using.")
		return

	i = input("Press any key to clear the "
			  "screen, then the menu will appear")
	os.system("cls")

	while 1:
		show_list()
		c = input("\nPlease input command:") # 输入选择命令
		os.system("cls")
		if c == 'q' or c == 'Q':
			print("\n\tThank you for your using.")
			break

		# 计算单科平均分
		elif c == 'm' or c == 'M':
			n = total_mark()
			if n == 0:
				print("\nError!")
				continue

			print("")
			for i in range( 0, swn):
				print("%-15s's average is: %.2f." %
					  ( schoolwork[i],
						(float)(total[i] / n)) )

		# 计算单科总分
		elif c == 't' or c == 'T':
			if total_mark() == 0:
				print("Error!")
				continue

			print("")
			for i in range( 0, swn):
				print("%-15s's total mark is: %d." %
					  (schoolwork[i], total[i]) )

		#按学生的姓名寻找记录
		elif c == 'n' or c == 'N':
			buf = input("Please input the student's "
				   "name you want to search: ")
			retrieve_n(buf.title())

		# 按学生的学号寻找记录
		elif c == 'c' or c == 'C':
			buf = input("Please input the student's "
				   "code you want to search: ")
			retrieve_c(buf)

		# 列出所有学生记录
		elif c == 'l' or c == 'L':
			list_stu()

		# 按总分从高到低排列显示
		elif c == 's' or c == 'S':
			display_list()

		else:
			print("Input command error, please input again")

main()

wujinxia 发布了7 篇原创文章 · 获赞 1 · 访问量 1534 私信 关注

标签:fp,管理程序,Python,代码,student,marks,print,input,total
来源: https://blog.csdn.net/wujinxia/article/details/102540471