用线性表实现的通讯录管理 C++代码

/****************************************/
/*主控菜单处理测试程序main2.c************/
/***************************************/
#include <iostream>
#include <string>
using namespace std;
 
#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10
 
int OK=1;
int OVERFLOW=0;
int ERROR=0;
 
typedef struct 
{  //通讯录结点类型
  char num[10];   //编号
  char name[20];  //姓名
  char sex[6];    //性别
  char phone[13]; //电话
  char addr[31];  //地址
} DataType;
 
typedef struct
{
       DataType *elem;
       int length;//当前长度
       int listsize;//线性表的长度
}SqList;
 
 
/*******************/
/* 菜单选择函数程序        */
/***************************/
int menu_select(  )
{
  int choice;
  cout<<"     通讯录管理系统  /n";
  cout<<"======================================================================/n";
  cout<<"1.建立/t"<<"2.插入/t"<<"3.查询/t"<<"4.删除/t"<<"5.输出/t"<<"0.退出/n";
  cout<<"======================================================================/n";
  cout<<"请  选 择 0-5: ";
  for( ; ; )
   {
     cin>>choice;
     if (choice<0||choice>5)
        cout<<"输入错误,重选0-5:";
     else
        break;
    }
  cout<<endl;
   return choice;
}
 
void InitList(SqList &L)
{
       L.elem=(DataType *)malloc(LIST_INIT_SIZE*sizeof(DataType));
 
    if(!L.elem)
       exit(OVERFLOW);
 
    L.length=0;
    L.listsize=LIST_INIT_SIZE;//以上步骤为初始化
}
 
/**************************/
/*建立通讯录线性表函数       */
/**************************/
void CreateList(SqList &L)
{
       L.elem=(DataType *)malloc(LIST_INIT_SIZE*sizeof(DataType));
 
    if(!L.elem)
       exit(OVERFLOW);
 
    L.length=0;
    L.listsize=LIST_INIT_SIZE;//因为建立一个通讯录,即重新开始建立一个,所以要初始化
                                                 //使不和以前的通讯录相混淆。
 
       int i=0;
       int flag=1;
      
       while(flag==1)
       {
              cout<<"/n编号(4)-姓名(8)-性别(3)-电话(11)-地址(31)/n";
 
              cin>>L.elem[i].num>>L.elem[i].name>>L.elem[i].sex>>L.elem[i].phone>>L.elem[i].addr;
              i++;
              L.length++;
             
              cout<<"/n是否还要输入?(0 or 1):";
              cin>>flag;
              cout<<endl;
       }//输入信息
}
 
/******************************/
/*在通讯录线性表中插入元素 */
/******************************/
int InsertNode(SqList &L,int i,DataType x)
{
          /*特殊情况的处理*/
       if(i<1||i>L.length+1)//这里L.length加1的目的是,用户可能会在最后边接上元素,并不插,
                                                 //只是顺序接到最后面
              return ERROR;
 
       DataType * newbase;
       DataType * p,* q;
       if(L.length>=L.listsize)
       {
              newbase=(DataType *)realloc(L.elem, (L.listsize+LISTINCREMENT)*sizeof(DataType));
              if(!newbase)
                     exit(OVERFLOW);
              L.elem=newbase;
              L.listsize+=LISTINCREMENT;
       }
 
       /*插入操作*/
       q=&(L.elem[i-1]);//q为第i个元素的位置
       for(p=&(L.elem[L.length-1]); p>=q; p--)
          {
                 strcpy((p+1)->num,p->num);
                 strcpy((p+1)->name,p->name);
                 strcpy((p+1)->sex,p->sex);
                 strcpy((p+1)->phone,p->phone);
                 strcpy((p+1)->addr,p->addr);//i-1之后的元素依次后移一位
          }
       strcpy(q->num,x.num);
          strcpy(q->name,x.name);
          strcpy(q->sex,x.sex);
          strcpy(q->phone,x.phone);
          strcpy(q->addr,x.addr);
//----------------------------------------------------------------------------------------
//“->”和“.”的区别:“->”是用于指针的,而“.”是用于对象调用的。
//----------------------------------------------------------------------------------------
 
 
       L.length++;
          return OK;
}
/******************************************/
/*   有序通讯录线性表的查找          */
/******************************************/
int ListFind(SqList &L)
{// 有序通讯录线性表上的查找
       int i=1;
          int xz;
       DataType *p;
       p=L.elem;
          char SNum[5];
          char SName[9];
 
 
          do
          {
                 cout<<"1 按编号查询  2 按姓名查询:";
                 cin>>xz;
                 cout<<endl;
                 if(xz!=1&&xz!=2)
                       cout<<"输入错误!/n/n";
          }while(xz!=1&&xz!=2);
 
          if(xz==1)
          {
                 int j=0;
                 cout<<"输入编号:";
                       cin>>SNum;
                 cout<<endl;
                 while(i<=L.length && strcmp(p[i-1].num,SNum)!=0)
                        i++;
                 if(i>L.length)
              return 0;
                 else
              return i;
          }
          else
          {
                 int j;
                 cout<<"输入姓名:";
                    cin>>SName;
                 cout<<endl;
 
                 while(i<=L.length && strcmp(p[i-1].name,SName)!=0)
                        i++;
 
                 if(i>L.length)
              return 0;
                 else
              return i;
          }
 
}
/*******************************/
/*     通讯录线性表上的结点删除    */
/*********************************/
int DelNode(SqList &L)
{
       DataType * p;
       DataType * q;
          int i;
 
          cout<<"要删除哪个位置上的元素?:";
          cin>>i;
          cout<<endl;
 
       if(i<1||i>L.length)
              return ERROR;//特殊情况的处理
 
       p=&(L.elem[i-1]);
       q=L.elem+L.length-1;
 
       for(p;p<q;p++)
       {
           strcpy(p->num,(p+1)->num);//i-1之后的元素依次后移一位
                 strcpy(p->name,(p+1)->name);
                 strcpy(p->sex,(p+1)->sex);
                 strcpy(p->phone,(p+1)->phone);
                 strcpy(p->addr,(p+1)->addr);
       }//i之后的元素依次左移
       L.length--;
          return OK;
}
   /**********************************/
   /*   通讯录线性表的输出函数         */
   /**********************************/
