卡片系统

来源:互联网 发布:洪恩软件开天辟地4 编辑:程序博客网 时间:2024/04/27 19:31

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

struct node *searchNode(int pos);

//结点声明
struct node
{
 //数据
 void *data;
 //链接
 struct node *next;
};

struct node *head=NULL;
struct node *pos=NULL;
int nodeCount=0;

struct card
{
 char name[20];
 char telephone[20];
 char address[20];
};

void insert(void *tdata)
{
 struct node *p;
 p=(struct node*)malloc(sizeof(struct node));
 if(p==NULL)
 {
  return;
 }
 p->data=tdata;
 p->next=NULL;
 if(head==NULL)
 {
  head=p;
  pos=p;
 }
 else
 {
  pos->next=p;
  pos=p;
 }
 nodeCount++;
}

int deleteAllNodes(void (*functionPtr)(void *))
{
 if(nodeCount==0)
 {
  return 0;
 }
 else
 {
  struct node *temp=NULL;
  while(head!=NULL)
  {
   temp=head;
   head=head->next;
   if(temp!=NULL)
   {
    functionPtr(temp->data);
    free(temp);
   }
  }
  nodeCount=0;
  pos=NULL;
  return 1;
 }
}

int deleteNode(void (*functionPtr_A)(void *),int (*functionPtr_B)(void *),int nodePos,void *data)
{
 if(nodePos==0)
 {
  return 0;
 }
 else
 {
  struct node *tempPtr;
  if(nodePos==-1)
  {
   int tempPos=functionPtr_B(data);
   if(tempPos==1)
   {
    tempPtr=head;
    head=head->next;
    if(tempPtr!=NULL)
    {
     //释放数据
     functionPtr_A(tempPtr->data);
     //释放结点
     free(tempPtr);
     nodeCount--;
     return 1;
    }
   }
   else
   {
    tempPtr=searchNode(tempPos-1);
    struct node *tempPtr_A;
    if(tempPos==nodeCount)
    {
     tempPtr_A=tempPtr->next;
     tempPtr->next=NULL;
     pos=tempPtr;
    
    }
    else
    {
     tempPtr_A=tempPtr->next;
     tempPtr->next=tempPtr->next->next;
    }
    //释放数据
    functionPtr_A(tempPtr_A->data);
    //释放结点
    free(tempPtr_A);
    nodeCount--;
    return 1;
   }

  }
  else
  {
   
   if(nodePos>=1)
   {
    if(nodePos==1)
    {
     tempPtr=head;
     head=head->next;
     if(tempPtr!=NULL)
     {
      //释放数据
      functionPtr_A(tempPtr->data);
      //释放结点
      free(tempPtr);
      nodeCount--;
      return 1;
     }
    }
    else
    {
     tempPtr=searchNode(nodePos-1);
     struct node *tempPtr_A;
     if(nodePos==nodeCount)
     {
      tempPtr_A=tempPtr->next;
      tempPtr->next=NULL;
      pos=tempPtr;
     
     }
     else
     {
      tempPtr_A=tempPtr->next;
      tempPtr->next=tempPtr->next->next;
     }
     //释放数据
     functionPtr_A(tempPtr_A->data);
     //释放结点
     free(tempPtr_A);
     nodeCount--;
     return 1;
    }
   }
  }

 }
}

int compareData(void *ptr)
{
 struct card *tempPtr_A=(struct card *)ptr;
 struct card *tempPtrData=NULL;
 struct node *tempPtr_B=NULL;
 if(head==NULL)
 {
  return 0;
 }
 for(int t=1;t<=nodeCount;t++)
 {
  tempPtr_B=searchNode(t);
  tempPtrData=(struct card *)tempPtr_B->data;
  if(strcmp(tempPtr_A->name,tempPtrData->name)==0)
  {
   return t;
  }

 }
}

void nodeEditor(void *tdata,void *newData,int (*functionPtr)(void *),void (*function_B)(void *))
{
 int pos=functionPtr(tdata);
 if(pos>=1)
 {
  struct node *tempPtr=searchNode(pos);
  function_B(tempPtr->data);
  tempPtr->data=newData;
 }
}

struct node *searchNode(int tpos)
{
 if(tpos==1)
 {
  return head;
 }
 if(tpos==nodeCount)
 {
  return pos;
 }
 struct node *ptr;
 ptr=head;
 for(int t=1;t<tpos;t++)
 {
  ptr=ptr->next;
 }
 if(ptr!=NULL)
 {
  return ptr;
 }
 else
 {
  return NULL;
 }
}

void deleteCard(void *ptr)
{
 if(ptr==NULL)
 {
  return;
 }
 struct card *tempPtr;
 tempPtr=(struct card *)ptr;
 if(tempPtr!=NULL)
 {
  free(tempPtr);
 }
}

void insertNewCard(char *tname,char *ttelephone,char *taddress)
{
 struct card *p=(struct card *)malloc(sizeof(struct card));
 strcpy(p->name,tname);
 strcpy(p->telephone,ttelephone);
 strcpy(p->address,taddress);
 insert(p);
}

