Linux下通讯录(链表加文件格式化存储)(纯数据库sqlite3)

链表加文件格式化存储

main.c

#include"link.h"

int main(int argc,char *argv[])
{
    int num = 0;
    person head;
    head.next = NULL;

    read_line(&head);

    while(1)
    {
        viewhome();
        printf("input num:\n");
        scanf("%d",&num);

        choose_num(&head,num);

        sleep(2);
        printf("sa\n");
    }

    return 0;
}

link.c

#include"link.h"

void viewhome()
{
    system("clear");
    printf("\t\t1.录入信息    2.查询所有\n");
    printf("\t\t3.查找用户    4.删除用户\n");
    printf("\t\t5.修改用户    6.退出系统\n");
}

int choose_num(person *head,int num)
{
    if(head == NULL)
    {
        return L_FAIL;
    }
    switch(num)
    {
        case 1:
        {
            head_insert(head);
            break;
        }
        case 2:
        {
            queryall(head);
            break;
        }
        case 3:
        {
            //queryone(head);
            display_one(queryone(head));
            break;
        }
        case 4:
        {
            delete_person(queryone(head));
            break;
        }
        case 5:
        {
            revise_user(queryone(head));
            break;
        }
        case 6:
        {
            write_line(head);
            if(L_OK == desptroy(head))
            {
                exit(1);
            }
        }
        default:
        {
            printf("dasd\n");
            printf("erorr!");
            exit(1);
        }
    }
}

int head_insert(person *head)
{
    if(head == NULL)
    {
        return L_FAIL;
    }

    char ch;
    person *pNode;
    do
    {
        pNode = (person *)malloc(sizeof(person));
        if(pNode == NULL)
        {
            return L_FAIL;
        }
        printf("input id:\n");
        scanf("%s",pNode->id);
        
        printf("input name:\n");
        scanf("%s",pNode->name);
        
        printf("input ipone:\n");
        scanf("%s",pNode->ipone);

        pNode->next = head->next;
        if(head->next != NULL)
        {
            head->next->per = pNode;
        }
        pNode->per = head;
        head->next = pNode;

        printf("continue y/n:\n");
        scanf("%*c%c",&ch);
    }while ('y' == ch);
    return L_OK;
}

