在C语言的学习过程中,总想着做一个小程序,但又始终不知道应该做个什么小程序比较好,今天闲着的时候做了一个简单版的通讯录。
首先来看一下设计的大体思路:
通讯录可以用来存储1000个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址。
功能有:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
下面附上代码:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<windows.h>
#define Max_Words 20
#define Single_Words 10
#define N 1000
typedef struct contacts
{
char name[Max_Words];
char sex[Single_Words];
char address[Max_Words];
char Phone_Num[Max_Words];
char age[Max_Words];
struct contacts * Next;
}Contacts ,* pContacts;
Contacts * ListCreate()
{
pContacts pList, Tail;
int i = 0, j = 0;
pList = (Contacts *)malloc(sizeof(Contacts));
Tail = pList;
if (NULL == pList)
{
printf("申请内存空间失败!\n");
exit(0);
}
strcpy(pList->name , "姓名");
strcpy(pList->age , "年龄");
strcpy(pList->address, "地址");
strcpy(pList->Phone_Num , "电话");
strcpy(pList->sex , "性别");
pList->Next = NULL;
return pList;
}
Contacts * Add_Contacts(Contacts * L, Contacts * pCurrent, int num) //添加联系人信息
{
pContacts pList, Tail, p, Final_L;
int i = 0, j = 0;
assert(pCurrent);
pList = (Contacts *)malloc(sizeof(Contacts));
Tail = pList;
if (NULL == pList)
{
printf("申请内存空间失败!\n");
exit(0);
}
else
{
for (j = 0; j < num; j++)
{
p = (Contacts *)malloc(sizeof(Contacts));
strcpy(p->address, pCurrent[j].address);
strcpy(p->name, pCurrent[j].name);
strcpy(p->sex, pCurrent[j].sex);
strcpy(p->Phone_Num, pCurrent[j].Phone_Num);
strcpy(p->age , pCurrent[j].age);
Tail->Next = p;
Tail = p;
}
Tail->Next = NULL;
}
Final_L = L;
while (L->Next != NULL)
{
L = L->Next;
}
L->Next = pList->Next;
return Final_L;
}
void Del_Contacts(Contacts * L , char * pName) //删除指定联系人信息
{
pContacts p, pLast;
int flag = 0;
p = L->Next;
pLast = L;
if (NULL == p)
{
printf("没有可删除的联系人信息:\n");
}
else
{
while (p!=NULL)
{
if (strcmp(p->name , pName)== 0)
{
pLast->Next = p->Next;
flag = 1;
}
pLast = p;
p = p->Next;
}
if (flag)
{
printf("删除成功!\n");
}
else
{
printf("删除失败,找不到这个联系人的信息\n");
}
}
}
Contacts * Find_Contacts(Contacts * L , char * pMation) //查找指定联系人信息
{
Contacts * p = NULL;
p = L->Next;
while (p!=NULL)
{
if (strcmp(p->name , pMation)== 0 ||strcmp(p->Phone_Num , pMation) == 0)
{
return p;
}
else
{
p = p->Next;
}
}
return NULL;
}
void Revise_Contacts(Contacts * L , char * pMation) //修改指定联系人信息
{
Contacts * p = NULL;
int choice = 0;
int flag = 0;
char New_Id[Max_Words];
p = L->Next;
while (p != NULL)
{
if (strcmp(p->name, pMation) == 0 || strcmp(p->Phone_Num, pMation) == 0)
{
while (1)
{
printf("请选择您要修改的内容:(1--姓名 2--性别 3--年龄 4--电话 5--住址 0--修改完成)\n");
scanf("%d", &choice);
if (choice == 0)
{
goto My_Tag;
}
else
{
switch (choice)
{
case 1:
printf("请输入修改后姓名:\n");
scanf("%s", New_Id);
strcpy(p->name, New_Id);
printf("修改成功!\n");
flag = 1;
break;
case 2:
printf("请输入修改后性别:\n");
scanf("%s", New_Id);
strcpy(p->sex, New_Id);
printf("修改成功!\n");
flag = 1;
break;
case 3:
printf("请输入修改后年龄:\n");
scanf("%s", New_Id);
strcpy(p->age, New_Id);
printf("修改成功!\n");
flag = 1;
break;
case 4:
printf("请输入修改后电话:\n");
scanf("%s", New_Id);
strcpy(p->Phone_Num, New_Id);
printf("修改成功!\n");
flag = 1;
break;
case 5:
printf("请输入修改后地址:\n");
scanf("%s", New_Id);
strcpy(p->address, New_Id);
printf("修改成功!\n");
flag = 1;
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
}
}
else
{
p = p->Next;
}
}
My_Tag: if (!flag)
{
printf("修改失败,未找到该联系人\n");
}
}
void Show_All_Contacts(Contacts * L) //显示所有联系人信息
{
pContacts p;
p = L;
if (NULL == p)
{
printf("None of Information for Your Contacts");
}
else
{
while (p != NULL)
{
printf("%s %s %s %s %s\n", p->name , p->sex , p->age , p->Phone_Num , p->address);
p = p->Next;
}
}
}
void Free_All_Contacts(Contacts * L) //清空所有联系人
{
if (L == NULL)
{
printf("清空联系人失败\n");
}
else
{
L->Next = NULL;
printf("清空联系人成功\n");
}
}
void Sort_By_Name( Contacts * L) //以名字排序所有联系人
{
pContacts p = NULL;
pContacts q = NULL;
pContacts p_First = NULL;
char *Parr[N];
int count = 0, i = 0, flag = 0;
if (L == NULL)
{
printf("排序联系人失败:\n");
}
else
{
p = L->Next;
p_First = p;
q = p;
while (p != NULL)
{
count++;
p = p->Next;
}
p = p_First;
printf("按姓名排序的结果为:\n");
for ( i = 0; i < count; i++)
{
while (p != NULL)
{
if (strcmp(p->name, q->name) < 0)
{
q = p;
}
flag++;
p = p->Next;
}
printf("%s %s %s %s %s\n", q->name, q->sex, q->age, q->Phone_Num, q->address);
Parr[flag - 1] = q->name;
strcpy(q->name, "zzz");
p = p_First;
strcpy(p[flag].name, "zzz");
}
p = p_First;
for (i = 0; i < count; i++)
{
strcpy(p[i].name , Parr[i]);
}
}
}
void Menu_Print()
{
printf("***************************** 我的通讯录 *****************************\n");
printf("1--添加联系人信息 2--删除指定联系人信息 3--查找指定联系人信息\n");
printf("4--修改指定联系人信息 5--显示所有联系人信息 6--清空所有联系人\n");
printf("7--以名字排序所有联系人 0--退出\n");
printf(" Welcome To My Contacts \n");
printf("**********************************************************************\n");
}
int main()
{
Contacts * pHead = NULL;
Contacts * pL = NULL;
pHead = ListCreate();
int choice = 0;
while (1)
{
printf("\n");
printf("----------------------------------------------------------------------\n");
Menu_Print();
printf("请输入您的选择->:\n");
scanf("%d", &choice);
if (choice <= 7 && choice >= 1)
{
Contacts * Pcontacts;
Contacts * pNow = NULL;
int num = 0, count = 0, i = 0, Ychoice = 0;
char ch;
char Del_Name[Max_Words], Find_Mation[Max_Words];
switch (choice)
{
case 1: //添加联系人信息
printf("请输入您要创建的联系人的个数:\n");
scanf("%d", &num);
if (num<=1000)
{
printf("请依次输入联系人的信息(格式:姓名、性别、年龄、电话、住址)->\n");
Pcontacts = (Contacts *)malloc(sizeof(Contacts)*num);
for (i = 0; i < num; i++)
{
scanf("%s %s %s %s %s", &Pcontacts[i].name, &Pcontacts[i].sex, &Pcontacts[i].age, &Pcontacts[i].Phone_Num, &Pcontacts[i].address);
}
pL = Add_Contacts(pHead, Pcontacts, num);
}
else
{
printf("通讯录容量不够:\n");
exit(0);
}
Sleep(1000);
system("cls");
break;
case 2: //删除指定联系人信息
printf("请输入您要删除的联系人的姓名:\n");
scanf("%s", &Del_Name);
Del_Contacts(pL, Del_Name);
Sleep(1000);
system("cls");
break;
case 3: //查找指定联系人信息
printf("请输入您要查找的联系人的信息:(姓名、电话均可):\n");
scanf("%s", &Find_Mation);
pNow = Find_Contacts(pL, Find_Mation);
if (pNow == NULL)
{
printf("Sorry,未找到该联系人\n");
}
else
{
printf("姓名 性别 年龄 电话 住址\n");
printf("%s %s %s %s %s\n", pNow->name, pNow->sex, pNow->age, pNow->Phone_Num, pNow->address);
}
printf("请输入字母Y或者y继续:\n");
while (ch = getchar())
{
if (ch == 'y'||ch == 'Y')
{
break;
}
}
system("cls");
break;
case 4: //修改指定联系人信息
printf("请输入您要修改的联系人的姓名或者电话:\n");
scanf("%s", &Find_Mation);
Revise_Contacts(pL, Find_Mation);
Sleep(2000);
system("cls");
break;
case 5: //显示所有联系人信息
Show_All_Contacts(pL);
Sleep(3000);
system("cls");
break;
case 6: //清空所有联系人
Free_All_Contacts(pL);
Sleep(1500);
system("cls");
break;
case 7: //以名字排序所有联系人
Sort_By_Name(pL);
printf("请输入字母Y或者y继续:\n");
while (ch = getchar())
{
if (ch == 'y' || ch == 'Y')
{
break;
}
}
system("cls");
break;
default:
break;
}
}
else if (choice == 0)
{
break;
}
else
{
printf("您的输入有错误,请重新输入:\n");
}
}
return 0;
}
界面如下:
做这么一个程序,我主要是为了能复习一下之前学过的链表。