How to write a simple address book program in C?
Introduction:
In modern society, the address book is a common tool used to store and manage basic information of personal contacts, such as names, phone numbers, and addresses. In this article, we will detail how to write a simple address book program using C.
Text:
Step 1: Define the structure of the address book entry
First, we need to define a structure to represent each entry in the address book. This structure can contain fields such as name, phone number, and address. For example, we can use the following code to define a structure called "AddressBookEntry":
struct AddressBookEntry { std::string name; std::string phoneNumber; std::string address; };
Step 2: Implement the basic functions of the address book program
Next, we need to implement some basic functions to Manipulate the address book, including adding new entries, finding entries, updating entries, and deleting entries. We can use C container classes to store address book entries, such as vector or list. The following is a simple implementation example:
#include <iostream> #include <vector> std::vector<AddressBookEntry> addressBook; // 存储地址簿条目的全局变量 // 添加新条目 void addEntry() { AddressBookEntry entry; std::cout << "请输入姓名:"; std::getline(std::cin, entry.name); std::cout << "请输入电话号码:"; std::getline(std::cin, entry.phoneNumber); std::cout << "请输入地址:"; std::getline(std::cin, entry.address); addressBook.push_back(entry); std::cout << "新增条目成功! "; } // 查找条目 void findEntry() { std::string name; std::cout << "请输入要查找的姓名:"; std::getline(std::cin, name); for (const auto& entry : addressBook) { if (entry.name == name) { std::cout << "姓名:" << entry.name << " "; std::cout << "电话号码:" << entry.phoneNumber << " "; std::cout << "地址:" << entry.address << " "; return; } } std::cout << "未找到匹配的条目。 "; } // 更新条目 void updateEntry() { std::string name; std::cout << "请输入要更新的姓名:"; std::getline(std::cin, name); for (auto& entry : addressBook) { if (entry.name == name) { std::cout << "请输入新的电话号码:"; std::getline(std::cin, entry.phoneNumber); std::cout << "请输入新的地址:"; std::getline(std::cin, entry.address); std::cout << "更新条目成功! "; return; } } std::cout << "未找到匹配的条目。 "; } // 删除条目 void deleteEntry() { std::string name; std::cout << "请输入要删除的姓名:"; std::getline(std::cin, name); for (auto it = addressBook.begin(); it != addressBook.end(); ++it) { if (it->name == name) { addressBook.erase(it); std::cout << "删除条目成功! "; return; } } std::cout << "未找到匹配的条目。 "; } // 显示地址簿 void displayAddressBook() { if (addressBook.empty()) { std::cout << "地址簿为空。 "; return; } for (const auto& entry : addressBook) { std::cout << "姓名:" << entry.name << " "; std::cout << "电话号码:" << entry.phoneNumber << " "; std::cout << "地址:" << entry.address << " "; } } // 主菜单 void mainMenu() { std::cout << "1. 添加新条目 "; std::cout << "2. 查找条目 "; std::cout << "3. 更新条目 "; std::cout << "4. 删除条目 "; std::cout << "5. 显示地址簿 "; std::cout << "0. 退出程序 "; std::cout << "请选择操作:"; } int main() { int choice; do { mainMenu(); std::cin >> choice; std::cin.ignore(); // 忽略输入缓冲区中的换行符 std::cout << " "; switch (choice) { case 1: addEntry(); break; case 2: findEntry(); break; case 3: updateEntry(); break; case 4: deleteEntry(); break; case 5: displayAddressBook(); break; case 0: std::cout << "退出程序。 "; break; default: std::cout << "无效的选择。 "; break; } std::cout << " "; } while (choice != 0); return 0; }
Conclusion:
Through the above steps, we have successfully written a simple address book program in C. This program can add new address book entries, find, update, and delete existing entries, and display the entire address book in an easy-to-read format. You can further expand and improve it according to your needs.
I hope this article can help you get started with C programming and successfully develop a practical address book program!
The above is the detailed content of How to write a simple address book program in C++?. For more information, please follow other related articles on the PHP Chinese website!