1. 程式人生 > >實現一個通訊錄,可以實現檔案儲存

實現一個通訊錄,可以實現檔案儲存

使用I/O流的概念,並使用IO函式,完成通訊錄的檔案儲存

Contact.h

#ifndef __Contact_H__
#define __Contact_H__

#pragma warning(disable:4996)

#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
enum SLECT
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	ALERT,
	SHOW,
	SORT,
	EMPTY
};
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_ADDR 30
#define MAX_TELE 20
#define MAX 2


typedef struct People
{
	char name[MAX_NAME];
	short agel;
	char sex[MAX_SEX];
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}People;
typedef struct Contact
{
	People *data;
	unsigned int sz;
	int capacity;
}Contact;

void AddPeople(Contact *ps);
int SearchPeople(Contact *ps);
void DelePeople(Contact *ps);
void AlertPeople(Contact *ps);
void ShowPeople(Contact *ps);
void SortPeople(Contact *ps);
void EmptyPeople(Contact *ps);
void InitContact(Contact *ps);
void CheckCapacity(Contact *ps);
void SaveContcat(Contact *ps);
void LoadContcat(Contact *ps);



#endif 

2.Contact.c

#include"contact.h"
//新增聯絡人
void AddPeople(Contact *ps)
{
	assert(ps != NULL);
	CheckCapacity(ps);
	printf("請輸入姓名:");
	scanf("%s", ps->data[ps->sz].name);
	printf("請輸入性別:");
	scanf("%s", ps->data[ps->sz].sex);
	printf("請輸入年齡:");
	scanf("%d", &ps->data[ps->sz].agel);
	printf("請輸入電話:");
	scanf("%s", ps->data[ps->sz].tele);
	printf("請輸入地址:");
	scanf("%s", ps->data[ps->sz].addr);
	ps->sz++;
	printf("新增成功\n");


}
//查詢聯絡人
int SearchPeople(Contact *ps)
{
	char names[20];
	printf("請輸入姓名:");
	scanf("%s", names);
	size_t i = 0;
	for (; i < ps->sz; i++)
	{
		if (0 == strcmp(ps->data[i].name, names))
		{
			return i;
		}
	}
	return -1;

}
int FindPeople(Contact *ps)
{
	char names[20];
	printf("請輸入姓名:");
	scanf("%s", names);
	size_t i = 0;
	for (; i < ps->sz; i++)
	{
		if (0 == strcmp(ps->data[i].name, names))
		{
			return i;
		}
	}
	return -1;
}
//刪除聯絡人
void DelePeople(Contact *ps)
{
	if (ps->sz == 0)
	{
		printf("通訊錄已空\n");
		return;
	}
	else
	{
		int ret = FindPeople(ps);
		if (ret < 0)
		{
			printf("查無此人\n");
			return;
		}
		else
		{
			size_t i = ret;
			for (; i < ps->sz - 1; i++)
			{
				ps->data[i] = ps->data[i + 1];
			}
			ps->sz--;
		}
	}

}

//修改聯絡人資訊
void AlertPeople(Contact *ps)
{
	int ret = FindPeople(ps);
	if (ret < 0)
	{
		printf("查無此人\n");
		return;
	}
	else
	{
		printf("請輸入要修改的姓名:");
		scanf("%s", ps->data[ret].name);
		printf("請輸入要修改的性別:");
		scanf("%s", ps->data[ret].sex);
		printf("請輸入要修改的年齡:");
		scanf("%d", &ps->data[ret].agel);
		printf("請輸入要修改的電話:");
		scanf("%s", ps->data[ret].tele);
		printf("請輸入要修改的地址:");
		scanf("%s", ps->data[ret].addr);
		printf("修改成功\n");
	}

}
//顯示所有聯絡人資訊
void ShowPeople(Contact *ps)
{
	if (ps->sz == 0)
	{
		printf("此通訊錄為空\n");
		return;
	}
	else
	{
		size_t i = 0;
		printf("%-5s\t%-5s\t%-3s\t%-12s\t%-12s\n", "名字", "性別", "年齡", "電話", "地址");
		for (; i < ps->sz; i++)
		{
			printf("%-5s\t%-5s\t%-3d\t%-12s\t%-17s\n",
				ps->data[i].name,
				ps->data[i].sex,
				ps->data[i].agel,
				ps->data[i].tele,
				ps->data[i].addr);
		}
	}

}

//按姓名排序通訊錄
void SortPeople(Contact *ps)
{
	if (ps->sz > 1)
	{
		size_t i = 0;
		for (; i < ps->sz - 1; i++)
		{
			size_t j = 0;
			for (; j < ps->sz - 1 - i; j++)
			{
				if (strcmp(ps->data[j].name, ps->data[j + 1].name))
				{
					People temp = ps->data[j];
					ps->data[j] = ps->data[j + 1];
					ps->data[j + 1] = temp;
				}
			}
		}
	}
	printf("排序成功\n");

}

//清空通訊錄
void EmptyPeople(Contact *ps)
{
	ps->sz = 0;
	printf("清空成功\n");
}
//初始化通訊錄
void InitContact(Contact *ps)

{
	ps->sz = 0;
	ps->data = malloc(sizeof(People)*MAX);
	if (ps->data == NULL)
	{
		perror("use malloc");
	}
	memset(ps->data, 0, sizeof(People)*MAX);
	ps->capacity = MAX;
	LoadContcat(ps);
}


void CheckCapacity(Contact *ps)
{
	if (ps->sz == ps->capacity)
	{
		People *tmp = realloc(ps->data, (ps->capacity + 10)*sizeof(People));
		if (tmp == NULL)
		{
			perror("use malloc");
			exit(0);
		}
		else
		{
			ps->data = tmp;
			ps->capacity += 10;
			printf("增容成功\n");
		}
	}
}
//保存錄入通訊錄聯絡人的資訊
void SaveContcat(Contact *ps)
{
	int i = 0;
	FILE* pfwrite = fopen("FILENAME", "w");
	if (pfwrite == NULL);
	{
		perror("use fopen to write");
		exit(0);
	}
	for (i = 0; i < ps->sz; i++)
	{
		fwrite(ps->data + i, sizeof(People), 1, pfwrite);
	}
	fclose(pfwrite);
	pfwrite = NULL;
}
//初始化的時候載入已經存在的聯絡人
void LoadContcat(Contact *ps)
{
	People tmp;
	FILE* pfread = fopen("FILENAME", "r");
	if (pfread == NULL)
	{
		perror("use fopen to read");
		exit(0);
	}
	while (fread(&tmp, sizeof(People), 1, pfread))
	{
		CheckCapacity(ps);
		ps->data[ps->sz] = tmp;
		ps->sz++;
	}
	fclose(pfread);
	pfread = NULL;
}

3.test.c


#include"contact.h"
void menu()
{
	printf("**********************************\n");
	printf("****  1.add          2.del    ****\n");
	printf("****  3.search       4.alert  ****\n");
	printf("****  5.show         6.sort   ****\n");
	printf("****  7.empty        0.exit   ****\n");
	printf("**********************************\n");
}

int main()
{
	int input = 0;

	Contact ps;
	InitContact(&ps);
	do
	{
		menu();
		printf("請選擇:");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			AddPeople(&ps);
			break;
		case DEL:
			DelePeople(&ps);
			break;
		case SEARCH:
			SearchPeople(&ps);
			break;
		case ALERT:
			AlertPeople(&ps);
			break;
		case SHOW:
			ShowPeople(&ps);
		case SORT:
			SortPeople(&ps);
		case EMPTY:
			EmptyPeople(&ps);
		default:
			printf("選擇錯誤\n");
			break;
		}
	} while (input);
	return 0;
}