基础背景
基于上次定长版本对通讯录升级(可参考下边博客):
C语言实现通讯录(定长版本)
C语言:动态内存管理
代码
不是特别细节,有些部分没有写,可参考定长版本完善
main.c
#include "contact.h"
static void Menu() {
printf("###############################\n");
printf("# 1. add 2. del 3. search #\n");
printf("# 4. mod 5. show 6. clear #\n");
printf("# 7. sort 0. exit #\n");
printf("###############################\n");
printf("Please Select:> ");
}
int main() {
int quit = 0;
int select = 0;
contact_t *ct = NULL;
InitContact(&ct);
while (!quit){
Menu();
scanf("%d", &select);
switch (select){
case 1:
AddPersonToContact(&ct);
break;
case 2:
DelPersonFromContact(ct);
break;
case 3:
SearchContact(ct);
break;
case 4:
//ModfiyContact(ct);
break;
case 5:
ShowContact(ct, ALL);
break;
case 6:
ClearContact(ct);
break;
case 7:
SortContact(ct);
break;
default:
quit = 1;
break;
}
}
printf("再见!!!");
system("pause");
return 0;
}
contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<windows.h>
#define NUM 128
#define INIT 1
#define INC_SIZE 1
#define ALL -1
#pragma warning(disable:4996)
typedef struct person
{
char name[NUM/4];
char sex;
int age;
char tel[NUM/4];
char address[NUM];
}person_t;
typedef struct contact {
int cap;
int size;
person_t persons[0];
}contact_t;
#endif
contact.c
#include "contact.h"
static int IsFull(contact_t *ct)
{
assert(ct);
return ct->size == ct->cap;
}
static int Inc(contact_t **ct)
{
assert(*ct);
int ret = 0;
contact_t *p = (contact_t*)realloc(*ct, sizeof(contact_t)+sizeof(person_t)*((*ct)->cap + INC_SIZE));
if (NULL != p){
printf("realloc success!\n");
p->cap = (*ct)->cap + INC_SIZE;
*ct = p;
ret = 1;
}
return ret;
}
static int IsExist(contact_t *ct, const char *telphone)
{
assert(ct);
person_t *p = ct->persons;
person_t *end = ct->persons + ct->size;
while (p < end){
if (0 == strcmp(p->tel, telphone)){
return p - ct->persons;
}
p++;
}
return -1;
}
void InitContact(contact_t **ct)
{
*ct = (contact_t*)malloc(sizeof(contact_t)+INIT*sizeof(person_t));
if (NULL == *ct){
printf("malloc error!\n");
exit(1);
}
(*ct)->size = 0;
(*ct)->cap = INIT;
}
void AddPersonToContact(contact_t **ct)
{
assert(*ct);
//IsFull():1, 0
if (!IsFull(*ct) || Inc(ct)){
//add person
person_t *p = (*ct)->persons + (*ct)->size;
printf("Please Enter Name# ");
scanf(" %s", p->name);
printf("Please Enter sex# ");
scanf(" %c", &(p->sex));
printf("Please Enter age# ");
scanf(" %d", &(p->age));
printf("Please Enter telphone# ");
scanf(" %s", p->tel);
//已经存在(1, 0),满了,空间不够了
if (IsExist(*ct, p->tel) >= 0){
printf("该用户已经存在!\n");
return;
}
printf("Please Enter address# ");
scanf(" %s", p->address);
(*ct)->size += 1;
printf("add %s success!\n", p->name);
}
else{
printf("realloc error!\n");
}
}
void ShowContact(contact_t *ct, int pos)
{
assert(ct);
printf("%d/%d\n", ct->size, ct->cap);
if (pos == -1){
if (ct->size == 0){
printf("empty!\n");
}
for (int i = 0; i < ct->size; i++){
printf("%10s | %10s\n", ct->persons[i].name, ct->persons[i].tel);
}
}
else{
printf("%10s | %10s\n", ct->persons[pos].name, ct->persons[pos].tel);
}
}
void DelPersonFromContact(contact_t *ct)
{
assert(ct);
char telphone[NUM / 4];
printf("Please Enter Delete Telphone# ");
scanf("%s", telphone);
int pos = IsExist(ct, telphone);
if (pos >= 0){
ct->persons[pos] = ct->persons[ct->size - 1];
ct->size -= 1;
printf("delete %s success!\n", telphone);
}
else{
printf("delete %s is not exist!\n", telphone);
}
}
void SearchContact(contact_t *ct)
{
assert(ct);
char telphone[NUM / 4];
printf("Please Enter Search Telphone# ");
scanf("%s", telphone);
int pos = IsExist(ct, telphone);
if (-1 == pos){
printf("user %s is not exist!\n", telphone);
}
else{
ShowContact(ct, pos);
}
}
void ClearContact(contact_t *ct)
{
assert(ct);
ct->size = 0;
}
void SortContact(contact_t *ct)
{
assert(ct);
//qsrot(ct->persons, ct->size, sizeof(person_t), compPerson);
}