vallec

Untitled

Mar 31st, 2025
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define MAX_WORD 256
  7. #define MAX_FILE_SIZE 10000
  8.  
  9. typedef enum {
  10. START, SIGN, DECIMAL, OCTAL, HEX_PREFIX, HEX, FLOAT, EXPONENT, EXPONENT_SIGN, EXPONENT_DIGITS, SUFFIX, UNSIGNED, INVALID
  11. } State;
  12.  
  13. int invalidType = 0;
  14. int startType = 1;
  15. int signType = 2;
  16. int integerType = 3;
  17. int octalType = 4;
  18. int hex_prefixType = 5;
  19. int hexType = 6;
  20. int floatType = 7;
  21. int exponentType = 8;
  22. int exponent_signType = 9;
  23. int exponent_digitsType = 10;
  24. int suffixType = 11;
  25. int unsignedType = 12;
  26. int longType = 13;
  27.  
  28. // Функция за класификация на числа чрез FSM
  29. int classify_number(const char *str, int *type, int *second_type) {
  30. int state = startType;
  31. int i = 0;
  32. bool is_unsigned = false, has_dot = false, has_exponent = false, is_real = false;
  33.  
  34. while (str[i] != '\0') {
  35. char c = str[i];
  36.  
  37. switch (state) {
  38. case 1:
  39. if (c == '+' || c == '-') {
  40. state = signType;
  41. } else if (c == '0') {
  42. state = octalType;
  43. } else if (isdigit(c)) {
  44. state = integerType;
  45. } else if (c == '.') {
  46. state = floatType;
  47. has_dot = true;
  48. } else {
  49. return invalidType;
  50. }
  51. break;
  52.  
  53. case 2:
  54. if (isdigit(c)) {
  55. state = integerType;
  56. } else if (c == '.') {
  57. state = floatType;
  58. has_dot = true;
  59. } else {
  60. return invalidType;
  61. }
  62. break;
  63.  
  64. case 3:
  65. if (isdigit(c)) {
  66. // Оставаме в десетично състояние
  67. } else if (c == '.') {
  68. state = floatType;
  69. has_dot = true;
  70. } else if (tolower(c) == 'e') {
  71. state = exponentType;
  72. has_exponent = true;
  73. } else if (tolower(c) == 'u') {
  74. if(str[0] == '+' || isdigit(str[0])) {
  75. state = unsignedType;
  76. }
  77. } else if (tolower(c) == 'l') {
  78. state = longType;
  79. } else if (tolower(c) == 'c') {
  80. return invalidType;
  81. } else {
  82. return invalidType;
  83. }
  84. break;
  85.  
  86. case 4:
  87. if (c >= '0' && c <= '7') {
  88. // Оставаме в осмично състояние
  89. } else if (tolower(c) == 'x') {
  90. state = hex_prefixType;
  91. } else if (tolower(c) == 'u' || tolower(c) == 'l') {
  92. if(tolower(c) == 'u') {
  93. *second_type = unsignedType;
  94. }
  95. } else {
  96. return invalidType;
  97. }
  98. break;
  99.  
  100. case 5:
  101. if (isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f')) {
  102. state = hexType;
  103. } else {
  104. return invalidType;
  105. }
  106. break;
  107.  
  108. case 6:
  109. if (isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f')) {
  110. // Оставаме в шестнадесетично състояние
  111. } else if (tolower(c) == 'u' || tolower(c) == 'l') {
  112. state = unsignedType;
  113. } else {
  114. return invalidType;
  115. }
  116. break;
  117.  
  118. case 7:
  119. if (isdigit(c)) {
  120. // Оставаме в състоянието FLOAT
  121. } else if (tolower(c) == 'e') {
  122. state = exponentType;
  123. has_exponent = true;
  124. } else {
  125. return invalidType;
  126. }
  127. break;
  128.  
  129. case 8:
  130. if (c == '+' || c == '-') {
  131. state = exponent_signType;
  132. } else if (isdigit(c)) {
  133. state = exponent_digitsType;
  134. } else {
  135. return invalidType;
  136. }
  137. break;
  138.  
  139. case 9:
  140. if (isdigit(c)) {
  141. state = exponent_digitsType;
  142. } else {
  143. return invalidType;
  144. }
  145. break;
  146.  
  147. case 10:
  148. if (isdigit(c)) {
  149. // Оставаме в експоненциално състояние
  150. } else {
  151. return invalidType;
  152. }
  153. break;
  154.  
  155. case 11:
  156. if (tolower(c) == 'l') {
  157. // UL, ULL вариант
  158. } else {
  159. return invalidType;
  160. }
  161. break;
  162. case 12:
  163. if(isdigit(c)) {
  164. return invalidType;
  165. } else {
  166. if(tolower(c) == 'l') {
  167. *second_type = longType;
  168. } else if (tolower(c) == 'c' ) {
  169. return invalidType;
  170. }
  171. }
  172. break;
  173. case 13:
  174.  
  175. if(isdigit(c) || (tolower(c) != 'u' && tolower(c) != 'l')) {
  176. return invalidType;
  177. } else {
  178. if(tolower(c) == 'u' && (isdigit(str[0]) || str[0] == '+')) {
  179. *second_type = unsignedType;
  180. } else {
  181. return invalidType;
  182. }
  183. }
  184. break;
  185. default:
  186. return invalidType;
  187. }
  188. i++;
  189. }
  190.  
  191. // Определяне на типа на числото
  192. if (has_dot || has_exponent) {
  193. *type = 6; // Float число
  194. if (str[strlen(str) - 1] == '0' && has_dot) {
  195. *type = 7; // Реално число
  196. }
  197. } else if (state == integerType) {
  198. *type = 1; // Десетично цяло число
  199. } else if (state == octalType) {
  200. *type = 2; // Осмично число
  201. } else if (state == hexType) {
  202. *type = 3; // Шестнадесетично число
  203. } else if (state == longType) {
  204. *type = 8; // Беззнаково число
  205. } else if (state == unsignedType) {
  206. *type = 4; // Беззнаково число
  207. } else {
  208. return invalidType;
  209. }
  210.  
  211. return state;
  212. }
  213.  
  214. // Главна функция (четене от файл)
  215. int main(int argc, char *argv[]) {
  216. if (argc != 2) {
  217. printf("Usage: %s <filename>\n", argv[0]);
  218. return 1;
  219. }
  220.  
  221. FILE *test_FSM_2025_ = fopen(argv[1], "r");
  222. if (!test_FSM_2025_) {
  223. perror("Error opening file");
  224. return 1;
  225. }
  226.  
  227. char line[MAX_FILE_SIZE];
  228. int total_count = 0, decimal_count = 0, octal_count = 0, hex_count = 0, float_count = 0;
  229. int unsigned_count = 0, integer_count = 0, real_count = 0, long_count = 0;
  230.  
  231. while (fgets(line, sizeof(line), test_FSM_2025_)) {
  232. char *token = strtok(line, " ,\n"); // Разделяме по интервал, запетая и нов ред
  233. while (token != NULL) {
  234. int type = 0;
  235. int second_type = 0;
  236. if(strcmp(token, "0") == 0) {
  237. total_count++;
  238. integer_count++;
  239. } else if (classify_number(token, &type, &second_type) != invalidType) {
  240. total_count++;
  241. if (type == 1) decimal_count++;
  242. else if (type == 2) {
  243. octal_count++;
  244. if(second_type == 12) {
  245. printf("%s ", token);
  246. unsigned_count++;
  247. }
  248. }
  249. else if (type == 3) {
  250. hex_count++;
  251. }
  252. else if (type == 4) {
  253. printf("%s ", token);
  254. unsigned_count++;
  255. if(second_type == 13) {
  256. long_count++;
  257. }
  258. }
  259. else if (type == 6) float_count++; // Float числа
  260. else if (type == 7) real_count++; // Реални числа
  261. else if (type == 8) {
  262. long_count++;
  263. if(second_type == unsignedType) {
  264. printf("%s ", token);
  265. unsigned_count++;
  266. }
  267. }
  268. else if (type != 6 && type != 7) {
  269. integer_count++;
  270. } // Цели числа
  271.  
  272. }
  273. token = strtok(NULL, " ,\n");
  274. }
  275. }
  276.  
  277. fclose(test_FSM_2025_);
  278.  
  279. printf("Total numbers: %d\n", total_count);
  280. printf("Integer constants: %d\n", integer_count);
  281. printf("Decimal constants: %d\n", decimal_count);
  282. printf("Octal constants: %d\n", octal_count);
  283. printf("Hexadecimal constants: %d\n", hex_count);
  284. printf("Unsigned integer constants: %d\n", unsigned_count);
  285. printf("Floating point constants: %d\n", float_count);
  286. printf("Real numbers: %d\n", real_count);
  287.  
  288. return 0;
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment