代码段如下:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#define size 10
typedef struct information STU;
int person = 0;
struct data
{
char name[20];
int year;
};
struct information
{
struct data data;
struct information *next;
};
void welcome()
{
system("clear");
printf("\n\n\n\n\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
printf("\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
printf("\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
printf("\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
printf("\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
printf("\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
printf("\033[1m\033[44;31m*************************WELCOME************************* \033[0m\n");
sleep(1);
}
void menu()
{
system("clear");
printf("\n\n\n\n\033[1m\033[42;35m*****************Welcome******************\033[0m\n");
printf("\033[1m\033[45;33m1、添加信息\033[0m \033[5m\033[47;34m2、查找信息\033[0m\n");
printf("\033[7m\033[1m\033[42;31m3、删除信息\033[0m \033[1m\033[46;35m4、修改信息\033[0m\n");
printf("\033[4m\033[1m\033[41;34m5、查看信息\033[0m \033[5m\033[43;35m6、退出\033[0m\n");
printf("\033[1m\033[5m\033[44;35m""***************请输入你想使用功能的序号****************""\033[0m\n");
}
void Init(STU **head, int fd)
{
if(*head == NULL)
{
*head = (STU *)malloc(sizeof(STU));
(*head)->next = NULL;
}
fd = open("xxxxx.c", O_RDWR | O_CREAT, S_IRWXU);
if(fd == -1)
{
perror("open");
exit(1);
}
int ret;
struct data tmp;
STU *p = *head;
while(1)
{
ret = read(fd, &tmp, sizeof(tmp));
if(ret != 0)
{
while(p->next != NULL)
{
p = p->next;
}
STU *q = (STU *)malloc(sizeof(STU));
q->data = tmp;
p->next = q;
q->next = NULL;
memset(&tmp, 0, sizeof(tmp));
}
else
{
break;
}
}
close(fd);
}
int add(STU *head, int fd)
{
system("clear");
int t = 0;
char temp[20] = {0};
lo: printf("请输入联系人的Name和Year:\n");
scanf("%s%d", temp, &t);
if(head == NULL)
{
printf("插入失败!\n");
return -1;
}
STU *p = head;
while(p->next != NULL)
{
p = p->next;
}
STU *q = (STU *)malloc(sizeof(STU));
strcpy(q->data.name, temp);
q->data.year = t;
p->next = q;
q->next = NULL;
fd = open("xxxxx.c", O_RDWR);
lseek(fd, 0, SEEK_END);
write(fd, &q->data, sizeof(q->data));
close(fd);
int i;
printf("您还想继续添加联系人吗,如果想输入1,不想输入0\n");
scanf("%d", &i);
if(i == 0)
{
return -1;
}
else
{
goto lo;
}
}
int seek(STU *head)
{
system("clear");
char temp[20];
if(head == NULL || head->next == NULL)
{
printf("列表中无联系人!\n");
return -1;
}
lp: printf("请输入您要查找的联系人:\n");
scanf("%s", temp);
STU *p = head->next;
while(p)
{
if(!strcmp(p->data.name, temp))
{
printf("查到此人如下:\n%s %d\n", p->data.name, p->data.year);
break;
}
p = p->next;
}
if(p == NULL)
{
int i;
printf("不好意思,联系簿中并没有您想找的人!\n是否还想继续查找其他人,若想输入1,不想输入0\n");
scanf("%d", &i);
if(i == 0)
{
return -1;
}
else
{
goto lp;
}
}
int q;
printf("您还想继续查看其他人吗,若想输入1,不想输入0\n");
scanf("%d", &q);
if(q == 0)
{
return -1;
}
else
{
goto lp;
}
}
int delete(STU *head, int fd)
{
system("clear");
char temp[20];
rp: if(head == NULL || head->next == NULL)
{
printf("列表中无联系人\n");
return -1;
}
int flag = 0;
int ret;
printf("选择你想删除的联系人并输入他(她)(它)的名字\n");
scanf("%s", temp);
STU *p = head;
while(p->next)
{
STU *q = p->next;
if(!strcmp(q->data.name, temp))
{
struct data datatmp;
fd = open("xxxxx.c", O_RDWR);
lseek(fd, 0, SEEK_SET);
while(1)
{
ret = read(fd, &datatmp, sizeof(datatmp));
if(!strcmp(datatmp.name, temp))
{
memset(&datatmp, 0, sizeof(datatmp));
lseek(fd, -sizeof(datatmp), SEEK_CUR);
write(fd, &datatmp, sizeof(datatmp));
break;
}
memset(&datatmp, 0, sizeof(datatmp));
if(ret == 0)
{
break;
}
}
p->next = q->next;
free(q);
printf("删除成功!\n");
flag = 1;
break;
}
p = p->next;
}
if(p->next == NULL && flag == 0)
{
int i;
printf("列表中没有您想删除的联系人\n如果想删除其他联系人请输入1不想输入0\n");
scanf("%d", &i);
if(i == 0)
{
return -1;
}
else
{
goto rp;
}
}
int j;
printf("是否还想删除其他联系人,若想输入1,不想输入0\n");
scanf("%d", &j);
if(j == 0)
{
return -1;
}
else
{
goto rp;
}
}
int alter(STU *head, int fd)
{
system("clear");
char temp[20];
jp: if(head == NULL || head->next == NULL)
{
printf("列表中没有联系人\n");
sleep(2);
return -1;
}
printf("请输入你要修改的信息的人名:\n");
scanf("%s", &temp);
STU *p = head->next;
while(p)
{
if(!strcmp(p->data.name, temp))
{
printf("您想修改人联系人信息如下:\n%s %d\n请输入您修改后的联系人姓名和年龄:\n", p->data.name, p->data.year);
scanf("%s%d", p->data.name, &p->data.year);
struct data datatmp;
int ret;
fd = open("xxxxx.c", O_RDWR);
lseek(fd, 0, SEEK_SET);
while(1)
{
ret = read(fd, &datatmp, sizeof(datatmp));
if(!strcmp(datatmp.name, temp))
{
lseek(fd, -sizeof(datatmp), SEEK_CUR);
write(fd, &p->data, sizeof(p->data));
break;
}
memset(&datatmp, 0, sizeof(datatmp));
if(ret == 0)
{
break;
}
}
printf("修改成功!\n");
break;
}
p = p->next;
}
if(p == NULL)
{
int i;
printf("不好意思,列表中并没有您想修改的人,如果想继续修改其他人的信息请输入1,不想输入0\n");
scanf("%d", &i);
if(i == 0)
{
return -1;
}
else
{
goto jp;
}
}
int o;
printf("您还想继续修改其他人的信息吗,如果想输入1,不想输入0\n");
scanf("%d", &o);
if(o == 0)
{
return -1;
}
else
{
goto jp;
}
}
int checker(STU *head)
{
system("clear");
getchar();
if(head == NULL || head->next == NULL)
{
printf("列表中无联系人!\n");
sleep(2);
return -1;
}
STU *q = head->next;
char temp[20];
while(q->next)
{
STU *p = q->next;
while(p)
{
if(strcmp(q->data.name, p->data.name) > 0)
{
strcpy(temp, q->data.name);
strcpy(q->data.name, p->data.name);
strcpy(p->data.name, temp);
}
p = p->next;
}
q = q->next;
}
q = head->next;
while(q)
{
printf("%s %d\n", q->data.name, q->data.year);
q = q->next;
}
printf("按任意键退出查看\n");
getchar();
}
int main()
{
struct information *head = NULL;
int num;
int fd;
welcome();
Init(&head, fd);
while(1)
{
menu();
lop:printf("请输入你想要执行的功能序号:\n");
scanf("%d",&num);
switch(num)
{
case 1:
add(head, fd);
break;
case 2:
seek(head);
break;
case 3:
delete(head, fd);
break;
case 4:
alter(head, fd);
break;
case 5:
checker(head);
break;
case 6:
exit(0);
break;
default:
goto lop;
}
}
return 0;
}