/*****************************************************
File name:tong_xun_lu.c
Author: dtt
Date:2018-01-12 20:16
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define FALSE 0;
#define TRUE 1;
typedef int ElemType;
typedef int Status;
typedef struct st
{
char num[20];
char name[30];
char telephone[20];
char address[50];
char email[30];
struct st *next;
}Contacts;
void Pos(int x, int y);
Status init(Contacts **head); //初始化
Status Add_tail(Contacts *head); //新建联系人
void Output (Contacts *head); //输出到屏幕
Status Read_file(Contacts *head); //读文件
void Search(Contacts *head); //查找
Status Delete(Contacts *head); //删除
Status update(Contacts *head); //修改联系人信息
void Output_file(Contacts *head); //输出到文件
void clear(Contacts **head); //清空
void Insert_Sort(Contacts *head); //根据学号排序
int main()
{
struct tm *t;
time_t tt;
time(&tt);
t = localtime(&tt); //为了打印出系统当前时间,百度的方法
Contacts *head;
init(&head);
Read_file(head);
int choose;
while(1)
{
printf("\n\n\n\n");
printf(" *********************************************\n");
printf(" * 主菜单 *\n");
printf(" * 输入数字执行操作: *\n");
printf(" * 1:查看联系人 4.更改联系人信息 *\n");
printf(" * 2:新建联系人 5.删除联系人 *\n");
printf(" * 3:查找联系人 0:退出 *\n");
printf(" * *\n");
printf(" * 操作时间:%4d年%02d月%2d日%02d:%02d:%02d *\n",
t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
printf(" *********************************************\n");
printf("\n\n");
scanf("%d",&choose);
switch(choose)
{
case 1:
Insert_Sort(head);
Output(head);
//Output_file(head);
break;
case 2:
Add_tail(head);
Output_file(head);
break;
case 3:
Search(head);
break;
case 4:
update(head);
Output_file(head);
break;
case 5:
Delete(head);
Output_file(head);
break;
case 0:
clear(&head);
break;
default:
printf("输入错误,重新输入!\n");
break;
}
if (choose == 0)
{
break;
}
getchar();
}
return 0;
}
Status init(Contacts **head)
{
Contacts *newContacts = (Contacts *) malloc (sizeof(Contacts));
if (newContacts == NULL)
{
return FALSE;
}
newContacts->next = NULL;
*head = newContacts;
return TRUE;
}
Status Read_file(Contacts *head)
{
int flag;
Contacts *newContacts,*p;
FILE *fp;
p = head;
fp = fopen("/mnt/hgfs/share/tong_xun_lu_a.txt","r");
fscanf(fp,"%d",&flag);
while(flag != 0)
{
p = head;
newContacts = (Contacts *) malloc (sizeof(Contacts));
if (newContacts == NULL)
{
return FALSE;
}
while(p->next)
{
p=p->next;
}
newContacts->next = NULL;
p->next = newContacts;
fscanf(fp,"%s%s%s%s%s",newContacts->num,newContacts->name,newContacts->telephone,
newContacts->address,newContacts->email);
fscanf(fp,"%d",&flag);
}
fclose(fp);
return TRUE;
}
Status Add_tail(Contacts *head)
{
Contacts *p;
Contacts *newContacts = (Contacts *) malloc (sizeof(Contacts));
if (newContacts == NULL)
{
return FALSE;
}
p = head;
while(p->next)
{
p = p->next;
}
printf("\n\n请输入新建联系人的学号,姓名,电话号码,地址,email:\n\n\n\n");
scanf("%s%s%s%s%s",newContacts->num,newContacts->name,newContacts->telephone,newContacts->address,newContacts->email);
newContacts->next = p->next;
p->next = newContacts;
printf("添加联系人成功!\n");
return TRUE;
}
void Search(Contacts *head)
{
int choose,flag = 0;
Contacts *q = head;
char array[50];
while(1)
{
printf(" **********************************************\n");
printf(" * 请选择查找方式: *\n");
printf(" * 1:根据姓名查找 2:根据号码查找 *\n");
printf(" * 0:返回上一级菜单 *\n");
printf(" **********************************************\n");
scanf("%d",&choose);
switch(choose)
{
case 1:
flag = 0;
printf("请输入要查找联系人的姓名:\n\t\t\t");
scanf("%s",array);
while(q->next)
{
if (strcmp(q->next->name,array) == 0)
{
flag = 1;
printf("找到该联系人:\n\n");
printf(" 学号:%s\t 姓名:%s\t 号码:%s\t 地址:%s\t Email:%s\n\n\n",q->next->num,
q->next->name,q->next->telephone,q->next->address,q->next->email);
}
q = q->next;
}
if(flag == 0)
{
printf(" 查无此人!\n");
}
q = head;
break;
case 2:
flag = 0;
printf("请输入要查找联系人的地址:\n\t\t\t");
scanf("%s",array);
while(q->next)
{
if (strcmp(q->next->address,array) == 0)
{
flag = 1;
printf("找到该联系人:\n\n");
printf(" 学号:%s\t 姓名:%s\t 号码:%s\t 地址:%s\t Email:%s\n\n\n",q->next->num,
q->next->name,q->next->telephone,q->next->address,q->next->email);
}
q = q->next;
}
if(flag == 0)
{
printf(" 查无此人!\n");
}
q = head;
break;
case 0:
break;
default:
printf("输入错误,重新输入!\n");
break;
}
if (choose == 0)
{
break;
}
getchar();
}
}
void Output (Contacts *head)
{
int i = 0;
if (head->next == NULL)
{
printf("无联系人信息!\n");
}
else
{
printf("\n\n\n\n\n\n\n");
printf(" 联系人信息:\n\n");
while(head->next)
{
printf(" 学号:%s\t 姓名:%s\t 号码:%s\t 地址:%s\t Email:%s\n"
,head->next->num,head->next->name,head->next->telephone,head->next->address,head->next->email);
head = head->next;
i++;
}
printf("\n\n\n");
}
}
Status update(Contacts *head)
{
int flag = 0;
Contacts *q = head;
Contacts *p = (Contacts *) malloc (sizeof(Contacts));
if (p == NULL)
{
return FALSE;
}
printf("请输入要修改联系人的姓名: ");
scanf("%s",p->name);
while(q->next)
{
if (strcmp(q->next->name,p->name) == 0)
{
flag = 1;
printf("输入要修改的信息:\n\t\t");
scanf("%s%s%s%s%s",q->next->num,q->next->name,q->next->telephone,q->next->address,q->next->email);
printf(" 修改成功!\n");
}
q = q->next;
}
if (flag == 0)
{
printf(" 查无此人!\n");
return FALSE;
}
free(p);
return TRUE;
}
void Output_file(Contacts *head)
{
FILE *fp;
int k = 1;
fp = fopen("/mnt/hgfs/share/tong_xun_lu_a.txt","w+");
while(head->next)
{
fprintf(fp, "%d ",k++); //输出的时候在每个联系人前面输个序号
fprintf(fp,"%s %s %s %s %s\r\n"
,head->next->num,head->next->name,head->next->telephone,head->next->address,head->next->email);
head = head->next;
}
fprintf(fp, "%d\n",0);
fclose(fp);
}
Status Delete(Contacts *head)
{
int flag = 0;
Contacts *p = head;
//Contacts *temp;
char name[50];
printf("请输入要删除联系人的姓名:\n\t\t\t");
scanf("%s",name);
while(p->next)
{
if (strcmp(p->next->name,name) == 0)
{
flag++;
}
p = p->next;
}
if (flag == 0)
{
printf(" 查无此人!\n");
}
if (flag == 1)
{
p = head;
while(p->next)
{
if (strcmp(p->next->name,name) == 0)
{
Contacts *temp = p->next;
p->next = temp->next;
free(temp);
printf("删除成功!\n");
}
else
{
p = p->next;
}
}
}
return TRUE;
}
void clear(Contacts **head)
{
Contacts *temp;
while((*head)->next)
{
temp = (*head)->next;
(*head)->next = (*head)->next->next;
free(temp);
}
free(*head);
}
void Insert_Sort(Contacts *head)
{
Contacts *p;
if (head->next->next == NULL || head->next == NULL)
{
return;
}
Contacts *head2 = head->next->next;
Contacts *q;
head->next->next = NULL;
while(head2)
{
p = head;
q = head2;
head2 = head2->next;
while(p->next)
{
if (strcmp(q->num,p->next->num) > 0)
{
p = p->next;
}
else
break;
}
q->next = p->next;
p->next = q;
}
}
通讯录
最新推荐文章于 2023-07-24 16:30:14 发布