編寫一個通訊錄軟件,有如下功能,1.增加 2修改 3刪除 4查詢 5列表 6退出,每個通訊錄項,包含姓名和手機號
函數接口API:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
typedef struct Node
{
char name[32];
int phonenum;
struct Person * next;
}Node;
typedef struct Link
{
int size;
Node * head;
Node * tail;
}Link;
void welcome();
Link * init();
void Add_person(Link * list);
void Modify_person(Link * list);
void Delete_person(Link * list);
void Search_person(Link * list);
void List_person(Link * list);
void Quit_AddressBook();
函數實現:
#include "method.h"
#include <stddef.h>
void welcome()
{
printf("------welcome to use address book-----\n");
sleep(1);
printf("|--------------1.add---------------|\n");
sleep(1);
printf("|--------------2.modify------------|\n");
sleep(1);
printf("|--------------3.delete------------|\n");
//sleep(1);
printf("|--------------4.search------------|\n");
sleep(1);
printf("|--------------5.list--------------|\n");
sleep(1);
printf("|--------------0.quit--------------|\n");
sleep(1);
printf("------------------------------------\n");
}
Link * init()
{
Link * list = (Link*)malloc(sizeof(Link));
if(link == NULL)
{
perror("malloc_init error");
exit(-1);
}
memset(list,0,sizeof(Link));
list->size = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
void Add_person(Link * list)
{
char name[32];
printf("please input name:");
scanf("%s",name);
int numphone = 0;
printf("please input number:");
scanf("%d",&numphone);
Node * node = (Node *)malloc(sizeof(Node));
if(node == NULL)
{
perror("malloc_node error");
exit(-1);
}
strcpy(node->name,name);
node->phonenum = numphone;
node->next = NULL;
if(list->size == 0)
{
list->head = node;
}
else
{
list->tail->next = node;
}
list->tail = node;
list->size++;
printf("add success!!!\n");
}
void Modify_person(Link * list)
{
char name[32];
printf("please input modify name:");
scanf("%s",name);
Node * node = list->head;
while(node)
{
if(strcmp(node->name,name) == 0)
{
printf("get it! waiting...\n");
sleep(1);
char tmpname[32];
printf("please input mofify name:");
scanf("%s",tmpname);
int numphone = 0;
printf("please input new numphone:");
scanf("%d",&numphone);
strcpy(node->name,tmpname);
node->phonenum = numphone;
break;
}
node = node->next;
}
if(node == NULL)
{
printf("No delete person\n");
return;
}
printf("modify success!!!\n");
}
void Delete_person(Link * list)
{
char name[32];
printf("please input delete name:");
scanf("%s",name);
Node * node = list->head;
Node * pre = node;
while(node)
{
if(strcmp(node->name,name) == 0)
{
printf("get it!,waiting...\n");
sleep(1);
if(node == list->head)
{
Node * tmp = node;
list->head = node->next;
free(tmp);
break;
}
Node * temp = node;
pre->next = node->next;
free(temp);
break;
}
pre = node;
node = node->next;
}
if(node == NULL)
{
printf("No search this person\n");
return;
}
else
{
printf("delete success\n");
list->size--;
}
}
void Search_person(Link * list)
{
char name[32];
printf("please search name:");
scanf("%s",name);
Node * node = list->head;
while(node)
{
if(strcmp(node->name,name) == 0)
{
printf("get it! waiting...\n");
sleep(1);
printf("name:%s\tphonenum:%d\n",node->name,node->phonenum);
break;
}
node = node->next;
}
if(node == NULL)
{
printf("No Search results\n");
}
}
void List_person(Link * list)
{
Node * tmp = list->head;
while(tmp != NULL)
{
printf("Address person num is:%d\n",list->size);
printf("name:%s\tnumphone:%d\n",tmp->name,tmp->phonenum);
tmp = tmp->next;
}
}
void Quit_AddressBook(Link * list)
{
printf("Enjoy your next use\n");
sleep(1);
exit(-1);
}
測試:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "method.h"
int main(void) {
Link * list = NULL;
list = init();
while(1)
{
welcome();
int select = 0;
printf("please your option:");
scanf("%d",&select);
switch(select)
{
case 1:
{
Add_person(list);
break;
}
case 2:
{
Modify_person(list);
break;
}
case 3:
{
Delete_person(list);
break;
}
case 4:
{
Search_person(list);
break;
}
case 5:
List_person(list);
break;
case 0:
Quit_AddressBook();
break;
}
system("clear");
}
return EXIT_SUCCESS;
}
總結:此通訊錄沒有保存功能,後面把文件操作的博客補完之後,會回來補完通訊錄的保存操作。同時用sqlite3數據庫保存。兩種方式全部實現一遍。