涉及动态内存的开辟
结构体的声明
指针的传参应用
代码如下
头文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define NAME 20
#define PHONE 20
#define ADDRESS 50
struct Person {
char name[NAME];
char sex;
int age;
char phoneNum[PHONE];
char address[ADDRESS];
char flag; //标记,=1表示人被删除
}*ppsn;
void add_person(struct Person * ppsn); //增加一个人
void del_person(struct Person * ppsn); //删除指定人
void find_person(struct Person * ppsn); //寻找指定人
void update(struct Person * ppsn); //更新人信息
void show_all(struct Person * ppsn); //显示所有人
void clear(); //清空所有人
void sort_by_name(struct Person * ppsn); //按名排序
void init(int size); //初始化
引用
#include "add_list.h"
#include <stdio.h>
#include <stdlib.h>
//通讯录目前总人数
int sumCount = 0;
//动态开辟的大小
int sizeOfAdd = 0;
void init(int size)
{
sizeOfAdd = size;
ppsn = NULL;
ppsn = (struct Person *)malloc(size * sizeof(struct Person));
}
void clear() {
free(ppsn);
ppsn = (struct Person *)malloc(sizeOfAdd * sizeof(struct Person));
if (ppsn == NULL) {
printf("清空失败");
}
}
void add_person(struct Person * ppsn)
{
ppsn += sumCount;
if (ppsn != NULL) {
printf("Please Enter Name: ");
scanf("%s", ppsn->name);
printf("Please Enter Sex: ");
scanf(" %c", &(ppsn->sex));
printf("Please Enter Age: ");
scanf(" %u", &(ppsn->age));
printf("Please Enter Tel: ");
scanf(" %s", ppsn->phoneNum);
printf("Please Enter Addr: ");
scanf(" %s", ppsn->address);
ppsn->flag = 0;
sumCount += 1;
}
}
void del_person(struct Person * ppsn)
{
char name[NAME];
printf("请输入删除人的姓名\n");
scanf("%s", name);
if (ppsn == NULL) {
return;
}
int count = sumCount;
for (; ppsn != NULL&&count; count--, ppsn++) {
if (strcmp(ppsn->name, name) == 0 && !(ppsn->flag)) {
printf("已经删除 %s %c %d %s %s\n", ppsn->name, ppsn->sex, ppsn->age, ppsn->phoneNum, ppsn->address);
ppsn->flag = 1;
return;
}
}
}
void show_all(struct Person * ppsn)
{
int count = sumCount;
while (ppsn != NULL && count--) {
if (!(ppsn->flag))
printf("%s %c %d %s %s\n", ppsn->name, ppsn->sex, ppsn->age, ppsn->phoneNum, ppsn->address);
ppsn++;
}
}
int cmp(const void *a, const void *b) {
struct Person *pa = (struct Person *)a;
struct Person *pb = (struct Person *)b;
return strcmp(pa->name, pb->name);
}
void sort_by_name(struct Person * ppsn) {
if (ppsn == NULL) {
return;
}
qsort(ppsn, sumCount, sizeof(struct Person), cmp);
}
void find_person(struct Person * ppsn) {
char name[NAME];
printf("请输入删除人的姓名\n");
scanf("%s", name);
if (NULL == name || ppsn == NULL) {
return;
}
int count = sumCount;
for (; ppsn != NULL && count; count--, ppsn++) {
if (strcmp(ppsn->name, name) == 0 && !(ppsn->flag)) {
printf("%s %c %d %s %s\n", ppsn->name, ppsn->sex, ppsn->age, ppsn->phoneNum, ppsn->address);
return;
}
}
printf("没有找到\n");
}
void update(struct Person * ppsn) {
char name[NAME];
printf("请输入删除人的姓名\n");
scanf("%s", name);
if (NULL == name || ppsn == NULL) {
return;
}
int count = sumCount;
for (; ppsn != NULL && count; count--, ppsn++) {
if (strcmp(ppsn->name, name) == 0 && !(ppsn->flag)) {
printf("找到联系人,请依次输入姓名 性别 年龄 电话 地址,中间用空格隔开\n");
scanf("%s %c %d %s %s", ppsn->name, &ppsn->sex, &ppsn->age, ppsn->phoneNum, ppsn->address);
return;
}
}
printf("没有找到\n");
}
主函数
#include "add_list.h"
#include <stdio.h>
void menu() {
printf("**************************************\n");
printf("********1.添加联系人信息 *************\n");
printf("********2.删除联系人信息 *************\n");
printf("********3.查找联系人信息 *************\n");
printf("********4.修改联系人信息 *************\n");
printf("********5.显示所有指定联系人信息******\n");
printf("********6.清空所有指定联系人信息******\n");
printf("********7.按名排序所有联系人**********\n");
printf("********0.退出************************\n");
printf("**************************************\n");
}
int main()
{
printf("请选择你想创建的通讯录大小\n");
int sizeOfAdd;
scanf("%d", &sizeOfAdd);
init(sizeOfAdd);
int chose;
do {
menu();
scanf("%d", &chose);
switch (chose) {
case 1:add_person(ppsn);
break;
case 2:del_person(ppsn);
break;
case 3:find_person(ppsn);
break;
case 4:update(ppsn);
break;
case 5:show_all(ppsn);
break;
case 6:clear(ppsn);
break;
case 7:sort_by_name(ppsn);
break;
default:break;
}
} while (chose);
system("pause");
}