int display(person *head)
{
    if(head == NULL)
    {
        return L_FAIL;
    }

    person *pNode = head->next;
    while(pNode != NULL)
    {
        printf("%s:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
        pNode = pNode->next;
    }
}

int mystrcmpy(char *dest,char *src)
{
    if(dest == NULL || src == NULL)
    {
        return L_FAIL;
    }
    
    int i = 0;
    while(dest[i] != '\0')
    {
        if(dest[i] != src[i])
        {
            return L_FAIL;
        }
        i++;
    }
    return L_OK;
    
}

person *queryone(person *head)
{
    if(head == NULL)
    {
        return NULL;
    }

    char src[MAX_SIEZ];
    person *pNode = head->next;
    printf("query candition:\n");
    scanf("%s",src);
    if(src[0] >= '0' && src[0] <= '9')
    {
        while (pNode != NULL)
        {
            if(L_OK == mystrcmpy(pNode->id,src))
            {
                // printf("%d:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
                return pNode;
            }
            pNode = pNode->next;
        }
    }
    else
    {
        while(pNode != NULL)
        {
            if(L_OK == mystrcmpy(pNode->name,src))
            {
                // printf("%d:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
                return pNode;
            }
            pNode = pNode->next;
        }
    }
    return NULL;
}

int  display_one(person *pNode)
{
    if(pNode == NULL)
    {
        printf("no user!\n");
        return L_FAIL;
    }
    printf("%s:%s:%s\n",pNode->id,pNode->name,pNode->ipone);

    return L_OK;
}

int delete_person(person *pNode)
{
    if(pNode == NULL)
    {
        printf("no user!\n");
        return L_OK;
    }

    if(pNode->per != NULL)
    {
        pNode->per->next = pNode->next;
    }
    if(pNode->next != NULL)
    {
        pNode->next->per = pNode->per;
    }
    free(pNode);
    pNode = NULL;

    return L_OK;
}

int revise_user(person *pNode)
{
    if(pNode == NULL)
    {
        printf("no user!\n");
        return L_FAIL;
    }
    printf("input id:\n");
    scanf("%s",pNode->id);
        
    printf("input name:\n");
    scanf("%s",pNode->name);
        
    printf("input ipone:\n");
    scanf("%s",pNode->ipone);

    return L_OK;
}

int desptroy(person *head)
{
    if(head == NULL )
    {
        return L_FAIL;
    }

    person *pNode = head->next;
    person *Temp = NULL;
    while(pNode != NULL)
    {
        Temp = pNode;
        if(pNode->per != NULL)
        {
            pNode->per->next = pNode->next;
        }
        if(pNode->next != NULL)
        {
            pNode->next->per = pNode->per;
        }
        pNode = pNode->next;
        
        if(Temp != NULL)
        {
            Temp->next = Temp;
            free(Temp);
            Temp = NULL;
        }
    }
    return L_OK;
}

link.h

#ifndef _LINK_
#define _LINK_

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

enum recrod
{
    MAX_SIEZ = 12,
    L_OK,
    L_FAIL
};

typedef struct Person
{
    char id[MAX_SIEZ];
    char name[MAX_SIEZ];
    char ipone[MAX_SIEZ];
    struct Person *per;
    struct Person *next;
}person;

void viewhome();
int choose_num(person *, int );
int head_insert(person *);
int display(person *);
person *queryone(person *);
int display_one(person *);
int delete_person(person *);
int revise_user(person *);
int desptroy(person *);
int write_line(person *);
int read_line(person *);
int queryall(person *);

#endif

queryall.c

#include"link.h"

int mystrcmpy1(char *dest,char *src)
{
    int i = 0;
    while(dest[i] == src[i])
    {
        i++;
        if(dest[i] == '\0')
        {
            return L_FAIL;
        }
        else if (src[i] == '\0')
        {
            return L_OK;
        }
    }
    if(dest[i]>src[i])
    {
        return L_OK;
    }
    else
    {
        return L_FAIL;
    }
}

int head1(person *Target,person *pNode)
{
    pNode->next = Target;
    if(Target->per != NULL)
    {
        Target->per->next = pNode;
    }
    pNode->per = Target->per;
    Target->per = pNode;    
}

int tail(person *Target,person *pNode)
{
    if(Target->next != NULL)
    {
        Target->next->per = pNode;
    }
    pNode->next = Target->next;
    Target->next = pNode;
    pNode->per = Target;
}

int queryall(person *head)
{
    if(head == NULL)
    {
        return L_FAIL;
    }
    if(head->next == NULL)
    {
        printf("on user!\n");
        return L_OK;
    }
    person *pNode = head->next;
    head->next = NULL;

    person *p = pNode;
    pNode = pNode->next;

    if(head->next != NULL)
    {
        head->next->per = p;
    }
    p->next = head->next;
    p->per = head;
    head->next = p;
    
    person *Temp;
    while(pNode != NULL)
    {
        p = pNode;
        pNode = pNode->next;
        Temp = head->next;
        while(Temp != NULL)
        {
            if(mystrcmpy1(Temp->name,p->name) == L_OK)
            {
                head1(Temp,p);
                break;
            }
            else if (Temp->next == NULL)
            {
                tail(Temp,p);
                break;
            }
            else
            {
                Temp = Temp->next;
            }
        }
    }


    pNode = head->next;
    while(pNode != NULL)
    {
        printf("%s:%s:%s\n",pNode->id,pNode->name,pNode->ipone);
        pNode = pNode->next;
    }


    return L_OK;
}

read_line.c

#include"link.h"

int read_line(person *head)
{
    if(head == NULL)
    {
        return L_FAIL;
    }
    FILE *fp;
    fp = fopen("ln.txt","a+");
    getc(fp);
    if(feof(fp) != 0)
    {
        return L_FAIL;
    }

    int i = 0;
    char ch = '0';
    char src[MAX_SIEZ];
    person *pNode;
    fread(&ch,1,1,fp);
    fseek(fp,0,SEEK_SET);
    do
    {
        pNode = (person *)malloc(sizeof(person));
        if(pNode == NULL)
        {
            return L_FAIL;
        }

        // fread(&ch,1,1,fp);
        
        // while((ch != '\n') && (feof(fp) == 0))
        // {
        //     while(ch != ':')
        //     {
        //         src[i] = ch;
        //         i++;
        //         fread(&ch,1,1,fp); 
        //     }
        //     src[i] = '\0';
        //     strcpy(pNode->id,src);
        //     i = 0;
        //     fread(&ch,1,1,fp);
            
        //     while(ch != ':')
        //     {
        
		//         src[i] = ch;
        //         i++;
        //         fread(&ch,1,1,fp); 
        //     }
        //     src[i] = '\0';
        //     strcpy(pNode->name,src);
        //     i = 0;
        //     fread(&ch,1,1,fp);
            
        //     while((ch != '\n') && (feof(fp) == 0))
        //     {
        //         src[i] = ch;
        //         i++;
        //         fread(&ch,1,1,fp); 
        //     }
        //     src[i] = '\0';
        //     strcpy(pNode->ipone,src);
        //     i = 0;


        // }

            fscanf(fp,"%s%s%s",pNode->id,pNode->name,pNode->ipone);




        pNode->next = head->next;
        if(head->next != NULL)
        {
            head->next->per = pNode;
        }
        pNode->per = head;
        head->next = pNode;

    }while(feof(fp) == 0);
    fclose(fp);
    return L_OK;
}

write_line.c

#include"link.h"

int read_line(person *head)
{
    if(head == NULL)
    {
        return L_FAIL;
    }
    FILE *fp;
    fp = fopen("ln.txt","a+");
    getc(fp);
    if(feof(fp) != 0)
    {
        return L_FAIL;
    }

    int i = 0;
    char ch = '0';
    char src[MAX_SIEZ];
    person *pNode;
    fread(&ch,1,1,fp);
    fseek(fp,0,SEEK_SET);
    do
    {
        pNode = (person *)malloc(sizeof(person));
        if(pNode == NULL)
        {
            return L_FAIL;
        }

        // fread(&ch,1,1,fp);
        
        // while((ch != '\n') && (feof(fp) == 0))
        // {
        //     while(ch != ':')
        //     {
        //         src[i] = ch;
        //         i++;
        //         fread(&ch,1,1,fp); 
        //     }
        //     src[i] = '\0';
        //     strcpy(pNode->id,src);
        //     i = 0;
        //     fread(&ch,1,1,fp);
            
        //     while(ch != ':')
        //     {
        
		//         src[i] = ch;
        //         i++;
        //         fread(&ch,1,1,fp); 
        //     }
        //     src[i] = '\0';
        //     strcpy(pNode->name,src);
        //     i = 0;
        //     fread(&ch,1,1,fp);
            
        //     while((ch != '\n') && (feof(fp) == 0))
        //     {
        //         src[i] = ch;
        //         i++;
        //         fread(&ch,1,1,fp); 
        //     }
        //     src[i] = '\0';
        //     strcpy(pNode->ipone,src);
        //     i = 0;


        // }

            fscanf(fp,"%s%s%s",pNode->id,pNode->name,pNode->ipone);




        pNode->next = head->next;
        if(head->next != NULL)
        {
            head->next->per = pNode;
        }
        pNode->per = head;
        head->next = pNode;

    }while(feof(fp) == 0);
    fclose(fp);
    return L_OK;
}

makefile

obj := main.o link.o write_line.o read_line.o queryall.o

list:$(obj)
	gcc $(obj) -o list
%.o:%.c
	gcc $< -c -o $@
.PHONY:clean
clean:
	rm -rf *.o list

纯数据库sqlite3

main.c

#include "list.h"

int main(int argc,char *argv[])
{
    int num;
    // scanf("%d",num);
    createlist();
    while(1)
    {
        viewhome();
        scanf("%d",&num);

        choose_num(num);

        sleep(2);
    }
    return 0;
}

list.c

#include "list.h"

sqlite3 *db;
char *errmsg;
void print_error(int ret,char *err,sqlite3 *db)
{
    if(ret != SQLITE_OK)
    {
        printf("%s:%s\n",err,sqlite3_errmsg(db));
        exit(1);
    }
}

void viewhome()
{
    system("clear");
    printf("\t\t1.录入信息    2.查询所有\n");
    printf("\t\t3.查找用户    4.删除用户\n");
    printf("\t\t5.修改用户    6.退出系统\n");
    printf("input num:\n");
}

void createlist()
{
    char sql[1024] = {0};
    int ret = sqlite3_open("test.db",&db);

    print_error(ret,"open",db);

    memset(sql,0,sizeof(sql));
    strcpy(sql,"create table if not exists person(id integer primary key,name text,ipone text)");

    ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);

    print_error(ret,"exec",db);

    return;
}

