通讯录

 

 

 

 

 

 

 

 

 

 

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct contacts
{
	char name[1024];
	int age;
	char phone[1024];

}contacts;
typedef struct contact
{
	contacts persons[300];
	int size;

}contact;

contact address_book;
contact *person_info = &address_book.persons;
void initial() {
	printf("***********************\n");
	printf("1:开始\n");
	printf("0:结束\n");
	printf("***********************\n");
	address_book.size = 0;
	for (int i = 0; i < 300; ++i) {
		address_book.persons[i].name[0] = '\0';
		address_book.persons[i].phone[0] = '\0';
		address_book.persons[i].age = 0;
	}
}
void addcontacts() {
	printf("请输入联系人\n");
	contacts *info = &address_book.persons[address_book.size];
	scanf("%s", info->name);
	printf("请输入电话\n");
	scanf("%s", info->phone);
	printf("请输入年龄\n");
	scanf("%d", &info->age);
	printf("新增成功\n");
	address_book.size++;
}
void deletecontacts() {
	printf("请输入要删除的联系人id\n");
	int id = 0;
	scanf("%d", &id);
	if (id < 0 || id >= address_book.size) {
		printf("你的输入有误\n");
		return;
	}
	else if (address_book.persons[0].name[0] == '\0') {
		printf("联系人为空,删除失败\n");
		return;
	}
	else {
		address_book.persons[id] = address_book.persons[address_book.size - 1];
		address_book.size--;
		printf("删出成功\n");
	}
}
void foundcontacts() {
	printf("请输入要查找的联系人\n");
	char arr[100];
	scanf("%s", arr);

	for (int i = 0; i < address_book.size; ++i) {
		//contact *info = &address_book.persons[i];
		contacts*info = &address_book.persons[i];
		int num = strcmp(arr, info->name);
		if (num == 0) {
			printf(" [%d} %s\t %s\t %d\n ", i, person_info->persons->name, person_info->persons->phone
				, person_info->persons->age);
		}
		printf("查找完毕\n");
	}
}
void modificationcontacts() {
	printf("请输入要修改的联系人姓名\n");
	char name[100];
	scanf("%s", name);
	for (int i = 0; i < address_book.size; ++i) {
		contacts*info = &address_book.persons[i];
		int num = strcmp(name, info->name);
		if (num == 0) {

			printf(" [%d} %s\t %s\t %d\n ", i, info->name, info->phone, info->age);
			printf("1.修改姓名\n");
			printf("2.修改电话\n");
			printf("3.修改年龄\n");
			printf("4.全部修改\n");
			int input = 0;
			scanf("%d", &input);
			if (input == 1) {
				printf("修改姓名输入\n");
				scanf("%s", info->name);
			}
			if (input == 2) {
				printf("修改电话输入\n");
				scanf("%s", info->phone);
			}
			if (input == 3) {
				printf("修改年龄输入\n");
				scanf("%d", &info->age);
			}
			if (input == 4) {
				printf("修改姓名输入\n");
				scanf("%s", info->name);
				printf("修改电话输入\n");
				scanf("%s", info->phone);
				printf("修改年龄输入\n");
				scanf("%d", &info->age);
			}
		}
	}
}
void showcontacts() {
	int i = 0;
	printf("打印联系人\n");
	if (address_book.persons[i].name[0] == '\0') {
		printf("联系人列表为空\n");
		return;
	}
	for (; i < address_book.size; ++i) {
		contacts*info2 = &address_book.persons[i];
		printf(" [%d} %s\t %s\t %d\n ", i, info2->name, info2->phone, info2->age);
	}
}
void emptycontacts() {
	address_book.size = 0;
	for (int i = 0; i < 300; ++i) {
		address_book.persons[i].name[0] = '\0';
		address_book.persons[i].phone[0] = '\0';
		address_book.persons[i].age = 0;
	}
	printf("清空完毕\n");
}

void arrangecontacts(contact*info) {
	int i = 0;
	int j = 0;

	for (; i < info->size; i++) {
		for (j = 1; j < info->size; ++j) {
			

			int num = strcmp(info->persons[j].name,info->persons[i].name);
			if (num < 0) {
				contacts arr;
				arr = info->persons[j];
				info->persons[j] = info->persons[i];
				info->persons[i] = arr;
			}
		}
	}
	printf("排列完毕\n");
}
void savecontacts() {

}
void loadcontacts() {

}
void begin() {
	system("cls");
	printf("1. 添加联系人\n");
	printf("2. 删除指定联系人信息\n");
	printf("3. 查找指定联系人信息\n");
	printf("4. 修改指定联系人信息\n");
	printf("5. 显示所有联系人信息\n");
	printf("6. 清空所有联系人\n");
	printf("7. 以名字排序所有联系人\n");
	typedef void(*func)();
	func arr[] = { 0, addcontacts,deletecontacts,foundcontacts,modificationcontacts,
		showcontacts,emptycontacts,arrangecontacts };
	while (1) {
		printf("请输入你的选择\n");
		int choice;
		scanf("%d", &choice);
		if (choice < 0 || choice>7) {
			printf("你的输入有误\n");
			continue;
		}
		else {
			arr[choice]();
			break;
		}
	}
}

int main() {

	initial();
	while (1) {

		printf("是否开始\n");
		int check;
		scanf("%d", &check);
		if (check == 1) {
			begin();
		}
		else if (check == 0)
			break;
		else
			printf("你的输入有误\n");
	}
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值