用python20.创建一个名为Student的类,包含姓名、学号和年级属性。从文件中读取数据并创建Student对象列表。将这些对象按学号排序并将结果写入另一个文件。 21.创建一个名为Book的类,包含书名、作者和价格属性。从文件中读取数据并创建Book对象列表。找出价格最高的书并输出书名和作者。 22.创建一个名为Employee的类,包含名字、工号和薪水属性。从文件中读取数据并创建Employee对象列表。将这些对象按薪水排序并将结果写入另一个文件。 23.创建一个名为Person的类,包含姓名、年龄和性别属性。从文件中读取数据并创建Person对象列表。将这些对象按年龄从大到小排序并将结果写入另一个文件。 创建一个名为Rectangle的类,包含长和宽属性。从文件中读取数据并创建Rectangle对象列表。计算并输出所有矩形对象的周长和面积之和。
时间: 2024-02-28 10:55:26 浏览: 131
好的,我已经了解您的要求了,下面是代码:
20.
```python
class Student:
def __init__(self, name, student_id, grade):
self.name = name
self.student_id = student_id
self.grade = grade
def read_students(file_name):
student_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
student_list.append(Student(data[0], data[1], data[2]))
return student_list
def write_students(student_list, file_name):
student_list.sort(key=lambda x: x.student_id)
with open(file_name, 'w') as f:
for student in student_list:
f.write(f'{student.name},{student.student_id},{student.grade}\n')
students = read_students('students.txt')
write_students(students, 'sorted_students.txt')
```
21.
```python
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def read_books(file_name):
book_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
book_list.append(Book(data[0], data[1], float(data[2])))
return book_list
def find_highest_priced_book(book_list):
highest_priced_book = book_list[0]
for book in book_list:
if book.price > highest_priced_book.price:
highest_priced_book = book
return highest_priced_book
books = read_books('books.txt')
highest_priced_book = find_highest_priced_book(books)
print(f'The highest priced book is {highest_priced_book.title} by {highest_priced_book.author}.')
```
22.
```python
class Employee:
def __init__(self, name, employee_id, salary):
self.name = name
self.employee_id = employee_id
self.salary = salary
def read_employees(file_name):
employee_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
employee_list.append(Employee(data[0], data[1], int(data[2])))
return employee_list
def write_employees(employee_list, file_name):
employee_list.sort(key=lambda x: x.salary)
with open(file_name, 'w') as f:
for employee in employee_list:
f.write(f'{employee.name},{employee.employee_id},{employee.salary}\n')
employees = read_employees('employees.txt')
write_employees(employees, 'sorted_employees.txt')
```
23.
```python
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def read_people(file_name):
person_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
person_list.append(Person(data[0], int(data[1]), data[2]))
return person_list
def write_people(person_list, file_name):
person_list.sort(key=lambda x: x.age, reverse=True)
with open(file_name, 'w') as f:
for person in person_list:
f.write(f'{person.name},{person.age},{person.gender}\n')
people = read_people('people.txt')
write_people(people, 'sorted_people.txt')
```
24.
```python
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
def read_rectangles(file_name):
rectangle_list = []
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(',')
rectangle_list.append(Rectangle(int(data[0]), int(data[1])))
return rectangle_list
def calculate_total_area_and_perimeter(rectangle_list):
total_area = 0
total_perimeter = 0
for rectangle in rectangle_list:
total_area += rectangle.area()
total_perimeter += rectangle.perimeter()
return total_area, total_perimeter
rectangles = read_rectangles('rectangles.txt')
total_area, total_perimeter = calculate_total_area_and_perimeter(rectangles)
print(f'Total area: {total_area} sq. units')
print(f'Total perimeter: {total_perimeter} units')
```
阅读全文