Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdlib.h>
- #include <iostream>
- #include <conio.h>
- #include <string>
- #include <windows.h>
- #include <stdio.h>
- #define HASH_MULTIPL 128 //Кратность хэша
- struct stWord
- {
- int n_start; //Номер первой буквы слова в тексте
- int n_end; //Номер последнией буквы
- std::string word_s; //Само слово
- stWord* next;
- };
- void Read_file(FILE*);
- int Symbol_number(FILE*);
- int Calc_hash(char* );
- stWord* Add_word(int );
- void Fill_hash(char* );
- void View_hash(void);
- void Search(std::string );
- int val_sym,val_word;
- char* cText;
- stWord *Hash[HASH_MULTIPL];
- using namespace std;
- HANDLE hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale (LC_CTYPE, "Russian");
- //Открываем файл и записываем его содержимое в массив
- FILE* fText;
- fText=fopen("text.txt","r");
- if (!fText)
- {
- SetConsoleTextAttribute (hStdout, 12);
- cout<<"Файл не найден\n";
- SetConsoleTextAttribute (hStdout, 7);
- return 0;
- }
- val_sym=Symbol_number(fText);
- cText=(char*)malloc(val_sym+2);
- fText=fopen("text.txt","r");
- Read_file(fText);
- fclose(fText);
- Fill_hash(cText);
- cout<<cText<<endl<<endl;
- SetConsoleTextAttribute (hStdout, 15);
- cout<<"Поиск: ";
- string key;
- cin>>key;
- SetConsoleTextAttribute (hStdout, 7);
- cout<<endl;
- SetConsoleTextAttribute (hStdout, 15);
- cout<<"--------------------------------------------------------------------------------";
- SetConsoleTextAttribute (hStdout, 7);
- Search(key);
- }
- void Read_file(FILE*text)
- {
- int i;
- cText[0]=' ';
- for (i=1; i<=val_sym;i++)
- cText[i]=fgetc(text);
- cText[i]='\0';
- }
- int Symbol_number(FILE*text)
- {
- text=fopen("text.txt","r");
- int n=0;
- char ch;
- while((ch=fgetc(text))!=EOF)
- n++;
- fclose(text);
- if (n==0)
- {
- SetConsoleTextAttribute (hStdout, 12);
- cout<<"Файл пуст\n";
- SetConsoleTextAttribute (hStdout, 7);
- exit(1);
- }
- return n;
- }
- int Calc_hash(char* tmp)
- {
- int sum=0;
- while ((*tmp>='A')&&(*tmp<='z'))
- {
- sum+=(*tmp);
- tmp++;
- }
- return sum%HASH_MULTIPL;
- }
- void Fill_hash(char* text)
- {
- int i;
- int nStart=0,nEnd=0,hash=0; //Номер буквы начала и конца слова, хэш-сумма слова
- stWord* tmpWord=0; //Указатель для создания нового элемента списка
- for(i=1;i<=val_sym;i++)//Идём по всему тексту по буквам
- {
- if ((cText[i]>='A')&&(cText[i]<='z')&&((cText[i-1]<'A')||(cText[i-1]>'z')))//Встретили начало слова
- {
- nStart=i;
- hash=Calc_hash(cText+nStart);
- }
- if (((cText[i+1]<'A')||(cText[i+1]>'z'))&&(nStart!=0)) //Встретили конец слова
- {
- nEnd=i;
- tmpWord=Add_word(hash); //Добавили новый элемент в списко с номером hash
- //И заполнили:
- tmpWord->n_end=nEnd;
- tmpWord->n_start=nStart;
- (tmpWord->word_s).insert(0,cText,nStart,(nEnd-nStart+1));
- nStart=0;
- }
- }
- }
- stWord* Add_word(int hash)
- {
- stWord* newWord=new stWord; //Выделяется память под слово
- newWord->next=0; //Новый элемент - хвост списка
- if(Hash[hash]==0)//Если списко был пуст, то новый элемент - начало списка
- {
- Hash[hash]=newWord;
- }
- else
- {
- stWord* tmp;
- tmp=Hash[hash];
- while(tmp->next!=0) tmp=tmp->next;//Иначе ищем конец списка
- tmp->next=newWord;//И присоединяем новый элемент в конец списка
- }
- return newWord;
- }
- void View_hash(void) //Вспомогательная функция, выводит на экран содержимое хэша
- {
- int i;
- stWord* ptr;
- for (i=0;i<HASH_MULTIPL;i++)
- {
- if(Hash[i]!=0)
- {
- ptr=Hash[i];
- printf("hash %i -",i);
- do
- {
- printf(" %i %i ", ptr->n_start, ptr->n_end);
- cout<<ptr->word_s<<" | ";
- ptr=ptr->next;
- }while(ptr!=0);
- }
- else printf("hash %i - empty", i);
- printf("\n");
- }
- }
- void Search(string sWord)
- {
- int i,match=0;
- stWord* hashedWords; //Указатель для перемещения по нужному списку
- int wHash; //Хэш sWord
- int len=sWord.length();
- char* cWord=(char*)malloc(len); //sWord будет в форме строки-массива
- sWord.copy(cWord,len,0); //Копируем
- wHash=Calc_hash(cWord); //Чтобы посчитать хэш
- hashedWords=Hash[wHash]; //И переходим к нужному списку
- if(!Hash[wHash])//Указатель на голову списка нулевой - результатов заведомо нет
- {
- cout<<cText<<endl;
- SetConsoleTextAttribute (hStdout, 15);
- cout<<"Совпадений не найдено"<<endl;
- SetConsoleTextAttribute (hStdout, 7);
- return;
- }
- for(i=1;i<=val_sym;i++)//Выводим текст посимвольно
- {
- if((i==(hashedWords->n_start))&&(!(hashedWords->word_s.compare(sWord))))
- {
- SetConsoleTextAttribute (hStdout, 224); //Включая подсветку в начале каждого совпадения
- match++;
- }
- if(i==hashedWords->n_end+1)
- {
- SetConsoleTextAttribute (hStdout, 7); //И выключая в конце
- if(hashedWords->next)
- hashedWords=hashedWords->next; //Не забываем бежать по списку
- }
- putch(cText[i]);
- }
- //Выводим количество совпадений
- SetConsoleTextAttribute (hStdout, 15);
- if(!match)
- cout<<endl<<"Совпадений не найдено"<<endl;
- else
- cout<<endl<<"Совпадения: "<<match<<endl;
- SetConsoleTextAttribute (hStdout, 7);
- }
Add Comment
Please, Sign In to add comment