void main()
{
 int chose=0;
 int temp_pos;
 struct card temp;
 char buf_1[20];
 char buf_2[20];
 char buf_3[20];
 char buf_4[20];
 while(1)
 {
 /* for(int t=0;t<20;t++)
  {
   buf_1[t]='/0';
   buf_2[t]='/0';
   buf_3[t]='/0';
   buf_4[t]='/0';
  }  */
  system("cls");
  printf("----------名片管理----------/n");
  printf("1.添加名片 /n");
  printf("2.名片修改 /n");
  printf("3.名片查询 /n");
  printf("4.名片删除 /n");
  printf("5.全部删除 /n");
  printf("6.打印全部名片 /n");
  printf("7.退出 /n");
  printf("----------------------------/n");
  printf("选择:");
  scanf("%d",&chose);
  switch(chose)
  {
  case 1:
   system("cls");
   printf("----------添加一个名片-----------/n");
   printf("姓名:");
   scanf("%s",buf_1);
   printf("电话:");
   scanf("%s",buf_2);
   printf("地址:");
   scanf("%s",buf_3);
   insertNewCard(buf_1,buf_2,buf_3);
   printf("---------------------------------/n");
   break;
  case 2:
   system("cls");
   printf("----------修改名片-------------/n");
   printf("输入已有名片的姓名:");
   scanf("%s",buf_4);
   strcpy(temp.name,buf_4);
   temp_pos=compareData(&temp);
   if(temp_pos>=1)
   {
    printf("已经找到数据,请修改...../n");
    printf("输入新的名字:");
    scanf("%s",buf_1);
    printf("输入新的电话:");
    scanf("%s",buf_2);
    printf("输入新的地址:");
    scanf("%s",buf_3);
    struct card temp_data;
    strcpy(temp_data.name,buf_1);
    strcpy(temp_data.telephone,buf_2);
    strcpy(temp_data.address,buf_3);
    nodeEditor(&temp,&temp_data,compareData,deleteCard);
    
   }
   else
   {
    printf("无法找到指定数据,请重新输入......./n");

   }
   printf("输入任意键继续...../n");
   _getch();
   break;
  case 3:
   system("cls");
   printf("-------------名片查询---------------/n");
   printf("输入姓名:");
   scanf("%s",buf_4);  
   strcpy(temp.name,buf_4);
   temp_pos=compareData(&temp);
   if(temp_pos>=1)
   {
    printf("已经找到数据,请修改...../n");
    struct node *temp_node=searchNode(temp_pos);
    struct card *ptr=(struct card *)temp_node->data;
    printf("------------------/n");
    printf("姓名: %s/n",ptr->name);`
    printf("电话: %s/n",ptr->telephone);
    printf("地址: %s/n",ptr->address);
    printf("------------------/n");
   }
   else
   {
    printf("无法找到指定数据,请重新输入......./n");
   }
   printf("输入任意键继续...../n");
   _getch();
   break;
  case 4:
   system("cls");
   printf("--------------名片删除----------------/n");
   printf("输入要删除名片的姓名:");
   scanf("%s",buf_4);
   strcpy(temp.name,buf_4);
   temp_pos=compareData(&temp);
   if(temp_pos>=1)
   {
    printf("已经找到数据,请删除...../n");
    if(deleteNode(deleteCard,compareData,-1,&temp)==1)
    {
              printf("Dlete Sucessfully!/n");
    }
   }
   else
   {
    printf("无法找到指定数据,请重新输入......./n");
   }
   printf("输入任意键继续...../n");
   _getch();
   break;
  case 5:
   system("cls");
   printf("--------------全部删除----------------/n");
   temp_pos=compareData(&temp);
   if(temp_pos>=1)
   {
    printf("已经找到数据,请删除...../n");
    deleteAllNodes(deleteCard);
    printf("Dlete Sucessfully!/n");
   }
   else
   {
    printf("无法找到指定数据,请重新输入......./n");
   }

   printf("输入任意键继续...../n");
   _getch();
   break;
   case 6:
   system("cls");
   printf("--------------打印全部名片----------------/n");
   temp_pos=compareData(&temp);
   if(temp_pos>=1)
   {
    printf("已经找到数据,请打印...../n");
    if(head==NULL)
    {
     return ;
    }
    for(int temp_pos=1;temp_pos<=nodeCount;temp_pos++)
    {
     struct node *temp_node=searchNode(temp_pos);
     struct card *ptr=(struct card *)temp_node->data;
     printf("------------------/n");
     printf("姓名: %s/n",ptr->name);
     printf("电话: %s/n",ptr->telephone);
     printf("地址: %s/n",ptr->address);
     printf("------------------/n");
    }
   }
   
   else
   {
    printf("无法找到指定数据......./n");
   }
   printf("输入任意键继续...../n");
   _getch();
   break; 
  
   


  case 7:
   system("cls");
   printf("释放内存空间............./n");
   deleteAllNodes(deleteCard);
   printf("成功释放....退出..../n");
   return;
   break;

  }
 }

}
 

原创粉丝点击