El lenguaje C realiza la libreta de direcciones basada en una matriz flexible (versión de longitud variable)

Antecedentes básicos

Actualice la libreta de direcciones en función de la última versión de longitud fija (consulte el blog a continuación): el
lenguaje C implementa la libreta de direcciones (versión de longitud fija)
Lenguaje C: gestión de memoria dinámica

Código

No es un detalle especial, algunas partes no están escritas, consulte la versión de longitud fija para mejorar
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;
}

contacto.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

contacto.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);
}

Supongo que te gusta

Origin blog.csdn.net/weixin_45532227/article/details/111463107
Recomendado
Clasificación