手机通讯录系统问题,大神帮我看下
为什么只有一个联系人的时候会出错,多写几个就没问题了?还有那个删除怎么删除不掉呢?下面是代码:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 80
void menu();
void choice();
void btianjia();
void bshanchu();
void bxiugai();
void bliulan();
void bcall();
void save();
void Read();
classifyfind();
typedef struct tagPERSON
{
int number;
char name[6];
int phone;
char classify;
char email[10];
char birthday[10];
}Person;
typedef struct tagNode
{
Person per; //联系人信息
struct tagNode* pNext; //指向下一个联系人
}Node;
Node *g_pHead = NULL;
Node* Find();
//主函数模块
void main()
{
menu();
}
//退出模块
void tuichu()
{
printf("再见啦!欢迎下次使用本系统.\n");
}
//主菜单模块
void menu()
{
system("cls");
printf("***手机通讯录管理系统***\n");
printf("*请选择: *\n");
printf("*1:添加联系人信息 *\n");
printf("*2:删除联系人信息 *\n");
printf("*3:修改联系人信息 *\n");
printf("*4:按编号查询联系人信 *\n");
printf("*5:按分类查询联系人信 *\n");
printf("*6:浏览所有联系人信息 *\n");
printf("*7:拨号 *\n");
printf("*0:退出 *\n");
printf("************************\n");
choice();
}
//选择模块
void choice()
{
int s;
scanf("%d",&s);
switch(s)
{
case 1:
btianjia();break;
case 2:
bshanchu();break;
case 3:
bxiugai();break;
case 4:
{
Node *pNode = Find();
if (pNode != NULL)
{
printf("编号:%d 姓名:%s 电话号码:%d 分类:%c 电子邮件:%s 生日:%s\n", pNode->per.number, pNode->per.name, pNode->per.phone, pNode->per.classify,pNode->per.email,pNode->per.birthday);
}
}
break;
case 5:
classifyfind();break;
case 6:
bliulan();break;
case 7:
bcall();break;
case 0:
tuichu();break;
default:
menu();
}
}
//添加联系人信息模块
void btianjia()
{
shuru:
printf("\n请输入联系人信息:编号 姓名 电话号码 分类(A:办公类 B:个人类C:商务类)\n电子邮件 生日\n");
Node* p;
p = g_pHead;
while (g_pHead != NULL && p->pNext != NULL)
{
p = p->pNext;
}
//为新的联系人分配一个空间
Node* pNewNode = (Node*)malloc(sizeof(Node));
pNewNode->pNext = NULL;
if (g_pHead == NULL)
{
g_pHead = pNewNode;
p = g_pHead;
}
else
{
p->pNext = pNewNode;//p的下一个节点为pNewNode
}
//输入新的联系人数据
scanf("%d %s %d %c %s %s",&pNewNode->per.number, pNewNode->per.name, &pNewNode->per.phone, &pNewNode->per.classify, &pNewNode->per.email,&pNewNode->per.birthday);
save();
printf("\n数据添加成功....");
printf("是否继续输入:Y/N\n");
getchar();
char a;
qe: scanf("%c",&a);
if(a=='Y'||a=='y')
{
system("cls");
goto shuru;
}
else if(a=='N'||a=='n')
{
printf("操作成功....\n");
system("pause");
system("cls");
menu();
}
else
{
printf("输入错误,请重新输入");
goto qe;
}
}
//信息储存
void save()
{
printf("\n");
FILE *pFile = fopen("contractor.data", "wb");
if (pFile == 0)
{
printf("打开文件失败\n");
return;
}
//写入数据
Node *p;
p = g_pHead;
while (p != NULL)
{
fprintf(pFile, "%d %s %d %c %s %s\n", p->per.number, p->per.name, p->per.phone, p->per.classify,p->per.email,p->per.birthday);
p = p->pNext;
}
printf("数据保存成功");
//关闭文件
fclose(pFile);
}
//信息显示
void Read()
{
//首先删除链表数据,然后从文件中读取数据
Node *p, *p2;
p = p2 = g_pHead;
while (p2 != NULL)
{
//卒个删除链表中的数据
p = p->pNext;
free(p2);
p2 = p;
}
g_pHead = NULL;
//打开文件
FILE *pFile = fopen("contractor.data", "rb");
if (pFile == 0)
{
printf("打开文件失败\n");
return;
}
//读取数据
while (!feof(pFile))
{
//分配空间以存储数据
Node *pTemp = (Node*)malloc(sizeof(Node));
//从文件中读取
fscanf(pFile, "%d %s %d %c %s %s\n", &pTemp->per.number, &pTemp->per.name, &pTemp->per.phone, &pTemp->per.classify,&pTemp->per.email,&pTemp->per.birthday);
//创建链表
if (g_pHead == NULL)
{
g_pHead = pTemp;
p = g_pHead;
}
else
{
p->pNext = pTemp;//p的下一个节点为temp
p = p->pNext;
p->pNext = NULL;
}
}
//关闭文件
fclose(pFile);
}
//查询联系人信息模块
Node* Find()
{
long Num;
printf("请输入要查找的联系人的编号:\n");
scanf("%d", &Num);
//先从文件中读取到链表中
Read();
Node* p;
p = g_pHead;
while (p != NULL)
{
if (p->per.number == Num)
{
printf("编号:%-6d 姓名:%-6s 电话号码:%-8d 分类:%-2c 电子邮件:%-10s 生日:%-10s\n", p->per.number, p->per.name, p->per.phone, p->per.classify,p->per.email,p->per.birthday);
break;
}
p = p->pNext;
}
//遍历完链表也没有找到信息
if (p == NULL)
{
printf("没有该联系人的信息\n");
}
system("pause");
menu();
return 0;
}
//删除联系人信息模块
void bshanchu()
{
int Num;
printf("请输入要删除联系人的编号:\n");
scanf("%d", &Num);
//先把文件中的数据读取到链表
Read();
Node *p, *p2;
p = g_pHead;
//判断是否是头结点
if (g_pHead->per.number == Num )
{
p2 = g_pHead;
g_pHead = g_pHead->pNext;//头指针指向删除元素后的首元素
free(p2);//释放节点
printf("删除成功\n");
system("pause");
menu();
return;
}
//不是头结点
while (p->pNext != NULL)
{
if ( p->pNext->per.number == Num )
{
p2 = p->pNext;
p->pNext = p->pNext->pNext;//删除该节点指针跳过此节点
free(p2);
printf("删除成功\n");
system("pause");
menu();
return;
}
p = p->pNext; //指向下一个节点
if (p->pNext == NULL)
{
//判断是否到了链表的结尾
break;
}
}
if (p->pNext == NULL)
{
printf("没有该联系人信息");
system("pause");
menu();
}
}
//修改联系人信息模块
void bxiugai()
{
int Num;
printf("请输入要修改的信息联系人的编号:\n");
scanf("%d", &Num);
//先把文件中的数据读取到链表中
Read();
Node* p;
p = g_pHead;
while (p != NULL)
{
if (p->per.number == Num)
{
printf("请输入要修改的信息:编号 姓名 电话号码 分类 电子邮件 生日\n");
scanf("%d %s %d %c %s %s", &p->per.number, p->per.name, &p->per.phone,&p->per.classify,p->per.email,p->per.birthday);
save();
printf("修改成功!\n");
break;
}
p = p->pNext;
}
if (p == NULL)
{
printf("没有该联系人信息!\n");
}
menu();
}
//浏览所有联系人信息模块
void bliulan()
{
printf("所有联系人信息如下:\n");
Read();
Node* p;
p = g_pHead;
while (p != NULL)
{
printf("编号:%-6d 姓名:%-6s 电话号码:%-8d 分类:%-2c 电子邮件:%-10s 生日:%-10s\n",
p->per.number,
p->per.name,
p->per.phone,
p->per.classify,
p->per.email,p->per.birthday);
p = p->pNext;
}
system("pause");
menu();
}
void bcall()
{
int Num;
printf("请输入要拨打的电话号码:\n");
scanf("%d", &Num);
//先把文件中的数据读取到链表中
Read();
Node* p;
p = g_pHead;
while (p != NULL)
{
if (p->per.phone == Num)
{
printf("正在拨号中...\n");
for(int a=0;a<30;a++)
printf("\a");
break;
}
p = p->pNext;
}
if (p == NULL)
printf("没有该联系人信息!\n");
system("pause");
menu();
}
//按类别读取联系人
classifyfind()
{
getchar();
printf("请输入你要查询的类别:\n");
printf("A:办公类\n");
printf("B:个人类\n");
printf("C:商务类\n");
char a;
int k=-1;
scanf("%c", &a);
//先从文件中读取到链表中
Read();
Node* p;
p = g_pHead;
while (p != NULL)
{
if (p->per.classify ==a)
{
printf("编号:%-6d 姓名:%-6s 电话号码:%-8d 分类:%-2c 电子邮件:%-10s 生日:%-10s\n", p->per.number, p->per.name, p->per.phone, p->per.classify,p->per.email,p->per.birthday);
k=1;
}
p = p->pNext;
}
//遍历完链表也没有找到信息
if (k == -1)
{
printf("没有该联系人的信息\n");
}
system("pause");
menu();
}