C语言课设模板(通讯录可修改)

如果需要对课设的论述可以
走小道找chat
通讯录的基本实现

Contact.c

#include"function.h"
typedef enum c
{
	EXit,
	ADd,
	FInd,
	DIsplay,
	SOrt,
	DElete,
	RIvers
};

int main()
{
	//建立菜单打印
	//增加 查找 显示 退出
	//查找方式:
	//找到后 改 删 
	int c1 = 0;
	Contact CON;
	//初始化通讯录
	Inicon(&CON);
	//读取数据
	my_fscanf(&CON);
	do
	{
		meun();
		scanf("%d", &c1);
		if (c1 < 0 || c1>6)
		{
			printf("输入错误请重新输入!\n");
			continue;
		}
		else if (c1 == EXit)//退出并保存
		{
			my_fprintf(&CON);
			printf("退出成功!\n");
			return 1;
		}
		else if (c1 == ADd)//增
		{
			Add(&CON);
		}
		else if (c1 == FInd)//查
		{
			int ret = my_find(&CON);
		}
		else if (c1 == DIsplay)//显示全部
		{
			dispaly(&CON);
		}
		else if (c1 == SOrt)
		{
			printf("请选择排序的方式。\n");
			meun_sort();
			my_sort(&CON);
		}
		else if (c1 == DElete)
		{
			printf("请输入要删除的人名字:\n");
			char s[20] = { 0 };
			scanf("%s", s);
			delete_name(&CON, s);
		}
		else if (c1 == RIvers)
		{
			int ret = my_rivers(&CON);
		}
		printf("三秒后清理屏幕\n");
		Sleep(3000);
		system("cls");
	} while (c1);
	return 0;
}

function.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<windows.h>
#define Name_max 20
#define Sex_max 5
#define Number_max 12
#define Address_max 20
#define Member_max 1000
typedef struct member
{
	char name[Name_max];
	int age;
	char sex[Sex_max];
	char number[Number_max];
	char Address[Address_max];
}member;
typedef struct Contact
{
	member data[Member_max];
	int sz;
}Contact;

//显示刚进入的菜单可选择 增 查 显示 排序 退出;
void meun();

//初始化函数
void Inicon(Contact* Con);

//增加函数
void Add(Contact* Con);

//显示全部通讯录
void dispaly(Contact* Con);

//按什么查找联系人
void meun_find();

//查找按查找方式某个人或某些人
int find_name(Contact* Con, const char* s);
int find_age(Contact* Con, const int age);
int find_sex(Contact* Con, const char* s);
int find_number(Contact* Con, const char* s);
int find_Address(Contact* Con, const char* s);

//总查找
int my_find(Contact* CON);

//排序
void meun_sort();
void my_sort(Contact* Con);
int com_name(void* e1, void* e2);
int com_age(void* e1, void* e2);
int com_number(void* e1, void* e2);
int com_Address(void* e1, void* e2);

//按名字删除
void delete_name(Contact* Con, const char* s);

//修改rivers
int my_rivers(Contact* Con);

//保存文件
void my_fprintf(Contact* Con);

//读取文件
void my_fscanf(Contact* Con);

function.c

#include"function.h"
void meun()
{
	printf("****************************\n");
	printf("***  1.add       2.find  ***\n");
	printf("***  3.display   4.sort  ***\n");
	printf("***  5.delete    0.exit  ***\n");
	printf("***  6.rivers            ***\n");
	printf("***  请输入要进行的选项  ***\n");
	printf("****************************\n");
}

void meun_find()
{
	printf("*******************************\n");
	printf("*** 1.按名字查找  2.按年龄查找 ***\n");
	printf("*** 3.按性别查找  4.按电话查找 ***\n");
	printf("*** 5.按地址查找  0.取消查找   ***\n");
	printf("*******************************\n");
}

