Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <vector>
- #include <atlstr.h>
- //#include <ctype.h>
- #include "iostream"
- #include <windows.h>
- #include <conio.h>
- #include <algorithm>
- #define BUTTON_SIZE 20 //Размер кнопок в меню
- using namespace std;
- void Read_file();
- void View_text_func();
- void Print_CStr(CString);
- CString Input_CStr();
- void Select_str();
- void Output_result(vector <int> ,int ,int );
- int Input_int();
- void Fill_file();
- void Write_str_in_file(CString str);
- //Горизонтальное меню
- int Menu (char** );
- void BackLight (int , int );
- int flag = 0;
- COORD coord = {0,0};
- COORD zero_coord={0,0};
- COORD insert_coord={0,1}; //Координаты области ввода
- COORD output_coord={0,3}; //Координаты области вывода
- COORD search_coord={42,9};//Коордиаты области поиска
- char* toolbar[] = {//Текст кнопок меню
- " Insert to begin ",
- " Copy string ",
- " Search string ",
- " Save and Exit ",
- NULL
- };
- void Insert_func();
- void Copy_func();
- void Search_func();
- void (*action[])(void)={Insert_func,Copy_func,Search_func,NULL};//Действия кнопок
- HANDLE hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cci;
- vector <CString> Text;
- int _tmain(int argc, _TCHAR* argv[])
- {
- Read_file();
- //Скрываем курсор
- cci.dwSize=1;
- cci.bVisible=FALSE;
- SetConsoleCursorInfo(hStdout,&cci);
- int k;
- for(;;)//Меню
- {
- system("cls");
- View_text_func();
- k=Menu(toolbar);
- SetConsoleTextAttribute (hStdout, 0x0f);
- system("cls");
- if (k==3)
- {
- Fill_file();
- cout<<endl;
- break;
- }
- action[k]();
- }
- exit(1);
- }
- int Menu(char** str)
- {
- char ch;
- SetConsoleCursorPosition (hStdout, zero_coord);
- SetConsoleTextAttribute (hStdout, 0x0f);
- while (*str) printf ("%s",*str++);
- SetConsoleCursorPosition (hStdout, coord);
- SetConsoleTextAttribute (hStdout, 0x70);
- cout<<toolbar[coord.X/BUTTON_SIZE];
- for(;;)
- {
- ch=getch();
- switch (ch)
- {
- case 77 : if (coord.X==(3*BUTTON_SIZE)) break;//Стрелка вправо
- coord.X += BUTTON_SIZE; SetConsoleCursorPosition (hStdout, coord); break;
- case 75 : if (coord.X==0) break;//Стрелка влево
- coord.X -= BUTTON_SIZE; SetConsoleCursorPosition (hStdout, coord); break;
- case 13 : return(coord.X/BUTTON_SIZE); break;//Enter
- }
- BackLight (coord.X, coord.Y);
- }
- }
- void BackLight (int x, int y)
- {
- coord.X = flag;
- SetConsoleCursorPosition (hStdout, coord);
- SetConsoleTextAttribute (hStdout, 0x0f);
- printf("%s",*(toolbar+(flag/BUTTON_SIZE)));
- SetConsoleCursorPosition (hStdout, coord);
- coord.X = x;
- SetConsoleCursorPosition (hStdout, coord);
- SetConsoleTextAttribute (hStdout, 0x70);
- printf("%s",*(toolbar+(x/BUTTON_SIZE)));
- SetConsoleCursorPosition (hStdout, coord);
- flag = x;
- }
- void Read_file()
- {
- FILE* fText=fopen("text.txt","r");
- if (!fText)
- {
- cout<<"No file!\n";
- exit(1);
- }
- CString buf;
- while (!feof (fText))
- {
- char c=fgetc(fText);
- if ((c == 10) || (c == EOF))
- {
- Text.push_back (buf);
- buf.Empty();
- } else buf.AppendChar(c);
- }
- fclose (fText);
- }
- void Fill_file()
- {
- FILE* fText=fopen("text.txt","w");
- fclose(fText);
- for_each(Text.begin(),Text.end(),Write_str_in_file);
- }
- void Write_str_in_file(CString str)
- {
- FILE* fText=fopen("text.txt","a");
- int i;
- for(i=0;i<str.GetLength();i++)
- {
- fprintf(fText,"%c",str[i]);
- }
- fprintf(fText,"%c",10);
- fclose(fText);
- }
- int Input_int()//Ввод целых положителльных чисел
- {
- int c,n;
- c=NULL;
- n=NULL;
- for(;;)
- {
- n=getch();
- if ((!c) && (n==48)) continue;//Первая цифра не ноль
- if ((n==13) && (c==0)) continue;//Нельзя ввести пустоту
- if (n==8 && c)//Backspace
- {
- printf("\b \b");
- c/=10;
- }
- if (n==13) break;//Enter
- if ((n>=48) && (n<=58))
- {
- putch(n);
- c=(c*10);
- c+=(n-48);
- }
- }
- return c;
- }
- void View_text_func()
- {
- SetConsoleCursorPosition (hStdout, output_coord);
- SetConsoleTextAttribute (hStdout, 0x7);
- for_each(Text.begin(),Text.end(),Print_CStr);
- }
- void Print_CStr(CString str)
- {
- int n=str.GetLength();
- for (int i=0;i<=n;i++)
- {
- cout<<((char)str[i]);
- }
- cout<<endl;
- }
- CString Input_CStr()
- {
- char buf[10000];
- char* ptr=buf;
- SetConsoleTextAttribute (hStdout, 0xf);
- cout<<"Enter text:"<<endl;
- SetConsoleTextAttribute (hStdout, 0x7);
- CString tmp;
- cin>>buf;
- while(*ptr)
- {
- tmp.AppendChar(*ptr);
- ptr++;
- }
- return tmp;
- }
- void Insert_func()
- {
- CString str=Input_CStr();
- Text.insert(Text.begin(),str);
- }
- void Copy_func()
- {
- int n;
- do
- {
- system("cls");
- Select_str();
- SetConsoleTextAttribute (hStdout, 0xf);
- cout<<"Select string:"<<endl;
- SetConsoleTextAttribute (hStdout, 0x7);
- }
- while((n=Input_int())>(Text.size()));
- CString str=Text[n-1];
- Text.insert(Text.begin()+n-1,str);
- //system("cls");
- }
- void Search_func()
- {
- vector <int> results;
- int str_number=0,key_len,i=0;
- do
- {
- system("cls");
- Select_str();
- SetConsoleTextAttribute (hStdout, 0xf);
- cout<<"Select string"<<endl;
- SetConsoleTextAttribute (hStdout, 0x7);
- }while((str_number=Input_int())>(Text.size()));
- cout<<endl;
- CString key=Input_CStr();
- key_len=key.GetLength();
- results.push_back(Text[str_number-1].Find(key));
- while(Text[str_number-1].Find(key,results.back()+1)>0)
- {
- results.push_back(Text[str_number-1].Find(key,results.back()+1));
- }
- if(results[0]==-1)
- {
- system("cls");
- cout<<"No results!"<<endl;
- getch();
- return;
- }
- system("cls");
- for_each(Text.begin(),Text.begin()+str_number-1,Print_CStr);
- cout<<endl;
- Output_result(results,str_number,key_len);
- cout<<endl;
- for_each(Text.begin()+str_number,Text.end(),Print_CStr);
- getch();
- }
- void Select_str()
- {
- SetConsoleTextAttribute (hStdout, 0x7);
- for(int i=0;i<Text.size();i++)
- {
- cout<<i+1<<". ";
- Print_CStr(Text[i]);
- }
- }
- void Output_result(vector <int> results,int str_number,int key_len)
- {
- int str_len=Text[str_number-1].GetLength(),k=0;
- for (int i=0;i<str_len;i++)
- {
- if(k<results.size())
- {
- if(i==(results[k]))
- {
- SetConsoleTextAttribute (hStdout, 224);
- for(int j=0;j<key_len;j++)
- {
- putch(Text[str_number-1].GetAt(i+j));
- }
- i+=key_len-1;
- SetConsoleTextAttribute (hStdout, 0x7);
- k++;
- }
- else
- putch(Text[str_number-1].GetAt(i));
- }
- else
- putch(Text[str_number-1].GetAt(i));
- }
- }
Add Comment
Please, Sign In to add comment