C++通信录系统

通讯录主要功能
1.实现插入
用ofstream of(“xxx”,ios::out|ios::app); 就行 其中ios::out是写入模式,ios::app是在尾部写入
ios::in是以输入模式打开 ios::trunc重置文本内容,ios::binary是用二进制方式打开文件
ios::ate是将文件指针偏移末尾
2.查找
用C字符串的指针偏移就行
3.搜索不合格的电话然后加上“”
用字符串的迭代器偏移用length计数
4.删除
我之前是打算用seekg偏移来做的,但是用偏移的话不好删除有点麻烦
偏移有三个位置一个是ios::beg偏移到头,第二个是ios::cur偏移到当前,
第三个ios::end偏移到末尾
但是我是用map字典做的,我的方法是一次性都读到字典,然后在字典删除,在写入文本
欢迎各位大佬推进下更好的方法
上代码:

#include <iostream>
#include <string>
#include <string_view>
#include <sstream>
#include <iterator>
#include <fstream>
#include <map>
#include <vector>
#include <stack>
using namespace std;
template<class T>
class Date {
public:
	Date() {
		length = 0;
		char a;
		cout << "1.录入,2.查找,3.电话是否合法,4.删除联系人 ";
		a = cin.get();
		cin.ignore();// 必须要清空上次输入不然下面读不了
		if (a == '1') {
			cout << "输入姓名和电话号码" << endl;
			cin.getline(s, 256, '\n');
			data = s;
			plus();
		}
		else if (a == '2') {
			find();
		}
		else if (a == '3') {
			sceon();
		}
		else if (a == '4') {
			pop();
		}
	};
	~Date() { data="", length=0, memset(s, 0, 256); };
	void plus()noexcept(false);
	void find()noexcept(false);
	void sceon()noexcept(false);
	void pop()noexcept(false);
	auto Map();
	T getdata() { return data; }
private:
	T data;
	char s[256];
	int length;
};
template<class T>
void Date<T>::plus() {
	ofstream os("text.txt", ios::out | ios::app);
	try {	
		if (!os.good())
			throw "text.txt";
	}
	catch (string& s) {
		os.open(s, ios::out | ios::app);
	}
	os << data<<'\n';
	os.close();
}
template<class T>
void Date<T>::find() {
	ifstream IF("text.txt", ios::in);
	string s;
	try {
		if (!IF.good())
			throw "text.txt";
	}
	catch (string& s) {
		IF.open(s, ios::in);
	}
	cout << "输入查找人的性或名" << endl;
	char ch[256];
	cin.getline(ch,256,'\n');
	bool isIF = true;
	while (IF) {
		if (isIF) 
			IF >> s;
		const char* name = s.c_str(); 
		A:
		if(*ch==*name){
			cout << s << ":";
			IF >> s ;
			cout << s << endl;
			continue;
		}
		else if (*name) {
			name+=2;
			isIF = false;
			goto A;
		}
		isIF = true;
		if (!IF) {
			cout << "通讯列表没有更多人" << endl;
			break;
		}
	}
	IF.close();
}
template<class T>
void Date<T>::sceon() {
	ifstream IF("text.txt", ios::in);
	ofstream OF;
	ostringstream os;
	try {
		if (!IF.good())
			throw "text.txt";
	}
	catch (string& s) {
		IF.open(s, ios::in);
	}
	while (IF.getline(s, 300,'\n')) {
		for (auto i = begin(s);i!=end(s);i++) {
			if (*i >= '0' && *i <= '9') {
				length++;
			}
			else if (!*i) {
				break;
			}
		}
		if (length != 11) {
			os << '"' << s << '"';
			OF.open("text.txt", ios::out | ios::app);
			OF << '\n' << s << '\n';
			OF.close();
			cout << s <<"电话号不正确" << endl;
		}
		length = 0;
	}
	IF.close();
}
template<class T>
void Date<T>::pop() {
	ofstream OF;
	string name;
	string ss;
	map<string,string> m;
	char str[256];
	m = Map();
	cout << "输入要删除的联系人" << endl;
	cin >> name;
	auto key = m.find(name);ios::cu
	if (key != m.end()) {
		m.erase(key);
	}
	OF.open("text.txt", ios::out | ios::trunc);
	try {
		if (!OF.good())
			throw "text.txt";
	}
	catch (string& s) {
		OF.open(s, ios::out|ios::trunc);
	}
	for (const auto [name, id] : m) {
		OF << name << ' ' << id << '\n';
	}
	OF.close();
}
template <class T>
auto Date<T>::Map() {
	ifstream IF("text.txt", ios::in);
	try {
		if (!IF.good())
			throw "text.txt";
	}
	catch (string& s) {
		IF.open(s, ios::in);
	}
	char str[256];
	string name;
	string id;
	map<string, string>m;
	while (IF) {
		IF >> name;
		IF >> id;
		m[name] = id;
	}
	return m;
}
void Stack() {
	stack<string>sta;
	char str[256];
	ifstream IF("text.txt", ios::in);
	while (IF.getline(str, 256, '\n')) { // 先进后出
		sta.push(str);
	}
	while (!sta.empty()) {
		cout << sta.top() << endl;
		sta.pop();
	}
}
int main() {
	while (true)
	Date<string>a;
	while (true);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值