Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <cctype>
  6. using namespace std;
  7. // C:\onlyformydoggers\h.txt
  8. bool isConst(string str)
  9. {
  10. bool checkConst = true;
  11. if (isdigit(str[0]))
  12. {
  13. for (int i = 0; i < str.length() && checkConst; i++)
  14. if (!(isdigit(str[i]))) checkConst = false;
  15. if (checkConst) return true;
  16. else return false;
  17. }
  18. else checkConst = false;
  19. return checkConst;
  20. }
  21.  
  22. bool isVariable(string str)
  23. {
  24.  
  25. if (isConst(str)) return false;
  26. else return true;
  27.  
  28. }
  29.  
  30.  
  31.  
  32. bool isSymbols(string str)
  33. {
  34. return str.find("=") != -1;
  35. }
  36.  
  37. string getString(string &s)
  38. {
  39. ifstream workFile;
  40. string result = "", str = "";
  41.  
  42. workFile.open(s);
  43. while (!workFile.eof())
  44. {
  45. getline(workFile, str);
  46. result += str + " ";
  47. str = "";
  48. }
  49. workFile.close();
  50. return result += ";";
  51. }
  52.  
  53. enum states { START, CONST, CLEAR, SKIP };
  54.  
  55.  
  56. int main()
  57. {
  58. string str = "", resultString = "", word = "", path = "";
  59. cin >> path;
  60. str = getString(path);
  61.  
  62. states state = START;
  63. while (!str.empty())
  64. {
  65. int k = 1;
  66. bool loop = true;
  67.  
  68. while (loop)
  69. {
  70. switch (state)
  71. {
  72. case START:
  73. {
  74. if (!str.empty() && isSymbols(str) && k > 0)
  75. {
  76. k = str.find("=");
  77. if (k > 0)
  78. k--;
  79. else state = SKIP;
  80. while (isspace(str[k]) && k > 0) k--;
  81. while (k >= 0 && isalnum(str[k]))
  82. {
  83. word += str[k];
  84. k--;
  85. }
  86. if (word.empty()) state = SKIP;
  87. else
  88. {
  89. reverse(word.begin(), word.end());
  90. if (isVariable(word))
  91. {
  92. resultString += word + " = ";
  93. word = "";
  94. state = CONST;
  95. }
  96. else
  97. {
  98. str.erase(0, str.find("=") + 1);
  99. state = START;
  100. resultString = "";
  101. }
  102. break;
  103. }
  104. }
  105. else state = SKIP;
  106.  
  107. break;
  108. }
  109. case CONST:
  110. {
  111. k = str.find("=");
  112. k++;
  113. while (isspace(str[k]) && k < str.length()) k++;
  114. while (k < str.length() && isalnum(str[k]))
  115. {
  116. word += str[k];
  117. k++;
  118. }
  119. if (isConst(word) && !word.empty())
  120. {
  121. state = CLEAR;
  122. resultString += word;
  123. cout << resultString << "\n";
  124. str.erase(0, k);
  125.  
  126. }
  127. else state = SKIP;
  128. }
  129. break;
  130. case CLEAR:
  131. {
  132. word = "";
  133. resultString = "";
  134. state = START;
  135. }
  136. break;
  137. case SKIP:
  138. {
  139. word = "";
  140. resultString = "";
  141. loop = false;
  142. if (k < str.length() && isSymbols(str))
  143. str.erase(0, str.find("=") + 1);
  144. else str = "";
  145. state = START;
  146. }
  147. break;
  148.  
  149. }
  150. }
  151. }
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement