c++实现电话薄管理程序,Mytel类描述单个电话号码,TelManager类负责管理电话号码,把增删改查功能封装到该类,要求电话号码能从磁盘读写
时间: 2024-04-30 11:19:09 浏览: 141
以下是一个简单的实现:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Mytel {
private:
string name;
string number;
public:
Mytel(string n = "", string num = ""): name(n), number(num) {}
string getName() const { return name; }
string getNumber() const { return number; }
void setName(string n) { name = n; }
void setNumber(string num) { number = num; }
void print() const { cout << name << ": " << number << endl; }
};
class TelManager {
private:
vector<Mytel> phonebook;
string filename;
public:
TelManager(string fname) : filename(fname) {}
void load() {
ifstream fin(filename);
if (!fin.is_open()) {
cout << "Error opening file " << filename << endl;
return;
}
string name, number;
while (fin >> name >> number) {
Mytel entry(name, number);
phonebook.push_back(entry);
}
fin.close();
}
void save() const {
ofstream fout(filename);
if (!fout.is_open()) {
cout << "Error opening file " << filename << endl;
return;
}
for (const auto& entry : phonebook) {
fout << entry.getName() << " " << entry.getNumber() << endl;
}
fout.close();
}
void addEntry(const Mytel& entry) {
phonebook.push_back(entry);
}
void removeEntry(const Mytel& entry) {
for (auto it = phonebook.begin(); it != phonebook.end(); it++) {
if (it->getName() == entry.getName() && it->getNumber() == entry.getNumber()) {
phonebook.erase(it);
break;
}
}
}
void updateEntry(const Mytel& oldEntry, const Mytel& newEntry) {
for (auto& entry : phonebook) {
if (entry.getName() == oldEntry.getName() && entry.getNumber() == oldEntry.getNumber()) {
entry = newEntry;
break;
}
}
}
Mytel findEntry(const string& name) const {
for (const auto& entry : phonebook) {
if (entry.getName() == name) {
return entry;
}
}
return Mytel("", "");
}
void printAllEntries() const {
for (const auto& entry : phonebook) {
entry.print();
}
}
};
int main() {
TelManager manager("phonebook.txt");
manager.load();
cout << "Select option:" << endl;
cout << "1. Add entry" << endl;
cout << "2. Remove entry" << endl;
cout << "3. Update entry" << endl;
cout << "4. Find entry" << endl;
cout << "5. Print all entries" << endl;
cout << "6. Exit" << endl;
int option;
do {
cout << "Option: ";
cin >> option;
switch (option) {
case 1: {
string name, number;
cout << "Enter name: ";
cin >> name;
cout << "Enter number: ";
cin >> number;
Mytel entry(name, number);
manager.addEntry(entry);
manager.save();
break;
}
case 2: {
string name, number;
cout << "Enter name: ";
cin >> name;
cout << "Enter number: ";
cin >> number;
Mytel entry(name, number);
manager.removeEntry(entry);
manager.save();
break;
}
case 3: {
string oldName, oldNumber, newName, newNumber;
cout << "Enter old name: ";
cin >> oldName;
cout << "Enter old number: ";
cin >> oldNumber;
Mytel oldEntry(oldName, oldNumber);
cout << "Enter new name: ";
cin >> newName;
cout << "Enter new number: ";
cin >> newNumber;
Mytel newEntry(newName, newNumber);
manager.updateEntry(oldEntry, newEntry);
manager.save();
break;
}
case 4: {
string name;
cout << "Enter name: ";
cin >> name;
Mytel entry = manager.findEntry(name);
if (entry.getName() != "") {
entry.print();
} else {
cout << "Entry not found" << endl;
}
break;
}
case 5: {
manager.printAllEntries();
break;
}
case 6: {
cout << "Exiting..." << endl;
break;
}
default: {
cout << "Invalid option" << endl;
}
}
} while (option != 6);
return 0;
}
```
在这个实现中,`Mytel` 类描述单个电话号码,有一个姓名和一个电话号码。`TelManager` 类负责管理电话号码,有一个 `vector` 存储所有电话号码,以及一个文件名用于读写电话号码。`load` 和 `save` 函数分别从文件中读取和写入电话号码,`addEntry`、`removeEntry`、`updateEntry` 和 `findEntry` 函数分别实现增加、删除、更新和查找电话号码的功能,`printAllEntries` 函数打印所有电话号码。在 `main` 函数中,程序通过一个循环读取用户选择的操作,然后调用相应的函数实现该操作。每次操作后,程序都会将电话号码保存到文件中。
阅读全文