Linux C 주소록 파일 버 전

7959 단어 공부 하 다
헤더 파일:
#ifndef _LINKLIST_H_
#define _LINKLIST_H_

typedef int DATA;
typedef enum{TRUE,FALSE,ERROR}BOOL;
enum{QUIT,INSERT,DISPLAY,SEARCH,DELETE};
typedef struct addlist
{
	long pertel;
	long comtel;
	int  id;
	char address[20];
	char name[20];
}Txl;

typedef struct _node
{
	Txl te;
	
	struct _node *next;
}Node;
typedef struct _list
{
	Node *head;
}List;
//       
List *CreateList();
//    
void Destory(List *pList);
//     
BOOL Insert_Frined(List *pList);
//     
void Display(List *pList);


//     
BOOL Delete_Friend(List *pList);
//     
BOOL Find_Friend(List *pList);

BOOL Sava_Friends(List *pList);
BOOL Load_Friends(List *pList);
BOOL Insert_Last(List *pList,Txl* data);
int myrand(int Rand_Max);
#endif //_LINKLIST_H_

기능 함수
#include "LinkList.h"
#include 
#include 
#include 
#include 
#define SIZE 30
DATA randcount = 0;
int count_friend = 0;
List *CreateList()
{
	List *p = (List*)malloc(sizeof(List)/sizeof(char));
	if(NULL == p)
	{
		return NULL;
	}
	p->head = (Node*)malloc(sizeof(Node)/sizeof(char));
	p->head->next = NULL;
	if(NULL == p->head)
	{
		free(p);
		return NULL;
	}
	return p;
}
int myrand(int Rand_Max)//     
{
	
	
	int i=0;
	srand(time(NULL));
	
	int rnum=rand()%Rand_Max;
	
	return rnum;
	
}
BOOL Insert_Friend(List *pList)
{
	if(NULL == pList)
	{
		return ERROR;
		printf("    
"); } /* Node *p = (Node*)malloc(sizeof(Node)/sizeof(char)); if(NULL == p) { return ERROR; printf("
"); } p->next = NULL; */ Txl data; printf(" :
"); scanf("%s",data.name); int ran = ++randcount; data.id = ran;// ID printf(" ID:%d
",ran); printf(" :
"); scanf("%ld",&data.pertel); printf(" :
"); scanf("%ld",&data.comtel); printf(" :
"); scanf("%s",data.address); printf(" !
"); Insert_Last(pList,&data); /* Node *tmp = pList->head; while(tmp->next) { tmp = tmp->next; } tmp->next = p; */ count_friend++; return TRUE; } BOOL Sava_Friends(List *pList) { if(pList == NULL) { return; } FILE *fp1 = fopen("txl","wb+"); if(NULL == fp1) { perror("fail open fp1"); return FALSE; } FILE *fp2 = fopen("count","ab+"); if(NULL == fp2) { perror("fail open fp2"); return FALSE; } Node *tmp = pList->head->next; int save_count = count_friend; fwrite(&save_count,sizeof(int),1,fp2); int i; while(tmp!=NULL) { fwrite(&tmp->te,sizeof(Txl),1,fp1); printf("save success
"); tmp = tmp->next; } fclose(fp1); fclose(fp2); return TRUE; } BOOL Load_Friends(List *pList) { FILE* fp3 = fopen("txl","rb+"); if(NULL == fp3) { perror("fail open fp3"); return -1; } FILE* fp4 = fopen("count","rb+"); if(NULL == fp4) { perror("fail open fp4"); return -1; } int i; int load_count=0; while(1) { size_t ret = fread(&load_count,sizeof(int),1,fp4); if (0 == ret) // , { if (feof(fp4) == 0) // { printf ("
"); return -1; } break; } } count_friend = load_count; printf("load_count = %d
",load_count); printf("cofriend %d
",count_friend); Txl history[load_count]; for(i=0;ite.pertel = data->pertel; p->te.comtel = data->comtel; p->te.id = data->id; strcpy(p->te.address,data->address); strcpy(p->te.name,data->name); p->next = NULL; Node *tmp = pList->head; while(tmp->next) { tmp = tmp->next; } tmp->next = p; return TRUE; } BOOL Delete_Friend(List *pList)// { if(NULL == pList) { return ERROR; } char deletename[20];// printf(" :
"); scanf("%s",deletename); Node *tmp =pList->head; DATA count = 0;// while(tmp->next)// { if(!(strcmp(tmp->next->te.name,deletename))) { count++; } tmp = tmp->next; } Node *lp = pList->head->next; Node *tp = pList->head; Node *mp = pList->head; if(count >1)// { printf(" , ID
"); int deleteid = 0;// ID, ID while(lp) { if(!(strcmp(lp->te.name,deletename)))// { printf("ID : %d
",lp->te.id); printf(" : %s
",lp->te.name); printf(" : %ld
",lp->te.pertel); printf(" : %ld
",lp->te.comtel); printf(" : %s
",lp->te.address); } lp = lp->next; } scanf("%d",&deleteid);// ID while(mp->next)// ID { if(mp->next->te.id == deleteid) { Node *p = mp->next; mp->next = p->next; free(p); printf("
"); count_friend--; return TRUE; } mp = mp->next; } } else// , { while(tp->next) { if(!(strcmp(tp->next->te.name,deletename))) { Node *p = tp->next; tp->next = p->next; free(p); printf("
"); count_friend--; return TRUE; } tp = tp->next; } } printf(" ,
"); return FALSE; } BOOL Find_Friend(List *pList)// { if(NULL == pList) { return ERROR; } Node *tmp =pList->head->next; char searchname[20]; printf(" :
"); scanf("%s",searchname); while(tmp!=NULL) { if(!(strcmp(tmp->te.name,searchname))) { printf("ID : %d
",tmp->te.id); printf(" : %s
",tmp->te.name); printf(" : %ld
",tmp->te.pertel); printf(" : %ld
",tmp->te.comtel); printf(" : %s
",tmp->te.address); return TRUE; } tmp = tmp->next; } printf("
"); return FALSE; } void Destory(List *pList)// { if(NULL==pList) { return ; } Node *tmp = pList->head; while(tmp->next) { Node *p = tmp->next; tmp->next = p->next; free(p); } free(pList->head); free(pList); } void Display(List *pList)// { if(NULL==pList) { return ; } Node *tmp = pList->head->next; while(tmp) { printf("ID : %d
",tmp->te.id); printf(" : %s
",tmp->te.name); printf(" : %ld
",tmp->te.pertel); printf(" : %ld
",tmp->te.comtel); printf(" : %s
",tmp->te.address); tmp = tmp->next; } printf("
"); }

주 함수:
#include 
#include "LinkList.h"
#include 
int main()
{
	int com = 0;
	List *l = CreateList();
	Load_Friends(l);
	 while(1)
	{
		printf("\t**************
"); printf("\t*1. *
"); printf("\t*2. *
"); printf("\t*3. *
"); printf("\t*4. *
"); printf("\t*0. *
"); printf("\t**************
"); scanf("%d",&com); switch(com) { case INSERT: system("clear"); Insert_Friend(l); break; case DISPLAY: system("clear"); Display(l); break; case SEARCH: system("clear"); Find_Friend(l); break; case DELETE: system("clear"); Delete_Friend(l); break; case QUIT: Sava_Friends(l); printf(" !
"); return ; default: printf("
"); break; } } return 0; }

좋은 웹페이지 즐겨찾기