#include <iostream>
using namespace std;
/* 声明 */
struct PersionInfo;
bool is_number(string str);
void showTip();
PersionInfo* endP(PersionInfo* head);
void add_persionInfo(PersionInfo* head);
void show_persionInfo(PersionInfo* head);
/*定义结构体*/
struct PersionInfo
{
int num;
string name;
PersionInfo* next;
};
void showTip() {
cout << "*******************" << endl;
cout << "*******************" << endl;
cout << "请选择功能" << endl;
cout << "1、添加联系人" << endl;
cout << "2、显示联系人" << endl;
cout << "3、删除联系人" << endl;
cout << "4、查找联系人" << endl;
cout << "5、修改联系人" << endl;
cout << "6、清空联系人" << endl;
cout << "7、退出通讯录" << endl;
cout << "*******************" << endl;
cout << "*******************" << endl;
}
bool is_number(string str)
{
if (str.c_str()[0] != 45)
{
for (int i = 0; i < str.length(); i++)
{
if (str.c_str()[i] < '0' || str.c_str()[i] > '9')
{
return false;
}
}
return true;
}
else
{
for (int i = 1; i < str.length(); i++)
{
if (str.c_str()[i] < '0' || str.c_str()[i] > '9')
{
return false;
}
}
return true;
}
}
void show_persionInfo(PersionInfo* head) {
PersionInfo* q = head->next;
PersionInfo* p;
if (q == NULL) {
cout <<"无联系人" << endl;
}
while (q != NULL)
{
cout << q->name <<"--------->"<<q->num << endl;
p = q;
q = q->next;
if (q == NULL) {
cout<<"尾节点为:"<<p->name << endl;
}
}
}
/*获取链表的尾结点*/
PersionInfo* endP(PersionInfo* head) {
PersionInfo* q = head->next;
if (q == NULL) {
return head;
}
PersionInfo* p;
while (q != NULL)
{
p = q;
q = q->next;
if (q == NULL) {
cout <<"获取尾节点!!!" << endl;
return p;
}
}
}
void add_persionInfo(PersionInfo* head) {
PersionInfo* p = NULL;
PersionInfo* q;
int num = 0;
string name = "xxx";
string input;
q = endP(head);
cout << "请输入号码" << endl;
cin >> input;
while (input != "-1")
{
if (is_number(input)) {
num = atoi(input.c_str());
cout << "请输入姓名" << endl;
cin >> name;
p = new PersionInfo;
p->num = num;
p->name = name;
p->next = NULL;
q->next = p;
q = q->next;
}
else {
cout << "号码请输入数字!" << endl;
}
cout << "请输入号码" << endl;
cin >> input;
}
}
void find_PersionInfo(PersionInfo* head, int findnum) {
PersionInfo* q = head->next;
while ( q != NULL )
{
if (q->num == findnum) {
cout<<"查询到:" << endl;
cout << q->name << "--------->" << q->num << endl;
break;
}
else {
q = q->next;
}
}
};
void changNum_PersionInfo
(PersionInfo* head, int findnum) {
PersionInfo* q = head->next;
while (q != NULL)
{
if (q->num == findnum) {
cout << "请输入想要修改的数据:" << endl;
cin >> q->num;
cin >> q->name;
cout << q->name << "--------->" << q->num << endl;
break;
}
else {
q = q->next;
}
}
};
int main()
{
showTip();
PersionInfo* head = NULL;
head = new PersionInfo;
head->next = NULL;
int sel = 0;
cin >> sel;
while ( sel != 7 ) {
if (sel == 1) {
add_persionInfo( head );
}
else if (sel == 2) {
show_persionInfo(head);
}
else if (sel == 3) {
}
else if (sel == 4) {
string findnum = "";
cin >> findnum;
if (is_number(findnum)) {
find_PersionInfo(head, atoi(findnum.c_str()));
}
else
{
cout << "号码请输入数字!" << endl;
}
}
else if (sel == 5) {
string changNum = "";
cin >> changNum;
if (is_number(changNum)) {
changNum_PersionInfo(head, atoi(changNum.c_str()));
}
else
{
cout << "号码请输入数字!" << endl;
}
}
else if (sel == 6) {
head = NULL;
head = new PersionInfo;
head->next = NULL;
}
else if (sel == 2) {
}
showTip();
cin >> sel;
}
}
2022-7-20-----C++------用链表实现通讯录功能
最新推荐文章于 2024-01-21 00:08:52 发布