记录一下鹏哥C语言上的小作业
contact.h
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
//类型声明
typedef struct PeoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo;
typedef struct Contact
{
PeoInfo data[MAX];
int count;
}Contact;
void InitContact(Contact* pc);
void AddContact(Contact* pc);
void DelContact(Contact* pc);
void SearchContact(Contact* pc);
void ModifyContact(Contact* pc);
void ShowContact(const Contact* pc);
void SortContact(Contact* pc);
void SearchContact(const Contact* pc);
void ModifyContact(Contact* pc);
void SortContact(Contact* pc);
contact.c
#include "contact.h"
void InitContact(Contact* pc)
{
assert(pc);
pc->count = 0;
memset(pc->data, 0, sizeof(pc->data));
}
void AddContact(Contact* pc)
{
assert(pc);
if (pc->count == MAX)
{
printf("通讯录已满,无法添加!\n");
return;
}
printf("请输入姓名:\n->");
scanf("%s", pc->data[pc->count].name);
printf("请输入年龄:\n->");
scanf("%d", &(pc->data[pc->count].age));
printf("请输入性别:\n->");
scanf("%s", pc->data[pc->count].sex);
printf("请输入电话:\n->");
scanf("%s", pc->data[pc->count].tele);
printf("请输入地址:\n->");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf("添加完成\n");
}
void ShowContact(const Contact* pc)
{
assert(pc);
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
static int FindByName(Contact* pc, char name[])
{
assert(pc);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (0 == strcmp(pc->data[i].name, name))
{
return i;
}
}
return -1;
}
void DelContact(Contact* pc)
{
assert(pc);
int i = 0;
char name[MAX_NAME] = { 0 };
if (pc->count == 0)
{
printf("通讯录为空,没有元素可删除!");
return;
}
printf("请输入被删除人姓名:\n->");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("被删除人不存在!\n");
return;
}
for (i = pos; i < pc->count - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->count--;
printf("删除成功\n");
}
void SearchContact(const Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
printf("请输入要查找人的姓名:\n->");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要查找的人不存在!\n");
return;
}
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
void ModifyContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
printf("请输入要修改人的姓名:\n->");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要修改的人不存在!\n");
return;
}
printf("请输入姓名:\n->");
scanf("%s", pc->data[pos].name);
printf("请输入年龄:\n->");
scanf("%d", &(pc->data[pos].age));
printf("请输入性别:\n->");
scanf("%s", pc->data[pos].sex);
printf("请输入电话:\n->");
scanf("%s", pc->data[pos].tele);
printf("请输入地址:\n->");
scanf("%s", pc->data[pos].addr);
printf("修改完成\n");
}
int CmpPeoByName(const void* e1, const void* e2)
{
return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{
assert(pc);
qsort(pc->data, pc->count, sizeof(PeoInfo), CmpPeoByName);
printf("排序完成\n");
}
test.c
#include "contact.h"
void menu()
{
printf("------------------------------\n");
printf("| 1.add 2.del |\n");
printf("| 3.search 4.modify |\n");
printf("| 5.show 6.sort |\n");
printf("| 0.exit |\n");
printf("------------------------------\n");
}
int main()
{
Contact con;
InitContact(&con);
int input = 0;
do
{
menu();
printf("请选择:\n->");
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 6:
SortContact(&con);
break;
case 0:
printf("退出通讯录...\n");
break;
default:
printf("选择错误!\n->");
break;
}
} while (input);
}
Comments | NOTHING