通讯录说明文档(一)
语言:c语言
格式: 编号 姓 名 住址 电话
201701 *** xxxx 183****5668
要求:使用结构体形式对数据存储
功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
语言:c语言
格式: 编号 姓 名 住址 电话
201701 *** xxxx 183****5668
要求:使用结构体形式对数据存储
功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
注意事项:在增、删、改、查过程中,如果姓名相同怎么进行选择操作。
头文件
/*****************************************************
> File name: student.h
> Author: Mr.YUAN
> 日期: 2017-12-04 16:14
*****************************************************/
#ifndef STUDENT_H_
#define STUDENT_H_
#define success 1001
#define failure 1002
struct student
{
char num[20];
char name[20];
char phone[20];
char tel[20];
char addr[20];
struct student *next;
};
typedef struct student stu;
typedef stu *Stu;
int list_init(Stu *L);
int menu();
int list_insert(Stu *L);
int list_traverse(Stu L);
int modify_list(Stu L);
int delete_list(Stu L);
int search_list(Stu L);
#endif
接口函数
/*****************************************************
> File name: Student.c
> Author: Mr.YUAN
> 日期: 2017-12-04 16:14
*****************************************************/
#include <stdio.h>
#include"student.h"
#include<string.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int list_init(Stu *L)
{
(*L) = (Stu )malloc(sizeof(stu));
if(NULL == (*L))
return failure;
(*L)->next == NULL;
return success;
}
int menu()
{
int choice;
int ret;
printf("====================================================\n");
printf("| |\n");
printf("|-------------Welcome to telephone system----------|\n");
printf("| |\n");
printf("|==================================================|\n");
printf("| 1.input information | 2.show information |\n");
printf("| 3.modify information| 4.delete information |\n");
printf("| 5.search information| 6.exit system |\n");
printf("|==================================================|\n");
printf("| please choose :..... |\n");
printf("|--------------------------------------------------|\n");
ret = scanf("%d",&choice);
while(ret != 1 ||(choice != 1 && choice != 2 && choice != 3 && choice != 4 &&
choice != 5 && choice != 6 && choice != 7))
{
printf("enter wrong,please enter again!\n");
scanf("%d",&choice);
}
if(choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 ||
choice == 6 || choice == 7)
return choice;
}
int list_insert(Stu *L)
{
Stu p = (Stu )malloc(sizeof(stu));
Stu q = *L;
Stu s = q;
printf("please input num:...\n");
scanf("%s",p->num);
while(q->next != NULL)
{
if(strcmp(p->num,q->num) == 0)
{
printf("num duplication,please input again!\n");
scanf("%s",p->num);
}
q = q->next;
}
q = (*L);
printf("please input name:...\n");
scanf("%s",p->name);
while(q->next != NULL)
{
if(strcmp(p->name,q->name) == 0)
{
printf("name duplication,please input again!\n");
scanf("%s",p->name);
}
q = q->next;
}
q = (*L);
printf("please input phone:...\n");
scanf("%s",p->phone);
while(strlen(p->phone) != 11)
{
printf("input wrong,please input again!\n");
scanf("%s",p->phone);
}
while(q->next != NULL)
{
if(strcmp(p->phone,q->phone) == 0)
{
printf("phone duplication,please input again!\n");
scanf("%s",p->phone);
}
q = q->next;
}
q = (*L);
printf("please input tel:...\n");
scanf("%s",p->tel);
while(strlen(p->tel) != 8)
{
printf("input wrong,please input again!\n");
scanf("%s",p->tel);
}
while(q->next != NULL)
{
if(strcmp(p->tel,q->tel) == 0)
{
printf("tel duplication,please input again!\n");
scanf("%s",p->tel);
}
q = q->next;
}
q = (*L);
printf("please input addr:...\n");
scanf("%s",p->addr);
while(q->next != NULL)
{
if(strcmp(q->next->name,p->name) > 0)
{
p->next = q->next;
q->next = p;
return success;
}
q = q->next;
}
p->next = q->next;
q->next = p;
/* int fd;
int ret;
fd = open("student.txt",O_CREAT | O_RDWR | O_APPEND,S_IRUSR | S_IWUSR);
if(-1 == fd)
{
perror("open1");
return;
}
ret = write(fd,p,sizeof(stu));
if(-1 == ret)
{
perror("write1");
return;
}
close(fd);*/
return success;
}
int list_traverse(Stu L)
{
Stu p = L->next;
if(L == NULL)
return failure;
while(p != NULL)
{
printf("num :%s\n",p->num);
printf("name :%s\n",p->name);
printf("tel :%s\n",p->tel);
printf("phone :%s\n",p->phone);
printf("addr :%s\n",p->addr);
printf("\n");
p = p->next;
}
/*int fd;
int ret;
fd = open("student.txt", O_RDONLY, S_IRUSR | S_IWUSR);
if(fd == -1)
{
perror("open1\n");
return;
}
Stu p = (Stu)malloc(sizeof(stu));
Stu h = (Stu)malloc(sizeof(stu));
*/
}
int modify_list(Stu L)
{
char a[20];
char b[20];
char c[20];
char name[20];
Stu p = L->next;
Stu m = L->next;
int choice,d;
printf("please input the name you want to modify\n");
scanf("%s",name);
if(p == NULL)
return failure;
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
while(1)
{
printf("===================================\n");
printf("| 1.phone 2.tel 3.addr |\n");
printf("===============4.exit==============\n");
printf("please input your choise:...\n");
d = scanf("%d",&choice);
while(d != 1 ||(choice != 1 && choice != 2 && choice != 3 && choice != 4))
{
printf("enter wrong,please enter again!\n");
scanf("%d",&choice);
}
switch(choice)
{
case 1:
{
system("clear");
printf("please input new phone:...\n");
scanf("%s",a);
while(strlen(a) != 11)
{
printf("input wrong,please input again\n");
scanf("%s",a);
}
while(m != NULL)
{
if(strcmp(m->phone,a) == 0)
{
printf("phone duplication,please input again!\n");
scanf("%s",a);
continue;
}
m = m->next;
}
strcpy(p->phone,a);
printf("modify phone success!\n");
break;
}
case 2:
{
system("clear");
printf(" please input new tel:...\n");
scanf("%s",b);
while(strlen(b) != 8)
{
printf("input wrong,please input again\n");
scanf("%s",b);
}
while(m != NULL)
{
if(strcmp(m->tel,b) == 0)
{
printf("tel duplication,please input again!\n");
scanf("%s",b);
continue;
}
m = m->next;
}
strcpy(p->tel,b);
printf("modify tel success!\n");
break;
}
case 3:
{
system("clear");
printf("please input new addr:...\n");
scanf("%s",c);
strcpy(p->addr,c);
printf("modify addr success!\n");
break;
}
case 4:
{
system("clear");
return 0;
break;
}
default:printf("unknow,please input again!\n");
}
printf("按回车建返回选择菜单.....\n");
getchar();
getchar();
}
}
p = p->next;
}
return 0;
}
int search_list(Stu L)
{
char name[20];
Stu p = L->next;
printf("please input the name you want to search:...\n");
scanf("%s",name);
if(p == NULL)
return failure;
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("name:%s\nnum:%s\nphone:%s\n",p->name,p->num,p->phone);
printf("tel:%s\naddr:%s\n",p->tel,p->addr);
break;
}
p = p->next;
}
return success;
}
int delete_list(Stu L)
{
char name[20];
Stu p = L;
Stu m = (Stu)malloc(sizeof(stu));
printf("please input the name you want to delete:...\n");
scanf("%s",name);
if( p->next == NULL)
return failure;
while(p->next != NULL)
{
if(strcmp(p->next->name,name) == 0)
{
m = p->next;
p->next = m->next;
free(m);
return 1;
}
p = p->next;
}
}
主函数
/*****************************************************
> File name: TestStudent.c
> Author: Mr.YUAN
> 日期: 2017-12-04 16:15
*****************************************************/
#include <stdio.h>
#include"student.h"
#include <stdlib.h>
#include <string.h>
void write_data(Stu L)
{
FILE *fp;
Stu p = L->next;
fp = fopen("student","a+");
if(fp == NULL)
{
perror("fopen");
exit(1);
}
while(p)
{
fwrite(p,sizeof(stu),1,fp);
p = p->next;
}
fclose(fp);
}
void read_data(Stu L)
{
Stu tmp = (Stu)malloc(sizeof(stu));
FILE *fp = fopen("student","rb");
int ret;
Stu p = L;
if(fp == NULL)
{
perror("fopen");
exit(1);
}
while(ret = fread(tmp,1,sizeof(stu),fp))
{
tmp->next = p->next;
p->next = tmp;
p = p->next;
printf("num:%s\n",tmp->num);
printf("name:%s\n",tmp->name);
printf("phone:%s\n",tmp->phone);
printf("tel:%s\n",tmp->tel);
printf("addr:%s\n",tmp->addr);
tmp = (Stu)malloc(sizeof(stu));
}
fclose(fp);
}
int main()
{
Stu list;
int ret;
int choice = 0;
FILE *fp;
Stu p = (Stu)malloc(sizeof(stu));
if(NULL == p)
{
printf("malloc failure!\n");
}
ret = list_init(&list);
if(ret == failure)
printf("init failure!\n");
read_data(list);
sleep(3);
while(1)
{
system("clear");
choice = menu();
switch(choice)
{
case 1:
{
system("clear");
ret = list_insert(&list);
if(ret == success)
printf("insert informatoin success!\n");
break;
}
case 2:
{
system("clear");
ret == list_traverse(list);
if(ret == failure)
printf("traverse failure!\n");
break;
}
case 3:
{
system("clear");
ret = modify_list(list);
if(ret == failure)
printf("modify failure\n");
break;
}
case 4:
{
system("clear");
ret = delete_list(list);
if(ret == failure)
printf("delete failure\n");
break;
}
case 5:
{
system("clear");
ret = search_list(list);
if(ret == failure)
printf("search failure\n");
break;
}
case 6:
{
system("clear");
write_data(list);
printf("thank you for use..\n");
exit(0);
}
default :printf("unknow,please input again\n");
}
printf("\n\n按回车建返回主菜单........");
getchar();
getchar();
}
write_data(list);
return 0;
}