Advertisement
Guest User

Untitled

a guest
May 25th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. // Пусть в файле храняться строки, представляющие собой
  2. // исходный код программы на языке с++. Вывести синтаксически
  3. // правильные операторы присваивания одной переменной значение
  4. // другой. Список зарезервированных слов задать в отдельном файле.
  5. //
  6. // Created by Дмитрий Рябовский on 14/03/2019.
  7. // Copyright © 2019 Дмитрий Рябовский. All rights reserved.
  8. //
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14.  
  15. enum States {
  16. State_Start,
  17. State_Variable,
  18. State_Assignment,
  19. State_Complete,
  20. State_Skip
  21. };
  22.  
  23.  
  24. void getWay(char* way_cin_file) {
  25. cout << "\nВведите путь к файлу: (../filename.txt)" << endl; // /Users/dmitrijrabovskij/Desktop/C++/labs/4/4/cin.txt
  26. gets(way_cin_file);
  27. cout << endl;
  28. }
  29.  
  30.  
  31.  
  32. void getWordsFromOperation (string operation, string& word1, string& word2) {
  33. int i = 0;
  34. int k = 0;
  35. bool check = 0;
  36. while (operation[i] != '\0') {
  37. if (operation[i] == '=') {
  38. check = 1;
  39. k = 0;
  40. i++;
  41. }
  42. if (operation[i] != ' ' && operation[i] != ';' && int(operation[i]) != 10) {
  43. if (check) {
  44. word2.insert(k, 1, operation[i]);
  45. k++;
  46. } else {
  47. word1.insert(k, 1, operation[i]);
  48. k++;
  49. }
  50. }
  51. i++;
  52. }
  53. }
  54.  
  55.  
  56. bool wordsMatchCheck (string word1, string word2) {
  57. bool difference = true;
  58. int i = 0;
  59. bool check1 = 0;
  60. while (word1[i] != '\0' && !check1) {
  61. if (word1[i] != word2[i]) check1 = 1;
  62. i++;
  63. }
  64. difference = check1;
  65. return difference;
  66. }
  67.  
  68.  
  69. bool checkWordsForKeyword (string word1, string word2) {
  70. bool correct = true;
  71. string way_keywords_file = "/Users/dmitrijrabovskij/Desktop/C++/labs/4/4/keywords.txt";
  72. ifstream keywords_file(way_keywords_file);
  73. if (!keywords_file.is_open()) {
  74. cout << "Файл не найден! invalid way_keywords_file!!! " << way_keywords_file << "\n";
  75. exit(-1);
  76. }
  77. int i = 0;
  78. bool check1 = false;
  79. bool check2 = false;
  80. while (!keywords_file.eof()) {
  81. string buff;
  82. keywords_file >> buff;
  83. while (buff[i] != '\0') {
  84. if (word1[i] != buff[i]) {
  85. check1 = 1;
  86. }
  87. if (word2[i] != buff[i]) {
  88. check2 = 1;
  89. }
  90. i++;
  91. }
  92. if (check1 == 0 || check2 == 0) {
  93. correct = 0;
  94. }
  95. check1 = 0;
  96. check2 = 0;
  97. i = 0;
  98. }
  99. keywords_file.close();
  100. return correct;
  101. }
  102.  
  103.  
  104. bool checkOperationForKeyword (string operation) {
  105. bool correct = true;
  106. string word1 = "";
  107. string word2 = "";
  108. getWordsFromOperation(operation, word1, word2);
  109. correct = wordsMatchCheck(word1, word2);
  110. if (!correct) {
  111. return correct;
  112. }
  113. correct = checkWordsForKeyword(word1, word2);
  114. return correct;
  115. }
  116.  
  117.  
  118. void assignOperatorSearch_found(bool& check, int& i, States& state, string& operation) {
  119. if (checkOperationForKeyword(operation)) {
  120. cout << operation << endl << endl;
  121. }
  122. check = 0;
  123. state = State_Start;
  124. operation = "";
  125. i = 0;
  126. }
  127.  
  128.  
  129. void assignOperatorSearch() {
  130. char way_cin_file[256] = "";
  131. getWay(way_cin_file);
  132. ifstream cin_file(way_cin_file);
  133. if (!cin_file.is_open()) {
  134. cout << "Файл не найден!\n";
  135. assignOperatorSearch();
  136. } else {
  137. int symb;
  138. bool check = 0;
  139. string operation = "";
  140. int i = 0;
  141. States state = State_Start;
  142. while (!cin_file.eof())
  143. {
  144. symb = cin_file.get();
  145. operation.insert(i, 1, char(symb));
  146. i++;
  147.  
  148. switch (state) {
  149.  
  150. case State_Start:
  151. if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95) {
  152. state = State_Variable;
  153. } else if (symb == 32 || symb == 10 || (symb == 59 && !check)) {
  154. state = State_Start;
  155. } else {
  156. state = State_Skip;
  157. }
  158. break;
  159.  
  160.  
  161. case State_Variable:
  162. if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95 || (symb >= 48 && symb <= 57)) {
  163. state = State_Variable;
  164. } else if (symb == 32 || symb == 10) {
  165. if (!check) {
  166. state = State_Assignment;
  167. check = 1;
  168. } else {
  169. state = State_Complete;
  170. }
  171. } else {
  172. if (check) {
  173. if (symb == 59) {
  174. assignOperatorSearch_found(check, i, state, operation); //end
  175. } else {
  176. state = State_Skip;
  177. }
  178. } else {
  179. if (symb == 61) {
  180. check = 1; //State_Assignment
  181. state = State_Start;
  182. } else {
  183. state = State_Skip;
  184. }
  185. }
  186. }
  187. break;
  188.  
  189.  
  190. case State_Assignment:
  191. if (symb == 61) {
  192. state = State_Start;
  193. } else if (symb == 32 || symb == 10) {
  194. state = State_Assignment;
  195. } else {
  196. if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95) {
  197. check = 0; //state_start
  198. state = State_Variable;
  199. operation = "";
  200. operation.insert(0, 1, char(symb));
  201. i = 1;
  202. } else {
  203. state = State_Skip;
  204. }
  205. }
  206. break;
  207.  
  208.  
  209. case State_Complete:
  210. if (symb == 59) {
  211. assignOperatorSearch_found(check, i, state, operation); //end
  212. i = 0;
  213. } else if (symb == 32 || symb == 10) {
  214. state = State_Complete;
  215. } else {
  216. if ((symb >= 65 && symb <= 90) || (symb >= 97 && symb <= 122) || symb == 95) {
  217. check = 0; //state_start
  218. state = State_Variable;
  219. operation = "";
  220. operation.insert(0, 1, char(symb));
  221. i = 1;
  222. } else {
  223. state = State_Skip;
  224. }
  225. }
  226. break;
  227.  
  228.  
  229. case State_Skip:
  230. check = 0;
  231. if (symb == 32 || symb == 10 || symb == 59) {
  232. state = State_Start; //skip
  233. operation = "";
  234. i = 0;
  235. }
  236. break;
  237. }
  238. }
  239. cin_file.close();
  240. }
  241. }
  242.  
  243.  
  244.  
  245. int main() {
  246. cout << "Пусть в файле храняться строки, представляющие собой\nисходный код программы на языке с++. Вывести синтаксически\nправильные операторы присваивания одной переменной значение\nдругой. Список зарезервированных слов задать в отдельном файле.\n" << endl;
  247. assignOperatorSearch();
  248. return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement