Advertisement
josiftepe

Untitled

Apr 10th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.48 KB | None | 0 0
  1. Blagoja Mladenov gr.5\
  2. package com.blagoja;
  3. import java.util.Scanner;
  4.  
  5. class Main {
  6. static boolean isLetter(String s) {
  7. char m = s.charAt(0);
  8. return m >= 'a' && m <= 'z';
  9. }
  10.  
  11. static boolean isLeftOperator(String s) {
  12. char m = s.charAt(0);
  13. if (m == '|' || m == '&' || m == '?' || m == '<' || m == '>' || m == '+' || m == '-' || m == '*' || m == '/' || m == '%')
  14. return true;
  15. return false;
  16. }
  17.  
  18. static boolean isRightOperator(String s) {
  19. char m = s.charAt(0);
  20. if (m == '!' || m == '~' || m == '^' || m == '=')
  21. return true;
  22. return false;
  23. }
  24.  
  25. static int priority(String s) {
  26. char m = s.charAt(0);
  27. if (m == '=')
  28. return 1;
  29. if (m == '|')
  30. return 2;
  31. if (m == '&')
  32. return 3;
  33. if (m == '?')
  34. return 4;
  35. if (m == '<' || m == '>')
  36. return 5;
  37. if (m == '+' || m == '-')
  38. return 6;
  39. if (m == '*' || m == '/' || m == '%')
  40. return 7;
  41. if (m == '^')
  42. return 8;
  43. if (m == '!' || m == '~')
  44. return 9;
  45. if (m >= 'a' && m <= 'z')
  46. return 10;
  47. return 0;
  48. }
  49.  
  50. static boolean isUnary(String s) {
  51. char m = s.charAt(0);
  52. if (m == '~' || m == '!')
  53. return true;
  54. return false;
  55. }
  56.  
  57. static boolean isBinary(String s) {
  58. char m = s.charAt(0);
  59. if (m == '=' || m == '|' || m == '&' || m == '?' || m == '<' || m == '>' || m == '+' || m == '-' || m == '*' || m == '/' || m == '%' || m == '^')
  60. return true;
  61. return false;
  62.  
  63. }
  64.  
  65. public static void main(String[] args) {
  66. Scanner scanner = new Scanner(System.in);
  67. int n = Integer.parseInt(scanner.nextLine());
  68. for (int i = 0; i < n; i++) {
  69.  
  70. String s = new String();
  71. s = "";
  72. String input = scanner.nextLine();
  73. boolean flag = false;
  74. for(int crt1 = 0; crt1 < input.length(); crt1++) {
  75. if(input.charAt(crt1) != ' ' && flag == false) {
  76. flag = true;
  77. s += input.charAt(crt1);
  78. continue;
  79. }
  80. if(flag) {
  81. s += input.charAt(crt1);
  82. }
  83.  
  84. }
  85. if(s.length() == 0) {
  86. continue;
  87. }
  88.  
  89.  
  90. Stack stack = new Stack(s.length());
  91. String q = "" + s.charAt(0);
  92.  
  93. if (q.equals("I")) {
  94. String tmp = "";
  95. int at = 0;
  96. boolean x = true;
  97. int counter = 0;
  98. for (int k = 0; k < s.length() && x == true; k++) {
  99. String m = Character.toString(s.charAt(k));
  100. if (isLetter(m))
  101. counter++;
  102. }
  103. for (int l = 0; l < s.length() && x == true; l++) {
  104. String m = Character.toString(s.charAt(l));
  105. if (at == 0) {
  106. if (m.charAt(0) == '(') {
  107. at = 0;
  108. } else {
  109. if (isUnary(m)) {
  110. at = 2;
  111. } else {
  112. if (isLetter(m)) {
  113. at = 1;
  114. } else {
  115. if (isBinary(m))
  116. x = false;
  117. if (m.charAt(0) == ')')
  118. x = false;
  119. }
  120. }
  121. }
  122. } else {
  123. if (at == 2) {
  124. if (isUnary(m)) {
  125. at = 2;
  126. } else {
  127. if (isLetter(m)) {
  128. at = 1;
  129. } else {
  130. if (m.charAt(0) == '(') {
  131. at = 0;
  132. } else {
  133. if (isBinary(m)) {
  134. x = false;
  135. }
  136. if (m.charAt(0) == ')')
  137. x = false;
  138. }
  139. }
  140. }
  141. } else {
  142. if (at == 1) {
  143. if (m.charAt(0) == ')') {
  144. at = 1;
  145. } else {
  146. if (isBinary(m)) {
  147. at = 0;
  148. } else {
  149. if (isUnary(m))
  150. x = false;
  151. if (isLetter(m))
  152. x = false;
  153. if (m.charAt(0) == '(')
  154. x = false;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. if (at != 1)
  162. x = false;
  163. for (int j = 0; j < s.length() && x == true; j++) {
  164. String m = Character.toString(s.charAt(j));
  165. if (isLetter(m)) {
  166. tmp += m + " ";
  167. }
  168. if (m.charAt(0) == '(') {
  169. stack.push("(");
  170. }
  171. if (m.charAt(0) == ')') {
  172. if (!stack.isEmpty()) {
  173. while (stack.top().charAt(0) != '(') {
  174. tmp += stack.top() + " ";
  175. stack.pop();
  176. if (stack.isEmpty())
  177. break;
  178. }
  179. }
  180. if (stack.isEmpty())
  181. x = false;
  182. stack.pop();
  183. }
  184. if (isLeftOperator(m)) {
  185. if (!stack.isEmpty()) {
  186. while (priority(stack.top()) >= priority(m)) {
  187. tmp += stack.top() + " ";
  188. stack.pop();
  189. if (stack.isEmpty())
  190. break;
  191. }
  192. }
  193. stack.push(m);
  194. }
  195. if (isRightOperator(m)) {
  196. if (!stack.isEmpty()) {
  197. while (priority(stack.top()) > priority(m)) {
  198. tmp += stack.top() + " ";
  199. stack.pop();
  200. if (stack.isEmpty())
  201. break;
  202. }
  203. }
  204. stack.push(m);
  205. }
  206. }
  207. while (!stack.isEmpty() && x == true) {
  208. tmp += stack.top() + " ";
  209. if (stack.top().charAt(0) == '(')
  210. x = false;
  211. stack.pop();
  212. }
  213. String print = "";
  214. for (int r = 0; r < tmp.length() - 1; r++) {
  215. print += Character.toString(tmp.charAt(r));
  216. }
  217. if (x == true)
  218. System.out.println("ONP: " + print);
  219. else
  220. System.out.println("ONP: error");
  221. }
  222. if (q.equals("O")) {
  223. String array[] = new String[256];
  224. int l = 0;
  225. for (int p = 0; p < s.length(); p++) {
  226. String h = Character.toString(s.charAt(p));
  227. if (isBinary(h) || isUnary(h) || isLetter(h)) {
  228. array[l] = h;
  229. l++;
  230. }
  231. }
  232. stackArray st = new stackArray(l);
  233. stackArrayInt sP = new stackArrayInt(l);
  234.  
  235. String tmp = "";
  236. int liczbaOperandow = 0, liczbaOperatorow = 0;
  237. boolean x = true;
  238.  
  239. for (int j = 0; j < l; j++) {
  240. if (!array[j].equals("(") && !array[j].equals(")"))
  241. if (array[j].charAt(0) >= 'a' && array[j].charAt(0) <= 'z') {
  242. st.push(array[j]);//element jest operandem
  243. sP.push(priority(array[j]));
  244. liczbaOperandow++;
  245. } else {
  246. tmp = "";
  247.  
  248. if (!array[j].equals("~") && !array[j].equals("!")) {
  249. liczbaOperatorow++;
  250. if (liczbaOperandow <= 1)
  251. x = false;
  252. if (isLeftOperator(array[j])) {
  253. if (sP.top() <= priority(array[j]))
  254. tmp = "( " + st.pop() + " )";
  255. else
  256. tmp = st.pop();
  257.  
  258. sP.pop();
  259.  
  260. if (sP.top() < priority(array[j]))
  261. tmp = "( " + st.pop() + " )" + " " + array[j] + " " + tmp;
  262. else
  263. tmp = st.pop() + " " + array[j] + " " + tmp;
  264.  
  265. sP.pop();
  266. } else {
  267. if (isRightOperator(array[j])) {
  268. if (sP.top() < priority(array[j]))
  269. tmp = "( " + st.pop() + " )";
  270. else
  271. tmp = st.pop();
  272.  
  273. sP.pop();
  274.  
  275. if (sP.top() <= priority(array[j]))
  276. tmp = "( " + st.pop() + " )" + " " + array[j] + " " + tmp;
  277. else
  278. tmp = st.pop() + " " + array[j] + " " + tmp;
  279.  
  280. sP.pop();
  281. }
  282. }
  283. } else {
  284. if (sP.top() < priority(array[j]))
  285. tmp = array[j] + " ( " + st.pop() + " )";
  286. else
  287. tmp = array[j] + " " + st.pop();
  288.  
  289. sP.pop();
  290. }
  291.  
  292. st.push(tmp);
  293. sP.push(priority(array[j]));
  294. }
  295. }
  296.  
  297. if (liczbaOperandow - 1 == liczbaOperatorow && x == true)
  298. System.out.println("INF: " + st.pop());
  299. else
  300. System.out.println("INF: error");
  301. }
  302.  
  303. }
  304. }
  305. }
  306.  
  307. class Stack {
  308. int maxSize;
  309. String elem[];
  310. int top;
  311.  
  312. public Stack(int size) {
  313. maxSize = size;
  314. elem = new String[maxSize];
  315. top = 0;
  316. }
  317.  
  318. public void push(String x) {
  319. elem[top] = x;
  320. top++;
  321. }
  322.  
  323. public void pop() {
  324. top--;
  325. }
  326.  
  327. public String top() {
  328. return elem[top - 1];
  329. }
  330.  
  331. public boolean isEmpty() {
  332. return (top <= 0);
  333. }
  334. }
  335. class stackArray
  336. {
  337. private int maxSize;
  338. private String[] Elem;
  339. private int top;
  340.  
  341. public stackArray(int size)
  342. {
  343. maxSize = size;
  344.  
  345. Elem = new String[maxSize];
  346. top = maxSize;
  347. }
  348.  
  349. public void push(String x)
  350. {
  351. if(!isFull())
  352. Elem[--top] = x;
  353. }
  354.  
  355. public String pop()
  356. {
  357. if(isEmpty())
  358. return "";
  359. else
  360. return Elem[top++];
  361. }
  362.  
  363. public String top()
  364. {
  365. if ( isEmpty() )
  366. return "";
  367. else
  368. return Elem[top];
  369. }
  370.  
  371. public boolean isEmpty()
  372. {
  373. return (top == maxSize);
  374. }
  375.  
  376. public boolean isFull()
  377. {
  378. return (top == 0);
  379. }
  380. }
  381.  
  382. class stackArrayInt
  383. {
  384. private int maxSize;
  385. private int[] Elem;
  386. private int top;
  387.  
  388. public stackArrayInt(int size)
  389. {
  390. maxSize = size;
  391.  
  392. Elem = new int[maxSize];
  393. top = maxSize;
  394. }
  395.  
  396. public void push(int x)
  397. {
  398. if(!isFull())
  399. Elem[--top] = x;
  400. }
  401.  
  402. public int pop()
  403. {
  404. if(isEmpty())
  405. return 0;
  406. else
  407. return Elem[top++];
  408. }
  409.  
  410. public int top()
  411. {
  412. if ( isEmpty() )
  413. return 0;
  414. else
  415. return Elem[top];
  416. }
  417.  
  418. public boolean isEmpty()
  419. {
  420. return (top == maxSize);
  421. }
  422.  
  423. public boolean isFull()
  424. {
  425. return (top == 0);
  426. }
  427. }
  428.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement