C语言--通讯录
C语言--通讯录
在Linux环境下用C语言实现通讯录,可增删查改,通过文件保存数据。
大致框架
1.头文件child.h
2.子函数child.c
3.主函数
程序代码:
child.h
#ifndef CHILD_H
#define CHILD_H
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define NAME 20
#define PHONE 12
#define SEX 5
#define ADDR 20
#define MAX 100
enum{
ext,
look,
add,
del,
alter,
find
};
struct data{
char name[NAME];
char sex[SEX];
char phone[PHONE];
char addr[ADDR];
};
struct ps{
struct data *dat;
int count;
int sz;
};
void menu();
void Init(struct ps *p);
void Look(const struct ps *p);
void Add(struct ps *p);
void Find(const struct ps *p);
void Del(struct ps *p);
void Alter(struct ps *p);
void Save(struct ps *p);
#endif
child.c
#include"child.h"
//查找功能函数
static int Find_fun(const struct ps *p, char buf[])
{
int i;
for(i=0; i < p->sz; i++)
{
if(!strcmp(p->dat[i].name, buf))
{
return i;
}
}
if(i == p->sz)
{
return -1;
}
}
//扩容函数
int kuoron(struct ps *p)
{
struct data *p2;
if(p->sz >= p->count)
{
p2 = (struct data *)realloc(p->dat, (p->count + 2) * sizeof(struct data));
if(p2 == NULL)
{
perror("kuoron");
return -1;
}
p->dat = p2;
p->count += 2;
printf("kuoron success\n");
}
return 0;
}
//用户信息保存
void Save(struct ps *p)
{
int i = 0;
struct data tmp;
FILE *fp = fopen("sujuku.txt", "w");
if(fp == NULL)
{
perror("open");
return;
}
while(i < p->sz)
{
tmp = p->dat[i++];
fwrite(&tmp, sizeof(struct data), 1, fp);
memset(&tmp, 0, sizeof(tmp));
}
fclose(fp);
fp = NULL;
}
//用户信息写入
void pepinfo(struct ps *p)
{
struct data tmp;
FILE *fp = fopen ("sujuku.txt", "r");
if(fp == NULL)
{
perror("open");
return ;
}
while(fread(&tmp, sizeof(struct data), 1, fp))
{
kuoron(p);
p->dat[p->sz] = tmp;
p->sz++;
}
fclose(fp);
fp = NULL;
}
//菜单
void menu()
{
printf(" 菜单 \n");
printf(" 1.查看 2.增加 \n");
printf(" 3.删除 4.修改 \n");
printf(" 5.查找 0.退出 \n");
}
//初始化
void Init(struct ps *p)
{
//memset(p->dat, 0, sizeof(p->dat));
int i = 0;
p->dat = (struct data *)malloc(2 * sizeof(struct data));
p->sz = 0;
p->count = 2;
pepinfo(p);
}
//查看
void Look(const struct ps *p)
{
printf("%-20s\t%-5s\t%-12s\t%-20s\n", "姓名", "性别", "电话号码", "地址");
for (int i = 0; i < p->sz; i++)
{
printf("%-20s\t%-5s\t%-12s\t%-20s\n",p->dat[i].name,
p->dat[i].sex,
p->dat[i].phone,
p->dat[i].addr);
}
}
//添加
void Add(struct ps *p)
{
int ret = kuoron(p);
if(!ret)
{
printf("请输入姓名:");
scanf("%s", p->dat[p->sz].name);
printf("请输入性别:");
scanf("%s", p->dat[p->sz].sex);
printf("请输入电话:");
scanf("%s", p->dat[p->sz].phone);
printf("请输入住址:");
scanf("%s", p->dat[p->sz].addr);
printf("添加成功\n");
p->sz++;
}
}
//查找
void Find(const struct ps *p)
{
char buff[NAME] = {0};
int ret;
printf("请输入要查找的姓名:");
scanf("%s", buff);
ret = Find_fun(p, buff);
if(ret >= 0)
{
printf("%-20s\t%-5s\t%-12s\t%-20s\n", "姓名", "性别", "电话号码", "地址");
printf("%-20s\t%-5s\t%-12s\t%-20s\n",p->dat[ret].name,
p->dat[ret].sex,
p->dat[ret].phone,
p->dat[ret].addr);
}
else
printf("没有这个人的信息\n");
}
//删除
void Del(struct ps *p)
{
char buff[NAME] = {0};
printf("请输入要删除的姓名:");
scanf("%s", buff);
int ret = Find_fun(p, buff);
if(ret >= 0)
{
printf("正在删除%s...\n",p->dat[ret].name);
strcpy(p->dat[ret].name, p->dat[p->sz-1].name);
strcpy(p->dat[ret].sex, p->dat[p->sz-1].sex);
strcpy(p->dat[ret].phone, p->dat[p->sz-1].phone);
strcpy(p->dat[ret].addr, p->dat[p->sz-1].addr);
printf("删除成功\n");
p->sz--;
}
else
printf("没有这个人的信息\n");
}
//修改
void Alter(struct ps *p)
{
char buff[NAME] = {0};
printf("请输入要修改的姓名:");
scanf("%s", buff);
int ret = Find_fun(p, buff);
if(ret >= 0)
{
printf("请重新输入姓名:");
scanf("%s", p->dat[ret].name);
printf("\n");
printf("请重新输入性别:");
scanf("%s", p->dat[ret].sex);
printf("\n");
printf("请重新输入电话:");
scanf("%s", p->dat[ret].phone);
printf("\n");
printf("请重新输入住址:");
scanf("%s", p->dat[ret].addr);
printf("修改成功\n");
}
}
tonxunlu.c
#include "child.h"
int main()
{
int input;
struct ps *p;
system("clear");
Init(p);
do{
menu();
printf("请选择 ->");
scanf("%d", &input);
switch(input)
{
case ext:
Save(p);
printf("退出\n");
break;
case look:
Look(p);
break;
case add:
Add(p);
break;
case del:
Del(p);
break;
case alter:
Alter(p);
break;
case find:
Find(p);
break;
default:
printf("输入错误,请重新输\n");
break;
}
}while(input);
return 0;
}
makefile文件(用于编译)
结果
C语言--通讯录相关教程