文件cotact.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "contact.h"
void InitContact(pContact pc)
{pc->count = 0 ;memset (pc->data, 0 , sizeof (pc->data));
}
void AddContact(pContact pc)
{if (pc->count < 1000 ){printf ("请输入名字>" );scanf ("%s" , pc->data[pc->count].name);printf ("请输入年龄>" );scanf ("%d" , &pc->data[pc->count].age);printf ("请输入性别>" );scanf ("%s" , pc->data[pc->count].sex);pc->count++;}else {printf ("存不下了" );return ;}
}
void ShowContact(pContact pc)
{printf ("姓名: 年龄: 性别:\n" );if (pc->count != 0 ){for (int i = 0 ; i < pc->count; i++){if (pc->data[i].age != 0 ){printf ("%d|%-8s%11d%11s\n" , i,pc->data[i].name,pc->data[i].age,pc->data[i].sex);}}}else {printf ("通讯没有号码\n" );return ;}
}
void DelContact(pContact pc)
{printf ("请输入要删除的编号" );int i = 0 ,num;scanf ("%d" , &num);for (i = num; i < pc->count; i++){pc->data[i] = pc->data[i +1 ];}
}
void justice(pContact pc)
{for (int i = 0 ; i < pc->count-1 ; i++){if (!strcmp (pc->data[pc->count - 1 ].name , pc->data[i].name)){printf ("\n非法输入\n" );pc->data[pc->count -1 ] = pc->data[pc->count];}}
}
char compare(const void * a, const void * b)
{return (*(char *)a+1 - *(char *)b);
}void Sort(pContact pc)
{int i;qsort (pc, pc->count, sizeof (people), compare);for (i=0 ; i<pc->count;i++)printf ("%d|%-8s%8d%11s\n" , i,pc->data[i].name, pc->data[i].age, pc->data[i].sex);return ;
}
int search_son(pContact pc,char *name)
{int i, j;for (i = 0 ; i < pc->count; i++){if (!strcmp (name, pc->data[i].name)){return i;}}return -1 ;
}
void Search(pContact pc)
{int i = 0 ;char name[20 ];printf ("name>" );scanf ("%s" , &name);i = search_son(pc,name);if (i >= 0 ){printf ("找到了,呜啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦!\n" );printf ("%d|%-8s%8d%11s\n" ,i,pc->data[i].name,pc->data[i].age,pc->data[i].sex);printf ("******************************************\n" );}else {printf ("Can't find it\n" );}return ;
}
main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "contact.h" void menu()
{printf ("*************************\n" );printf ("**1.add 2.show*******\n" );printf ("**3.search 4.delete*****\n" );printf ("**5.sort 0.exit*******\n" );printf ("*************************\n" );
}int main()
{int input = 0 ;Contact my_con;InitContact(&my_con);do {menu();printf ("please chose" );scanf ("%d" , &input);switch (input){case ADD:AddContact(&my_con);justice(&my_con);break ;case SHOW:ShowContact(&my_con);break ;case DEL:DelContact(&my_con);break ;case EXIT:break ;case SEARCH:Search(&my_con);break ;case SORT:Sort(&my_con);break ;default :fflush(stdin);fflush(stdout);break ;}} while (input);system("pause" );return 0 ;
}
#ifndef __CONTACT_H__
#define __CONTACT_H__
enum OPTION
{EXIT,ADD,SHOW,SEARCH,DEL,SORT
};#define MAX_NAME 20
typedef struct PeoInfo
{char name[MAX_NAME];int age;char sex[4 ];
}people;typedef struct Contact
{people data[1000 ];int count;
}Contact,*pContact;void AddContact(pContact pc);
void InitContact(pContact pc);
void ShowContact(pContact pc);
void DelContact(pContact pc);
void justice(pContact pc);
void Search(pContact pc);
void Sort(pContact pc);
#endif // !__CONTACT_H__