用vector容器的存储结构写了一篇通讯录;
直接上代码
#pragma once
#include "head.h"
#include <vector>
class User
{
public:
User();
~User();
void Insert();
void Search();
void Display();
void Delete();
void Work();
private:
vector<PersonInfor> v_;
};
int chooseInMenu();
#include "user.h"
#include <string>
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<ctype.h>
using namespace std;
User::User()
{
ifstream infile("book.txt", ios::in);
if (!infile)
{
cerr << "open error" << endl;
exit(1);
}
string name;
string address;
string count;
//PersonInfor tmp;
while (infile >> name >> address >> count)
{
PersonInfor tmp(name,address,count);
v_.push_back(tmp);
}
infile.close();
}
User::~User()
{
//ofstream outfile("book.txt", ios::out);
//if (!outfile)
//{
// cerr << "open error" << endl;
// exit(1);
//}
//vector<PersonInfor>::iterator it;
//for (it = v_.begin(); it != v_.end();)
//{
// outfile << it->getName() << " ";
// outfile << it->getAddress() << " ";
// outfile << it->getCount()<< " ";
//}
//
//outfile.close();
}
void User::Insert()
{
PersonInfor p;
p.setPerson();
v_.push_back(p);
}
void User::Display()
{
vector<PersonInfor>::iterator it;
for (it = v_.begin(); it != v_.end();++it)
{
it->PersonDisplay();
}
}
void User::Search()
{
string name;
int i = 0;
cout << "请输入要查找的人的姓名:" << endl;
cin >> name;
vector<PersonInfor>::iterator it;
for (it = v_.begin(); it != v_.end();++it)
{
if (it->getName() == name)
{
it->PersonDisplay();
i++;
}
}
if (0 == i)
cout << "查无此人" << endl;
}
void User::Delete()
{
string name;
int i = 0;
cout << "请输入要删除人的姓名:" << endl;
cin >> name;
vector<PersonInfor>::iterator it;
for (it = v_.begin(); it != v_.end();)
{
if (it->getName() == name)
{
it->PersonDisplay();
i++;
it = v_.erase(it);
}
else {
++it;
}
}
if (0 == i)
cout << "查无此人" << endl;
}
void User::Work()
{
int iChoice;
int j = 0;
while (1)
{
iChoice = chooseInMenu();
switch (iChoice)
{
case 1:Insert();break;
case 2:Delete();break;
case 3:Search();break;
case 4:Display();break;
case 0:j = 1;break;
default:
break;
}
if (j == 1)
break;
}
}
int chooseInMenu()
{
int i;
while (1)
{
cout << endl;
cout << "+------------------------------------+" << endl;
cout << "+ 1.插入信息 2.删除信息 +" << endl;
cout << "+ 3.查找信息 4.显示信息 +" << endl;
cout << "+-------------------------------------+" << endl;
cout << "+ 0.退出 +" << endl;
cout << "+------------------------------------+" << endl;
cout << "请输入操作指令:";
cin >> i;
if (i >= 0 && i <= 9)
break;
else
cout << "请重新输入" << endl;
}
return i;
}
#include "head.h"
PersonInfor::PersonInfor(string name, string address, string count)
{
name_ = name;
address_ = address;
count_ = count;
}
PersonInfor PersonInfor::setPerson()
{
cout << "请输入姓名:地址:手机号 中间用空格隔开 " << endl;
cin >> name_ >> address_ >> count_;
return *this;
}
PersonInfor PersonInfor::PersonDisplay()
{
cout << "姓名:" << name_ << endl << "地址:" << address_ << endl << "手机号" << count_ << endl;
return *this;
}
string PersonInfor::getName()
{
return name_;
}
string PersonInfor::getAddress()
{
return address_;
}
string PersonInfor::getCount()
{
return count_;
}
#pragma once
#include "user.h"
int main()
{
cout << "+-------------------------------------------+" << endl;
cout << " ͨѶ¼ " << endl;
cout << "+-------------------------------------------+" << endl;
User t;
t.Work();
}