之前学习C语言的时候,用链表实现过通讯录的基本功能。最近写了一个C++版本的通讯录,参考代码如下所示。
main.cpp
/***************************************************** Copyright (C): 2017-2018 File name : main.cpp Author : Zhengqijun Date : 2017年02月12日 星期日 16时47分52秒 Description : 主函数 Funcion List : main() *****************************************************/ #include "../../include/head.h" personMessage pep; vector<personMessage> person; vector<personMessage>::iterator it; int main() { //personMessage pep; //vector<personMessage> person; char ch = 0; //system("clear"); while(ch != 'q') { if((ch != 'a') && (ch != 'c') && (ch != 'd') && (ch != 'f')) { system("clear"); ch = book_ui(); } switch(ch) { case 'a': { ch = add_person(); break; } case 'c': { ch = change_person(); break; } case 'd': { ch = delete_person(); break; } case 'e': { ch = display_person(); break; } case 'f': { ch = find_person(); break; } case 'q': { cout << "Byebye!" << endl; return 0; break; } default: { cout << "input error!" << endl; break; } } } return 0; }
head.h
/***************************************************** Copyright (C): 2017-2018 File name : head.h Author : Zhengqijun Date : 2017年02月12日 星期日 17时11分29秒 Description : Funcion List : *****************************************************/ #ifndef __HEAD_H__ #define __HEAD_H__ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stdio.h> #include <string.h> using namespace std; class personMessage { public: personMessage(); personMessage(string s); ~personMessage(); personMessage& operator=(string s); personMessage& operator=(personMessage& other); /* sort排序算法需要重载'<',注意加const! */ bool operator<(const personMessage& p) const; bool operator>(const personMessage& p) const; bool operator<=(const personMessage& p) const; bool operator>=(const personMessage& p) const; bool operator==(string s); friend istream& operator>>(istream& in, personMessage& p); friend ostream& operator<<(ostream& out, personMessage& p); int selectFlag; //用来选择哪一个私有成员! private: string name_; string addr_; string phone_; }; extern personMessage pep; extern vector<personMessage> person; extern vector<personMessage>::iterator it; extern char book_ui(); extern char add_person(); extern char change_person(); extern char delete_person(); extern char display_person(); extern char find_person(); #endif
book.cpp
/***************************************************** Copyright (C): 2017-2018 File name : book.cpp Author : Zhengqijun Date : 2017年02月12日 星期日 18时53分19秒 Description : Funcion List : *****************************************************/ #include "../../include/head.h" personMessage::personMessage() : selectFlag(0) { cout << "default coonstructor!" << endl; } personMessage::personMessage(string s) { name_ = s; } personMessage::~personMessage() { cout << "destroy person message!" << endl; } #if 1 personMessage& personMessage::operator=(string s) { name_ = s; return *this; } #endif personMessage& personMessage::operator=(personMessage& other) { if(this == &other) { return *this; } name_ = other.name_; addr_ = other.addr_; phone_ = other.phone_; return *this; } bool personMessage::operator>(const personMessage& p) const { return name_ > p.name_; } bool personMessage::operator>=(const personMessage& p) const { return name_ >= p.name_; } bool personMessage::operator<(const personMessage& p) const { return name_ < p.name_; } bool personMessage::operator<=(const personMessage& p) const { return name_ <= p.name_; } bool personMessage::operator==(string s) { if(selectFlag == 1) { return name_ == s; } else if(selectFlag == 2) { return addr_ == s; } else if(selectFlag == 3) { return phone_ == s; } else { return false; } } #if 1 istream& operator>>(istream& in, personMessage& p) { string name; string addr; string phone; cout << "请输入新的成员免费精选名字大全:" << endl; in >> name; p.name_ = name; cout << "请输入新的成员地址:" << endl; in >> addr; p.addr_ = addr; cout << "请输入新的成员电话:" << endl; in >> phone; p.phone_ = phone; return in; } ostream& operator<<(ostream& out, personMessage& p) { out << "免费精选名字大全: " << p.name_ << endl; out << "地址: " << p.addr_ << endl; out << "电话: " << p.phone_ << endl; return out; } #endif
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/483376.html