ExtremeДобавлено (2011-03-11, 9:58 Am)
---------------------------------------------
// XPE.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <locale>
#include <gtest/gtest.h>
using namespace std;
struct Tree_Cell
{
char name[20];
int value;
Tree_Cell* next[50];
int iMax_Cell;
};
///////////////////////////////////////////////////
//////////////////////////////////HESH DATA
///////////////////////////////////////////////////
class HeshData
{
private:
int makehash(string name)
{
int sum;
sum=0;
for(int i=0; i<name.length(); i++)
{
sum += name[i];
}
sum = abs(sum)/10;
sum = min(99,sum);
return sum;
}
public:
HeshData() { for(int i=0; i<100; i++){heshnames[i] = "*";} maxOHNames=0;}
int SetHash(string name)
{
int sum;
sum = makehash(name);
while(heshnames[sum] != "*")
{
sum++;
if (sum>100) {sum = 0;}
}
heshnames[sum] = name;
sOrgHeshNames[maxOHNames]=name;
maxOHNames++;
return sum;
}
int GetHash(string name)
{
int sum;
sum = makehash(name);
while(heshnames[sum]!=name)
{
if (heshnames[sum]=="*"){return -1;}
sum++;
if (sum>100) {sum = 0;}
}
return sum;
}
string sOrgHeshNames[100];
int maxOHNames;
private:
string heshnames[100];
};
///////////////////////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////
void MakeData(HeshData *Hesh,Tree_Cell** cNods, char *fname)
{
/////Читаем строчку
char buffer [256];
char name[20],cValue[100];
string sLine;
fstream fin;
fin.open(fname,ios::in);
fin.getline(buffer,256);
sLine = buffer;
Tree_Cell* Last_Header;
Last_Header = new Tree_Cell;
Last_Header->iMax_Cell=0;
Tree_Cell* Last_Main_Header;
Last_Main_Header = new Tree_Cell;
Last_Main_Header = Last_Header;
while(sLine != "")
{
int i2=0;
for(int i=0; i<sLine.length(); i++)
{
if(sLine[i]!=':')
{
name[i2]=sLine[i]; //Читаем имя до двоеточия
i2++;
}else{
name[i2]='\0'; //Считали имя
i2 = 0;
//MessageBox(hWnd,name,"MESSAGE",MB_OK);
strcpy(Last_Header->name,name); i+=1; //read ':'
int k=0;
while((sLine[i]!=':')&&(sLine[i]!=','))//Читаем значения до двоеточия
{
cValue[k]=sLine[i];
k++; i++;
if (i>sLine.length()) {break;}
}
cValue[k]='\0'; //Считали значение
//MessageBox(hWnd,cValue,"MESSAGE",MB_OK);
Last_Header->value = atoi(cValue); //Заполняем ячейку
/////////////////////////////////////////////////////
if(Last_Header != Last_Main_Header) //Добавляем ее к отцу
{
Last_Main_Header->next[Last_Main_Header->iMax_Cell] = new Tree_Cell;
Last_Main_Header->next[Last_Main_Header->iMax_Cell] = Last_Header;
Last_Main_Header->iMax_Cell++;
}
Last_Header = new Tree_Cell; //Создаем новый лист для следуещго параметра, если его нет то зря тратим память*/
}//else
}//for
string sNodName;
sNodName = Last_Main_Header->name;
cNods[Hesh->SetHash(sNodName)] = Last_Main_Header; //Загоняем в Массив строительных ячеек по имени.
Last_Main_Header = new Tree_Cell; //для следующей итерации, создаем новый нод;
Last_Main_Header->iMax_Cell = 0;
Last_Header = new Tree_Cell;
Last_Header = Last_Main_Header;
fin.getline(buffer,256);
sLine = buffer;
//MessageBox(hWnd,sNodName.c_str(),"MESSAGE",MB_OK);
//MessageBox(hWnd, buffer,"MESSAGE",MB_OK);
}
//cout<<sNodName.c_str();
}
void main()
{
setlocale(LC_ALL,"");//".1251");
HeshData Hesh;
Tree_Cell* cNods[100];
char fname[256];
strcpy(fname,"input.txt");
MakeData(&Hesh,cNods,fname);
string sNodName = "амбарку";
cout<<Hesh.sOrgHeshNames[1]<<endl;
cout<<cNods[Hesh.GetHash(sNodName)]->next[1]->name;
HeshData Hesh1;
return;
}