实现电话簿管理程序, Mytel类描述单个电话号码,TelManager类负责管理电话号码。把增、删、改、查功能封装到该类中。要求电话号码能从磁盘读写。 用C++编写该程序
时间: 2023-06-20 22:08:16 浏览: 185
以下是实现电话簿管理程序的代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// Mytel类描述单个电话号码
class Mytel {
public:
Mytel() {}
Mytel(string name, string phone) : name(name), phone(phone) {}
string getName() const { return name; }
string getPhone() const { return phone; }
void setName(string name) { this->name = name; }
void setPhone(string phone) { this->phone = phone; }
// 从文件中读取电话号码
void read(ifstream& infile) {
infile >> name >> phone;
}
// 将电话号码写入文件
void write(ofstream& outfile) {
outfile << name << " " << phone << endl;
}
private:
string name;
string phone;
};
// TelManager类负责管理电话号码
class TelManager {
public:
// 从文件中读取电话号码列表
void load(string filename) {
ifstream infile(filename);
if (!infile) {
cerr << "Error: Could not open file " << filename << endl;
return;
}
Mytel mytel;
while (infile) {
mytel.read(infile);
if (infile) {
mytels.push_back(mytel);
}
}
infile.close();
}
// 将电话号码列表写入文件
void save(string filename) {
ofstream outfile(filename);
for (int i = 0; i < mytels.size(); i++) {
mytels[i].write(outfile);
}
outfile.close();
}
// 增加电话号码
void add(string name, string phone) {
Mytel mytel(name, phone);
mytels.push_back(mytel);
}
// 删除电话号码
void remove(string name) {
for (int i = 0; i < mytels.size(); i++) {
if (mytels[i].getName() == name) {
mytels.erase(mytels.begin() + i);
break;
}
}
}
// 修改电话号码
void update(string name, string phone) {
for (int i = 0; i < mytels.size(); i++) {
if (mytels[i].getName() == name) {
mytels[i].setPhone(phone);
break;
}
}
}
// 查找电话号码
string search(string name) {
for (int i = 0; i < mytels.size(); i++) {
if (mytels[i].getName() == name) {
return mytels[i].getPhone();
}
}
return "Not found";
}
private:
vector<Mytel> mytels;
};
int main() {
TelManager telmanager;
telmanager.load("mytels.txt");
while (true) {
cout << "1. Add a phone number" << endl;
cout << "2. Remove a phone number" << endl;
cout << "3. Update a phone number" << endl;
cout << "4. Search for a phone number" << endl;
cout << "5. Save phone numbers" << endl;
cout << "6. Quit" << endl;
int choice;
cout << "Enter your choice (1-6): ";
cin >> choice;
if (choice == 1) {
string name, phone;
cout << "Enter name: ";
cin >> name;
cout << "Enter phone number: ";
cin >> phone;
telmanager.add(name, phone);
cout << "Phone number added successfully." << endl;
}
else if (choice == 2) {
string name;
cout << "Enter name: ";
cin >> name;
telmanager.remove(name);
cout << "Phone number removed successfully." << endl;
}
else if (choice == 3) {
string name, phone;
cout << "Enter name: ";
cin >> name;
cout << "Enter new phone number: ";
cin >> phone;
telmanager.update(name, phone);
cout << "Phone number updated successfully." << endl;
}
else if (choice == 4) {
string name;
cout << "Enter name: ";
cin >> name;
string phone = telmanager.search(name);
cout << "Phone number: " << phone << endl;
}
else if (choice == 5) {
telmanager.save("mytels.txt");
cout << "Phone numbers saved successfully." << endl;
}
else if (choice == 6) {
break;
}
else {
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}
```
以上代码中,Mytel类描述单个电话号码,包括名字和电话号码。TelManager类负责管理电话号码,包括从文件中读取电话号码列表、将电话号码列表写入文件、增加电话号码、删除电话号码、修改电话号码、查找电话号码等功能。程序通过菜单方式提供增、删、改、查功能,同时能够将电话号码列表存储到磁盘中。
阅读全文