用数组来写通讯录有一个问题就是得事先设定好通讯录的容量。所以,改用链表来储存通讯录,这样节省了内存空间。
#pragma once
#include<cassert>
using namespace std;
typedef struct LNode {
char *name;
char *sex;
char *age;
char *tel_number;
struct LNode *next;
struct LNode(int &len_name, int &len_sex, int &len_age, int &len_tel_number) {
name = new char[len_name+1];
sex = new char[len_sex+1];
age = new char[len_age+1];
tel_number = new char[len_tel_number+1];
next = NULL;
};
}LNode, *pList;
enum DATE { NAME, SEX, AGE, NUMBER };
pList Create(pList node,pList head) {
if (head == NULL) {
head = (LNode *)malloc(sizeof(LNode));
head = node;
}
else {
pList tmp = head;
while (tmp->next)tmp = tmp->next;
tmp->next = (LNode *)malloc(sizeof(LNode));
tmp->next = node;
}
return head;
}
pList Search(const char *name,pList head) {
if (head == NULL) return NULL;
pList tmp = head;
while (tmp->name) {
if (!strcmp(tmp->name, name)) return tmp;
tmp = tmp->next;
}
return NULL;
}
pList Delete(const char *name,pList head) {
assert(head);
pList tmp = head;
if (!strcmp(name, head->name)) {
head = head->next;
delete tmp;
}
else {
while (strcmp(tmp->next->name, name)&&tmp)tmp = tmp->next;
if (!strcmp(tmp->next->name, name)) {
pList p = tmp->next;
tmp->next = tmp->next->next;
delete p;
}
}
return head;
}
void Change_Messenge(pList p,const char *date, DATE d) {
int len = strlen(date);
switch (d)
{
case NAME:
p->name = new char[len + 1];
strcpy(p->name, date);
p->name[len] = 0;
break;
case SEX:
p->sex = new char[len + 1];
strcpy(p->sex, date);
p->sex[len] = 0;
break;
case AGE:
p->age = new char[len + 1];
strcpy(p->age, date);
p->age[len] = 0;
break;
case NUMBER:
p->tel_number = new char[len + 1];
strcpy(p->tel_number, date);
p->tel_number[len] = 0;
break;
default:
break;
}
}
pList Create_Node(const char *name, const char *sex, const char *age, const char *num) {
pList node;
node = (LNode *)malloc(sizeof(LNode));
node->next = NULL;
int len_name = strlen(name);
int len_sex = strlen(sex);
int len_age = strlen(age);
int len_num = strlen(num);
node->name = new char[len_name + 1];
node->sex = new char[len_sex+1];
node->age = new char[len_age+1];
node->tel_number = new char[len_num+1];
strcpy(node->name, name);
strcpy(node->sex, sex);
strcpy(node->age, age);
strcpy(node->tel_number, num);
node->name[len_name]=0;
node->sex[len_sex]=0;
node->age [len_age]=0;
node->tel_number[len_num]=0;
return node;
}
void Destroy(pList head) {
assert(head);
while (head) {
pList tmp = head;
head = head->next;
delete tmp;
}
}
#include<iostream>
#include"list.h"
#include<string>
pList push_date(pList book) { //往通讯录中添加新成员
pList node;
node = (LNode *)malloc(sizeof(LNode));
node->next = NULL;
cout << "请输入名字:";
string name;
cin >> name;
cout << "请输入性别:";
string sex;
cin >> sex;
cout << "请输入年龄:";
string age;
cin >> age;
cout << "请输入号码:";
string num;
cin >> num;
const char *c_n = name.c_str();
const char *c_s = sex.c_str();
const char *c_a = age.c_str();
const char *c_num = num.c_str();
node=Create_Node(c_n, c_s, c_a, c_num);
book=Create(node, book);
return book;
}
void traverse_book(pList p) { //遍历通讯录
if (!p) cout << "这是一个空的通讯录" << endl;
else {
while (p) {
cout << "---------------" << endl;
cout << "姓名:" << p->name << endl;
cout << "性别:" << p->sex << endl;
cout << "年龄:" << p->age << endl;
cout << "号码:" << p->tel_number << endl;
cout << "---------------" << endl;
p = p->next;
}
}
}
int choice1() { //修改要修改的信息
int choice;
cout << "---------------------------" << endl;
cout << "请输入你需要修改的数据:" << endl;
cout << "0.退出" << endl;
cout << "1.改名字" << endl;
cout << "2.改性别" << endl;
cout << "3.改年龄" << endl;
cout << "4.改号码" << endl;
cout << "---------------------------" << endl;
cout << "您的选择:";
cin >> choice;
return choice;
}
int choice2() { //选择对通讯录进行的操作
int choice;
cout << "---------------------------" << endl;
cout << "请输入你需要对通讯录进行的操作:" << endl;
cout << "0.退出" << endl;
cout << "1.插入" << endl;
cout << "2.查找" << endl;
cout << "3.修改" << endl;
cout << "4.删除" << endl;
cout << "5.显示整个通讯录" << endl;
cout << "---------------------------" << endl;
cout << "您的选择:";
cin >> choice;
return choice;
}
pList Read_file(pList head) { //读取存储在文件里面的信息
char line[1024];
int len;
FILE *f;
f = fopen("通讯录.txt", "r");
int i = 0;
pList node;
while (fgets(line, 1024, f)) {
if (i == 0) {
node = (LNode *)malloc(sizeof(LNode));
node->next = NULL;
}
len = strlen(line);
line[len-1] = 0;
if (i == 0) {
node->name = new char[len];
strcpy(node->name, line);
}
else if (i == 1) {
node->sex = new char[len];
strcpy(node->sex, line);
}
else if (i == 2) {
node->age = new char[len];
strcpy(node->age, line);
}
else if (i == 3) {
node->tel_number = new char[len];
strcpy(node->tel_number, line);
}
++i;
if (!(i % 4)) { head=Create(node, head); }
i = i % 4;
}
fclose(f);
return head;
}
void Save_file(pList book) { //保持信息
pList p = book;
FILE *f;
f = fopen("通讯录.txt", "w");
while (p) {
fputs(p->name, f);
fputs("\n", f);
fputs(p->sex, f);
fputs("\n", f);
fputs(p->age, f);
fputs("\n", f);
fputs(p->tel_number, f);
if(p->next)fputs("\n", f);
p = p->next;
}
fclose(f);
}
void test() { //测试
pList book;
book = NULL;
book=Read_file(book);
while (1) {
int choice = choice2();
switch (choice) {
case 0: {
Save_file(book);
Destroy(book);
return;
}
case 1: {
book=push_date(book);
cout << "成功" << endl;
}break;
case 2: {
pList pos;
string name;
cout << "请输入你要查找人的名字:";
cin >> name;
const char *p = name.c_str();
pos = Search(p, book);
cout << "---------------" << endl;
cout << "姓名:" << pos->name << endl;
cout << "性别:" << pos->sex << endl;
cout << "年龄:" << pos->age << endl;
cout << "号码:" << pos->tel_number << endl;
}break;
case 3: {
pList pos;
string name;
cout << "请输入你要查找人的名字:";
cin >> name;
const char *p = name.c_str();
pos = Search(p, book);
DATE date;
switch (choice1()){
case 1:date = NAME; break;
case 2:date = SEX; break;
case 3:date = AGE; break;
case 4:date = NUMBER; break;
default:
break;
}
string str;
cout << "请输入要修改的内容:";
cin >> str;
const char *p1 = str.c_str();
Change_Messenge(pos, p1, date);
cout << "修改成功" << endl;
}break;
case 4: {
pList pos;
string name;
cout << "请输入你要查找人的名字:";
cin >> name;
const char *p = name.c_str();
book=Delete(p, book);
}break;
case 5: {
traverse_book(book);
}break;
}
}
}
void main() {
test();
system("pause");
}
转载请注明原文地址: https://ju.6miu.com/read-12423.html