TA的每日心情 | 奋斗 2016-11-5 20:47 |
---|
签到天数: 18 天 [LV.4]偶尔看看III
|
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct stucontact
{
char stuname[20]; //姓名
char stunum[20]; //电话号码
char stubirth[20]; //生日
char stuhome[20]; //籍贯
struct stucontact *next;
};
#define LEN sizeof(struct stucontact)
#define n 3
struct stucontact *creat(void) //初始化通讯录
{
int i = 0;
struct stucontact *p1 = NULL, *p2 = NULL, *head = NULL;
char a[20], b[20], c[20], d[20];
printf("请输入%d组学生信息(用空格分开)(姓名,手机号,生日,籍贯): \n",n);
while (i<n)
{ scanf("%s%s%s%s", a,b,c,d);
i++;
p1 = (struct stucontact *)malloc(LEN); //分配新结点
strcpy(p1->stuname, a); //字符串的赋值
strcpy(p1->stunum, b);
strcpy(p1->stubirth , c);
strcpy(p1->stuhome , d);
if (head == NULL)head = p1;
else
p2->next = p1;
p2 = p1;
}
p2->next = NULL;
return (head);
}
void list(struct stucontact *head){ //输出
struct stucontact *p;
if (head == NULL) printf("通讯录为空!");
else
{
printf("通讯录为:\n");
p = head;
while (p != NULL)
{
printf("%s %s %s %s\n", p->stuname, p->stunum, p->stubirth, p->stuhome);
p = p->next;
}
}
}
struct stucontact *add(struct stucontact *head)
{
char ch='y',h;
while(ch=='y'||ch=='Y'){
struct stucontact *p0, *p1, *p2 = NULL;
p0 = (struct stucontact *)malloc(LEN);
p0->next = NULL;
printf("请输入要填加的学生信息(用空格分开)(姓名,手机号,生日,籍贯): \n");
scanf("%s%s%s%s", p0->stuname,p0->stunum,p0->stubirth, p0->stuhome);
getchar(); //空读
if (head == NULL) head = p0;
else
{
p1 = head;
while ((strcmp(p0->stuname , p1->stuname)>0) && (p1->next != NULL)) //字符串的比较
{
p2 = p1;
p1 = p1->next;
}
if (strcmp(p0->stuname , p1->stuname)<=0)
{
if (head == p1){ head = p0;
printf("已添加");
printf("是否继续添加?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break; }
else {p2->next = p0;
p0->next = p1;
printf("已添加");
printf("是否继续添加?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break; }
}
else { p1->next = p0;
printf("已添加删除");
printf("是否继续删除添加?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break;}
}
}return (head);
}
struct stucontact *del(struct stucontact *head)
{
char ch='y',h;
while(ch=='y'||ch=='Y'){
char a[20]; //a就已经是一个首地址
char b[20];
struct stucontact *p1 = NULL, *p2 = NULL;
if (head == NULL) printf("通讯录为空,无法进行删除操作");
else
{
printf("请输入要删除的学生名字及电话号码(用空格分开): \n");
scanf("%s%s", a, b);
p1 = head;
while ((strcmp(a, p1->stuname) != 0) && (strcmp(b, p1->stunum) != 0) && (p1->next != NULL)) //查找
{
p2 = p1;
p1 = p1->next;
}
if ((strcmp(a, p1->stuname) == 0) && (strcmp(b, p1->stunum) == 0)) //姓名和电话号码必须都满足
{
if (head == p1) { head=head->next;} // 删除不了头结点。。。。望各位大神指导
else p2->next = p1->next; //链表问题最好画图,多画几次就能基本了解
printf("已删除");
printf("是否继续删除?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break;
}
else {printf("不存在");
printf("是否继续删除?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break; }
}
}
return (head);
}
struct stucontact *search(struct stucontact *head)
{char ch='y',h;
while(ch=='y'||ch=='Y'){
if (head == NULL) printf("通讯录为空,无法进行查询操作");
else
{
char a[20];
printf("请输入要查询的学生名字: ");
scanf("%s", a);
struct stucontact *p1 = NULL, *p2 = NULL;
p1 = head;
while ((strcmp(a, p1->stuname) != 0) && (p1->next != NULL))
{
p2 = p1;
p1=p1->next;
}
if ((strcmp(a, p1->stuname) == 0))
{
printf("该学生信息为: 姓名:%s 电话:%s 生日:%s 籍贯:%s \n", p1->stuname, p1->stunum, p1->stubirth, p1->stuhome);
printf("是否继续查询?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break; }
else printf("通讯录中不存在您要查找的信息。\n");
}
}
return (head);
}
struct stucontact *change(struct stucontact *head)
{
char ch='y',h;
while(ch=='y'||ch=='Y'){
if (head == NULL) printf("通讯录为空,无法进行查询操作");
else
{
char a[20];
printf("请输入要修改的学生名字: ");
scanf("%s", a);
struct stucontact *p1 = NULL, *p2 = NULL;
p1 = head;
while ((strcmp(a , p1->stuname) !=0) && (p1->next != NULL))
{
p2 = p1; //存放
p1=p1->next; //p1指向下一个
}
if ((strcmp(a, p1->stuname)==0))
{
printf("请输入修改的信息: \n");
scanf("%s%s%s%s", p1->stuname, p1->stunum, p1->stubirth, p1->stuhome); //执行完上边的循环之后,p2是要删除的结点的上一个,要删除的结点是p1,p1->next是要删除的结点的后一个结点,所以这样赋值就可以直接跳过要删的结点。
printf("已修改,是否继续修改?确认请输入 y/Y ");
scanf("%s",&h); //单个字符需要加&
getchar();
if(h=='y'||h=='Y')
ch=h;
else break;}
else printf("通讯录中不存在您要查找的信息。\n");
}
}
return(head);
}
void main()
{
struct stucontact *head;
head = creat();
list(head);
int select;
while (1)
{
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\t\t\t------------------------\n");
printf("\t\t\t \n");
printf("\t\t\t 学生通讯录管理系统 \n");
printf("\t\t\t \n");
printf("\t\t\t------------------------\n");
printf("\n");
printf("\t\t\t 1.增加 \n");
printf("\t\t\t 2.删除 \n");
printf("\t\t\t 3.搜索 \n");
printf("\t\t\t 4.修改 \n");
printf("\t\t\t 5.输出 \n");
printf("\t\t\t 0.退出 \n");
printf("请输入您接下来的操作: ");
scanf("%d", &select);
switch (select)
{
case 1:add(head); break;
case 2:del(head); break;
case 3:search(head); break;
case 4:change(head); break;
case 5:list(head); break;
case 0:printf("谢谢使用,再见!"); exit(1);
default:printf("\n 按键错误,请重新选择 \n"); break;
}
}
}
|
|