Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. bool varForExit = false;
  8.  
  9. typedef struct _Queue {
  10. int* stringElem;
  11. int n;
  12. } Queue;
  13.  
  14. typedef struct _Stak {
  15. char* stringElem;
  16. int n;
  17. } Stack;
  18.  
  19. void createQueue(Queue* q) {
  20. q->stringElem = (int*)calloc(1000, sizeof(int));
  21. }
  22.  
  23. void createStack(Stack* s) {
  24. s->stringElem = (char*)calloc(1000, sizeof(char));
  25. }
  26.  
  27. void pushQueue(Queue* q, int value) {
  28. q->stringElem[q->n++] = value;
  29. }
  30.  
  31. void popQueue(Queue* q) {
  32. if (q->n == 0) {
  33. varForExit = true;
  34. }
  35. q->n--;
  36. }
  37.  
  38. void pushStack(Stack* s, char value) {
  39. s->stringElem[s->n++] = value;
  40. }
  41.  
  42. void popStack(Stack* s) {
  43. if (s->n == 0) {
  44. varForExit = true;
  45. }
  46. s->n--;
  47. }
  48.  
  49. int calcOperation(char op, int first, int second) {
  50. switch (op) {
  51. case '+':
  52. return (first + second);
  53. break;
  54. case '-':
  55. return (first - second);
  56. break;
  57. case '*':
  58. return (first * second);
  59. break;
  60. case '/':
  61. if (second == 0) {
  62. printf("division by zero");
  63. exit(0);
  64. }
  65. return (first / second);
  66. break;
  67. default: return 0;
  68. }
  69. }
  70.  
  71. void checkString(char a) {
  72. if ((a < '(') || (a > '9') || (a == ',') || (a == '.')) {
  73. printf("syntax error");
  74. varForExit = true;
  75. return;
  76. }
  77. }
  78.  
  79. int priority(char element) {
  80. int x;
  81. switch (element) {
  82. case '-':
  83. case '+':
  84. x = 2;
  85. break;
  86. case '/':
  87. case '*':
  88. x = 4;
  89. break;
  90. case '(':
  91. case ')':
  92. x = 1;
  93. break;
  94. default: return 0;
  95. }
  96. return(x);
  97. }
  98.  
  99. void functionFotExit(Queue* queueForRPN, Queue* queueForAccount, Stack* stackForNumbers, char* string) {
  100. free(string);
  101. free(queueForRPN->stringElem);
  102. free(queueForAccount->stringElem);
  103. free(stackForNumbers->stringElem);
  104. free(queueForRPN);
  105. free(queueForAccount);
  106. free(stackForNumbers);
  107. }
  108.  
  109. int main() {
  110. char* string = calloc(1001, sizeof(char));
  111. if (scanf("%1000s", string) == 0) {
  112. printf("syntax error");
  113. free(string);
  114. return 0;
  115. }
  116.  
  117. if ((char)getchar() != '\n') {
  118. printf("syntax error");
  119. free(string);
  120. return 0;
  121. }
  122.  
  123. int lengthString = strlen(string);
  124. for (int i = 0; i < lengthString; i++) {
  125. checkString(string[i]);
  126. if (varForExit) {
  127. free(string);
  128. return 0;
  129. }
  130. }
  131.  
  132. Queue* queueForRPN = calloc(1, sizeof(Queue));
  133. Queue* queueForAccount = calloc(1, sizeof(Queue));
  134. Stack* stackForNumbers = calloc(1, sizeof(Stack));
  135. createQueue(queueForRPN);
  136. createQueue(queueForAccount);
  137. createStack(stackForNumbers);
  138.  
  139. int variableForBracket = 0;
  140. int variableForOperation = 0;
  141. for (int i = 0; i < lengthString; i++) {
  142. switch (string[i]) {
  143. case '+':
  144. case '-': {
  145. if (((i > 0) && ((string[i - 1] == '/') || (string[i - 1] == '*') || (string[i - 1] == '+') || (string[i - 1] == '-'))) || (queueForRPN->n == 0)) {
  146. printf("syntax error");
  147. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  148. return 0;
  149. }
  150. if ((stackForNumbers->n != 0) && (priority(stackForNumbers->stringElem[stackForNumbers->n - 1]) >= priority(string[i]))) {
  151. pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
  152. popStack(stackForNumbers);
  153. if (varForExit) {
  154. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  155. return 0;
  156. }
  157. }
  158. pushStack(stackForNumbers, string[i]);
  159. variableForOperation = 1;
  160. break;
  161. }
  162. case '*':
  163. case '/': {
  164. if (((i > 0) && ((string[i - 1] == '/') || (string[i - 1] == '*') || (string[i - 1] == '+') || (string[i - 1] == '-'))) || (queueForRPN->n == 0)) {
  165. printf("syntax error");
  166. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  167. return 0;
  168. }
  169. if (queueForRPN->n == 0) {
  170. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  171. return 0;
  172. }
  173. if ((stackForNumbers->n == 0) || (priority(stackForNumbers->stringElem[stackForNumbers->n - 1]) < priority(string[i]))) pushStack(stackForNumbers, string[i]); else {
  174. if (priority(stackForNumbers->stringElem[stackForNumbers->n - 1]) == priority(string[i])) {
  175. pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
  176. popStack(stackForNumbers);
  177. if (varForExit) {
  178. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  179. return 0;
  180. }
  181. pushStack(stackForNumbers, string[i]);
  182. }
  183. }
  184. variableForOperation = 1;
  185. break;
  186. }
  187. case '(':
  188. pushStack(stackForNumbers, string[i]);
  189. variableForBracket = 1;
  190. break;
  191. case ')':
  192. if (((i > 0) && ((string[i - 1] == '(') || (variableForBracket == 0))) || (i == 0)) {
  193. printf("syntax error");
  194. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  195. return 0;
  196. }
  197. while ((stackForNumbers->stringElem[stackForNumbers->n - 1]) != '(') {
  198. pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
  199. popStack(stackForNumbers);
  200. if (varForExit) {
  201. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  202. return 0;
  203. }
  204. }
  205. popStack(stackForNumbers);
  206. if (varForExit) {
  207. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  208. return 0;
  209. }
  210. break;
  211. default:
  212. if ((i > 0) && (string[i - 1] >= '0') && (string[i - 1] <= '9')) {
  213. queueForRPN->stringElem[queueForRPN->n - 1] = (queueForRPN->stringElem[queueForRPN->n - 1]) * 10 + ((int)string[i] - '0');
  214. }
  215. else pushQueue(queueForRPN, (int)string[i] - '0');
  216. }
  217. }
  218. if (((queueForRPN->n == 1) && (variableForOperation == 1)) || (queueForRPN->n == 0)) {
  219. printf("syntax error");
  220. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  221. return 0;
  222. }
  223.  
  224. if (variableForOperation == 0) {
  225. for (int i = 0; i < queueForRPN->n; i++) {
  226. printf("%d", queueForRPN->stringElem[i]);
  227. }
  228. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  229. return 0;
  230. }
  231.  
  232. while (stackForNumbers->n > 0) {
  233. pushQueue(queueForRPN, (int)stackForNumbers->stringElem[stackForNumbers->n - 1] - '0');
  234. popStack(stackForNumbers);
  235. if (varForExit) {
  236. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  237. return 0;
  238. }
  239. }
  240.  
  241. int variable;
  242. for (int i = 0; i < queueForRPN->n; i++) {
  243. if (queueForRPN->stringElem[i] < 0) {
  244. if (queueForAccount->n < 2) {
  245. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  246. return 0;
  247. }
  248. variable = calcOperation((char)queueForRPN->stringElem[i] + '0', queueForAccount->stringElem[queueForAccount->n - 2], queueForAccount->stringElem[queueForAccount->n - 1]);
  249. popQueue(queueForAccount);
  250. if (varForExit) {
  251. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  252. return 0;
  253. }
  254. popQueue(queueForAccount);
  255. if (varForExit) {
  256. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  257. return 0;
  258. }
  259. pushQueue(queueForAccount, variable);
  260. }
  261. else {
  262. pushQueue(queueForAccount, queueForRPN->stringElem[i]);
  263. }
  264. }
  265. printf("%d", queueForAccount->stringElem[queueForAccount->n - 1]);
  266. functionFotExit(queueForRPN, queueForAccount, stackForNumbers, string);
  267. return 0;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement