项目:简易通讯录
#ifndef _NODE_H_
#define _NODE_H_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SUCCESS 10000
#define FAILURE 10001
typedef char DataType;
struct person
{
DataType number[10];
DataType name[10];
DataType address[20];
DataType phone[20];
DataType telephone[20];
struct person *next;
};
typedef struct person Per;
void Add(Per *p);
void Query(Per *p);
void List(Per *p);
void Delete(Per *p);
void Updata(Per *p);
#endif
#include"Node.h"
/*char *number = (char *)malloc(sizeof(char) * 10);
char *name = (char *)malloc(sizeof(char) * 10);
char *phone = (char *)malloc(sizeof(char) * 20);
char *address = (char *)malloc(sizeof(char) * 20);
char *telephone = (char *)malloc(sizeof(char) * 10);*/
void Add(Per *p)
{
Per *s = p;
Per *n = (Per *)malloc(sizeof(Per));
if(NULL == n)
{
printf("ERROR!\n");
exit(1);
}
printf("开始添加联系人\n");
printf("请输入001-999编号:\n");
scanf("%s", n -> number);
getchar();
while(strlen(n -> number) != 3)
{
printf("请重新输入001-999编号:\n");
scanf("%s", n -> number);
getchar();
}
printf("请输入姓名:\n");
scanf("%s", n -> name);
getchar();
while(strlen(n -> name) > 10 || strlen(n -> name) < 1)
{
printf("请重新输入姓名:\n");
scanf("%s", n -> name);
getchar();
}
printf("请输入20以下地址:\n");
scanf("%s", n -> address);
getchar();
while(strlen(n -> address) > 20 || strlen(n -> address ) < 0)
{
printf("请重新输入20以下地址:\n");
scanf("%s", n -> address);
getchar();
}
printf("请输入11位号码:\n");
scanf("%s", n -> phone);
getchar();
while(strlen(n -> phone) != 11)
{
printf("请重新输入11位号码:\n");
scanf("%s", n -> phone);
getchar();
}
printf("请输入8位住宅电话:\n");
scanf("%s", n -> telephone);
getchar();
while(strlen(n -> telephone) != 8)
{
printf("请重新输入8位住宅电话:\n");
scanf("%s", n -> telephone);
getchar();
}
n -> next = p -> next;;
p -> next = n;
printf("添加成功!\n");
}
void List(Per *p)
{
int i = 0;
if(NULL == p)
{
printf("ERROR!\n");
}
Per *s = p -> next;
printf("\t编号\t姓名\t地址\t号码\t\t宅电\n");
printf("============================================================\n");
Per *tmp = (Per *)malloc(sizeof(Per));
Per *q1 = p;
Per *q2 = p -> next;
for (q1 = p; q1 = NULL; q1 = q1 -> next)
{
for(q2 = p -> next; q2 != NULL; q2 = q2 -> next)
{
if(strcmp(q2 -> name, q2 -> next -> name) > 0)
{
tmp = q2;
q2 = q2 -> next;
q2 -> next= tmp;
}
}
}
while(s)
{
printf("\t%s\t%s\t%s\t%s\t%s\n",s->number,s->name,s->address,s->phone,s->telephone);
// q(s->number,s->name,s->address,s->phone,s->telephone);
s = s -> next;
}
}
void Query(Per *p)
{
int count = 0;
if(NULL == p)
{
printf("ERROE\n");
}
char *n = (char *)malloc(sizeof(20));
printf("请输入要查找的姓名:\n");
scanf("%s",n);
printf("\t编号\t姓名\t地址\t号码\t\t宅电\t\n");
printf("============================================================\n");
Per *s = p -> next;
while(s)
{
if(strcmp(n, s -> name) == 0)
{
printf("\t%s\t%s\t%s\t%s\t%s\n",s->number,s->name,s->address,s->phone,s->telephone);
count++;
}
s = s -> next;
}
if(count == 0)
{
printf("未找到此人!\n");
}
}
void Delete(Per *p)
{
if(NULL == p)
{
printf("ERROE\n");
}
char *m = (char *)malloc(sizeof(char) * 10);
int count = 0;
printf("请输入要删除的姓名:\n");
scanf("%s",m);
Per *s = p;
while(s != NULL && s -> next != NULL)
{
if(strcmp(m, s ->next -> name) == 0)
{
Per *n = s -> next;
s -> next = n -> next;
free(n);
printf("删除 %s 成功!\n", m);
count++;
}
s = s -> next;
}
if(count == 0)
{
printf("未找到此人!删除失败!\n");
}
}
void Updata(Per *p)
{
char *m = (char *)malloc(sizeof(char) * 10);
if(NULL == p)
{
printf("ERROR!\n");
}
printf("请输入需要修改的联系人:\n");
scanf("%s", m);
Per *s = p -> next;
while(s != NULL)
{
if(strcmp(m, s -> name) == 0)
{
printf("请输入新的编号:");
scanf("%s", s -> number);
getchar();
printf("请输入新的姓名:");
scanf("%s", s -> name);
getchar();
printf("请输入新的电话:");
scanf("%s", s -> phone);
getchar();
printf("请输入新的住址:");
scanf("%s", s -> address);
getchar();
printf("请输入新的宅电:");
scanf("%s", s -> telephone);
getchar();
}
s = s -> next;
}
}
#include"Node.h"
/*void visit(DataType a, DataType b, DataType c, DataType d, DataType e)
{
printf("\t%s\t%s\t%s\t%s\t%s\n", a, b, c, d, e);
}*/
int main()
{
system("clear");
int choice;
char value;
Per *per = (Per *)malloc(sizeof(Per));
if( NULL == per)
{
printf("Malloc error!\n");
exit(1);
}
per -> next = NULL;
printf("\t*********************************\n");
printf("\t************渣渣通讯录***********\n");
printf("\t*********************************\n");
sleep(1);
system("clear");
while(1)
{
printf("\t1.添加好友\t2.列出好友\t3.查找好友\n");
printf("\t4.删除好友\t5.修改信息\t6.退出系统\n");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
Add(per);
break;
case 2:
List(per);
break;
case 3:
Query(per);
break;
case 4:
Delete(per);
break;
case 5:
Updata(per);
break;
case 6:
printf("BYE!\n");
sleep(1);
exit(1);
break;
default:
printf("请输入正确的选项!\n");
break;
}
}
return 0;
}
通讯录是个非常简易的小项目,简单实现了联系人的增删查改功能。代码中主要使用的是指针和单一的函数调用,参数个数较少,结构清晰,主要是对结构体赋值、调用的训练。注意的是要为头节点的指针分配空间,这是至关重要的。与平时指针的使用一样,野指针的存在会造成不必要的段错误。
本文介绍了一个简单的通讯录项目实现,包括联系人的增加、删除、查询和更新等功能。使用C语言和结构体,通过指针操作完成对联系人信息的管理。
1261

被折叠的 条评论
为什么被折叠?



