基于文件的通讯录管理系统。通讯录个人信息包括姓名、性别、年龄、电话、地址。用文件读/写操作编程实现通讯录的增加、查找、显示、删除、修改功能,完成通讯录编程、成功运行。
时间: 2023-12-22 14:24:16 浏览: 72
通讯录管理系统 为方便管理个人通讯记录,编写一个简单的通讯录管理程序。系统记录必须有姓名、电话号码、移动电话,电子邮件
4星 · 用户满意度95%
以下是Python代码实现:
```python
# 定义通讯录类
class AddressBook:
def __init__(self):
self.contacts = []
# 添加联系人
def add_contact(self, name, gender, age, phone, address):
contact = {
"name": name,
"gender": gender,
"age": age,
"phone": phone,
"address": address
}
self.contacts.append(contact)
# 查找联系人
def search_contact(self, name):
for contact in self.contacts:
if contact["name"] == name:
return contact
return None
# 显示所有联系人
def show_contacts(self):
for contact in self.contacts:
print(f"姓名:{contact['name']},性别:{contact['gender']},年龄:{contact['age']},电话:{contact['phone']},地址:{contact['address']}")
# 删除联系人
def delete_contact(self, name):
for i, contact in enumerate(self.contacts):
if contact["name"] == name:
del self.contacts[i]
return True
return False
# 修改联系人
def modify_contact(self, name, gender, age, phone, address):
for contact in self.contacts:
if contact["name"] == name:
contact["gender"] = gender
contact["age"] = age
contact["phone"] = phone
contact["address"] = address
return True
return False
# 保存通讯录到文件
def save_to_file(self, filename):
with open(filename, "w") as f:
for contact in self.contacts:
f.write(f"{contact['name']},{contact['gender']},{contact['age']},{contact['phone']},{contact['address']}\n")
# 从文件中加载通讯录
def load_from_file(self, filename):
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
data = line.strip().split(",")
name, gender, age, phone, address = data
self.add_contact(name, gender, age, phone, address)
# 测试代码
if __name__ == "__main__":
address_book = AddressBook()
address_book.load_from_file("contacts.txt")
while True:
print("请选择操作:")
print("1. 添加联系人")
print("2. 查找联系人")
print("3. 显示所有联系人")
print("4. 删除联系人")
print("5. 修改联系人")
print("6. 保存通讯录到文件")
print("7. 退出程序")
choice = input("请输入操作编号:")
if choice == "1":
name = input("请输入姓名:")
gender = input("请输入性别:")
age = input("请输入年龄:")
phone = input("请输入电话:")
address = input("请输入地址:")
address_book.add_contact(name, gender, age, phone, address)
print("联系人添加成功!")
elif choice == "2":
name = input("请输入要查找的姓名:")
contact = address_book.search_contact(name)
if contact:
print("联系人信息如下:")
print(f"姓名:{contact['name']},性别:{contact['gender']},年龄:{contact['age']},电话:{contact['phone']},地址:{contact['address']}")
else:
print("未找到该联系人!")
elif choice == "3":
print("所有联系人如下:")
address_book.show_contacts()
elif choice == "4":
name = input("请输入要删除的姓名:")
if address_book.delete_contact(name):
print("联系人删除成功!")
else:
print("未找到该联系人!")
elif choice == "5":
name = input("请输入要修改的姓名:")
gender = input("请输入性别:")
age = input("请输入年龄:")
phone = input("请输入电话:")
address = input("请输入地址:")
if address_book.modify_contact(name, gender, age, phone, address):
print("联系人修改成功!")
else:
print("未找到该联系人!")
elif choice == "6":
address_book.save_to_file("contacts.txt")
print("通讯录保存成功!")
elif choice == "7":
print("程序已退出。")
break
else:
print("输入有误,请重新输入!")
```
该程序中,通讯录类`AddressBook`中包含了添加联系人、查找联系人、显示所有联系人、删除联系人、修改联系人、保存通讯录到文件和从文件中加载通讯录等方法。在程序运行时,用户可以通过输入操作编号来选择需要进行的操作,实现了通讯录管理系统的基本功能。
阅读全文