contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#define MAX_NAME 20
#define MAX_SEX 3
#define MAX_TELE 12
#define MAX_ADDR 20
#define MAX 1000
#include<stdio.h>
enum op
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
CLR,
SORT
};
typedef struct Peo_Info
{
char name[MAX_NAME ];
char sex[MAX_SEX ];
int age;
char tele[MAX_TELE ];
char addr[MAX_ADDR ];
}Peo_Info; //重命名
typedef struct Dhb
{
Peo_Info pinfo[MAX ];
int count;
}Dhb,* pDhb; //
void menu();
void init_dhb(pDhb pdhb);//结构体指针
void add_dhb(pDhb pdhb);
void del_dhb(pDhb pdhb);
void search_dhb(pDhb pdhb);
void modify_dhb(pDhb pdhb);
void show_dhb(pDhb pdhb);
void clear_dhb(pDhb pdhb);
void sort_dhb(pDhb pdhb);
#endif //__CONTACT_H__
contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
printf( "*************************\n" );
printf( "**** 1.add 2.del ****\n" );
printf( "**** 3.search 4.modify***\n" );
printf( "**** 5.show 6.clear ***\n" );
printf( "*****7.sort 0.exit ***\n" );
printf( "*************************\n" );
}
void add_dhb(pDhb pdhb)
{
if (pdhb ->count >= MAX)
{
printf( "电话本已满,无法添加\n" );
return;
}
printf( "名字:>" );
scanf( "%s", pdhb ->pinfo[pdhb->count].name );
//count是pdhb的成员
printf( "性别:>" );
scanf( "%s", pdhb ->pinfo[pdhb->count].sex);
printf( "年龄:>" );
scanf( "%d", &pdhb ->pinfo[pdhb->count].age);
//age是个变量,
printf( "电话:>" );
scanf( "%s", pdhb ->pinfo[pdhb->count].tele);
printf( "住址:>" );
scanf( "%s", pdhb ->pinfo[pdhb->count].addr);
pdhb->count++;
printf( "添加成功\n" );
}
void del_dhb(pDhb pdhb)
{
char name[MAX_NAME ];
int ret = 0;
printf( "请输入要删除的人的名字:>" );
scanf( "%s", name);
ret = find_entry( pdhb, name);
if (ret == -1)
{
printf( "要删除的人不存在\n" );
return;
}
else
{
int j = 0;
for (j = ret; j < pdhb ->count - 1; j++)
{
pdhb->pinfo[j] = pdhb ->pinfo[j + 1];
} //整个结构体对象可以直接赋值。
pdhb->count--;
printf( "删除成功\n" );
}
}
void search_dhb(pDhb pdhb)
{
char name[MAX_NAME ];
int ret = 0;
printf( "请输入要查找的人的名字:>" );
scanf( "%s", name);
ret = find_entry( pdhb, name);
if (ret == -1)
{
printf( "要查找的人不存在\n" );
return;
}
else
{
printf( "%10s\t%5s\t%4s\t%10s\t%10s\n" , "name", "sex", "age", "tele" , "addr");
printf( "%10s\t%5s\t%3d\t%10s\t%10s\n" ,
pdhb->pinfo[ret].name,
pdhb->pinfo[ret].sex,
pdhb->pinfo[ret].age,
pdhb->pinfo[ret].tele,
pdhb->pinfo[ret].addr);
}
}
void modify_dhb(pDhb pdhb)
{
char name[MAX_NAME ];
int ret = 0;
printf( "请输入要修改的人的名字:>" );
scanf( "%s", name);
ret = find_entry( pdhb, name);
if (ret == -1)
{
printf( "要修改的人不存在\n" );
return;
}
else
{
printf( "名字:>" );
scanf( "%s", pdhb ->pinfo[ret].name);
printf( "性别:>" );
scanf( "%s", pdhb ->pinfo[ret].sex);
printf( "年龄:>" );
scanf( "%d", &pdhb ->pinfo[ret].age);
printf( "电话:>" );
scanf( "%s", pdhb ->pinfo[ret].tele);
printf( "住址:>" );
scanf( "%s", pdhb ->pinfo[ret].addr);
}
}
void show_dhb(pDhb pdhb)
{
int i= 0;
printf( "%10s\t%5s\t%4s\t%10s\t%10s\n" , "name", "sex", "age", "tele" , "addr");
for (i = 0; i < pdhb ->count; i++)
{
printf( "%10s\t%5s\t%3d\t%10s\t%10s\n" ,
pdhb->pinfo[i].name,
pdhb->pinfo[i].sex,
pdhb->pinfo[i].age,
pdhb->pinfo[i].tele,
pdhb->pinfo[i].addr);
}
}
void clear_dhb(pDhb pdhb)
{
pdhb->count = 0;
}
void sort_dhb(pDhb pdhb)
{
int i = 0;
int j = 0;
for (i = 0; i < pdhb ->count - 1; i++)
{
for (j = 0; j < pdhb ->count - 1 - i; j++)
{
if (strcmp(pdhb ->pinfo[j].name, pdhb->pinfo[j + 1].name)>0)
{
Peo_Info tmp = pdhb ->pinfo[j];
pdhb->pinfo[j] = pdhb ->pinfo[j + 1];
pdhb->pinfo[j + 1] = tmp;
}
}
}
}
static int find_entry(pDhb pdhb,const char * name)
{
int i = 0;
for (i = 0; i < pdhb ->count; i++)
{
if (0 == (strcmp(name , pdhb->pinfo[i].name)))
{
return i;
}
}
}
void init_dhb(pDhb pdhb)
{
pdhb->count = 0;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"contact.h"
#include<stdlib.h>
int main()
{
Dhb dhb;
int input = 1;
init_dhb(&dhb);
while (input)
{
menu();
printf( "请选择>:" );
scanf( "%d", &input);
switch (input)
{
case ADD:
add_dhb(&dhb);
break;
case DEL:
del_dhb(&dhb);
break;
case SEARCH:
search_dhb(&dhb);
break;
case MODIFY:
modify_dhb(&dhb);
break;
case SHOW:
show_dhb(&dhb);
break;
case CLR:
clear_dhb(&dhb);
break;
case SORT:
sort_dhb(&dhb);
break;
case EXIT:
exit( EXIT_SUCCESS);
break;
}
}
return 0;
}
位段:填充
int 32位
struct A
{
int a : 2; //4
int b : 3;
int c : 4;
int d; //4
int e : 21; //4
int f : 15; //4 从低位到高位存储
//一共16
};
//联合
union UN
{
char c;
int i;
};
int main()
{
int a = 1;
union UN un;
un.i = 1;
if (un.c == 1)
{
printf("little\n");
}
else
{
printf("big\n");
}
}
柔性数组
结构体中的最后一个允许是一个未知大小的数组,这就叫做柔性数组的成员,但结构中的柔性数组成员前面必须至少有一个其他成员
struct mem
{
int size;
int arr[0];
}
int main()
{
//printf("%d\n", sizeof(struct Mem)); //4
PMem pmem = (PMem)malloc(sizeof(struct Mem) + 100 * sizeof(int));
int i = 0;
for (i = 0; i < 100; i++)
{
pmem->arr[i] = i;
}
for (i = 0; i < 100; i++)
{
printf("%d ", pmem->arr[i]);
}
free(pmem);
system("pause");
return 0;
}
小型电话本
原创
©著作权归作者所有:来自51CTO博客作者wx63118e2bb7416的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:栈的应用-后缀表达式
下一篇:数据结构-使用两个栈实现一个队列
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
bzoj 4175 小G的电话本
SAM+NTT+卡常
数据 #define 多项式 -
java循环判断条件符合就删除元素
一、Windows下GO开发环境的搭建及体验 使用GO前需要先搭建GO的开发环境,之前是在DEBIAN下安装了GO环境,现在WINDOWS用用看,作个简单的记录。下载解压安装/解压Golang,到下载地址:https://www.golangtc.com/download 找到对应的版本,我这里使用的是:Download and in
java循环判断条件符合就删除元素 windows golang go开发环境搭建 for循环