주소록 1 동적 메모리 개발

4452 단어 데이터 구조
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include

struct stu
{
	char name[5];
	char sex[2];
	int age;
	char  phone[15];
	char add[20];
};
typedef struct info
{
	struct stu *n;
	int  size;
}stu_info;
void init_info(stu_info *p)
{
	assert(p);
	int i = 0;
	int j = 0;
	p->n = (struct stu*)malloc(sizeof(struct stu) * 1000);
	printf("              :");
	scanf("%d", &i);
	if (i > 0 && i <= 1000)
	{
		for (;j < i;j++)
		{
			printf("    %d       :", j + 1);
			scanf("%s", &p->n[j].name);
			printf("    %d       :", j + 1);
			scanf("%s", &p->n[j].sex);
			printf("    %d       :", j + 1);
			scanf("%d", &p->n[j].age);
			printf("    %d       :", j + 1);
			scanf("%s", &p->n[j].phone);
			printf("    %d       :", j + 1);
			scanf("%s", &p->n[j].add);
			p->size++;
		}
	}
}
void print_info(stu_info *p)
{
	assert(p);
	int i = 0;
	int j = p->size;
	printf("name\tsex\tage\tphone\tadd
"); while (j--) { printf("%s\t", p->n[i].name); printf("%s\t", p->n[i].sex); printf("%d\t", p->n[i].age); printf("%s\t", p->n[i].phone); printf("%s
", p->n[i].add); i++; } } void insert_info(stu_info *p) { assert(p); int j = 0; printf(" %d :", j + 1); scanf("%s", &(p->n[p->size].name)); printf(" %d :", j + 1); scanf("%s", &(p->n[p->size].sex)); printf(" %d :", j + 1); scanf("%d", &(p->n[p->size].age)); printf(" %d :", j + 1); scanf("%s", &(p->n[p->size].phone)); printf(" %d :", j + 1); scanf("%s", &(p->n[p->size].add)); p->size++; } void delete_info(stu_info *p) { int i = 0; int j = p->size; char name[20] = { 0 }; assert(p); printf(" :"); scanf("%s", &name); for (;i < j;i++) { if (strcmp(p->n[i].name, name) == 0) { for (;i < j - 1;i++) { p->n[i] = p->n[i + 1]; } p->size--; return; } } printf("
"); } void search_info(stu_info *p) { int i = 0; int j = p->size; char name[20] = { 0 }; printf(" :"); scanf("%s", &name); for (;i < j;i++) { if (strcmp(p->n[i].name, name) == 0) { printf(" :
"); printf("name\tsex\tage\tphone\tadd
"); printf("%s\t%s\t%d\t%s\t%s
", p->n[i].name, p->n[i].sex, p->n[i].age, p->n[i].phone, p->n[i].add); return; } } printf(" "); } void modify_info(stu_info *p) { int i = 0; int j = p->size; char name[20] = { 0 }; printf(" :"); scanf("%s", &name); for (;i < j;i++) { if (strcmp(p->n[i].name, name) == 0) { printf(" :
"); scanf("%s", &p->n[i].name); printf(" :
"); scanf("%s", &p->n[i].sex); printf(" :
"); scanf("%d", &p->n[i].age); printf(" :
"); scanf("%s", &p->n[i].phone); printf(" :
"); scanf("%s", &p->n[i].add); printf(" !
"); } } } void clear_info(stu_info *p) { memset(p->n, '0', sizeof(struct stu) * 1000); } void sort_info(stu_info *p) { int i = 0; int k = 0; int j = p->size; struct stu n = { 0 }; printf(" :
"); for (i=0;i < j - 1;i++) { for (k = 0;k < j - i - 1;k++) { if (p->n[k].name < p->n[k + 1].name) { n = p->n[k]; p->n[k] = p->n[k + 1]; p->n[k + 1] = n; } } } } int main() { stu_info p; p.size = 0; init_info(&p); // print_info(&p);// insert_info(&p);// print_info(&p); delete_info(&p);// print_info(&p); search_info(&p);// modify_info(&p);// print_info(&p); sort_info(&p);// print_info(&p); clear_info(&p);// free(p.n); system("pause"); return 0; }

좋은 웹페이지 즐겨찾기