|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
怎样才能完成有效性判断啊?前面用getchar()只能过第一个判断,后面就一直判断失败(清除缓冲区也没用)
改成整型用scanf输入数字好多了,可刚才试了下输入字母,又过不了判断了,就在*标记位置*改成‘return 0;’结束程序都退出不了
具体代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//输入的有效性判断未完成
struct Contact
{
char name[128];
char num[18];
struct Contact *next;
};
int mark = 0; //处理标记
void addPerson(struct Contact **head);
void findPerson(struct Contact *head);
void changePerson(struct Contact **head);
void delPerson(struct Contact **head);
void displayContacts(struct Contact **head);
void releaseMem(struct Contact *head);
int main()
{
int ch;
struct Contact *head = NULL;
printf("\
##########################################\n\
# 通 讯 录 程 序 #\n\
##########################################\n");
while(1)
{
printf("功能列表:\n\
1.插入新的联系人\n\
2.查找已有的联系人\n\
3.更改已有的联系人\n\
4.删除已有的联系人\n\
5.显示当前通讯录\n\
6.退出通讯录程序\n");
printf("请输入指令:");
scanf("%d", &ch);
while(ch < 1 || ch > 6)
{
printf("请输入有效的数字!\n");
scanf("%d", &ch); *******标注位置*******
}
switch(ch)
{
case 1:addPerson(&head);break;
case 2:
{
mark = 0;
findPerson(head);
if(mark == 0)
{
printf("\
***********************************\n\
*很抱歉,未能找到符合条件的联系人!*\n\
***********************************\n");
}
break;
}
case 3:
{
mark = 0;
changePerson(&head);
if(mark == 0)
{
printf("\
***********************************\n\
*很抱歉,未能找到符合条件的联系人!*\n\
***********************************\n");
}
break;
}
case 4:
{
mark = 0;
changePerson(&head);
delPerson(&head);
if(mark == 0)
{
printf("\
***********************************\n\
*很抱歉,未能找到符合条件的联系人!*\n\
***********************************\n");
}
break;
}
case 5:displayContacts(&head);break;
case 6:{
releaseMem(head);
return 0;
}
}
}
}
void addPerson(struct Contact **head)
{
struct Contact *current = *head;
struct Contact *previous = NULL;
struct Contact *new = (struct Contact *)malloc(sizeof(struct Contact));
if(new == NULL)
{
printf("内存分配失败了!\n");
exit(-1);
}
printf("请输入新建联系人姓名:");
scanf("%s", new->name);
printf("请输入新建联系人联系方式:");
scanf("%s", new->num);
if(*head == NULL)
{
*head = new;
new->next = NULL;
}
else
{
while(strcmp(current->num, new->num) < 0 && current->next != NULL)
{
previous = current;
current = current->next;
}
if(previous == NULL)
{
new->next = current;
*head = new;
}
else if(current == NULL)
{
new->next = NULL;
current = new;
}
else
{
new->next = current;
previous->next = new;
}
}
}
void findPerson(struct Contact *head)
{
char target[128];
struct Contact *previous = NULL;
struct Contact *current = head;
printf("请输入要查找的联系人姓名或号码:");
scanf("%s", target);
do
{
if(!strcmp(current->num, target) || !strcmp(current->name, target))
{
mark = 1;
printf("已经找到符合条件的联系人...\n");
printf("\t姓名:%s\t联系方式%s\n", current->name, current->num);
}
previous = current;
current = current->next;
}while(current != NULL);
}
void changePerson(struct Contact **head)
{
int confirm;
char new1[128];
char target[128];
struct Contact *previous = NULL;
struct Contact *current = *head;
printf("请输入要修改的联系人姓名或号码:");
scanf("%s", target);
while(current != NULL)
{
if(!strcmp(current->num, target) || !strcmp(current->name, target))
{
mark = 1;
printf("已经找到符合条件的联系人...\n");
printf("\t姓名:%s\t联系方式%s\n", current->name, current->num);
printf("您想将%s修改为:", target);
scanf("%s", new1);
printf("确认修改(修改:1/取消:2):");
scanf("%d", &confirm);
while(confirm != 1 && confirm != 2)
{
printf("输入错误请重新输入!\n确认修改(修改:1/取消:2):");
}
if(confirm == 1)
{
if(!strcmp(current->num, target))
{
strcpy(current->num, new1);
}
else
{
strcpy(current->name, new1);
}
}
}
previous = current;
current = current->next;
}
}
void delPerson(struct Contact **head)
{
int confirm;
char target[128];
struct Contact *previous = NULL;
struct Contact *current = *head;
if(*head == NULL)
{
printf("联系人总数为0,无法完成该操作!\n");
printf("\
************************************\n\
***联系人总数为0,无法完成该操作!***\n\
************************************\n");
return;
}
printf("请输入要删除的联系人姓名或号码:");
scanf("%s", target);
while(current != NULL)
{
if(!strcmp(current->num, target) || !strcmp(current->name, target))
{
mark = 1;
printf("已经找到符合条件的联系人...\n");
printf("\t姓名:%s\t联系方式%s\n", current->name, current->num);
printf("确认删除(删除:1/取消:2):");
scanf("%d", &confirm);
while(confirm != 1 && confirm != 2)
{
printf("输入错误请重新输入!\n确认删除(删除:1/取消:2):");
}
if(confirm == 1)
{
if(previous == NULL) //删除的目标为第一个联系人
{
previous = *head;
*head = (*head)->next;
free(previous);
}
else
{
previous->next = current->next;
free(current);
}
}
}
previous = current;
current = current->next;
}
}
void displayContacts(struct Contact **head)
{
if(*head == NULL)
{
printf("无联系人可显示!\n");
return;
}
struct Contact *current = *head;
printf("\n\
*********************\n\
* 显示联系人信息如下*\n\
*********************\n");
while(current != NULL)
{
printf("\t姓名:%s\t联系方式%s\n", current->name, current->num);
current = current->next;
}
printf("\n");
}
void releaseMem(struct Contact *head)
{
struct Contact *previous = NULL;
struct Contact *current = head;
while(current != NULL)
{
previous = current;
current = current->next;
free(previous);
}
}
本帖最后由 ba21 于 2017-7-18 08:52 编辑 while(1)
{
printf("是否继续添加数据?(y/n):");
do
{
ch=getchar();
}while(ch != 'y' && ch !='n');
if (ch == 'y')
add(&head);
else
break;
}
下面 strcmp 要引入 #include "string.h" printf("已经找到该记录,是否删除?(y/n)");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
for(j=i;j<m;j++)
comm[j]=comm[j+1];
m--;
printf("成功删除!\n");
}
|
|