void meun_sort()
{
	printf("**************************\n");
	printf("***  请选择排序的方式:***\n");
	printf("***  1.按名字排序      ***\n");
	printf("***  2.按年龄排序      ***\n");
	printf("***  3.按电话排序      ***\n");
	printf("***  4.按地址排序      ***\n");
	printf("**************************\n");
}

void Inicon(Contact* Con)
{
	Con->sz = 0;
	memset(Con->data, '0', sizeof(Con->data));
}

void Add(struct Contact* Con)
{
	if (Con->sz == Member_max)
	{
		printf("通讯录已满,无法添加!\n");
		return;
	}
	printf("请输入名字:\n");
	scanf("%s", Con->data[Con->sz].name);
	printf("请输入年龄:\n");
	scanf("%d", &Con->data[Con->sz].age);
	printf("请输入性别:\n");
	scanf("%s", Con->data[Con->sz].sex);
	printf("请输入电话:\n");
	scanf("%s", Con->data[Con->sz].number);
	printf("请输入住址:\n");
	scanf("%s", Con->data[Con->sz].Address);
	Con->sz++;
	return;
}

void dispaly(Contact* Con)
{
	printf("\t%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n", "姓名", "年龄", "性别", "电话", "地址");
	for (int i = 0; i < Con->sz; i++)
	{
		printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
	}
}

int find_name(Contact* Con, const char* s)
{
	for (int i = 0; i < Con->sz; i++)
	{
		if (strcmp(Con->data[i].name, s) == 0)
		{
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			return i;
		}
	}
	printf("没找到!\n");
	return -1;
}

int find_age(Contact* Con, const int age)
{
	for (int i = 0; i < Con->sz; i++)
	{
		if (Con->data[i].age == age)
		{
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			return i;
		}
	}
	printf("没找到!\n");
	return -1;
}

int find_sex(Contact* Con, const char* s)
{
	for (int i = 0; i < Con->sz; i++)
	{
		if (strcmp(Con->data[i].sex, s) == 0)
		{
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			return i;
		}
	}
	printf("没找到!\n");
	return -1;
}

int find_number(Contact* Con, const char* s)
{
	for (int i = 0; i < Con->sz; i++)
	{
		if (strcmp(Con->data[i].number, s) == 0)
		{
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			return i;
		}
	}
	printf("没找到!\n");
	return -1;
}

int find_Address(Contact* Con, const char* s)
{
	for (int i = 0; i < Con->sz; i++)
	{
		if (strcmp(Con->data[i].Address, s) == 0)
		{
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			return i;
		}
	}
	printf("没找到!\n");
	return -1;
}

void my_sort(Contact* Con)
{
	if (Con->sz == 0 || Con->sz == 1)
	{
		printf("联系人太少无法排序:\n");
		return;
	}
	int so = 1;
	scanf("%d", &so);
	if (so == 1)
	{
		qsort(Con->data, Con->sz, sizeof(Con->data[0]), com_name);
		printf("按名字排序:\n");
	}
	else if (so == 2)
	{
		qsort(Con->data, Con->sz, sizeof(Con->data[0]), com_age);
		printf("按年龄排序:\n");
	}
	else if (so == 3)
	{
		qsort(Con->data, Con->sz, sizeof(Con->data[0]), com_number);
		printf("按电话号排序:\n");
	}
	else if (so == 4)
	{
		qsort(Con->data, Con->sz, sizeof(Con->data[0]), com_Address);
		printf("按地址排序:\n");
	}
	else
		printf("输入错误没有排序。\n");
	dispaly(Con);
}

int com_name(void* e1, void* e2)
{
	return strcmp(((member*)e1)->name, ((member*)e2)->name);
}

int com_age(void* e1, void* e2)
{
	return *(int*)e1 - *(int*)e2;
}

int com_number(void* e1, void* e2)
{
	return strcmp(((member*)e1)->number, ((member*)e2)->number);
}

int com_Address(void* e1, void* e2)
{
	return strcmp(((member*)e1)->Address, ((member*)e2)->Address);
}

