// lab4.cpp : Defines the entry point for the console application.
//
 
#include <stdafx.h>
#include <iostream>
#include <memory.h>
#include <conio.h>
#include <string.h>
 
using namespace std;
 
struct Pair
{
	char name[21];	//имя
	int value;	//значение
	Pair& operator=(int);//установить значение
	Pair& operator=(const char*);//установить имя
	friend istream& operator>>(istream& is, Pair&);//ввод
	friend ostream& operator<<(ostream& os, Pair&);//вывод
};
 
Pair& Pair::operator=(int x)
{
	value=x;
	return (Pair&)x;
}
 
Pair& Pair::operator=(const char* nm)
{
	strcpy(name,nm);
	return (Pair&)name;
}
 
istream& operator>>(istream& is, Pair& p)
{
	printf("oper vvoda");
	return (is>>p.name>>p.value);
}
 
ostream& operator<<(ostream& os, Pair& p)
{
	return (os<<p.name<<" = "<<p.value<<endl);
}
 
 
class Pairs
{
public:
	Pairs();
	Pairs(int);
	Pairs(const Pairs&);
	~Pairs();
private:
	Pair* prs;	//массив пар
	int length;	//размер массива
	int count;	//заполненное количество пар
public:
	void Init(int len);
	bool GetValue(char *, int&);
	int SetValue(char*, int);
	friend void Print(const Pairs&);
	Pairs& operator=(const Pairs&);//копирование пар
	Pairs& operator+=(Pairs&);//добавление пар
	Pairs& operator+=(const Pair&);//добавление новой пары
	char * operator[](int);//получение имени по значению
	int operator[](const char *);//получение значения по имени
	friend ostream& operator<<(ostream& os, const Pairs&);//вывод
};
 
Pairs& Pairs::operator+=(Pairs& pr)
{
		length=pr.length+1;
		return (Pairs&)prs;
}	
 
Pairs& Pairs::operator+=(const Pair& p)
{
	for (int i=0; i<count; i++)
	{
		if (strcmp(prs[i].name,p.name)==0)
		{
			prs[i] = p.value;
			return (Pairs&)prs;
		}
	}
	if ((count < length) && (count >= 0))
	{
		prs[count] = p.name;
		prs[count] = p.value;
		count++;
		return (Pairs&)prs;
	}
}
 
Pairs& Pairs::operator =(const Pairs& t)
{
	if (this!=&t)
	{
		count = t.count;
		length = t.length;
		prs = new Pair[t.length];
		for (int i=0; i<t.count; i++)
			prs[i] = t.prs[i];	
	}
	return (Pairs&)prs;
}
 
int Pairs::operator[](const char* nm)
{
	for (int i=0; i<count; i++)
		if (strcmp(prs[i].name,nm)==0) 
			return(prs[i].value);
}
 
char* Pairs::operator[](int vl)
{
	for (int i=0; i<count; i++)
		if (prs[i].value==vl) 
			return (prs[i].name); 
	return ("no");
}
 
ostream& operator<<(ostream& os, const Pairs& pr2)
{
	for (int i=0;i<pr2.count;i++) 
		os<<pr2.prs[i]<<endl;
	return os;
}
 
Pairs::Pairs()
{
	printf("Defualt constructor is done!\n");
}
 
Pairs::Pairs(int len)
{
	length = len;
	count = 0;
	prs = new Pair[len];
	printf("constructor is done!\n");
}
 
Pairs::~Pairs()
{
	delete [] prs;
	printf("Destructor is done\n");
}
 
Pairs::Pairs(const Pairs &pp)
{
	count = pp.count;
	length = pp.length;
	prs = new Pair[length];
	for (int i=0; i<count; i++)
	{
		strcpy(prs[i].name, pp.prs[i].name);
		prs[i].value = pp.prs[i].value;
	}
	printf("copy constructor is done\n");
}
 
void Pairs::Init(int len)
{
	length = len;
	count = 0;
	prs = new Pair[len];
}
 
int Pairs::SetValue(char* name, int value)
{
	for (int i=0; i<count; i++)
	{
		if (strcmp(prs[i].name,name)==0)
		{
			prs[i] = value;
			return 2;
		}
	}
	if ((count < length) && (count >= 0))
	{
		prs[count] = name;
		prs[count] = value;
		count++;
		return 1;
	}
	else 
		return 0;
}
 
bool Pairs::GetValue(char *name, int& value)
{
	bool b = false;
	for (int i=0; i<count; i++)
		if (strcmp(prs[i].name,name)==0) 
		{
			value = prs[i].value;
			b=true;
		}
	return b;
}
 
 
void Print(const Pairs& p)
{
	for (int i=0; i<p.count; i++) 
	{
		printf("%s=%d ", p.prs[i].name, p.prs[i].value);
	}
	printf("\n");
}
 
void main()
{
	Pairs p1;
	int len, val1;
	char name[21];
	int answer, a;
	bool d = false;
	cout<<"Enter lenght of pairs..."<<"\n";
	cin>>len;
	Pairs p = Pairs(len);
	do
	{
		printf("What do you want to do? (Set/Print/Get/Copy/Add pair/Exit)");
		answer = getche();
		cout << "\n";
		if (answer < 'a')
			answer = answer + ('a'-'A');
		switch(answer){
			case 's':
				Pair q;
				cin>>q;
				p+=q;
				break;	
			case 'p':
				cout<<p;
				break;
			case 'g':
					printf("Do you want get by Name or Value");
					answer = getche(); 
					cout << "\n";
						if (answer < 'a')
							answer = answer + ('a'-'A');
						if (answer=='n')
						{
							printf("Enter name = ");
							scanf("%s",name);
							a = p[name];
							if (!a)
								printf("Name is not found... Try again?\n");
							else
								printf("Value = %d\n", a);
						}
						if (answer=='v')
						{
							printf("Enter value = ");
							scanf("%d",&val1);
							if (p[val1]=="no")
								printf("Value is not found... Try again?\n");
							else
								printf("Name = %s\n", p[val1]);
						}
				break;
			case 'c':
				p1 = p;
				printf("Do you want to print of copy pairs? (y/n) ");
				if (getche()=='y')
					Print(p1);
				break;
			case 'a':
				p+=p;
				break;
			case 'e':
				printf("Good bye!!!\n");
				break;
 
			default:
				printf("Input error!!!\n");
		}
	}while (answer != 'e');
	getch();
}