Консультация № 181504
21.12.2010, 11:43
49.88 руб.
0 1 1
Здравствуйте, уважаемые эксперты!

Короче говоря
есть готовая лабораторная работа
http://files.mail.ru/8VVSIK или зеркало http://depositfiles.com/files/d7p79wmj4
Её нужно переделать так чтобы в ней использовались map или multimap.

Работать должно на Windows 7 в microsoft visual studio 2008.

Обсуждение

давно
Профессор
230118
3054
22.12.2010, 10:38
общий
это ответ
Здравствуйте, Иванов Евгений Витальевич!

Эта программа использует map
[code h=200]
#include <map>
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

struct Person
{
string family, name, second_name;
string birth_date; //"31.12.2010"
string tel;

void input()
{
cout<<"\nInput Person:";
cout<<"\n family-->"; cin>>family;
cout<<"\n name-->"; cin>>name;
cout<<"\n second_name-->"; cin>>second_name;
cout<<"\n birth_date-->"; cin>>birth_date;
cout<<"\n tel-->"; cin>>tel;
}
void print()
{
cout<<"\nPerson:";
cout<<"\n"<<family;
cout<<"\n"<<name;
cout<<"\n"<<second_name;
cout<<"\n"<<birth_date;
cout<<"\n"<<tel;
}
void write(ofstream &f)
{
f<<family<<'\n';
f<<name<<'\n';
f<<second_name<<'\n';
f<<birth_date<<'\n';
f<<tel<<'\n';
}
void read(ifstream &f)
{
getline(f, family);
getline(f, name);
getline(f, second_name);
getline(f, birth_date);
getline(f, tel);
}
};

//bool EqFamily(Person x, Person y) {return x.family==y.family;}
//bool EqBirthDate(Person x, Person y) {return x.birth_date==y.birth_date;}
//bool EqTel(Person x, Person y) {return x.tel==y.tel;}

class MyNoteBook
{
map<string,Person> a;
string filename;
public:
MyNoteBook() {filename="MyNoteBook.txt";}
void New() {this->a.clear();}
void Open();
void Save();
void Add(Person x) {
std::map<std::string,float> col;
string s=x.family;
a[s]=x;
}
map<string,Person>::iterator FindByFamily(string s, bool &found);
map<string,Person>::iterator FindByBirthDate(string s, bool &found);
map<string,Person>::iterator FindByTel(string s, bool &found);


};

map<string,Person>::iterator MyNoteBook::FindByFamily(string s, bool &found)
{
found=false;
if(a.find(s)!=a.end()) found=true;

return a.find(s);
}
map<string,Person>::iterator MyNoteBook::FindByBirthDate(string s, bool &found)
{
found=false;
map<string,Person>::iterator it;

for(it=a.begin(); it!=a.end(); it++)
if((*it).second.birth_date == s)
{
found=true;
break;
}
return it;
}

map<string,Person>::iterator MyNoteBook::FindByTel(string s, bool &found)
{
found=false;
map<string,Person>::iterator it;

for(it=a.begin(); it!=a.end(); it++)
if((*it).second.tel == s)
{
found=true;
break;
}
return it;
}

void err(char *mes)
{
cout<<"\nERROR: "<<mes;
system("pause");
exit(1);
}

void MyNoteBook::Save()
{
ofstream f(filename.c_str());
if(!f)
err("Cannot open output file");
f<<a.size()<<'\n';
map<string,Person>::iterator it;
for(it=a.begin(); it!=a.end(); it++)
(*it).second.write(f);
}

void MyNoteBook::Open()
{
ifstream f(filename.c_str());
if(!f)
err("Cannot open input file");

string s;
getline(f,s);
int n = atoi(s.c_str());

Person x;
for(int i=0;i<n;i++)
{
x.read(f);
a.insert(make_pair(x.family,x));
}
}

void main()
{
MyNoteBook book;

Person x;
string sChoice, s;
map<string,Person>::iterator it;
bool found;

while(1)
{
system("cls");
cout<<"Choose from menu:";
cout<<"\n N - New";
cout<<"\n O - Open";
cout<<"\n S - Save";
cout<<"\n A - Add";
cout<<"\n F - FindByFamily";
cout<<"\n B - FindByBirthDate";
cout<<"\n T - FindByTel";
cout<<"\n E - Exit";
cout<<"\nInput your choice: ";
getline(cin, sChoice);

if(sChoice.length()==0)
continue;
if(sChoice[0]=='E' || sChoice[0]=='e')
break;

switch(sChoice[0])
{
case 'A':
case 'a':
x.input();
book.Add(x);
break;
case 'F':
case 'f':
cout<<"\nInput family: ";
cin>>s;
it = book.FindByFamily(s, found);
if(!found)
cout<<"\nNot found";
else
{
cout<<"\nFound\n";
(*it).second.print();
}
break;
case 'B':
case 'b':
cout<<"\nInput Birth Date (format dd.mm.yyyy): ";
cin>>s;
it = book.FindByBirthDate(s, found);
if(!found)
cout<<"\nNot found";
else
{
cout<<"\nFound\n";
(*it).second.print();
}
break;
case 'T':
case 't':
cout<<"\nInput Telephone: ";
cin>>s;
it = book.FindByTel(s, found);
if(!found)
cout<<"\nNot found";
else
{
cout<<"\nFound\n";
(*it).second.print();
}
break;
case 'S':
case 's':
book.Save();
break;
case 'O':
case 'o':
book.Open();
break;
}

}
getline(cin,s);
}
[/code]
Форма ответа