void delete_name(Contact* Con, const char* s)
{
	assert(Con);
	if (Con->sz == 0)
	{
		printf("没有联系人无法删除!\n");
		return;
	}
	int ret = 0;
	for (int j = 0; j < Con->sz; j++)
	{
		if (strcmp(Con->data[j].name, s) == 0)
		{
			ret = j;
			printf("删除了:\n");
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[j].name, Con->data[j].age, Con->data[j].sex, Con->data[j].number, Con->data[j].Address);
			for (int i = ret; i < Con->sz - 1; i++)
			{
				Con->data[i] = Con->data[i + 1];
			}
			Con->sz--;
			return;
		}
	}
	printf("并没有找到要删除的人,请查找正确后在删除!\n");
}

int my_find(Contact* CON)
{
	assert(CON);
	int c2 = 0;
	printf("请选择查找方式:\n");
	meun_find();
	scanf("%d", &c2);
	int ret = -1;
	while (c2 > 0 && c2 <= 5)
	{
		if (c2 == 1)
		{
			printf("请输入要查找的名字:\n");
			char s[20] = { 0 };
			scanf("%s", s);
			ret = find_name(&CON, s);
		}
		else if (c2 == 2)
		{
			printf("请输入要查找的年龄:\n");
			int age_f = 0;
			scanf("%d", &age_f);
			ret = find_age(&CON, age_f);
		}
		else if (c2 == 3)
		{
			printf("请输入要查找性别:\n");
			char s[5] = { 0 };
			scanf("%s", s);
			ret = find_sex(&CON, s);
		}
		else if (c2 == 4)
		{
			printf("请输入要查找的电话号:\n");
			char s[12] = { 0 };
			scanf("%s", s);
			ret = find_number(&CON, s);
		}
		else if (c2 == 5)
		{
			printf("请输入要查找的地址:\n");
			char s[20] = { 0 };
			scanf("%s", s);
			ret = find_Address(&CON, s);
		}
		else
		{
			printf("输入错误重新输入!\n");
		}
	}
	return ret;
}

int my_rivers(Contact* Con)
{
	printf("请输入要修改的人名字:\n");
	char s[20] = { 0 };
	scanf("%s", s);
	for (int i = 0; i < Con->sz; i++)
	{
		if (strcmp(Con->data[i].name, s) == 0)
		{
			printf("这就是该联系人的信息:");
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			printf("请输入名字:\n");
			scanf("%s", Con->data[i].name);
			printf("请输入年龄:\n");
			scanf("%d", &Con->data[i].age);
			printf("请输入性别:\n");
			scanf("%s", Con->data[i].sex);
			printf("请输入电话:\n");
			scanf("%s", Con->data[i].number);
			printf("请输入住址:\n");
			scanf("%s", Con->data[i].Address);
			printf("将原联系人修改为:\n");
			printf("\t%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", Con->data[i].name, Con->data[i].age, Con->data[i].sex, Con->data[i].number, Con->data[i].Address);
			return i;
		}
	}
}

void my_fprintf(Contact* Con)
{
	FILE* fp = fopen("text.txt", "ab+");
	if (fp == NULL)
	{
		perror("fopen:\n");
	}
	for (int i = 0; i < Con->sz; i++)
	{
		fwrite(Con->data, sizeof(member), 1, fp);
	}
	printf("保存成功!\n");
	fclose(fp);
	fp = NULL;
}

void my_fscanf(Contact* Con)
{
	FILE* fp = fopen("text.txt", "ab+");
	if (fp == NULL)
	{
		perror("fopen:\n");
	}
	member ptr = { 0 };
	int i = 0;
	while (fread(&ptr, sizeof(member), 1, fp))
	{
		Con->data[i] = ptr;
		Con->sz++;
		i++;
	}
	printf("成功读取文件!\n");
	fclose(fp);
	fp = NULL;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值