Advertisement
josiftepe

Untitled

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