Guest User

Untitled

a guest
Aug 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.58 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <atlstr.h>
  4. //#include <ctype.h>
  5. #include "iostream"
  6. #include <windows.h>
  7. #include <conio.h>
  8. #include <algorithm>
  9.  
  10. #define BUTTON_SIZE 20 //Размер кнопок в меню
  11.  
  12. using namespace std;
  13.  
  14. void Read_file();
  15. void View_text_func();
  16. void Print_CStr(CString);
  17. CString Input_CStr();
  18. void Select_str();
  19. void Output_result(vector <int> ,int ,int );
  20. int Input_int();
  21. void Fill_file();
  22. void Write_str_in_file(CString str);
  23.  
  24. //Горизонтальное меню
  25. int Menu (char** );
  26. void BackLight (int , int );
  27. int flag = 0;
  28. COORD coord = {0,0};
  29. COORD zero_coord={0,0};
  30. COORD insert_coord={0,1}; //Координаты области ввода
  31. COORD output_coord={0,3}; //Координаты области вывода
  32. COORD search_coord={42,9};//Коордиаты области поиска
  33. char* toolbar[] = {//Текст кнопок меню
  34.                 " Insert to  begin   ",
  35.                 " Copy string        ",
  36.                 " Search string      ",
  37.                 " Save and Exit      ",
  38.                 NULL
  39.                };
  40. void Insert_func();
  41. void Copy_func();
  42. void Search_func();
  43. void (*action[])(void)={Insert_func,Copy_func,Search_func,NULL};//Действия кнопок
  44.  
  45. HANDLE  hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
  46. CONSOLE_CURSOR_INFO cci;
  47.  
  48.  
  49. vector <CString> Text;
  50.  
  51. int _tmain(int argc, _TCHAR* argv[])
  52. {
  53.     Read_file();
  54.     //Скрываем курсор
  55.     cci.dwSize=1;
  56.     cci.bVisible=FALSE;
  57.     SetConsoleCursorInfo(hStdout,&cci);
  58.  
  59.     int k;
  60.     for(;;)//Меню
  61.     {
  62.         system("cls");
  63.         View_text_func();
  64.         k=Menu(toolbar);
  65.         SetConsoleTextAttribute (hStdout, 0x0f);
  66.         system("cls");
  67.         if (k==3)
  68.         {
  69.             Fill_file();
  70.             cout<<endl;
  71.             break;
  72.         }
  73.     action[k]();
  74.     }
  75.  
  76.     exit(1);
  77. }
  78.  
  79. int Menu(char** str)
  80. {
  81.     char ch;
  82.     SetConsoleCursorPosition (hStdout, zero_coord);
  83.     SetConsoleTextAttribute (hStdout, 0x0f);
  84.     while (*str) printf ("%s",*str++);
  85.     SetConsoleCursorPosition (hStdout, coord);
  86.     SetConsoleTextAttribute (hStdout, 0x70);
  87.     cout<<toolbar[coord.X/BUTTON_SIZE];
  88.     for(;;)
  89.     {
  90.         ch=getch();
  91.         switch (ch)
  92.         {
  93.         case 77 : if (coord.X==(3*BUTTON_SIZE)) break;//Стрелка вправо
  94.             coord.X += BUTTON_SIZE; SetConsoleCursorPosition (hStdout, coord); break;
  95.         case 75 : if (coord.X==0) break;//Стрелка влево
  96.             coord.X -= BUTTON_SIZE; SetConsoleCursorPosition (hStdout, coord); break;
  97.         case 13 : return(coord.X/BUTTON_SIZE); break;//Enter
  98.         }
  99.         BackLight (coord.X, coord.Y);  
  100.     }
  101. }
  102.  
  103. void BackLight (int x, int y)
  104. {
  105.     coord.X = flag;
  106.     SetConsoleCursorPosition (hStdout, coord);
  107.     SetConsoleTextAttribute (hStdout, 0x0f);
  108.     printf("%s",*(toolbar+(flag/BUTTON_SIZE)));
  109.     SetConsoleCursorPosition (hStdout, coord);
  110.     coord.X = x;
  111.     SetConsoleCursorPosition (hStdout, coord);
  112.     SetConsoleTextAttribute (hStdout, 0x70);
  113.     printf("%s",*(toolbar+(x/BUTTON_SIZE)));
  114.     SetConsoleCursorPosition (hStdout, coord);
  115.     flag = x;
  116. }
  117.  
  118. void Read_file()
  119. {
  120.     FILE* fText=fopen("text.txt","r");
  121.     if (!fText)
  122.     {
  123.         cout<<"No file!\n";
  124.         exit(1);
  125.     }
  126.     CString buf;
  127.     while (!feof (fText))
  128.     {
  129.         char c=fgetc(fText);
  130.         if ((c == 10) || (c == EOF))
  131.         {
  132.             Text.push_back (buf);
  133.             buf.Empty();
  134.         } else buf.AppendChar(c);
  135.     }
  136.     fclose (fText);
  137. }
  138.  
  139. void Fill_file()
  140. {
  141.     FILE* fText=fopen("text.txt","w");
  142.     fclose(fText);
  143.     for_each(Text.begin(),Text.end(),Write_str_in_file);
  144. }
  145.  
  146. void Write_str_in_file(CString str)
  147. {
  148.     FILE* fText=fopen("text.txt","a");
  149.     int i;
  150.     for(i=0;i<str.GetLength();i++)
  151.     {
  152.         fprintf(fText,"%c",str[i]);
  153.     }
  154.     fprintf(fText,"%c",10);
  155.     fclose(fText);
  156. }
  157.  
  158. int Input_int()//Ввод целых положителльных чисел
  159. {
  160.     int c,n;
  161.     c=NULL;
  162.     n=NULL;
  163.     for(;;)
  164.     {
  165.         n=getch();
  166.         if ((!c) && (n==48)) continue;//Первая цифра не ноль
  167.         if ((n==13) && (c==0)) continue;//Нельзя ввести пустоту
  168.         if (n==8 && c)//Backspace
  169.         {
  170.             printf("\b \b");
  171.             c/=10;
  172.         }
  173.         if (n==13) break;//Enter
  174.         if ((n>=48) && (n<=58))
  175.         {
  176.             putch(n);
  177.             c=(c*10);
  178.             c+=(n-48);
  179.         }
  180.     }
  181.     return c;
  182. }
  183.  
  184. void View_text_func()
  185. {
  186.     SetConsoleCursorPosition (hStdout, output_coord);
  187.     SetConsoleTextAttribute (hStdout, 0x7);
  188.     for_each(Text.begin(),Text.end(),Print_CStr);
  189. }
  190.  
  191. void Print_CStr(CString str)
  192. {
  193.     int n=str.GetLength();
  194.     for (int i=0;i<=n;i++)
  195.     {
  196.         cout<<((char)str[i]);
  197.     }
  198.     cout<<endl;
  199. }
  200.  
  201. CString Input_CStr()
  202. {
  203.     char buf[10000];
  204.     char* ptr=buf;
  205.     SetConsoleTextAttribute (hStdout, 0xf);
  206.     cout<<"Enter text:"<<endl;
  207.     SetConsoleTextAttribute (hStdout, 0x7);
  208.     CString tmp;
  209.  
  210.     cin>>buf;
  211.     while(*ptr)
  212.     {
  213.         tmp.AppendChar(*ptr);
  214.         ptr++;
  215.     }
  216.     return tmp;
  217. }
  218.  
  219. void Insert_func()
  220. {
  221.     CString str=Input_CStr();
  222.     Text.insert(Text.begin(),str);
  223. }
  224.  
  225. void Copy_func()
  226. {
  227.     int n;
  228.     do
  229.     {
  230.         system("cls");
  231.         Select_str();
  232.         SetConsoleTextAttribute (hStdout, 0xf);
  233.         cout<<"Select string:"<<endl;
  234.         SetConsoleTextAttribute (hStdout, 0x7);
  235.     }
  236.     while((n=Input_int())>(Text.size()));
  237.     CString str=Text[n-1];
  238.     Text.insert(Text.begin()+n-1,str);
  239.     //system("cls");
  240.  
  241. }
  242.  
  243. void Search_func()
  244. {
  245.     vector <int> results;
  246.     int str_number=0,key_len,i=0;
  247.     do
  248.     {
  249.         system("cls");
  250.         Select_str();
  251.         SetConsoleTextAttribute (hStdout, 0xf);
  252.         cout<<"Select string"<<endl;
  253.         SetConsoleTextAttribute (hStdout, 0x7);
  254.     }while((str_number=Input_int())>(Text.size()));
  255.  
  256.     cout<<endl;
  257.  
  258.     CString key=Input_CStr();
  259.     key_len=key.GetLength();
  260.     results.push_back(Text[str_number-1].Find(key));
  261.  
  262.     while(Text[str_number-1].Find(key,results.back()+1)>0)
  263.     {
  264.         results.push_back(Text[str_number-1].Find(key,results.back()+1));
  265.     }
  266.    
  267.     if(results[0]==-1)
  268.     {
  269.         system("cls");
  270.         cout<<"No results!"<<endl;
  271.         getch();
  272.         return;
  273.     }
  274.     system("cls");
  275.     for_each(Text.begin(),Text.begin()+str_number-1,Print_CStr);
  276.     cout<<endl;
  277.     Output_result(results,str_number,key_len);
  278.     cout<<endl;
  279.     for_each(Text.begin()+str_number,Text.end(),Print_CStr);
  280.     getch();   
  281. }
  282.  
  283. void Select_str()
  284. {
  285.     SetConsoleTextAttribute (hStdout, 0x7);
  286.     for(int i=0;i<Text.size();i++)
  287.     {
  288.         cout<<i+1<<". ";
  289.         Print_CStr(Text[i]);
  290.     }
  291. }
  292.  
  293. void Output_result(vector <int> results,int str_number,int key_len)
  294. {
  295.     int str_len=Text[str_number-1].GetLength(),k=0;
  296.     for (int i=0;i<str_len;i++)
  297.     {
  298.         if(k<results.size())
  299.         {
  300.             if(i==(results[k]))
  301.             {
  302.                 SetConsoleTextAttribute (hStdout, 224);
  303.                 for(int j=0;j<key_len;j++)
  304.                 {
  305.                     putch(Text[str_number-1].GetAt(i+j));
  306.                 }
  307.                 i+=key_len-1;
  308.                 SetConsoleTextAttribute (hStdout, 0x7);
  309.                 k++;
  310.             }
  311.             else
  312.             putch(Text[str_number-1].GetAt(i));
  313.         }
  314.         else
  315.         putch(Text[str_number-1].GetAt(i));
  316.     }
  317. }
Add Comment
Please, Sign In to add comment