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

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很好,以下是一个简单的实现: ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace std; struct Contact { string name; string phone; }; void addContact(vector<Contact>& contacts) { Contact newContact; cout << "请输入姓名:"; cin >> newContact.name; cout << "请输入电话号码:"; cin >> newContact.phone; contacts.push_back(newContact); cout << "添加成功!" << endl; } void searchContact(const vector<Contact>& contacts, const string& keyword) { bool found = false; for (const auto& contact : contacts) { if (contact.name.find(keyword) != string::npos || contact.phone.find(keyword) != string::npos) { cout << contact.name << " " << contact.phone << endl; found = true; } } if (!found) { cout << "未找到匹配的记录!" << endl; } } void deleteContact(vector<Contact>& contacts, const string& keyword) { auto it = remove_if(contacts.begin(), contacts.end(), [&](const Contact& contact) { return contact.name.find(keyword) != string::npos || contact.phone.find(keyword) != string::npos; }); int count = distance(it, contacts.end()); contacts.erase(it, contacts.end()); cout << "删除成功,共删除 " << count << " 条记录!" << endl; } void showContacts(const vector<Contact>& contacts) { cout << "通讯录:" << endl; for (const auto& contact : contacts) { cout << contact.name << " " << contact.phone << endl; } } int main() { vector<Contact> contacts; string filename = "contacts.txt"; ifstream fin(filename); if (fin.is_open()) { string line; while (getline(fin, line)) { Contact contact; size_t pos = line.find(' '); contact.name = line.substr(0, pos); contact.phone = line.substr(pos + 1); contacts.push_back(contact); } fin.close(); cout << "已从文件 " << filename << " 中读取 " << contacts.size() << " 条记录!" << endl; } while (true) { cout << "请输入操作:1-新增,2-查找,3-删除,4-显示所有记录,0-退出:"; int cmd; cin >> cmd; if (cmd == 1) { addContact(contacts); ofstream fout(filename, ios::app); fout << contacts.back().name << " " << contacts.back().phone << endl; fout.close(); } else if (cmd == 2) { string keyword; cout << "请输入关键字:"; cin >> keyword; searchContact(contacts, keyword); } else if (cmd == 3) { string keyword; cout << "请输入关键字:"; cin >> keyword; deleteContact(contacts, keyword); ofstream fout(filename); for (const auto& contact : contacts) { fout << contact.name << " " << contact.phone << endl; } fout.close(); } else if (cmd == 4) { showContacts(contacts); } else if (cmd == 0) { break; } else { cout << "无效的操作!" << endl; } } return 0; } ``` 这个程序使用了一个 `Contact` 结构体来表示单个通讯录条目,其中包含姓名电话号码两个字段。通讯录数据存储在一个 `vector<Contact>` 中,并且可以通过文件读写来实现数据的持久化。程序提供了四个操作:新增、查找、删除和显示所有记录。其中,查找和删除操作都支持不完全输入的关键字匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值