这个程序是一个简单的通讯录管理系统,允许用户进行联系人信息的添加、查看、删除、查找、修改以及清空所有联系人等操作。以下是各个功能的介绍:
-
添加联系人:用户可以输入联系人姓名、性别、年龄、电话号码和地址,将新联系人添加到通讯录中。
-
查看联系人:用户可以查看已添加的所有联系人信息,包括姓名、性别、年龄、电话号码和地址。
-
删除联系人:用户可以通过输入联系人的姓名来删除该联系人。在确认后,系统会将该联系人从通讯录中移除。
-
查找联系人:用户可以根据姓名查找特定的联系人,系统会返回该联系人的详细信息。
-
修改联系人信息:用户可以选择要修改的字段(姓名、性别、年龄、电话号码或地址)并进行相应的更改。
-
清空所有联系人:用户可以选择清空通讯录中的所有联系人信息,系统会要求确认操作。
-
退出系统:用户可以选择退出通讯录管理系统。
C++代码
/*
* author: qixianmango
* date: 2023-09-10
*/
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000 // Define the maximum capacity of the address book
// Display the menu
void Menu() {
cout << "Address Book Management System" << endl;
cout << "-----------" << endl;
cout << "1. Add Contact" << endl;
cout << "2. Show Contacts" << endl;
cout << "3. Delete Contact" << endl;
cout << "4. Find Contact" << endl;
cout << "5. Modify Contact" << endl;
cout << "6. Clear All Contacts" << endl;
cout << "7. Exit" << endl;
}
// Define the structure for a contact, including name, gender, age, phone, and address
struct People {
string name; // Name
int sex; // Gender (1. Male 0. Female)
int age; // Age
string phone; // Phone number
string address; // Address
};
// Define the structure for the address book, containing an array of contacts and the current size
struct Contacts {
People peoplearray[MAX]; // Array to store contacts
int size; // Current number of contacts
};
// Add a contact to the address book
void Addpeople(Contacts* abs) {
// Check if the address book is full
if (abs->size == MAX) {
cout << "Address book is full" << endl;
return;
} else {
// Enter contact name
string name;
cout << "Enter name: " << endl;
cin >> name;
abs->peoplearray[abs->size].name = name;
// Enter gender
int sex;
cout << "Enter gender (1. Male 0. Female): " << endl;
while (true) {
cin >> sex;
if (sex == 1 || sex == 0) {
abs->peoplearray[abs->size].sex = sex;
break;
}
cout << "Invalid input, please re-enter" << endl;
}
// Enter age
int age;
cout << "Enter age: " << endl;
while (true) {
cin >> age;
if (age >= 0 && age <= 150) {
abs->peoplearray[abs->size].age = age;
break;
}
cout << "Invalid input, please re-enter" << endl;
}
// Enter phone number and validate
string phone;
cout << "Enter phone number (11 digits): " << endl;
while (true) {
cin >> phone;
if (phone.length() == 11 && phone.find_first_not_of("0123456789") == string::npos) {
abs->peoplearray[abs->size].phone = phone;
break;
}
cout << "Invalid phone number, please re-enter 11 digits: " << endl;
}
// Enter address
string address;
cout << "Enter address: " << endl;
cin >> address;
abs->peoplearray[abs->size].address = address;
abs->size++; // Increase the number of contacts
cout << "Contact added successfully" << endl;
}
}
// Display all contacts
void Showpeople(Contacts* abs) {
if (abs->size == 0) {
cout << "The address book is empty, please add contacts" << endl;
} else {
for (int i = 0; i < abs->size; i++) {
cout << "Name: " << abs->peoplearray[i].name << "\t";
cout << "Gender: " << (abs->peoplearray[i].sex == 1 ? "Male" : "Female") << "\t";
cout << "Age: " << abs->peoplearray[i].age << "\t";
cout << "Phone: " << abs->peoplearray[i].phone << "\t";
cout << "Address: " << abs->peoplearray[i].address << endl;
}
}
}
// Check if a contact exists, return the index if found, otherwise return -1
int isExist(Contacts* abs, string name) {
for (int i = 0; i < abs->size; i++) {
if (abs->peoplearray[i].name == name) {
return i;
}
}
return -1;
}
// Find a contact
void Findpeople(Contacts* abs) {
cout << "Enter the name of the contact to find: " << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if (index == -1) {
cout << "Contact not found" << endl;
} else {
cout << "Contact found:" << endl;
cout << "Name: " << abs->peoplearray[index].name << "\t";
cout << "Gender: " << (abs->peoplearray[index].sex == 1 ? "Male" : "Female") << "\t";
cout << "Age: " << abs->peoplearray[index].age << "\t";
cout << "Phone: " << abs->peoplearray[index].phone << "\t";
cout << "Address: " << abs->peoplearray[index].address << endl;
}
}
// Delete a contact
void Deletepeople(Contacts* abs) {
cout << "Enter the name of the contact to delete: " << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if (index == -1) {
cout << "Contact not found" << endl;
} else {
cout << "Are you sure you want to delete " << abs->peoplearray[index].name << "? (Enter 'yes' to confirm): ";
string confirm;
cin >> confirm;
if (confirm == "yes") {
// Shift contacts forward to delete the selected one
for (int i = index; i < abs->size - 1; i++) {
abs->peoplearray[i] = abs->peoplearray[i + 1];
}
abs->size--; // Decrease the number of contacts
cout << "Contact deleted successfully" << endl;
} else {
cout << "Deletion canceled" << endl;
}
}
}
// Modify a contact's information
void ModifyContact(Contacts* abs) {
cout << "Enter the name of the contact to modify: ";
string name;
cin >> name;
int index = isExist(abs, name);
if (index == -1) {
cout << "Contact not found, please try again." << endl;
return;
} else {
// Display current information
cout << "Contact found: " << endl;
cout << "Name: " << abs->peoplearray[index].name << endl;
cout << "Gender: " << (abs->peoplearray[index].sex == 1 ? "Male" : "Female") << endl;
cout << "Age: " << abs->peoplearray[index].age << endl;
cout << "Phone: " << abs->peoplearray[index].phone << endl;
cout << "Address: " << abs->peoplearray[index].address << endl;
// Choose the field to modify
cout << "Select the information to modify: " << endl;
cout << "1. Modify name" << endl;
cout << "2. Modify gender" << endl;
cout << "3. Modify age" << endl;
cout << "4. Modify phone number" << endl;
cout << "5. Modify address" << endl;
int select;
cin >> select;
switch (select) {
case 1:
cout << "Enter the new name: ";
cin >> abs->peoplearray[index].name;
cout << "Name updated successfully!" << endl;
break;
case 2:
cout << "Enter the new gender (1. Male 0. Female): ";
while (true) {
int sex;
cin >> sex;
if (sex == 1 || sex == 0) {
abs->peoplearray[index].sex = sex;
cout << "Gender updated successfully!" << endl;
break;
}
cout << "Invalid input, please enter (1. Male 0. Female): " << endl;
}
break;
case 3:
cout << "Enter the new age: ";
while (true) {
int age;
cin >> age;
if (age >= 0 && age <= 150) {
abs->peoplearray[index].age = age;
cout << "Age updated successfully!" << endl;
break;
}
cout << "Invalid input, please enter a number between 0 and 150: " << endl;
}
break;
case 4:
cout << "Enter the new phone number (11 digits): ";
while (true) {
string phone;
cin >> phone;
if (phone.length() == 11 && phone.find_first_not_of("0123456789") == string::npos) {
abs->peoplearray[index].phone = phone;
cout << "Phone number updated successfully!" << endl;
break;
}
cout << "Invalid phone number, please re-enter 11 digits: " << endl;
}
break;
case 5:
cout << "Enter the new address: ";
cin >> abs->peoplearray[index].address;
cout << "Address updated successfully!" << endl;
break;
default:
cout << "Invalid input" << endl;
break;
}
}
}
// Clear all contacts
void Cleanpeople(Contacts* abs) {
cout << "Are you sure you want to clear all contacts? Enter 'yes' to confirm: ";
string confirm;
cin >> confirm;
if (confirm == "yes") {
abs->size = 0;
cout << "All contacts cleared!" << endl;
} else {
cout << "Clear operation canceled" << endl;
}
}
// Main function
int main() {
Contacts abs; // Create an address book object
abs.size = 0; // Initialize the number of contacts to 0
while (true) {
Menu(); // Display the menu
cout << "Please select an option: ";
int select;
cin >> select;
switch (select) {
case 1: // Add a contact
Addpeople(&abs);
break;
case 2: // Show all contacts
Showpeople(&abs);
break;
case 3: // Delete a contact
Deletepeople(&abs);
break;
case 4: // Find a contact
Findpeople(&abs);
break;
case 5: // Modify a contact
ModifyContact(&abs);
break;
case 6: // Clear all contacts
Cleanpeople(&abs);
break;
case 7: // Exit the system
cout << "Exiting the program" << endl;
return 0;
default:
cout << "Invalid input, please select again" << endl;
}
}
return 0;
}
(附:电话号码必须十一位录入,否则不予记录。)