Advertisement
SkeptaProgrammer

Untitled

Jun 24th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. // blyadishev4444.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //вывести присвоение целочисленной хуеты
  3. #include "pch.h"
  4. #include <fstream>
  5. #include <iostream>
  6. #include <string>
  7. #include <sstream>
  8. #include <cctype>
  9. using namespace std;
  10.  
  11. bool isVariable(string str)
  12. {
  13.     ifstream resFile;
  14.     resFile.open("C:\\onlyformydoggers\\more than words.txt");
  15.  
  16.     string tempStr = "";
  17.     bool checkConst = true, isVar = true;
  18.     if (str[0] >= 48 && str[0] <= 57)
  19.     {
  20.         for (int i = 0; i < str.length() && checkConst; i++)
  21.             if (!(isdigit(str[i]))) checkConst = false;
  22.         if (checkConst) return true;
  23.         else return false;
  24.     }
  25.  
  26.     while (!resFile.eof() && isVar)
  27.     {
  28.         resFile >> tempStr;
  29.         if (str == tempStr) isVar = false;
  30.     }
  31.     for (int i = 0; i < str.length() && isVar; i++)
  32.         if (str[i] >= 58 && str[i] <= 64 || str[i] >= 0 && str[i] <= 47 || str[i] >= 91 && str[i] < 95 || str[i] == 96 || str[i] >= 123) isVar = false;
  33.  
  34.     resFile.close();
  35.     return isVar;
  36. }
  37.  
  38. bool isSymbols(string str)
  39. {
  40.     return str.find("=") != -1;
  41. }
  42. string getString(string &s)
  43. {
  44.     ifstream workFile;
  45.     string result = "", str = "";
  46.     getline(cin, s);
  47.     workFile.open(s);
  48.     while (!workFile.eof())
  49.     {
  50.         getline(workFile, str);
  51.         result += str + " ";
  52.         str = "";
  53.     }
  54.     workFile.close();
  55.     return result+=";";
  56. }
  57.  
  58. enum states { START, ADDEND,NEXT_ADDEND,CLEAR, OPERATION_MARK, SKIP};
  59.  
  60.  
  61. int main()
  62.  {
  63.     //ifstream workFile;
  64.     string str = "", resultString = "", word = "",path="";
  65.     str = getString(path);
  66.     //workFile.open(path);
  67.     //cout << workFile.is_open();
  68.     states state = START;
  69.     while (!str.empty())
  70.     {
  71.         int k = 1;
  72.         bool loop = true;
  73.         //getline(workFile, str);
  74.         while (loop)
  75.         {
  76.             switch (state)
  77.             {
  78.             case START:
  79.             {
  80.                 if (!str.empty() && isSymbols(str) &&k>0)
  81.                 {
  82.                     k = str.find("=");
  83.                     if (k>0)
  84.                     k--;
  85.                     else state = SKIP;
  86.                     while (isspace(str[k]) && k > 0) k--;
  87.                     while (k >= 0 && isalnum(str[k]))
  88.                     {
  89.                         word += str[k];
  90.                         k--;
  91.                     }
  92.                     if (word.empty()) state = SKIP;
  93.                     else
  94.                     {
  95.                         reverse(word.begin(), word.end());
  96.                         if (isVariable(word))
  97.                         {
  98.                             resultString += word + " = ";
  99.                             word = "";
  100.                             state = ADDEND;
  101.                         }
  102.                         else
  103.                         {
  104.                             str.erase(0, str.find("=") + 1);
  105.                             state = START;
  106.                             resultString = "";
  107.                         }
  108.                         break;
  109.                     }
  110.                 }
  111.                 else state = SKIP;
  112.                
  113.                 break;
  114.             }
  115.             case ADDEND:
  116.             {
  117.                 k = str.find("=");
  118.                 k++;
  119.                 while (isspace(str[k]) && k < str.length()) k++;
  120.                 while (k < str.length() && isalnum(str[k]))
  121.                 {
  122.                     word += str[k];
  123.                     k++;
  124.                 }
  125.                 //reverse(word.begin(), word.end());
  126.                 if (isVariable(word)&&!word.empty())
  127.                 {
  128.                     state = OPERATION_MARK;
  129.                     resultString += word;
  130.                     //word = "";
  131.  
  132.                 }
  133.                 else
  134.                 {
  135.                     word = "";
  136.                     str.erase(0, k);
  137.                     state = START;
  138.                     resultString = "";
  139.  
  140.                 }
  141.             }
  142.             break;
  143.             case CLEAR:
  144.             {
  145.                 word = "";
  146.                 resultString = "";
  147.                 state = START;
  148.             }
  149.             break;
  150.             case OPERATION_MARK:
  151.             {
  152.                 while (isspace(str[k]))
  153.                     k++;
  154.  
  155.                 if ((str[k] == '+' || str[k] == '-'))
  156.                 {
  157.                     state = NEXT_ADDEND;
  158.                     if (str[k] == '+')
  159.                         resultString += " + ";
  160.                     else resultString += " - ";
  161.                 }
  162.                 else
  163.                 {
  164.                     str.erase(0, k);    //str.find(resultString.erase(resultString.find(" ")+1, resultString.length())));
  165.                     state = CLEAR;
  166.                    
  167.                 }
  168.             }
  169.             break;
  170.             case NEXT_ADDEND:
  171.             {
  172.                 word = "";
  173.                 k++;
  174.                 while (isspace(str[k]) && k < str.length()) k++;
  175.                 while (isalnum(str[k]) && k < str.length())
  176.                 {
  177.                     word += str[k];
  178.                     k++;
  179.                 }
  180.                 if (isVariable(word))
  181.                 {
  182.                     resultString += word;
  183.                     cout << resultString << "\n";
  184.                     state = CLEAR;
  185.                     str.erase(0, k);
  186.  
  187.                 }
  188.                 else state = SKIP;
  189.             }
  190.             break;
  191.             case SKIP:
  192.             {
  193.                 word = "";
  194.                 resultString = "";
  195.                 loop = false;
  196.                 if (k<str.length()&&isSymbols(str))
  197.                 str.erase(0, str.find("=")+1);// str.find(";") + 1);
  198.                 else str = "";
  199.                 state = START;
  200.             }
  201.             break;
  202.  
  203.             }
  204.         }
  205.     }
  206.     //workFile.close();
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement