#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
void new_contact();//add contact using linked list
void list_contact();
void delete_contact();
//int sorting_contact();//sort contact name using sorting
void search_contact();//search for contact phone number using searching
struct contact
{
char phone_number[20];
char contact_name[70];
char address [80];
struct contact *ptrnext;
};
struct contact *headptr, *newptr, *currentptr, *previousptr;
int main()
{
system("color B");
int key;
char ch;
int choice=TRUE;
headptr=(struct contact *)NULL;
while(choice==TRUE)
{
printf("\n\t\tWelcome to Smart Phone Book System");
printf("\n\nAdd a contact :A\nList a contact :L\nDelete a contact :D"
"\nSearch for contact :S\nSort phonebook :T\nExit :x\n");
printf("\nPlease enter the mode you want:");
scanf(" %c%*c", &ch);
switch(ch)
{
case 'A': new_contact();system("CLS");break;
case 'L': system("CLS");list_contact();break;
case 'D': system("CLS");delete_contact();break;
case 'S': search_contact();break;
//case 'T': sorting_contact(); break;
case 'X': choice=FALSE; break;
default: system("CLS");printf("\nPlease enter a proper mode");
}
}
}
void new_contact()//add data
{
newptr=(struct contact *)malloc(sizeof (struct contact)); // pointer to a new
// memory allocation
if (headptr==(struct contact *)NULL)//node is empty?
{
headptr=newptr; //first pointer point to first node
newptr->ptrnext=(struct contact *)NULL; //first node pointer point to null
}
else
{
newptr->ptrnext=headptr;// new node pointer point to previous first node
headptr=newptr; // head point to new node,new node become first node
}
printf("\nEnter a new name : ");
fgets( newptr->contact_name,sizeof(newptr->contact_name),stdin);
printf("\nEnter a number : ");
fgets(newptr->phone_number,sizeof(newptr->phone_number),stdin);
//scanf(" %d", &newptr->phone_number);
//getchar();
printf("\nEnter address : ");
fgets(newptr->address,sizeof(newptr->address),stdin);
}
void list_contact()
{
int i=1;
if (headptr==(struct contact*)NULL) //empty list
{
printf("\nEmpty list");
return;
}
currentptr=headptr;
do
{
printf("\nContact number %d",i);
printf("\nName : %s", currentptr->contact_name);
printf("\nPhone number : %s", currentptr->phone_number);
printf("\n\nAddress : %s", currentptr->address);
i++;
printf("\n");
currentptr=currentptr->ptrnext; //point to next node
}
while(currentptr !=(struct contact *)NULL);
}
void delete_contact()
{
char contact_name_delete[20];
if (headptr==(struct contact *)NULL)//node is empty?
{
printf("\n\nThe list is empty. Cannot delete!!!\n");
//inform the user that the list is empty
}
else
{
printf("\nDelete list by name.");
printf("\nEnter contact name to delete: ");
fgets(contact_name_delete,sizeof(contact_name_delete),stdin);
currentptr=headptr;
while(currentptr ->ptrnext!=(struct contact *)NULL)
{
if (strncmp(currentptr->contact_name,contact_name_delete,6)==0)
//found the location
{
break;
}
else
{
previousptr=currentptr;//save the previous current address
currentptr=currentptr->ptrnext;
//point to next node
}
}
if (strncmp(currentptr->contact_name,contact_name_delete,6)==0)
{
if ( currentptr==headptr) //number is the first and only node in list
{
headptr=currentptr ->ptrnext; //head point to NULL
free(currentptr);
}
else //delete at the middle of link list
{
previousptr->ptrnext=currentptr->ptrnext;
free(currentptr);//destroy node, free the memory.
}
printf("\nPhone number were deleted!\n");
}
else
printf("\nNumber to be deleted is not in the list!!! ");
}
}
我的问题出在 search_contact()
函数中:
void search_contact()//The function for searching the contact based on name
{
currentptr=headptr;
char contact_name_add[30];
if (headptr ==(struct contact *)NULL)
{
printf("The phonebook is empty..");
}
else
{
printf("Please enter the name of your contact for searching : ");//user inputs name for searching
fgets(contact_name_add,sizeof(contact_name_add),stdin);//uses linear search concept
printf("\n\nThe computer will now search for your contact");
do
{
if (strncmp(currentptr->contact_name,contact_name_add,5)==0)
{
printf("\nName : %s", currentptr->contact_name);
printf("\nPhone Number : %s",currentptr->phone_number);
printf("\nAddress : %s", currentptr->address);
currentptr=currentptr->ptrnext;
}
else
{
currentptr=currentptr->ptrnext;
我发现下面的行是我的问题,但我不知道为什么
if (currentptr->ptrnext=NULL
&& strncmp(currentptr->contact_name,contact_name_add,5)!=0)
{
printf("\nThe name you seek is not in the phonebook~~");
}
}
} while(currentptr!=(struct contact *)NULL);
}
}
我可以很好地搜索我插入的联系人,但当我有 2 个或更多联系人时,就会出现问题。搜索功能与我的删除功能非常相似,但我将其编码为显示多于 1 个联系人(在联系人姓名相似的情况下),但当它达到 NULL 时,它会给出错误,我找不到它在 Google 上(我在这方面做得不好)。
最佳答案
尝试使用以下 IF:
if(currentptr->ptrnext==NULL && strncmp(currentptr->contact_name,contact_name_add,5)!=0)
关于c - 我的搜索功能无法正常工作,因为我的 if 语句有问题,并且我正在使用链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23927001/