Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 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. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. using namespace std;
  16.  
  17. enum States {
  18. State_Start,
  19. State_Variable,
  20. State_Assignment,
  21. State_Complete,
  22. State_Skip
  23. };
  24.  
  25.  
  26. void getStrFromFile (char* str, char* way) {
  27. ifstream fin(way);
  28. if (!fin.is_open()) {
  29. cout << "Файл не найден!\n";
  30. exit(-1);
  31. }
  32. string line;
  33. char line_char[300] = "";
  34. int i = 0;
  35. while (getline(fin, line)) {
  36. while (line[i] != '\0') {
  37. line_char[i] = line[i];
  38. i++;
  39. }
  40. strcat(str, " ");
  41. strcat(str, line_char);
  42. i = 0;
  43. while (line_char[i] != '\0') {
  44. line_char[i] = '\0';
  45. i++;
  46. }
  47. i = 0;
  48. }
  49. fin.close();
  50. }
  51.  
  52.  
  53. bool checkWord(char* var, char* key_words) {
  54. bool correct = 1;
  55.  
  56. return correct;
  57. }
  58.  
  59.  
  60. bool verificationKeyword(char* operation) {
  61. bool correct = 1;
  62. char var1[250] = "";
  63. char var2[250] = "";
  64. char key_words[500] = "";
  65. int i = 0;
  66. bool found_equal = 0;
  67. getStrFromFile(key_words, "/Users/dmitrijrabovskij/Desktop/C++/labs/5/5/1keywords.txt"); // 1
  68. //cout << 1 << endl << key_words << endl;
  69.  
  70. while (operation[i] != ';') {
  71. if (operation[i] == '=') {
  72. found_equal = 1;
  73. } else {
  74. char symb[2];
  75. symb[0] = operation[i];
  76. if (operation[i] != ' ') {
  77. if (!found_equal) {
  78. strcat(var1, symb);
  79. } else {
  80. strcat(var2, symb);
  81. }
  82. }
  83. }
  84. i++;
  85. }
  86. //cout << var1 << ' ' << var2 << endl;
  87.  
  88. correct = (checkWord(var1, key_words) && checkWord(var2, key_words));
  89. return correct;
  90. }
  91.  
  92.  
  93. void assignOperatorSearch(string text) { //char* text) {
  94. States state = State_Start;
  95. char operation[100] = "";
  96.  
  97. bool count = 0;
  98. int current = int(text[0]);
  99. char symb[2];
  100. for (int i = 0; current != '\0'; i++) {
  101. current = int(text[i]);
  102. symb[0] = text[i];
  103. strcat(operation, symb);
  104.  
  105. switch (state) {
  106.  
  107. case State_Start:
  108. if (current < 57 && current > 48) {
  109. state = State_Skip;
  110. count = 0;
  111. } else if ((current <= 122 && current >= 97) || (current <= 90 && current >= 65)
  112. || (current == 95)) {
  113. state = State_Variable;
  114. } else {
  115. if (current != 32) {
  116. count = 0;
  117. state = State_Skip;
  118. }
  119. }
  120. break;
  121.  
  122.  
  123. case State_Variable:
  124. if ((current == 32) || (cout && (current == 59)) || (!count && current == 61) ) {
  125. if (count) {
  126. //cout << "\n +++ " << text[i] << " " << i<< "\n";
  127. verificationKeyword(operation);
  128. cout << operation << endl;
  129. count = 0;
  130. state = State_Start;
  131. } else {
  132. if (current == 61) {
  133. state = State_Start;
  134. count = 1;
  135. } else {
  136. state = State_Assignment;
  137. }
  138. }
  139. } else if ((current > 122 || current < 97) && (current > 90 || current < 65)
  140. && (current > 57 || current < 48) && (current != 95)) { // a-z 97-122 A-Z 65-90 0-9 48-57 _ 95
  141. state = State_Skip;
  142. }
  143. break;
  144.  
  145.  
  146. case State_Assignment:
  147. if (current == 32) {
  148.  
  149. } else if (current == 61) { // =
  150. state = State_Start;
  151. count = 1;
  152. } else {
  153. i = i - 2;
  154. state = State_Skip;
  155. }
  156. break;
  157.  
  158.  
  159. case State_Complete:
  160. if (current == 32) {
  161. } else if (current == 59) { // ;
  162. //cout << "\n +++ \n";
  163. verificationKeyword(operation);
  164. cout << operation << endl;
  165. } else {
  166. state = State_Skip;
  167. }
  168. break;
  169.  
  170.  
  171. case State_Skip:
  172. operation[0] = '\0'; // !!! !!! !!! !!! !!! !!!
  173. if (current == 32 || current == 59) {
  174. state = State_Start;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180.  
  181.  
  182. int main() {
  183.  
  184. char str_cin[9999] = "";
  185. getStrFromFile(str_cin, "/Users/dmitrijrabovskij/Desktop/C++/labs/5/5/cin.txt");
  186. //cout << str_cin;
  187.  
  188. assignOperatorSearch(str_cin);
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement