#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
void test(SUM* pc)
{/*char arr[] = "222";
change(pc,arr);
print(pc);*/
int i = 0;
char arr[name_MAX];
printf("请输出指令:");
while (1)
{scanf("%d", &i);
switch (i)
{
case ADD_1:
ADD(pc);
printf("请再次输出指令:");
break;
case delet_2:
printf("请输出要删除的联系人姓名:");
scanf("%s", arr);
delet(pc, arr);
printf("请再次输出指令:");
break;
case change_3:
printf("请输出要批改的联系人姓名:");
scanf("%s", arr);
change(pc, arr);
printf("请再次输出指令:");
break;
case search_4:
printf("请输出要搜寻的联系人姓名:");
scanf("%s", arr);
print_one(pc, search(pc, arr));
printf("请再次输出指令:");
break;
case print_5:
print(pc);
printf("请再次输出指令:");
break;
case print_one_6:
printf("请输出要显示的联系人姓名:");
scanf("%s", arr);
print_one(pc, search(pc, arr));
printf("请再次输出指令:");
break;
default:
printf("输出有误!请从新输出:");
break;
}
}
return;
}
int main()
{
// 构造体初始化
SUM Data = {.people = {{"111","444","777"},{"222","555","888"},{"333","666","999"}},
.size = 3
};
test(&Data);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
// 减少联系人
void ADD(SUM* const pc)
{printf("请输出姓名:");
scanf("%s", &(pc->people[pc->size].name));
printf("请输出单位:");
scanf("%s", &(pc->people[pc->size].danwei));
printf("请输出号码:");
scanf("%s", &(pc->people[pc->size].tele));
(pc->size)++;
return;
}
// 搜寻联系人, 依据姓名查找,返回对应的下标
int search(SUM* const pc, const char * arr)
{for (int i = 0;i<pc->size;i++)
{if (strcmp(((pc->people)[i].name), arr) == 0)
{return i; // 找到了,返回对应下标}
}
return -1; //- 1 示意没找到
}
// 删除联系人
void delet(SUM* const pc, const char *arr)
{int a = search(pc,arr);
for (a;a<(pc->size-1);a++)
{pc->people[a] = pc->people[a + 1];
}
(pc->size)--;
return;
}
// 打印联系人
void print(SUM* const pc)
{for (int i = 0; i < pc->size; i++)
{printf("%s", (pc->people)[i].name);
printf("%s", (pc->people)[i].danwei);
printf("%s", (pc->people)[i].tele);
printf("\n");
}
return;
}
// 打印某一个联系人
void print_one(SUM* const pc, const int i)
{printf("%s", (pc->people)[i].name);
printf("%s", (pc->people)[i].danwei);
printf("%s", (pc->people)[i].tele);
printf("\n");
return;
}
// 改
void change(SUM* const pc, const char* arr)
{int a = search(pc, arr);
printf("请输出姓名:");
scanf("%s",pc->people[a].name);
printf("请输出单位:");
scanf("%s", pc->people[a].danwei);
printf("请输出号码:");
scanf("%s", pc->people[a].tele);
return;
}
#pragma once
#include <stdio.h>
#include <string.h>
#define name_MAX 20
#define danwei_MAX 20
#define tele_MAX 12
#define people_MAX 100
#define ADD_1 1
#define delet_2 2
#define change_3 3
#define search_4 4
#define print_5 5
#define print_one_6 6
typedef struct Contact
{char name[name_MAX];
char danwei[danwei_MAX];
char tele[tele_MAX];
}Contact;
typedef struct SUM
{Contact people[people_MAX];
int size;
}SUM;
void ADD(SUM* const pc);
void delet(SUM* const pc, const char* arr);
void change(SUM* const pc, const char* arr);
int search(SUM* const pc, const char* arr);
void print(SUM* const pc);
void print_one(SUM* const pc, const int i);