https://blog.csdn.net/weixin_71138261/article/details/126999227?spm=1001.2014.3001.5501
基础版在上面的链接中已经详细解说过了
但是上一次的代码有很严重的问题:
如果我只想要储存两个人的信息,还要开辟100个人的信息吗?没有
如果储存1000000个,放不下了。
那么我们程序员到底有没有能力把这个空间变得富有弹性?
当然!!!!!!!!!!
动态内存开辟的版本来啦~
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contact.h"
void menu(void)
{
printf("**** 1.Addition 2.Delete ****\n");
printf("**** 3.Modifier 4.Sort ****\n");
printf("**** 5.Show 6.Search ****\n");
printf("**** 0.exit ****\n");
}
int main()
{
struct contact con;
Init(&con);
int a = 0;//conserve the number user inputted
do
{
menu();
scanf("%d", &a);
switch (a)
{
case 1:
{
Add(&con);
break;
}
case 2:
{
Del_name(&con);
break;
}
case 3:
{
Mod(&con);
break;
}
case 4:
{
Sort(&con);
break;
}
case 5:
{
Show(&con);
break;
}
case 6:
{
Search(&con);
break;
}
case 0:
{
Destory(&con);
printf("have exited\n");
break;
}
default:
{
printf("Please input again\n");
break;
}
}
} while (a);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contact.h"
void Init(struct contact* con)
{
assert(con);
con->date = (struct People_Information*)malloc(PRIME*sizeof(struct People_Information));
if (con->date == NULL)
{
perror("Init()");
return;
}
con->sz = 0;
con->capacity = PRIME;
}
int check(struct contact* p)
{
if (p->sz == p->capacity)
{
struct People_Information*ptr = (struct People_Information*)realloc(p->date, (p->capacity+ADD)*sizeof(struct People_Information));
if (ptr != NULL)
{
p->date = ptr;
p->capacity += ADD;
printf("success add the memory\n");
return 1;
}
else
{
perror("check()");
return 0;
}
}
else
return 1;
}
void Add(struct contact* con)
{
assert(con);
if (check(con) == 0)
{
return;
}
printf("Please input the name : \n");
scanf("%s", con->date[con->sz].name);
printf("PLease input the gender : \n");
scanf("%s", con->date[con->sz].gender);
printf("Please input the age : \n");
scanf("%d", &con->date[con->sz].age);
printf("Please input the telephone : \n");
scanf("%s", con->date[con->sz].telephone);
printf("Please input the adress : \n");
scanf("%s", con->date[con->sz].adress);
con->sz++;
printf("success! \n");
}
void Destory(struct contact*con)
{
free(con->date);
con->capacity = 0;
con->sz = 0;
con->date = NULL;
}
int find_name(struct contact* con, char* arr)
{
int i = 0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->date[i].name, arr) == 0)
{
return i;
}
}
return -1;
}
int find_gender(struct contact* con, char* arr)
{
int i = 0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->date[i].gender, arr) == 0)
{
return i;
}
}
return -1;
}
int find_age(struct contact* con, int a)
{
int i = 0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->date[i].age, a) == 0)
{
return i;
}
}
return -1;
}
int find_telephone(struct contact* con, char* arr)
{
int i = 0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->date[i].telephone, arr) == 0)
{
return i;
}
}
return -1;
}
int find_adress(struct contact* con, char* arr)
{
int i = 0;
for (i = 0; i < con->sz; i++)
{
if (strcmp(con->date[i].adress, arr) == 0)
{
return i;
}
}
return -1;
}
void Del_name(struct contact* con)
{
printf("Please input name you want to delete\n");
char arr[20];
scanf("%s", arr);
int ret = find_name(con,arr);
if (ret!=-1 )
{
printf("have fund the member\n");
int i = 0;;
for (i = ret; i < con->sz-1; i++)
{
con->date[i] = con->date[i + 1];
}
con->sz--;
printf("success\n");
}
else
{
printf("Can not find the member,please input again:\n");
}
}
void menu2(void)
{
printf("**** 1.name 2.gender ****\n");
printf("**** 3.age 4.telephone ****\n");
printf("**** 5.adress ****\n");
}
void Mod(struct contact* con)
{
printf("Please input name you want to modifier\n");
char arr[20];
scanf("%s", arr);
int ret = find_name(con, arr);
if (ret!=-1)
{
printf("have fund the member ,please choose the information:\n");
menu2();
int a = 0;
scanf("%d", &a);
switch (a)
{
case 1:
{
printf("name :\n");
scanf("%s", con->date[ret].name);
break;
}
case 2:
{
printf("gender :\n");
scanf("%s", con->date[ret].gender);
break;
}
case 3:
{
printf("age :\n");
scanf("%d", &con->date[ret].age);
break;
}
case 4:
{
printf("telephone :\n");
scanf("%s", con->date[ret].telephone);
break;
}
case 5:
{
printf("adress :\n");
scanf("%s", con->date[ret].adress);
break;
}
default:
{
printf("error\n");
return;
}
}
printf("success\n");
}
else
{
printf("Can not find the member,please input again:\n");
}
}
int com_name(void* p1, void* p2)
{
return (strcmp(((struct People_Information*)p1)->name , ((struct People_Information*)p2)->name));
}
int com_gender(void* p1, void* p2)
{
return (strcmp(((struct People_Information*)p1)->gender, ((struct People_Information*)p2)->gender));
}int com_age(void* p1, void* p2)
{
return (strcmp(((struct People_Information*)p1)->age, ((struct People_Information*)p2)->age));
}
int com_telephone(void* p1, void* p2)
{
return (strcmp(((struct People_Information*)p1)->telephone, ((struct People_Information*)p2)->telephone));
}
int com_adress(void* p1, void* p2)
{
return (strcmp(((struct People_Information*)p1)->adress, ((struct People_Information*)p2)->adress));
}
void Sort(struct contact* con)
{
menu2();
printf("Please input the element you want to sort\n");
int a = 0;
scanf("%d", &a);
switch (a)
{
case 1:
{
qsort(con->date, con->sz, sizeof(struct People_Information), com_name);
break;
}
case 2:
{
qsort(con->date, con->sz, sizeof(struct People_Information), com_gender);
break;
}
case 3:
{
qsort(con->date, con->sz, sizeof(struct People_Information), com_age);
break;
}
case 4:
{
qsort(con->date, con->sz, sizeof(struct People_Information), com_telephone);
break;
}
case 5:
{
qsort(con->date, con->sz, sizeof(struct People_Information), com_adress);
break;
}
default:
{
printf("error\n");
return;
}
}
printf("success\n");
}
void Show(struct contact*con)
{
printf("%-20s%-10s%-5s %-20s%-20s\n", "name", "gender", "age", "telephone", "adress");
for(int i=0;i<con->sz;i++)
{
printf("%-20s", con->date[i].name);
printf("%-10s", con->date[i].gender);
printf("%-5d", con->date[i].age);
printf("%-20s", con->date[i].telephone);
printf("%-20s\n", con->date[i].adress);
}
}
void Search(struct contact* con)
{
menu2();
printf("Please input the element youo want to search: \n");
int a = 0;
scanf("%d", &a);
switch (a)
{
case 1:
{
printf("Please input name\n");
char arr[20];
int ret=find_name(con,arr);
if (ret != -1)
{
printf("Have fund!\n");
printf("%-20s\n", con->date[ret].name);
printf("%-6s\n", con->date[ret].gender);
printf("%-5d\n", con->date[ret].age);
printf("%-20s\n", con->date[ret].telephone);
printf("%-20s\n", con->date[ret].adress);
con->sz--;
}
break;
}
case 2:
{
printf("Please input gender\n");
char arr[20];
int ret = find_gender(con, arr);
if (ret != -1)
{
printf("Have fund!\n");
printf("%-20s", con->date[ret].name);
printf("%-6s", con->date[ret].gender);
printf("%-5d", con->date[ret].age);
printf("%-20s", con->date[ret].telephone);
printf("%-20s\n", con->date[ret].adress);
con->sz--;
}
break;
}
case 3:
{
printf("Please input age\n");
int a=0;
int ret = find_age(con, a);
if (ret != -1)
{
printf("Have fund!\n");
printf("%-20s", con->date[ret].name);
printf("%-6s", con->date[ret].gender);
printf("%-5d", con->date[ret].age);
printf("%-20s", con->date[ret].telephone);
printf("%-20s\n", con->date[ret].adress);
con->sz--;
}
break;
}
case 4:
{
printf("Please input telephone\n");
char arr[20];
int ret = find_telephone(con, arr);
if (ret != -1)
{
printf("Have fund!\n");
printf("%-20s", con->date[ret].name);
printf("%-6s", con->date[ret].gender);
printf("%-5d", con->date[ret].age);
printf("%-20s", con->date[ret].telephone);
printf("%-20s\n", con->date[ret].adress);
con->sz--;
}
break;
}
case 5:
{
printf("Please input adress\n");
char arr[20];
int ret = find_adress(con, arr);
if (ret != -1)
{
printf("Have fund!\n");
printf("%-20s", con->date[ret].name);
printf("%-6s", con->date[ret].gender);
printf("%-5d", con->date[ret].age);
printf("%-20s", con->date[ret].telephone);
printf("%-20s\n", con->date[ret].adress);
con->sz--;
}
break;
}
default:
{
printf("error\n");
return;
}
}
printf("success\n");
}
#pragma once
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define PRIME 3
#define ADD 2
struct People_Information
{
char name[20];
char gender[6];
int age;
char telephone[20];
char adress[20];
};
struct contact
{
struct People_Information* date;
int sz;
int capacity;
};
void Init(struct contact* con);
void Add(struct contact* con);
void Del_name(struct contact* con);
void Mod(struct contact* con);
void Sort(struct contact* con);
void Show(struct contact* con);
void Search(struct contact* con);
一定要注意最后把内存释放!!!!!!!!文章来源:https://www.toymoban.com/news/detail-531285.html
这份代码还增强了上次代码不能实现的功能(更完善),让使用者有更多的选择文章来源地址https://www.toymoban.com/news/detail-531285.html
到了这里,关于【C语言】struct PLUS版~的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!