实现一个客户信息管理系统,功能包括添加客户、修改客户、删除客户、显示客户列表。
学完了C语言和链表没事情干拿出来写一写,检测下学习成果
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
int num;
char name[20];
char gender;
char year;
char phone[14];
char email[30];
struct Node *next;
} people;
typedef struct Head // 头指针
{
people *next;
} list;
void Initlinkedlist(list *p) // 初始化链表
{
p->next = NULL;
}
void AddPeoplelinkedlist(list *head) // 添加成员,尾插
{
printf("\n--------------添加成员--------------");
people *p = head->next;
people *q;
if (p == NULL)
{
q = (people *)malloc(sizeof(people));
if (q == NULL)
{
printf("err");
exit(0);
}
printf("\n请输入姓名:");
getchar();
scanf("%s", q->name);
printf("\n请输入性别:");
getchar();
scanf("%c", &q->gender);
printf("\n请输入年龄:");
getchar();
scanf("%c", &q->year);
printf("\n请输入电话号码:");
getchar();
scanf("%s", q->phone);
printf("\n请输入电子邮箱:");
getchar();
scanf("%s", q->email);
q->num = 1;
head->next = q;
q->next = NULL;
}
else
{
q = (people *)malloc(sizeof(people));
if (q == NULL)
{
printf("err");
exit(0);
}
printf("\n请输入姓名:");
getchar();
scanf("%s", &q->name);
printf("\n请输入性别:");
getchar();
scanf("%c", &q->gender);
printf("\n请输入年龄:");
getchar();
scanf("%c", &q->year);
printf("\n请输入电话号码:");
getchar();
scanf("%s", &q->phone);
printf("\n请输入电子邮箱:");
getchar();
scanf("%s", &q->email);
while (p->next != NULL)
{
p = p->next;
}
q->num = p->num + 1;
p->next = q;
q->next = NULL;
}
}
void TraverseLinkedList(list *head) // 遍历打印
{
people *q;
q = head->next;
printf("\n--------------客户列表--------------");
printf("\n编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱");
while (q != NULL)
{
printf("\n%d", q->num);
printf("\t\t%s", q->name);
printf("\t\t%c", q->gender);
printf("\t\t%c", q->year);
printf("\t\t%s", q->phone);
printf("\t\t%s", q->email);
q = q->next;
}
}
void deleteLinkedList(int n, list *q) // 删除元素
{
people *t, *p;
int data;
p = q->next;
if (n > 1)
{
for (; (p->next)->num != n;)
{
p = p->next;
}
t = p->next;
p->next = t->next;
free(t);
printf("删除成功");
}
if (n == 1)
{
t = q->next;
q->next = t->next;
free(t);
printf("删除成功");
}
}
void changeLinkedlist(int n, list *q) // 修改元素
{
people *t, *p;
int data;
p = q->next;
if (n > 1)
{
for (; (p->next)->num != n;)
{
p = p->next;
}
p = p->next;
printf("\n请输入姓名:");
getchar();
scanf("%s", p->name);
printf("\n请输入性别:");
getchar();
scanf("%c", &p->gender);
printf("\n请输入年龄:");
getchar();
scanf("%c", &p->year);
printf("\n请输入电话号码:");
getchar();
scanf("%s", p->phone);
printf("\n请输入电子邮箱:");
getchar();
scanf("%s", p->email);
printf("修改成功");
}
if (n == 1)
{
printf("\n请输入姓名:");
getchar();
scanf("%s", p->name);
printf("\n请输入性别:");
getchar();
scanf("%c", &p->gender);
printf("\n请输入年龄:");
getchar();
scanf("%c", &p->year);
printf("\n请输入电话号码:");
getchar();
scanf("%s", p->phone);
printf("\n请输入电子邮箱:");
getchar();
scanf("%s", p->email);
printf("修改成功");
}
}
void menu(list *customer) // 菜单显示
{
while (1)
{
int i, n;
char flag;
printf("\n--------------客户信息管理软件--------------");
printf("\n\t\t1添加客户");
printf("\n\t\t2修改客户");
printf("\n\t\t3删除客户");
printf("\n\t\t4客户列表");
printf("\n\t\t5退出\n请输入1-5:");
scanf("%d", &i);
switch (i)
{
case 1:
AddPeoplelinkedlist(customer);
break;
case 2:
printf("\n输入要修改的客户的编号(-1退出):");
scanf("%d", &n);
if (n == -1)
{
break;
}
else
{
changeLinkedlist(n, customer);
}
break;
case 3:
printf("\n输入要删除的客户的编号(-1退出):");
scanf("%d", &n);
if (n == -1)
{
break;
}
else
{
deleteLinkedList(n, customer);
}
break;
case 4:
TraverseLinkedList(customer);
break;
case 5:
printf("是否确认要退出(Y或N): ");
getchar(); // 过滤回车
scanf("%c", &flag);
if (flag == 'Y' || flag == 'y')
{
printf("已退出");
exit(0);
}
if (flag == 'N' || flag == 'n')
{
break;
}
default:
printf("请输入1-5的值!");
break;
}
}
}
void main()
{
list *customer;
customer->next = NULL;
Initlinkedlist(customer);
menu(customer);
}
标签:----,people,scanf,next,链表,printf,C语言,输入,getchar
From: https://www.cnblogs.com/akash1/p/18411530