原程序:
#include<bits/stdc++.h>
using namespace std;
typedef struct Lnode
{
int num;
string name;
string telephone;
int searchTime;
struct Lnode* next;
}Lnode, *Linklist;
void insertSequential(Linklist &head);
int str_to_int(char* s)
{
int i = 0;
int result = 0;
while(s[i])
{
result =result*10 + (s[i]-'0');
i++;
}
return result;
}
void creatIncerLInk(Linklist &head)
{ Linklist q = head;
int way;
cout<<"******************请建立通讯录******************"<<endl;
cout<<"* 1 从本地文件中读取通讯录 *"<<endl;
cout<<"* 2 手动建立通讯录 *"<<endl;
cout<<"* 0 这次先不建立通讯录啦 *"<<endl;
cout<<"************************************************"<<endl;
cout <<"请选择:"<<endl;
cin >>way;
switch(way)
{
case 1:
{
FILE*fptr = fopen("D:/tongxunlu/mytxt.txt", "r");
if(!fptr)
{
perror("fail");
}
char *s, *temp, *savestr;
while(fgets(s, 50, fptr))
{
Linklist p = new Lnode;
temp = strtok(s, " ");
p->num = str_to_int(temp);
temp = strtok(NULL, " ");
p->name = temp;
temp = strtok(NULL, " ");
p->telephone = temp;
p->searchTime = 0;
q->next = p;
q = q->next;
}
q->next = NULL;
return;
}
case 2:
{
int n;
cout <<" 请输入你要建立的通讯录个数"<<endl;
cin >> n;
while(n--)
insertSequential(head);
return;
case 0:
return;
}
default:
cout <<"没有此选项"<<endl;
return;
}
}
void changeNode(Linklist &p, Linklist &q)
{
int tNum, tTime,searchTime;
string tName, tTelephone;
tNum = p->num;
tTime = p->searchTime;
tName = p->name;
tTelephone = p->telephone;
p->num = q->num;
p->searchTime = q->searchTime;
p->name = q->name;
p->telephone = q->telephone;
p->num = tNum;
p->searchTime = searchTime;
p->name = tName;
p->telephone = tTelephone;
}
void sortBytime(Linklist &head)
{
Linklist p = head->next;
Linklist q = p;
while(p->next)
{
while(q&&q->next!=p)
{
if(q->searchTime>q->next->searchTime)
changeNode(q, q->next);
}
}
}
void searchName(Linklist &head)
{
cout << "请输入要查询的姓名:"<<endl;
string hisName;
cin >>hisName;
Linklist p = head->next;
while(p)
{
if(p->name == hisName)
{
break;
}
}
if(!p)
{
cout << "不存在该联系人!"<<endl;
}
else
{
cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
p->searchTime++;
}
}
void searchNum(Linklist &head)
{
cout << "请输入要查询的学号:"<<endl;
int hisNum;
cin >>hisNum;
Linklist p = head->next;
while(p)
{
if(p->num == hisNum)
{
break;
}
}
if(!p)
{
cout << "不存在该联系人!"<<endl;
}
else
{
cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
p->searchTime++;
}
}
void search_friend(Linklist &head)
{
int way;
cout<<"*****************请选择查询方法******************"<<endl;
cout<<"* 1 按姓名查询 *"<<endl;
cout<<"* 2 按学号查询 *"<<endl;
cout<<"* 0 这次先不查啦 *"<<endl;
cout<<"************************************************"<<endl;
cin >>way;
switch(way)
{
case 1:
{
searchName(head);
// sortBytime(head);
break;
}
case 2:
{
searchNum(head);
// sortBytime(head);
break;
}
case 0:
break;
}
}
void deleteElem(Linklist &head)
{
int i;
cout <<" 请输入删除的序号:"<<endl;
cin >> i;
linklist p = head;
for(int t = 1; t < i&&p; t++)
p = p->next;
if(!p)
cout << "不存在第"<<i<<"个节点"<<endl;
else
{
p->next = p->next->next;
}
}
void delNum(Linklist &head)
{
int num;
cout <<" 请输入要删除的联系人学号:"<<endl;
cin >> num;
Linklist p = head;
while(p->next->num!=num && p)
{
p = p->next;
}
if(!p)
cout <<"不存在此联系人"<<endl;
else
p->next = p->next->next;
}
void delName(Linklist &head)
{
int name;
cout <<" 请输入要删除的联系人姓名:"<<endl;
cin >> name;
Linklist p = head;
while(p->next->name!=name && p)
{
p = p->next;
}
if(!p)
cout <<"不存在此联系人"<<endl;
else
p->next = p->next->next;
}
void delnode(Linklist &head)
{
int way;
cout<<"*****************请选择删除方法******************"<<endl;
cout<<"* 1 按序号删除 *"<<endl;
cout<<"* 2 按学号删除 *"<<endl;
out<<"* 3 按姓名删除 *"<<endl;
cout<<"* 0 这次先不删除啦 *"<<endl;
cout<<"************************************************"<<endl;
cin >> way;
switch(way)
{
case 1:
deleteElem(head);
break;
case 2:
delNum(head);
break;
case 3:
delName(head);
break;
case 0:
break;
}
}
void insertSequential(Linklist &head)//新插入的节点放到最前面
{
cout <<"请输入要加入通讯录的id, 名字和电话号码"<<endl;
Linklist q = new Lnode;
cin >> q->num >>q->name>>q->telephone;
q->searchTime = 0;
q->next = head->next;
head->next = q;
}
void printList(Linklist &head)
{
Linklist p = head->next;
while(p)
{
cout << p->num<<" "<<p->name<<" "<<p->telephone<<endl;
p = p->next;
}
}
int main()
{
Linklist head = new Lnode;
head->next = NULL;
int menu = 1;
while(menu)
{
printf("\n ***************** ^@^欢迎使用通讯录系统***********\n");
printf(" * 1 通讯录的建立 *\n");
printf(" * 2 插入通讯记录 *\n");
printf(" * 3 查询通讯记录 *\n");
printf(" * 4 删除通讯记录 *\n");
printf(" * 5 显示通讯录信息 *\n");
printf(" * 0 退出管理系统 *\n");
printf(" **************** ^@^欢迎使用通讯录系统************\n");
cout <<" 请选择0-5:"<<endl;
cin>>menu;
switch(menu)
{
case 1:
{
creatIncerLInk(head);
break;
}
case 2:
insertSequential(head);
break;
case 3:
search_friend(head);
break;
case 4:
delnode(head);
break;
case 5:
printList(head);
break;
}
}
}
问题:
1.查找操作后一直循环;
2删除操作不显示
解决:
1.while循环没加p==->next
2. Linklist写成linklist
修改后:
#include<bits/stdc++.h>
using namespace std;
typedef struct Lnode
{
int num;
string name;
string telephone;
int searchTime;
struct Lnode* next;
}Lnode, *Linklist;
void insertSequential(Linklist &head);
int str_to_int(char* s)
{
int i = 0;
int result = 0;
while(s[i])
{
result =result*10 + (s[i]-'0');
i++;
}
return result;
}
void creatIncerLInk(Linklist &head)
{ Linklist q = head;
int way;
cout<<"******************请建立通讯录******************"<<endl;
cout<<"* 1 从本地文件中读取通讯录 *"<<endl;
cout<<"* 2 手动建立通讯录 *"<<endl;
cout<<"* 0 这次先不建立通讯录啦 *"<<endl;
cout<<"************************************************"<<endl;
cout <<"请选择:"<<endl;
cin >>way;
switch(way)
{
case 1:
{
FILE*fptr = fopen("D:/tongxunlu/mytxtin.txt", "r");
if(!fptr)
{
perror("fail");
}
char *s, *temp, *savestr;
while(fgets(s, 50, fptr))
{
char *find;
find = strchr(s, '\n');
if(find)
*find = '\0';
Linklist p = new Lnode;
temp = strtok(s, " ");
p->num = str_to_int(temp);
temp = strtok(NULL, " ");
p->name = temp;
temp = strtok(NULL, " ");
p->telephone = temp;
p->searchTime = 0;
q->next = p;
q = q->next;
}
q->next = NULL;
return;
}
case 2:
{
int n;
cout <<" 请输入你要建立的通讯录个数"<<endl;
cin >> n;
while(n--)
insertSequential(head);
return;
case 0:
return;
}
default:
cout <<"没有此选项"<<endl;
return;
}
}
void changeNode(Linklist &p, Linklist &q)
{
int tNum, tTime,searchTime;
string tName, tTelephone;
tNum = p->num;
tTime = p->searchTime;
tName = p->name;
tTelephone = p->telephone;
p->num = q->num;
p->searchTime = q->searchTime;
p->name = q->name;
p->telephone = q->telephone;
q->num = tNum;
q->searchTime = searchTime;
q->name = tName;
q->telephone = tTelephone;
}
void sortBytime(Linklist &head)
{
for(Linklist p = head->next; p->next != NULL; p = p->next)
for(Linklist q = head->next; q->next != NULL; q = q->next)
{
if(q->searchTime<q->next->searchTime)
changeNode(q, q->next);
}
}
void searchName(Linklist &head)
{
cout << "请输入要查询的姓名:"<<endl;
string hisName;
cin >>hisName;
Linklist p = head->next;
while(p)
{
if(p->name == hisName)
{
break;
}
p=p->next;
}
if(!p)
{
cout << "不存在该联系人!"<<endl;
}
else
{
cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
p->searchTime=p->searchTime+1;
}
}
void searchNum(Linklist &head)
{
cout << "请输入要查询的学号:"<<endl;
int hisNum;
cin >>hisNum;
Linklist p = head->next;
while(p)
{
if(p->num == hisNum)
{
break;
}
p=p->next;
}
if(!p)
{
cout << "不存在该联系人!"<<endl;
}
else
{
cout <<"num:"<<p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
p->searchTime=p->searchTime+1;
}
}
void search_friend(Linklist &head)
{
int way;
cout<<"*****************请选择查询方法******************"<<endl;
cout<<"* 1 按姓名查询 *"<<endl;
cout<<"* 2 按学号查询 *"<<endl;
cout<<"* 0 这次先不查啦 *"<<endl;
cout<<"************************************************"<<endl;
cin >>way;
switch(way)
{
case 1:
{
searchName(head);
// sortBytime(head);
break;
}
case 2:
{
searchNum(head);
// sortBytime(head);
break;
}
case 0:
break;
}
}
void deleteElem(Linklist &head)
{
int i;
cout <<" 请输入删除的序号:"<<endl;
cin >> i;
Linklist p = head;
for(int t = 1; t < i&&p; t++)
p = p->next;
if(!p)
cout << "不存在第"<<i<<"个节点"<<endl;
else
{
p->next = p->next->next;
}
}
void delNum(Linklist &head)
{
int num;
cout <<" 请输入要删除的联系人学号:"<<endl;
cin >> num;
Linklist p = head;
while(p->next->num!=num && p)
{
p = p->next;
}
if(!p)
cout <<"不存在此联系人"<<endl;
else
p->next = p->next->next;
}
void delName(Linklist &head)
{
string name;
cout <<" 请输入要删除的联系人姓名:"<<endl;
cin >> name;
Linklist p = head;
while(p->next->name!=name && p)
{
p = p->next;
}
if(!p)
cout <<"不存在此联系人"<<endl;
else
p->next = p->next->next;
}
void delnode(Linklist &head)
{
int way;
cout<<"*****************请选择删除方法******************"<<endl;
cout<<"* 1 按序号删除 *"<<endl;
cout<<"* 2 按学号删除 *"<<endl;
cout<<"* 3 按姓名删除 *"<<endl;
cout<<"* 0 这次先不删除啦 *"<<endl;
cout<<"************************************************"<<endl;
cin >> way;
switch(way)
{
case 1:
deleteElem(head);
break;
case 2:
delNum(head);
break;
case 3:
delName(head);
break;
case 0:
break;
}
}
void insertSequential(Linklist &head)//新插入的节点放到最前面
{
cout <<"请输入要加入通讯录的id, 名字和电话号码"<<endl;
Linklist q = new Lnode;
cin >> q->num >>q->name>>q->telephone;
q->searchTime = 0;
q->next = head->next;
head->next = q;
}
void printList(Linklist &head)
{
Linklist p = head->next;
while(p)
{
cout <<"num:"<< p->num<<" "<<"name:"<<p->name<<" "<<"telephone:"<<p->telephone<<endl;
p = p->next;
}
}
void toMytxt(Linklist &head)
{
ofstream fpto;
fpto.open("D:/tongxunlu/mytxtout.txt", ios::out);
Linklist p = head->next;
while(p)
{
fpto<<p->num<<" "<<p->name<<" "<<p->telephone<<endl;
p = p->next;
}
fpto.close();
}
int main()
{
Linklist head = new Lnode;
head->next = NULL;
int menu = 1;
while(menu)
{
printf("\n ***************** ^@^欢迎使用通讯录系统***********\n");
printf(" * 1 通讯录的建立 *\n");
printf(" * 2 插入通讯记录 *\n");
printf(" * 3 查询通讯记录 *\n");
printf(" * 4 删除通讯记录 *\n");
printf(" * 5 显示通讯录信息 *\n");
printf(" * 6 将通讯录写入我的文件 *\n");
printf(" * 0 退出管理系统 *\n");
printf(" **************** ^@^欢迎使用通讯录系统************\n");
cout <<" 请选择0-6:"<<endl;
cin>>menu;
switch(menu)
{
case 1:
{
creatIncerLInk(head);
break;
}
case 2:
insertSequential(head);
break;
case 3:
{
search_friend(head);
sortBytime(head);
break;
}
case 4:
delnode(head);
break;
case 5:
printList(head);
break;
case 6:
toMytxt(head);
break;
default:
exit(0);
break;
}
}
}
另外,文件读取操作是这样的:
按行读:
FILE*fptr = fopen("D:/tongxunlu/mytxtin.txt", "r");
if(!fptr)
{
perror("fail");
}
char *s;
while(fgets(s, 50, fptr)
ifstream也可以用来读取(c++)
写入文件:
ofstream fpto;
fpto.open("D:/tongxunlu/mytxtout.txt", ios::out);
Linklist p = head->next;
while(p)
{
fpto<<p->num<<" "<<p->name<<" "<<p->telephone<<endl;
p = p->next;
}
fpto.close();