void choose_num(int num)
{
    switch(num)
    {
        case 1:
        {
            insert_data();
            break;
        }
        case 2:
        {
            queryall();
            break;
        }
        case 3:
        {
            queryone();
            break;
        }
        case 4:
        {
            delete_list();
            break;
        }
        case 5:
        {
            revise_user();
            break;
        }
        case 6:
        {
            my_exit();
            break;
        }
    }
}

void insert_data()
{
    char sql[1024]={0};
    int id;
    char name[20];
    char ipone[20];
    char rc = '0';
    do
    {
        printf("id:\n");
        scanf("%d",&id);

        printf("name:\n");
        scanf("%s",name);

        printf("ipone:\n");
        scanf("%s",ipone);

        sprintf(sql,"insert into person(id,name,ipone)values(%d,'%s',%s);",id,name,ipone);
        int ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);

        print_error(ret,"insert",db);
        printf("is continue y/n\n");
        
        scanf("%*c%c",&rc);
    }while(rc == 'y');
    return;
}

void queryall()
{
    char sql[1024];
    char **result;
    int nrow;
    int ncolumn;
    memset(sql,0,sizeof(sql));
    strcpy(sql,"select *from person order by name asc");
    int ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg); 

    for(int i=1; i<=nrow; i++)
    {
        for(int j=0; j<ncolumn; j++)
        {
            printf("%s|",result[i*ncolumn + j]);
        }
        printf("\n");
    }
    sqlite3_free_table(result);
}