void PrintList(SqList &L)
{
    int i;  
       for(i=0;i<L.length;i++)
       {
              cout<<L.elem[i].num<<" "<<L.elem[i].name<<" "<<L.elem[i].sex<<" "
                              <<L.elem[i].phone<<" "<<L.elem[i].addr<<endl;
       }
       if(L.length==0)
              cout<<"通讯录中没有元素!/n";
       cout<<endl;
}
 
//主函数
void main()
{
    SqList L;
       InitList(L);
       for( ; ; )
       {
              switch(menu_select( ) )
              {
                case 1:
                       cout<<"**********************************/n";
                       cout<<"*    通 讯 录 线 性 表 的 建 立     */n";
                       cout<<"**********************************/n";
                       CreateList(L);
                       break;
                case 2:
                       cout<<"**********************************/n";
                       cout<<"*    通 讯 者 信 息 的 添 加     */n";
                       cout<<"**********************************/n";
 
                       cout<<endl;
 
                       cout<<"编号(4)-姓名(8)-性别(3)-电话(11)-地址(31)/n";
                       DataType p; //申请新结点
                       cin>>p.num>>p.name>>p.sex>>p.phone>>p.addr;
 
                       int i;
                       int m;
                       cout<<"想插到哪个位置上? ";
                       cin>>i;
                       cout<<endl;
 
                       m=InsertNode(L,i,p);
 
                       if(m==ERROR)
                              cout<<"你输入的元素位置超过了界限!/n/n";
                       else
                              cout<<"已经插入了该元素!/n/n";
                       break;
                case 3:
                       int a;
                       cout<<"**********************************/n";
                       cout<<"*    通 讯 者 信 息 的 查 询     */n";
                       cout<<"**********************************/n";
                       a=ListFind(L);
 
                       if(a!=0)
                       {
                              cout<<"编号(4)-姓名(8)-性别(3)-电话(11)-地址(31)/n";
                              cout<<L.elem[a-1].num<<" "<<L.elem[a-1].name<<" "<<L.elem[a-1].sex<<" "
                                          <<L.elem[a-1].phone<<" "<<L.elem[a-1].addr<<endl;
                              cout<<endl;
                       }
 
                       else
                            cout<<"没有查到要查询的通讯者!/n/n";
                       break;
                case 4:
                       int b;
                      
                       cout<<"**********************************/n";
                       cout<<"*    通 讯 者 信 息 的 删 除     */n";
                       cout<<"**********************************/n";
 
                       b=DelNode(L);  //删除结点
                       if(b==0)
                              cout<<"你输入的元素位置超过界限!/n/n";
                       else
                              cout<<"已经成功删除了该元素!/n/n";
                     break;
                case 5:
                       cout<<"**********************************/n";
                       cout<<"*    通 讯 者 信 息 的 输 出     */n";
                       cout<<"**********************************/n";
                       PrintList(L);
                       break;
                case 0:
                       cout<<"/t  再  见! /n";
                       return;
              }
       }
}

 

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值