按姓名查找电话(完全与不完全)

来源:互联网 发布:拓扑图坐标算法 编辑:程序博客网 时间:2024/04/30 18:56

#include <iostream>
#include <string>
#define MAX 20
using namespace std;
typedef struct Dict
{
 char NA[MAX];
 char PN[MAX];
}Dict;
//存储信息的全局
Dict Data[200];
int N = 0;
void INSERT()
{
 char na[MAX],no[MAX];

 int i = 0;
 
 cout<<"电话薄:"<<endl;
 cout<<"请输入姓名,电话(以-1结束):"<<endl;

 // 输入数据
 cout<<i+1<<" . ";
 while(cin>>na>>no,na[0]!='-'&&na[1]!='1'&&no[0]!='-'&&no[1]!='1')
 {
  strcpy(Data[i].NA,na);
  strcpy(Data[i].PN,no);
  
  i++;
  N++;
  cout<<i+1<<" . ";
 }
}


int FullResearch(char str[])
{
 int i,j;
 int lenstr = strlen(str);

 for(i = 0; i < N; i++)
 {
  for(j = 0; j < strlen(Data[i].NA); j++)
  {

   if(str[j] == Data[i].NA[j])
   {
    continue;
   }
   else
   {
    break;
   }
  }
  if(j == strlen(Data[i].NA))
  {
   return i;
  }
  else
  {
   continue;
  }
 }
 return -1;
 

}

void FULL()
{
 char str[MAX];
 int j;
 cout<<"姓名 :";
 while(cin>>str,str[0]!='-'&&str[1]!='1')
 {
  getchar();
  j = FullResearch(str);
  if(j != -1)
  cout<<"电话 :"<<Data[j].PN<<endl;
  else
   cout<<"未找到!"<<endl;
 }
 cout<<"结束"<<endl;
 exit(1);
}

void UNFullResearch(char *str)
{
 int lenstr = strlen(str);

 int i,j,t=0;

 for(i = 0; i < N; i++)
 {
  for(j = 0; j < strlen(Data[i].NA); j++)
  {

   if(j == lenstr)
   {
    break;
   }
   if( str[j] == Data[i].NA[j])
   {
    continue;
   }
   else
   {
    break;
   }
  }
  if(j == lenstr)
  {
   cout<<"^^^^^^^^^^^^^^^^"<<endl;
   cout<<"姓名 :"<<Data[i].NA<<endl;
   cout<<"电话 :"<<Data[i].PN<<endl;
   t = 1;
  }

 }
 if(t == 0)
 {
  cout<<"未找到"<<endl;
 }
}

void UNFULL()
{
 char str[MAX];
 cout<<"姓名 :";
 cin>>str;
 UNFullResearch(str);
}

int main()
{
 INSERT();

 cout<<"****************查找方式******************"<<endl;
 cout<<"1为完全查找"<<endl;
 cout<<"2为不完全查找"<<endl;
 cout<<"请输入查找方式 :";
 int so;
 cin>>so;
 switch(so)
 {
 case 1:FULL();break;
 case 2:UNFULL();break;

 }
 return 0;
}