int my_sqlite_callback(void *para,int contun,char **columvalue,char **columnName)
{
    int i;
    for(i=0; i<contun; i++)
    {
        printf("%s:%s|",columnName[i],columvalue[i]);
    }
    printf("\n");
    return 0;
}

void queryone()
{    
    int i;
    char sql[1024] ={0};
    char ch[20];
    printf("input :\n");
    scanf("%s",ch);
    if(ch[0] > '0' && ch[0] < '9')
    {
        sprintf(sql,"select *from person where id = %s;",ch);
    }
    else
    {
        sprintf(sql,"select *from person where name = '%s';",ch);
    }

    int ret = sqlite3_exec(db,sql,my_sqlite_callback,NULL,&errmsg);
    print_error(ret,"select one",db);
    return;
}

void delete_list()
{
    char sql[1024]={0};
    char ch[20];
    printf("input :\n");
    scanf("%s",ch);
    if(ch[0] > '0' && ch[0] < '9')
    {
        sprintf(sql,"delete from person where id = %s",ch);
    }
    else
    {
        sprintf(sql,"delete from person where name = '%s';",ch);
    }
    int ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    print_error(ret,"select",db);
    return;
}

void revise_user()
{
    char sql[1024]={0};
    int id;
    char name[20];
    char ipone[20];
    char ch[20]={0};
    do
    {
        printf("input revise:\n");
        scanf("%s",ch);

        printf("id:\n");
        scanf("%d",&id);

        printf("name:\n");
        scanf("%s",name);

        printf("ipone:\n");
        scanf("%s",ipone);

        if(ch[0] > '0' && ch[0] < '9')
        {
            sprintf(sql,"update person set id = %d,name = '%s',ipone = %s where id = %s;",id,name,ipone,ch);
        }
        else
        {
            sprintf(sql,"update person set id = %d,name = '%s',ipone = %s where name = '%s';",id,name,ipone,ch);
        }
        
        int ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);

        print_error(ret,"insert",db);
    }while(0);
    return;

} 
void my_exit()
{
    exit(1);
}

list.h

#ifndef _LIST_H_
#define _LIST_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <unistd.h>

void createlist();
void viewhome();
void queryall();
void choose_num(int num);
void insert_data();
void queryall();
void queryone();
void delete_list();
void revise_user();
void my_exit();

