Advertisement
Guest User

synt_break

a guest
Dec 8th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.92 KB | None | 0 0
  1. #include <stdio.h> //*
  2. #include <conio.h>
  3. #include <fstream> //Чтение и сохранение данных
  4. #include <iostream> //Работа с данными
  5. #include <string> //работа со строками //back
  6. using namespace std;
  7.  
  8. int main(void)
  9. {
  10.  
  11.     string str; //создание переменной типа string
  12.  
  13.     ifstream file("dank.txt"); //создание объекта класса
  14.  
  15.     int num_break = 0, num_while = 0, num_switch = 0, num_for = 0, num_open = 0, num_close = 0;
  16.     int str_break = 0, str_switch = 0, str_for = 0, num_str = 0;
  17.     int fl_break[50], str_open = 0, str_close = 0, str_while = 0;
  18.     int pos_open[50], pos_close[50], pos_break[50], pos_while[50], pos_for[50], pos_switch[50], kol_while = 0, kol_for = 0, kol_switch = 0;
  19.  
  20.     while (getline(file, str)) //cout << str << endl; //getline - чтение строки из текста
  21.     {
  22.         num_str++;
  23.         if (str.find('"') > str.length())
  24.             if (str.rfind('"') > str.length())
  25.             {
  26.                 int o = 0, c = 0, fl_1 = 0, fl_2 = 0;
  27.  
  28.                 if (str.find("while") < str.length())
  29.                 {
  30.                     if (str.find("(") < str.length())
  31.                     {
  32.                         o = str.find("(");
  33.  
  34.                         if (str.find(")") < str.length())
  35.                         {
  36.                             c = str.find(")");
  37.  
  38.                             if (o < c)
  39.                             {
  40.  
  41.                                 pos_while[num_str] = str.find("while");
  42.  
  43.                                 str_while = num_str;
  44.  
  45.                                 kol_while++;
  46.                             }
  47.                             else
  48.                                 cout << "error[" << num_str << "]: nekorrektnoe napisanie while " << endl;
  49.                         }
  50.                         else
  51.                             cout << "error[" << num_str << "]: nekorrektnoe napisanie while " << endl;
  52.                     }
  53.                     else
  54.                         cout << "error[" << num_str << "]: nekorrektnoe napisanie while " << endl;
  55.  
  56.                 }
  57.                 else
  58.                     pos_while[num_str] = -1;
  59.  
  60.                 if (str.find("for") < str.length())
  61.                 {
  62.                     if (str.find("(") < str.length())
  63.                     {
  64.                         o = str.find("(");
  65.  
  66.                         if (str.find(")") < str.length())
  67.                         {
  68.                             c = str.find(")");
  69.  
  70.                             if (o < c)
  71.                             {
  72.                                 if (str.find(";") < str.length())
  73.                                 {
  74.                                     fl_1 = str.find(";");
  75.  
  76.                                     if (str.find_first_of(";", str.find(";") + 1) < str.length())
  77.                                     {
  78.                                         fl_2 = str.find_first_of(";", str.find(";") + 1);
  79.  
  80.                                         if (o < fl_1 && fl_1 < fl_2 && fl_2 < c)
  81.                                         {
  82.                                             pos_for[num_str] = str.find("for");
  83.                                             str_for = num_str;
  84.                                             kol_for++;
  85.                                         }
  86.                                         else
  87.  
  88.                                             cout << "error[" << num_str << "]: nekorrektnoe napisanie for  " << endl;
  89.  
  90.  
  91.                                     }
  92.                                 }
  93.                             }
  94.                         }
  95.                         else
  96.                             cout << "error[" << num_str << "]: nekorrektnoe napisanie for  " << endl;
  97.                     }
  98.                     else
  99.                         cout << "error[" << num_str << "]: nekorrektnoe napisanie for  " << endl;
  100.                 }
  101.                 else
  102.                     pos_for[num_str] = -1;
  103.  
  104.                 if (str.find("switch") < str.length())
  105.                 {
  106.                     if (str.find("(") < str.length())
  107.                     {
  108.                         o = str.find("(");
  109.  
  110.                         if (str.find(")") < str.length())
  111.                         {
  112.                             c = str.find(")");
  113.  
  114.                             if (o < c)
  115.                             {
  116.                                 pos_switch[num_str] = str.find("switch");
  117.                                 str_switch = num_str;
  118.                                 kol_switch++;
  119.                             }
  120.                             else
  121.                                 cout << "error[" << num_str << "]: nekorrektnoe napisanie switch " << endl;
  122.                         }
  123.                         else
  124.                             cout << "error[" << num_str << "]: nekorrektnoe napisanie switch " << endl;
  125.                     }
  126.                     else
  127.                         cout << "error[" << num_str << "]: nekorrektnoe napisanie switch " << endl;
  128.  
  129.                 }
  130.                 else
  131.                     pos_switch[num_str] = -1;
  132.  
  133.                 if (str.find("break") < str.length())
  134.                 {
  135.                     fl_1 = str.find("break");
  136.  
  137.                     if (str.find_first_of(";", str.find("break") + 1) < str.length())
  138.                     {
  139.                         fl_2 = str.find_first_of(";", str.find("break") + 1);
  140.  
  141.                         if (fl_1 > fl_2)
  142.                             cout << "error[" << num_str << "]: nekorrektnoe napisanie break " << endl;
  143.  
  144.  
  145.  
  146.                     }
  147.                     else
  148.                         cout << "error[" << num_str << "]: nekorrektnoe napisanie break " << endl;
  149.  
  150.                 }
  151.  
  152.                 if ((kol_while > 0 || kol_for > 0 || kol_switch > 0) && (str.find("{") < str.length() || str.find("}") < str.length()) && (pos_while[str_while] <= (str.find("{") && pos_while[str_while] <= (str.find("}")))) && (pos_for[str_for] <= (str.find("{") && pos_for[str_for] <= (str.find("}")))) && (pos_switch[str_switch] <= (str.find("{") && pos_switch[str_switch] <= (str.find("}")))))
  153.                 {
  154.                     do
  155.                     {
  156.                         if (str.find("{") < str.length())
  157.                         {
  158.                             num_open++;
  159.                             pos_open[num_str] = str.find("{");
  160.  
  161.                             if (str.find("}") > str.length())
  162.                                 break;
  163.  
  164.                         }
  165.                         else
  166.                         {
  167.                             pos_open[num_str] = -1;
  168.                         }
  169.  
  170.                         if (str.find("}") < str.length())
  171.                         {
  172.                             num_close++;
  173.                             pos_close[num_str] = str.find("}");
  174.                        
  175.                                 if (kol_while > 0 && str_while > str_for > str_switch)
  176.                                     kol_while--;
  177.                                 if (kol_for > 0 && str_for > str_while > str_switch)
  178.                                     kol_for--;
  179.                                 if (kol_switch > 0 && str_switch > str_for > str_while)
  180.                                     kol_switch--;
  181.                            
  182.                        
  183.                         }
  184.                         else
  185.                         {
  186.                             pos_close[num_str] = -1;
  187.                         }
  188.  
  189.                         num_str++;
  190.  
  191.                     } while (getline(file, str));
  192.  
  193.  
  194.                 }
  195.  
  196.             }
  197.  
  198.     }
  199.     file.close();
  200.  
  201.     ifstream br("dank.txt");
  202.  
  203.  
  204.     num_str = 0;
  205.  
  206.  
  207.     while (getline(br, str))
  208.     {
  209.         num_str++;
  210.         if (str.find('"') > str.length())
  211.             if (str.rfind('"') > str.length())
  212.                 if (str.find("break") < str.length())
  213.                 {
  214.  
  215.                     do
  216.                     {
  217.                         if (str.rfind(';') < str.length())
  218.                         {
  219.                             pos_break[num_str] = str.find("break");
  220.                             break;
  221.                         }
  222.                         else
  223.                             pos_break[num_str] = -1;
  224.  
  225.                         num_str++;
  226.                     } while (getline(file, str));
  227.                 }
  228.     }
  229.     br.close();
  230.  
  231.     int s = num_str, fl_3 = 0, fl_4=0;
  232.     int kol_open = 0, kol_close = 0;
  233.  
  234.     for (num_str = 1; num_str <= s; num_str++)
  235.     {
  236.  
  237.         if (pos_break[num_str] > -1)
  238.         {
  239.             str_break = num_str;
  240.         }
  241.  
  242.         if (pos_while[num_str] > -1)
  243.         {
  244.             str_while = num_str;
  245.         }
  246.  
  247.         if (pos_for[num_str] > -1)
  248.         {
  249.             str_for = num_str;
  250.         }
  251.  
  252.         if (pos_switch[num_str] > -1)
  253.         {
  254.             str_switch = num_str;
  255.         }
  256.  
  257.  
  258.         if (pos_open[num_str] > -1)
  259.         {
  260.             kol_open++;
  261.  
  262.             if (str_open == 0)
  263.                 str_open = num_str;
  264.         }
  265.         if (pos_close[num_str] > -1)
  266.         {
  267.             kol_close++;
  268.             str_close = num_str;
  269.         }
  270.  
  271.     }
  272.  
  273.     cout << "kol_open " << kol_open << endl;
  274.     cout << "kol_close " << kol_close << endl;
  275.  
  276.     if (kol_open != kol_close)
  277.     {
  278.         cout << "error [-]:  ne hvataet figurnih skobok dla raboti break" << endl;
  279.     }
  280.  
  281.  
  282.  
  283.     for (num_str = 1; num_str <= s; num_str++)
  284.     {
  285.         if (pos_break[num_str] > -1)
  286.         {
  287.             if (str_open != 0 && str_close != 0)
  288.             {
  289.                 fl_4 = 1;
  290.             }
  291.             if (str_while == 0 && str_for == 0 && str_switch == 0)
  292.             {
  293.                 fl_4 = 1;
  294.             }
  295.             else
  296.             {
  297.                 if (num_str < str_while)
  298.                     fl_4 = 1;
  299.                 if (num_str < str_for)
  300.                     fl_4 = 1;
  301.                     if (num_str < str_switch)
  302.                         fl_4 = 1;
  303.             }
  304.  
  305.             if ((num_str < str_open || num_str > str_close) && fl_4 == 1)
  306.             {
  307.  
  308.                 cout << "error [" << num_str << "]:  break vne cikla" << endl;
  309.             }
  310.            
  311.                
  312.  
  313.  
  314.         }
  315.  
  316.  
  317.     }
  318.  
  319.     _getch();
  320.     return 0;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement