一、 学生成绩核算系统的设计与实现(25) 要求:(1)按班级按课程从文件中读入相应的平时成绩、期中考试成绩和期末考试成绩。 (2)三个成绩对总评成绩的百分比被定义为常数,各占总成绩的30%、30%和40%。 (3)计
时间: 2024-03-27 21:37:31 浏览: 134
学生成绩计算
算每个学生的总评成绩,并按照总评成绩从高到低的顺序输出学生的姓名、学号、总评成绩和排名。以下是一个可能的实现方式:
1. 定义一个 Student 类,包含姓名、学号、平时成绩、期中考试成绩和期末考试成绩等属性,以及计算总评成绩和排名的方法。
2. 从文件中读入每个学生的信息,并创建相应的 Student 对象,保存到一个列表中。
3. 对列表中的每个 Student 对象,调用其计算总评成绩的方法,得到总评成绩,并保存到 Student 对象的属性中。
4. 对列表中的每个 Student 对象,比较其总评成绩,得到排名,并保存到 Student 对象的属性中。
5. 对列表按照总评成绩从高到低排序,并输出每个学生的姓名、学号、总评成绩和排名。
下面是一个简单的 Python 实现:
```python
class Student:
def __init__(self, name, id, regular_score, midterm_score, final_score):
self.name = name
self.id = id
self.regular_score = regular_score
self.midterm_score = midterm_score
self.final_score = final_score
self.total_score = 0
self.rank = 0
def calculate_total_score(self, regular_percent, midterm_percent, final_percent):
self.total_score = self.regular_score * regular_percent + self.midterm_score * midterm_percent + self.final_score * final_percent
def __lt__(self, other):
return self.total_score > other.total_score
def read_students_from_file(filename):
students = []
with open(filename, 'r') as f:
for line in f:
fields = line.strip().split(',')
name = fields[0]
id = fields[1]
regular_score = float(fields[2])
midterm_score = float(fields[3])
final_score = float(fields[4])
student = Student(name, id, regular_score, midterm_score, final_score)
students.append(student)
return students
def calculate_ranks(students):
rank = 1
for i, student in enumerate(students):
if i > 0 and student.total_score < students[i-1].total_score:
rank = i + 1
student.rank = rank
def print_students(students):
print('{:<10}{:<10}{:<10}{:<10}'.format('Name', 'ID', 'TotalScore', 'Rank'))
for student in students:
print('{:<10}{:<10}{:<10.2f}{:<10}'.format(student.name, student.id, student.total_score, student.rank))
if __name__ == '__main__':
students = read_students_from_file('scores.txt')
for student in students:
student.calculate_total_score(0.3, 0.3, 0.4)
students.sort()
calculate_ranks(students)
print_students(students)
```
其中,`scores.txt` 文件的格式为:
```
Alice,001,80,85,90
Bob,002,70,75,80
Charlie,003,85,90,95
...
```
阅读全文