#endif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的通讯录链表实现,包括增删查改和文件保存功能: ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; // 通讯录联系人节点结构体 struct ContactPerson { string name; // 姓名 string phone; // 电话 string email; // 邮箱 ContactPerson* next; // 指向下一个节点的指针 }; // 通讯录链表类 class ContactList { public: ContactList(); // 构造函数 ~ContactList(); // 析构函数 void addContactPerson(); // 添联系人 void deleteContactPerson(); // 删除联系人 void searchContactPerson(); // 查找联系人 void modifyContactPerson(); // 修改联系人 void displayContactList(); // 显示所有联系人 void saveToFile(); // 将当前通讯录保存到文件 void loadFromFile(); // 从文件中读取通讯录 private: ContactPerson* head; // 头指针 }; ContactList::ContactList() { head = NULL; } ContactList::~ContactList() { ContactPerson* p = head; while (p) { ContactPerson* q = p; p = p->next; delete q; } } void ContactList::addContactPerson() { ContactPerson* newPerson = new ContactPerson; cout << "请输入联系人姓名:"; cin >> newPerson->name; cout << "请输入联系人电话:"; cin >> newPerson->phone; cout << "请输入联系人邮箱:"; cin >> newPerson->email; newPerson->next = NULL; if (head == NULL) { head = newPerson; } else { ContactPerson* p = head; while (p->next) { p = p->next; } p->next = newPerson; } cout << "添成功!" << endl; } void ContactList::deleteContactPerson() { if (head == NULL) { cout << "通讯录为空!" << endl; return; } string name; cout << "请输入要删除的联系人姓名:"; cin >> name; ContactPerson* p = head; ContactPerson* q = NULL; while (p) { if (p->name == name) { if (q == NULL) { head = p->next; } else { q->next = p->next; } delete p; cout << "删除成功!" << endl; return; } q = p; p = p->next; } cout << "未找到该联系人!" << endl; } void ContactList::searchContactPerson() { if (head == NULL) { cout << "通讯录为空!" << endl; return; } string name; cout << "请输入要查找的联系人姓名:"; cin >> name; ContactPerson* p = head; while (p) { if (p->name == name) { cout << "姓名:" << p->name << endl; cout << "电话:" << p->phone << endl; cout << "邮箱:" << p->email << endl; return; } p = p->next; } cout << "未找到该联系人!" << endl; } void ContactList::modifyContactPerson() { if (head == NULL) { cout << "通讯录为空!" << endl; return; } string name; cout << "请输入要修改的联系人姓名:"; cin >> name; ContactPerson* p = head; while (p) { if (p->name == name) { cout << "请输入新的联系人姓名:"; cin >> p->name; cout << "请输入新的联系人电话:"; cin >> p->phone; cout << "请输入新的联系人邮箱:"; cin >> p->email; cout << "修改成功!" << endl; return; } p = p->next; } cout << "未找到该联系人!" << endl; } void ContactList::displayContactList() { if (head == NULL) { cout << "通讯录为空!" << endl; return; } ContactPerson* p = head; while (p) { cout << "姓名:" << p->name << endl; cout << "电话:" << p->phone << endl; cout << "邮箱:" << p->email << endl; p = p->next; } } void ContactList::saveToFile() { ofstream file("contacts.txt"); if (!file) { cout << "无法打开文件!" << endl; return; } ContactPerson* p = head; while (p) { file << p->name << " " << p->phone << " " << p->email << endl; p = p->next; } file.close(); cout << "保存成功!" << endl; } void ContactList::loadFromFile() { ifstream file("contacts.txt"); if (!file) { cout << "无法打开文件!" << endl; return; } ContactPerson* p = head; while (p) { ContactPerson* q = p; p = p->next; delete q; } head = NULL; string line; while (getline(file, line)) { ContactPerson* newPerson = new ContactPerson; sscanf(line.c_str(), "%s %s %s", &newPerson->name, &newPerson->phone, &newPerson->email); newPerson->next = NULL; if (head == NULL) { head = newPerson; } else { ContactPerson* p = head; while (p->next) { p = p->next; } p->next = newPerson; } } file.close(); cout << "读取成功!" << endl; } int main() { ContactList contactList; int choice = -1; while (choice != 0) { cout << "请选择操作:" << endl; cout << "1. 添联系人" << endl; cout << "2. 删除联系人" << endl; cout << "3. 查找联系人" << endl; cout << "4. 修改联系人" << endl; cout << "5. 显示所有联系人" << endl; cout << "6. 保存到文件" << endl; cout << "7. 从文件中读取" << endl; cout << "0. 退出程序" << endl; cout << "请选择:"; cin >> choice; switch (choice) { case 1: contactList.addContactPerson(); break; case 2: contactList.deleteContactPerson(); break; case 3: contactList.searchContactPerson(); break; case 4: contactList.modifyContactPerson(); break; case 5: contactList.displayContactList(); break; case 6: contactList.saveToFile(); break; case 7: contactList.loadFromFile(); break; case 0: break; default: cout << "无效的选择!" << endl; break; } } return 0; } ``` 以上代码中,通讯录联系人节点结构体包含姓名、电话、邮箱和指向下一个节点的指针。通讯录链表类包含添、删除、查找、修改、显示、保存到文件和从文件中读取等功能。其中保存和读取都是通过文件实现的,文件格式为每行一个联系人,每个联系人用空格分隔姓名、电话和邮箱。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值