Advertisement
josiftepe

Untitled

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