利用動态記憶體開辟,以減少記憶體浪費問題
//contast.h頭檔案子產品
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct phone
{
//一個人的資訊
char name[10];
char sex[10];
int age;
char tele[12];//電話
char addr[20];//住址
};
//通訊錄構成
struct contact
{
struct phone *s1;//指向一塊空間
int size;//記錄目前已有的資訊
int phonemax;//目前通訊錄容量 當max==size時擴容
};
//聲明函數
void Initcontact(struct contact* ps);
void addcontact(struct contact* ps);
void showcontact(const struct contact* ps);
void delcontact(struct contact* ps);
void searchcontact(const struct contact* ps);
void modifycontact(struct contact* ps);
void destroy_memory(struct const* ps);
//contast.c子產品
#include "contast.h"
void Initcontact(struct contact* ps)//開辟空間、初始化
{
ps->s1 =(struct phone*) malloc(3 * sizeof(struct phone));
if (ps->s1 == NULL)
return;
ps->size = 0;
ps->phonemax = 3;//初始3個結構體記憶體
}
void memory_ptr(struct contact* ps)
{
if (ps->size == ps->phonemax)
{ //增容
struct phone*ptr = (struct phone*)realloc(ps->s1, (ps->phonemax + 2) * sizeof(struct phone));
if (ptr != NULL)
{
ps->s1 = ptr;
ps->phonemax += 2;
printf("增容成功\n");
}
else
printf("增容失敗\n");
}
}
void addcontact(struct contact* ps)
{
memory_ptr(ps);//檢測目前通訊錄容量
//增加對象
printf("請輸入姓名:");
scanf("%s",ps->s1[ps->size].name);
printf("請輸入姓别:");
scanf("%s", ps->s1[ps->size].sex);
printf("請輸入年齡:");
scanf("%d", &ps->s1[ps->size].age);
printf("請輸入電話:");
scanf("%s", ps->s1[ps->size].tele);
printf("請輸入位址:");
scanf("%s", ps->s1[ps->size].addr);
ps->size++;//增加
printf("添加成功\n");
}
void showcontact(const struct contact* ps)
{
//顯示
if (ps->size == 0)
{
printf("通訊錄為空\n");
}
else
{
int i = 0;
//标題
printf("%-20s\t%-5s\t%-4s\t%-12s\t%-20s\n","姓名","性别","年齡","電話","位址");
//資料
for (i = 0; i < ps->size; i++)
{
printf("%-20s\t%-5s\t%-4d\t%-12s\t%-20s\n", ps->s1[i].name,ps->s1[i].sex,
ps->s1[i].age, ps->s1[i].tele, ps->s1[i].addr);
}
printf("\n");
}
}
static int findname(const struct contact* ps, char name[20])
//del、search、modify函數都要使用,static讓findname函數隻能在目前子產品函數使用
{
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (strcmp(ps->s1[i].name, name) == 0)
{
return i;
}
}
return -1;//找不到
}
void delcontact( struct contact* ps)
{
//聯系人删除
char name[20];
printf("請輸入要删除人的名字:");
scanf("%s",name);
int pos = findname(ps, name);
//找到傳回元素下标
//找不到傳回 -1
if (pos == -1)
{
printf("要删除人的資訊不存在\n");
}
else
{
//删除
int j = 0;
for (j = pos; j < ps->size - 1; j++)
{
ps->s1[j] = ps->s1[j + 1];//後面的放前面
}
ps->size--;
printf("删除成功\n");
}
}
void searchcontact(const struct contact* ps)
{
//查找聯系人
char name[20];
printf("請輸入要查找人的資訊:");
scanf("%s",name);
int pos = findname(ps, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
}
else//找到列印下标
{
//标題
printf("%-20s\t%-5s\t%-4s\t%-12s\t%-20s\n", "姓名", "性别", "年齡", "電話", "位址");
//資料
printf("%-20s\t%-5s\t%-4d\t%-12s\t%-20s\n", ps->s1[pos].name, ps->s1[pos].sex,
ps->s1[pos].age, ps->s1[pos].tele, ps->s1[pos].addr);
}
}
void modifycontact(struct contact* ps)
{
//修改聯系人
char name[20];
printf("請輸入要修改人的資訊:");
scanf("%s", name);
int pos = findname(ps, name);
if (pos == -1)
{
printf("要修改人的資訊不存在\n");
}
else
{
printf("請輸入姓名:");
scanf("%s", ps->s1[pos].name);
printf("請輸入姓别:");
scanf("%s", ps->s1[pos].sex);
printf("請輸入年齡:");
scanf("%d", &ps->s1[pos].age);
printf("請輸入電話:");
scanf("%s", ps->s1[pos].tele);
printf("請輸入位址:");
scanf("%s", ps->s1[pos].addr);
printf("修改成功\n");
}
}
void destroy_memory(struct contact* ps)
{
ferr(ps->s1);
ps->s1=NULL;
}
//main.c主函數子產品
#include "contast.h"
void menu()
{
printf(" 歡迎來到通訊錄小程式 \n");
printf("*************************************\n");
printf("********1、add 2、del************\n");
printf("********3、search 4、modify*********\n");
printf("********5、show 0、exit***********\n");
printf("**************************************\n");
printf("**************************************\n");
printf("**************************************\n");
}
int main()
{
int input = 0;
//建立通訊錄
struct contact con;//date指針和size、phonemax
Initcontact(&con);
do
{
menu();
printf("請選擇:");
scanf("%d", &input);
switch (input)
{
case 1:
addcontact(&con);
break;
case 2:
delcontact(&con);
break;
case 3:
searchcontact(&con);
break;
case 4:
modifycontact(&con);
break;
case 5:
showcontact(&con);
break;
case 0:
destroy_memory(&con);//釋放記憶體
printf("謝謝使用,已退出通訊錄\n");
break;
default :
printf("選擇錯誤\n");
break;
}
} while (input);
return 0;
}