1. 程式人生 > >用C語言實現一個通訊錄

用C語言實現一個通訊錄

實現一個通訊錄;
通訊錄可以用來儲存1000個人的資訊,每個人的資訊包括:
姓名、性別、年齡、電話、住址

提供方法:
1.    新增聯絡人資訊
2.    刪除指定聯絡人資訊
3.    查詢指定聯絡人資訊
4.    修改指定聯絡人資訊
5.    顯示所有聯絡人資訊
6.    清空所有聯絡人

7.    以名字排序所有聯絡人

沒有開闢動態記憶體的方法:

標頭檔案:test.h

#ifndef __CONTACT
#define __CONTACT
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define N 1000
typedef struct contact
{
	char name[30];
	char gender[10];
	int age;
	int telephone;
	char address[100];
};//結構體沒有定義變數屬於宣告一個結構體型別
void meau();
void show(struct contact *p, int len);//struct contact *p:結構體指標指向這個結構體, int len:結構體陣列的長度
void Add_linkman(struct contact  *p, int len,int flag);
int  Delete_linkman(struct contact *p, int d_number, int len);//int d_number選擇要刪除第幾個結構體的內容
void Find_member(struct contact *p, int d_number, int len);//int d_number表示要查詢的第幾個結構體的內容(即聯絡人的資訊)
void empty(struct contact *p);
void  Modify(struct contact *p, int M_member);//int M_member:要修改的第幾個聯絡人
int cmp(const void *a, const void *b);//qsort裡面的比較函式定義任意型別
void sort(struct contact *p, int len);//根據聯絡人的名字進行排序
#endif//條件編譯
contact.c檔案
#include"test.h"
void meau()
{
	printf("             *******************************************\n");
	printf("             *******************************************\n");
	printf("             ##############My address book##############\n");
	printf("             *1-Add       2-Delete       3-Find        *\n");
	printf("             *4-Empty     5-Modify       6-Sort        *\n");
	printf("             *0-Exit                     7-Show        *\n");
	printf("             *******************************************\n");
	printf("             *******************************************\n");
}
void show(struct contact *p, int len)
{
	assert(p);
	int i = 0;
	for (i = 0; i < len; i++)
	{
		printf("name:%s gender:%s age:%d telephone:%d address:%s", \
			p[i].name, p[i].gender, p[i].age, p[i].telephone, p[i].address);
		printf("\n");
	}
}
void Add_linkman(struct contact  *p, int len,int flag)
{
	assert(p);
	int i = 0;
	for (i = flag; i < len; i++)
	{
		printf("請輸入姓名:");
		scanf("%s", &p[i].name);
		printf("請輸入姓別:");
		scanf("%s", &p[i].gender);
		printf("請輸入年齡:");
		scanf("%d", &p[i].age);
		printf("請輸入電話:");
		scanf("%d", &p[i].telephone);
		printf("請輸入地址:");
		scanf("%s", &p[i].address);
	}
}
int  Delete_linkman(struct contact *p, int d_number, int len)
{
	
	assert(p);
	int i = 0;
	for (i = d_number - 1; i < len - 1; i++)
	{
		p[i] = p[i + 1];

	}
}
void Find_member(struct contact *p, int d_number, int len)
{
	assert(p);
	if (d_number - 1 >= 0 || d_number - 1<len)
	{
		printf("name:%s gender:%s age:%d telephone:%d address:%s", \
			p[d_number].name, p[d_number].gender, p[d_number].age, p[d_number].telephone, p[d_number].address);
		printf("\n");
	}
	else
	{
		printf("不存在該聯絡人:");
		return;
	}

}
void empty(struct contact *p)
{
	assert(p);
	int i = 0;
	for (i = 0; i <1000; i++)
	{
		memset(p+i, 0, sizeof(struct contact));
	}

}
void  Modify(struct contact *p, int M_member)
{
	assert(p);
	printf("修改之前聯絡人的資訊為:");
	printf("\n");
	printf("name:%s gender:%s age:%d telephone:%d address:%s", \
		p[M_member - 1].name, p[M_member - 1].gender, p[M_member - 1].age, p[M_member - 1].telephone, p[M_member - 1].address);
	printf("\n");
	printf("請輸入要修改的資訊:");
	printf("請輸入姓名:");
	scanf("%s", &p[M_member - 1].name);
	printf("請輸入姓別:");
	scanf("%s", &p[M_member - 1].gender);
	printf("請輸入年齡:");
	scanf("%d", &p[M_member - 1].age);
	printf("請輸入電話:");
	scanf("%d", &p[M_member - 1].telephone);
	printf("請輸入地址:");
	scanf("%s", &p[M_member - 1].address);

}
int cmp(const void *a, const void *b)
{

	struct contact *aa = (struct contact *)a;
	struct contact *bb = (struct contact *)b;
	if (aa->name != bb->name)
		return(strcmp((aa->name), (bb->name)));

}
void sort(struct contact *p, int len)
{
	assert(p);
	qsort(p, len, sizeof(struct contact), cmp);

}
test.c檔案:
#include"test.h"
int main()
{
	int num = 0;
	struct contact student[N];
	int len = 0;
	int flag = 0;//定義一個標誌位來結構體陣列中每個結構體的位置
	int total = N;
	int delete_number = 0;
	meau();
	while (1)
	{
		printf("請輸入數字進行選擇:");
		scanf("%d", &num);
			switch (num)
			{
			case 1:{
					   printf("請新增len個學生的資訊:");
					   scanf("%d", &len);
					   Add_linkman(student,len+flag,flag);
					   flag=flag+len;
					  
					   
			}break;

			case 2:{
					   printf("請輸入要刪除的第i個學生的資訊:");
					   scanf("%d", &delete_number);
					   Delete_linkman(student, delete_number, len+flag);
					   flag = flag - 1;
			}break;

			case 3:{
					   int Find_number = 0;
					   printf("請輸入要查詢的第i個學生的資訊:");
					   scanf("%d", &Find_number);
					   Find_member(student, delete_number, len+flag);

			}break;

			case 4:{
					   printf("清空所有聯絡人:");
					   empty(student);

			}break;

			case 5:{
					   printf("請輸入要修改的的第i個學生的資訊:");
					   int M_member = 0;
					   scanf("%d", &M_member);
					   Modify(student, M_member);

			}break;

			case 6:{
					   printf("根據名字排序所有聯絡人:\n");
					   sort(student, len+flag);

			}break;

			case 7:{
					   printf("列印所有聯絡人的資訊:\n");
					   show(student, flag);

			}break;
			case 0:{
					   exit(1);
			}

			default:printf("enter error data!!!");

		}
	}
	system("pause");
	return 0;

}