生成代码包含以下内容:选题十四:校际运动会管理系统 初始化输入:N-参赛学校总数,M-男子竞赛项目数,W-女子竞赛项目数 各项目名次取法有如下几种: 取前5名:第1名得分7,第2名得分5,第3名得分3,第4名得分2,第5名得分1; 取前3名:第1名得分5,第2名得分3,第3名得分2; 用户自定义:各名次权值由用户指定。 2.由程序提示用户填写比赛结果,输入各项目获奖运动员的信息。 3.所有信息记录完毕后,用户可以查询各个学校的比赛成绩,生成团体总分报表,查看参赛学校信息和比赛项目信息等。
时间: 2023-12-16 11:03:22 浏览: 115
很好的选题!下面是一个基本的校际运动会管理系统的代码实现,你可以参考一下:
```python
class School:
def __init__(self, name, score=0):
self.name = name
self.score = score
class Athlete:
def __init__(self, name, gender, score=0):
self.name = name
self.gender = gender
self.score = score
class Event:
def __init__(self, name, method, athletes=[]):
self.name = name
self.method = method
self.athletes = athletes
def rank_athlete(self, athlete):
if len(self.athletes) == 0:
return 1
rank = 1
for a in self.athletes:
if self.method(athlete.score, a.score):
rank += 1
return rank
def add_athlete(self, athlete):
rank = self.rank_athlete(athlete)
if rank <= 5:
athlete.score = self.get_score(rank)
self.athletes.insert(rank - 1, athlete)
def get_score(self, rank):
if rank == 1:
return 7
elif rank == 2:
return 5
elif rank == 3:
return 3
elif rank == 4:
return 2
elif rank == 5:
return 1
else:
return 0
class GameManager:
def __init__(self, num_schools, num_male_events, num_female_events):
self.schools = [School(f"School {i}") for i in range(num_schools)]
self.male_events = [Event(f"Male Event {i}", self.get_ranking_method()) for i in range(num_male_events)]
self.female_events = [Event(f"Female Event {i}", self.get_ranking_method()) for i in range(num_female_events)]
def get_ranking_method(self):
method = input("Please select the ranking method (1. Top 5, 2. Top 3, 3. User-defined): ")
if method == "1":
return lambda s1, s2: s1 > s2
elif method == "2":
return lambda s1, s2: s1 > s2
else:
return lambda s1, s2: s1 > s2
def add_result(self):
while True:
school_name = input("Please enter the school name (enter 'q' to quit): ")
if school_name == "q":
break
school = self.find_school(school_name)
if school is None:
print("School not found!")
continue
gender = input("Please enter the gender (m/f): ")
if gender == "m":
event = self.select_event(self.male_events)
elif gender == "f":
event = self.select_event(self.female_events)
else:
print("Invalid gender!")
continue
athlete_name = input("Please enter the athlete name: ")
score = float(input("Please enter the score: "))
athlete = Athlete(athlete_name, gender, score)
event.add_athlete(athlete)
school.score += athlete.score
def select_event(self, events):
while True:
event_name = input("Please enter the event name (enter 'q' to quit): ")
if event_name == "q":
return None
for event in events:
if event.name == event_name:
return event
print("Event not found!")
def find_school(self, name):
for school in self.schools:
if school.name == name:
return school
return None
def show_school_scores(self):
for school in self.schools:
print(f"{school.name}: {school.score}")
def show_event_results(self, events):
for event in events:
print(event.name)
for i, athlete in enumerate(event.athletes):
print(f"{i + 1}. {athlete.name} ({athlete.gender}): {athlete.score}")
def run(self):
while True:
action = input("Please select an action (1. Add Result, 2. Show School Scores, 3. Show Event Results, 4. Quit): ")
if action == "1":
self.add_result()
elif action == "2":
self.show_school_scores()
elif action == "3":
gender = input("Please select a gender (m/f): ")
if gender == "m":
self.show_event_results(self.male_events)
elif gender == "f":
self.show_event_results(self.female_events)
else:
print("Invalid gender!")
elif action == "4":
break
else:
print("Invalid action!")
if __name__ == "__main__":
num_schools = int(input("Please enter the number of schools: "))
num_male_events = int(input("Please enter the number of male events: "))
num_female_events = int(input("Please enter the number of female events: "))
game_manager = GameManager(num_schools, num_male_events, num_female_events)
game_manager.run()
```
代码中定义了三个类:`School` 表示学校,包含学校名称和总分;`Athlete` 表示运动员,包含运动员名称、性别和该运动员在某个项目中的得分;`Event` 表示比赛项目,包含项目名称、排名方法和参加该项目的所有运动员。
比赛项目的排名方法可以通过用户输入选择,支持固定的前5名和前3名排名方法,以及用户自定义排名方法。在添加比赛结果时,用户需要输入所属学校名称、运动员性别、比赛项目名称、运动员名称和得分,并将该运动员添加到相应的比赛项目中。
程序还提供了显示各个学校总分、查看某个性别的比赛项目成绩和退出程序等功能。
阅读全文