c语言通讯录人数显示,c语言实现通讯录
#include#define MAXN 20
#define MAXS 5
#define MAXA 15
static count = 1;
static int peocnt = 1;
typedef struct
{
char name[MAXN];
char sex[MAXS];
char addr[MAXA];
char num[11];
int age;
}stu;
enum optin//枚舉類型(用在main函數的switch分支語句中,免得寫出挫挫的case 1之類的代碼)
{
EXIT,//0
ADD,//1
DEL,//2
SEARCH,//3
MODIFY,//4
SHOW,//5
EMPTY,//6
};
void menu()
{
printf("*************************************************************\n");
printf("****** 1增加通訊錄人數 2刪除通訊錄人數 *******\n");
printf("****** 3查找通訊錄 4修改通訊錄 *******\n");
printf("****** 5顯示通訊錄 6清空通訊錄 *******\n");
printf("****** 0退出通訊錄 *******\n");
printf("*************************************************************\n");
}
void Add(stu* p)
{
if (count > peocnt)
{
stu *p1 = realloc(p,count*sizeof(stu));
if (p1 != NULL)
p = p1;
printf("增容成功!\n");
}
printf("請輸入姓名:\n");
scanf("%s", p[count-1].name);
printf("請輸入性別:\n");
scanf("%s", p[count - 1].sex);
printf("請輸入地址:\n");
scanf("%s", p[count - 1].addr);
printf("請輸入電話:\n");
scanf("%s", p[count - 1].num);
printf("請輸入年齡:\n");
scanf("%d", &p[count - 1].age);
count++;
}
void Del(stu* p)
{
int i = 0;
char tmp[MAXN] = { 0 };
printf("請輸入你要刪除的名字:\n");
scanf("%s", tmp);
for (i = 0; i < count-1; i++)
{
if (strcmp(tmp, p[i].name) == 0)
{
strcpy(p[i].name, p[count - 2].name);
strcpy(p[i].sex, p[count - 2].sex);
strcpy(p[i].addr, p[count - 2].addr);
strcpy(p[i].num, p[count - 2].num);
p[i].age = p[count - 2].age;
count--;
return;
}
}
if (i == count-1)
printf("查無此人\n");
}
void Search(stu* p)
{
char tmp[MAXN] = { 0 };
int i = 0;
printf("請輸入你要查找的人:\n");
scanf("%s", tmp);
for (i = 0; i < count-1; i++)
{
if (strcmp(tmp, p[i].name) == 0)
{
printf("姓名 年齡 性別 地址 電話\n");
printf("%5s %5d %5s %5s %5s", p[i].name, p[i].age, p[i].sex, p[i].addr, p[i].num);
printf("\n");
break;
}
}
if (i == count-1)
printf("查無此人\n");
}
void Modify(stu* p)
{
int i = 0;
char tmp[MAXN] = { 0 };
printf("請輸入你要修改的人名;\n");
scanf("%s", tmp);
for (i = 0; i < count-1; i++)
{
if (strcmp(tmp, p[i].name) == 0)
{
printf("請輸入姓名:\n");
scanf("%s", p[i].name);
printf("請輸入性別:\n");
scanf("%s", p[i].sex);
printf("請輸入地址:\n");
scanf("%s", p[i].addr);
printf("請輸入電話:\n");
scanf("%s", p[i].num);
printf("請輸入年齡:\n");
scanf("%d", &p[i].age);
}
}
if (i == count-1)
printf("查無此人\n");
}
void Show(stu* p)
{
int i = 0;
printf("姓名 年齡 性別 地址 電話\n");
for (i = 0; i < count-1; i++)
{
printf("%5s %5d %5s %5s %5s", p[i].name, p[i].age, p[i].sex, p[i].addr, p[i].num);
printf("\n");
}
}
void Empty(stu* p)
{
count = 0;
}
int main()
{
int input = 1;
stu *p = NULL;
stu *p1 = malloc(peocnt*sizeof(stu));
if (p1 != NULL)
p = p1;
else
perror("malloc:\n");
memset(p, 0, peocnt*sizeof(stu));
while (input)
{
menu();
printf("請輸入選項>");
scanf("%d", &input);
switch (input)
{
case ADD:
Add(p);
break;
case DEL:
Del(p);
break;
case SEARCH:
Search(p);
break;
case MODIFY:
Modify(p);
break;
case SHOW:
Show(p);
break;
case EMPTY:
Empty(p);
break;
case EXIT:
break;
default:
printf("選擇有誤!\n");
break;
}
}
system("pause");
return 0;
}
總結
以上是生活随笔為你收集整理的c语言通讯录人数显示,c语言实现通讯录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5高仿mac桌面,WinDyna
- 下一篇: 【自动驾驶轨迹规划之RRT算法】