Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.53 KB | None | 0 0
  1. //Karol Szawlis
  2. import java.util.Scanner;
  3. // implementacja stosu do ONP
  4. class Stack {
  5. private int maxsize;
  6. private char elem[];
  7. private int top;
  8. private int size;
  9.  
  10. public Stack(int s) {
  11. maxsize = s;
  12. elem = new char[maxsize];
  13. top = -1;
  14. size = 0;
  15. }
  16.  
  17. public Stack() {
  18. maxsize = 0;
  19. elem = new char[maxsize];
  20. top = -1;
  21. size = 0;
  22. }
  23.  
  24. public void push(char ch) {
  25. top = top + 1;
  26. ;
  27. elem[top] = ch;
  28. size = size + 1;
  29. }
  30.  
  31. public char pop() {
  32. top = top - 1;
  33. size = size - 1;
  34. return elem[top + 1];
  35. }
  36.  
  37. public boolean isEmpty() {
  38. if (top == -1) {
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44.  
  45. public char top() {
  46. return elem[top];
  47. }
  48.  
  49. public int size() {
  50. return size;
  51. }
  52. }
  53. // koniec implementaacji stosu do ONP
  54. class Wyrazenie {
  55. public String operator;
  56. public int priorytet;
  57. public String wyraz;
  58.  
  59.  
  60. public static int OperatorNaPrioryet(String op)
  61. {
  62. switch (op) {
  63. case "=":
  64. return 0;
  65.  
  66. case "<":
  67. case ">":
  68. return 1;
  69.  
  70. case "+":
  71. case "-":
  72. return 2;
  73.  
  74. case "*":
  75. case "/":
  76. case "%":
  77. return 3;
  78.  
  79. case "^":
  80. return 4;
  81.  
  82. case "~":
  83. return 5;
  84.  
  85. default: // operand
  86. return 6;
  87. }
  88.  
  89. }
  90.  
  91. public Wyrazenie(String wyraz_a, String wyraz_b, String op) {
  92. operator = op;
  93. wyraz = wyraz_a + op + wyraz_b;
  94.  
  95. priorytet = OperatorNaPrioryet(op);
  96. }
  97.  
  98. public Wyrazenie(String wyraz_a, String op)
  99. {
  100. operator = op;
  101.  
  102. priorytet = 5;
  103.  
  104. wyraz = "~" + wyraz_a;
  105. }
  106.  
  107. public Wyrazenie(String lit)
  108. {
  109. priorytet = 6;
  110. wyraz = lit;
  111. operator = "";
  112. }
  113.  
  114. }
  115.  
  116. class StacktoInfix {
  117. private int maxsize;
  118. private Wyrazenie elem[];
  119. private int top;
  120. private int size;
  121.  
  122. public StacktoInfix(int s) {
  123. maxsize = s;
  124. elem = new Wyrazenie[maxsize];
  125. top = -1;
  126. size = 0;
  127. }
  128.  
  129. public StacktoInfix() {
  130. maxsize = 0;
  131. elem = new Wyrazenie[maxsize];
  132. top = -1;
  133. size = 0;
  134. }
  135.  
  136. public void push(Wyrazenie w) {
  137. top = top + 1;
  138. elem[top] = w;
  139. size = size + 1;
  140. }
  141.  
  142. public Wyrazenie pop() {
  143. top = top - 1;
  144. size = size - 1;
  145. return elem[top + 1];
  146. }
  147.  
  148. public boolean isEmpty() {
  149. if (top == -1) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155.  
  156. public Wyrazenie top() {
  157. return elem[top];
  158. }
  159.  
  160. public int size() {
  161. return size;
  162. }
  163. }
  164.  
  165. class ONPToInfix {
  166. private StacktoInfix theStacktoInfix;
  167. private String input;
  168. private String output = "";
  169.  
  170. public ONPToInfix(String in) {
  171. input = in;
  172. int stacktoInfixSize = input.length();
  173. theStacktoInfix = new StacktoInfix(stacktoInfixSize);
  174. }
  175.  
  176. public void transToInfix() {
  177. for (int j = 0; j < input.length(); j++) {
  178. String ch = input.charAt(j) + "";
  179.  
  180. // if(!theStacktoInfix.isEmpty())
  181. // System.out.println(theStacktoInfix.top().wyraz);
  182.  
  183. int prio_obecny = Wyrazenie.OperatorNaPrioryet(ch);
  184.  
  185. if (prio_obecny == 6) // czyli operand
  186. {
  187. theStacktoInfix.push(new Wyrazenie(ch));
  188. continue;
  189. }
  190.  
  191. if (ch.equals("~")) // jedyny unarny, więc rozpatrujemy go osobno
  192. {
  193. if(theStacktoInfix.isEmpty())
  194. {
  195. System.out.print("error");
  196. return;
  197. }
  198.  
  199. Wyrazenie a = theStacktoInfix.pop();
  200.  
  201. if (a.priorytet <= prio_obecny)
  202. theStacktoInfix.push(new Wyrazenie("(" + a.wyraz + ")", ch));
  203. else
  204. theStacktoInfix.push(new Wyrazenie(a.wyraz, ch));
  205.  
  206. continue;
  207. }
  208.  
  209. //czas na binarne
  210. if(theStacktoInfix.size() < 2)
  211. {
  212. System.out.print("error");
  213. return;
  214. }
  215.  
  216. Wyrazenie b = theStacktoInfix.pop();
  217. Wyrazenie a = theStacktoInfix.pop();
  218.  
  219. String tymczasowa_lewa, tymczasowa_prawa;
  220.  
  221. if (a.priorytet < prio_obecny ){//|| (a.priorytet == prio_obecny && (ch.equals("^") ))){//|| ch.equals("=") ) )) {
  222. tymczasowa_lewa = "(" + a.wyraz + ")";
  223. } else {
  224. tymczasowa_lewa = a.wyraz;
  225. }
  226.  
  227. if (b.priorytet <= prio_obecny){ // || (b.priorytet == prio_obecny && (!ch.equals("^")))){ // && !ch.equals("=")) )) {
  228. tymczasowa_prawa = "(" + b.wyraz + ")";
  229. } else {
  230. tymczasowa_prawa = b.wyraz;
  231. }
  232.  
  233. theStacktoInfix.push(new Wyrazenie(tymczasowa_lewa, tymczasowa_prawa, ch));
  234. }
  235.  
  236. if(theStacktoInfix.size() != 1)
  237. {
  238. System.out.print("error");
  239. return;
  240. }
  241.  
  242.  
  243. Wyrazenie koniec = theStacktoInfix.pop();
  244. System.out.print(koniec.wyraz);
  245.  
  246.  
  247.  
  248.  
  249. }
  250. }
  251.  
  252. class InfixToONP {
  253. private Stack theStack;
  254. private String input;
  255. private String output = "";
  256.  
  257. public InfixToONP(String in) {
  258. input = in;
  259. int stackSize = input.length();
  260. theStack = new Stack(stackSize);
  261. }
  262.  
  263. public String transToOnp() { // wejscie
  264. for (int j = 0; j < input.length(); j++) {
  265. char ch = input.charAt(j);
  266. switch (ch) {
  267. case '=':
  268. gotOper(ch, 0);
  269. break;
  270. case '<':
  271. case '>':
  272. gotOper(ch, 1);
  273. break;
  274. case '+':
  275. case '-':
  276. gotOper(ch, 2);
  277. break;
  278. case '*':
  279. case '/':
  280. case '%':
  281. gotOper(ch, 3);
  282. break;
  283. case '^':
  284. gotOper(ch, 4);
  285. break;
  286. case '~':
  287. gotOper(ch, 5);
  288. break;
  289. case '(':
  290. theStack.push(ch);
  291. break;
  292. case ')':
  293. gotParen(ch);
  294. break;
  295. default:
  296. output = output + ch;
  297. break;
  298. }
  299. }
  300. while (!theStack.isEmpty()) {
  301. output = output + theStack.pop();
  302. }
  303. return output;
  304. }
  305.  
  306. public Boolean SprawdzInfix() {
  307. int nawiasy = 0;
  308. boolean czynastepnyoperand = true;
  309. boolean operand = true;
  310. for (int i = 0; i < input.length(); i++) {
  311. if (input.charAt(i) == '(') nawiasy++;
  312. else if (input.charAt(i) == ')') {
  313. if(nawiasy==0)return false;
  314. if(opwnawiasie(i)) nawiasy--;
  315. }
  316. else if (czynastepnyoperand && czyOperand(input.charAt(i))) {
  317. czynastepnyoperand = false;
  318. operand = true;
  319. } else if (!czynastepnyoperand && czyOperand(input.charAt(i))) return false;
  320. else if ((czynastepnyoperand && input.charAt(i) != '~') || (!operand && input.charAt(i) != '~'))
  321. return false;
  322. else if (input.charAt(i) == '~') operand = false;
  323. else
  324. czynastepnyoperand = true;
  325. }
  326. return (nawiasy == 0 && !czynastepnyoperand);
  327. }
  328. boolean opwnawiasie(int i){
  329. if(i>0&&input.charAt(i-1)=='~')return false;
  330. while(--i >=0&&input.charAt(i)!='(')
  331. if(
  332. czyOperand(input.charAt(i)))return true;
  333. return false;
  334. }
  335. public boolean czyOperand(char operand){
  336. return operand >= 'a' && operand <= 'z';
  337. }
  338.  
  339. public void gotOper(char opThis, int prec1) {
  340. String strona_wyjscia;
  341. String stronaczubka = " ";
  342. if (opThis == '=' || opThis == '^' || opThis == '~') {
  343. strona_wyjscia = "prawy";
  344. } else {
  345. strona_wyjscia = "lewy";
  346. }
  347. while (!theStack.isEmpty()) {
  348. char opTop = theStack.pop();
  349. if (opTop == '(') {
  350. theStack.push(opTop);
  351. break;
  352. } else {
  353. int prec2 = 0;
  354. if (opTop == '=') {
  355. prec2 = 0;
  356. stronaczubka = "prawy";
  357. }
  358. if (opTop == '<' || opTop == '>') {
  359. prec2 = 1;
  360. stronaczubka = "lewy";
  361. }
  362. if (opTop == '+' || opTop == '-') {
  363. prec2 = 2;
  364. stronaczubka = "lewy";
  365. }
  366. if (opTop == '*' || opTop == '/' || opTop == '%') {
  367. prec2 = 3;
  368. stronaczubka = "lewy";
  369. }
  370. if (opTop == '^') {
  371. prec2 = 4;
  372. stronaczubka = "prawy";
  373. }
  374. if (opTop == '~') {
  375. prec2 = 5;
  376. stronaczubka = "prawy";
  377. }
  378. if ((stronaczubka == "lewy" && prec2 >= prec1) || (stronaczubka == "prawy" && prec2 > prec1)) {
  379. output = output + opTop;
  380. } else {
  381. theStack.push(opTop);
  382. break;
  383. }
  384. }
  385. }
  386. theStack.push(opThis);
  387. }
  388.  
  389. public void gotParen(char ch) {
  390. while (!theStack.isEmpty()) {
  391. char chx = theStack.pop();
  392. if (chx == '(')
  393. break;
  394. else
  395. output = output + chx;
  396. }
  397. }
  398. }
  399.  
  400. public class Karola {
  401.  
  402. public static Scanner in = new Scanner(System.in);
  403.  
  404. public static void main(String args[]) {
  405. int ilosc = in.nextInt();
  406. in.nextLine();
  407. while (ilosc-- != 0){
  408. String line = in.nextLine();
  409. String type = line.substring(0, 4);
  410. String exp = line.substring(5);
  411. String inONP = "qwertyuiopasdfghjklzxcvbnm=<>+-*/%^~";
  412. String inINF = inONP + "()"; //
  413.  
  414. if (type.equals("INF:")) {
  415. StringBuilder b = new StringBuilder();
  416. for (int i = 0; i < exp.length(); i++)
  417. if (inINF.indexOf(exp.charAt(i)) >= 0)
  418. b.append(exp.charAt(i));
  419.  
  420.  
  421. InfixToONP inf = new InfixToONP(b.toString());
  422. System.out.print("ONP: ");
  423.  
  424. if (b.toString().length() == 0)
  425. {
  426. System.out.println();
  427. continue;
  428. }
  429.  
  430. if(!inf.SprawdzInfix() )
  431. {
  432. System.out.println("error");
  433. continue;
  434. }
  435.  
  436.  
  437. System.out.println(inf.transToOnp());
  438. continue;
  439.  
  440. }
  441.  
  442. if (type.equals("ONP:")) {
  443. StringBuilder b = new StringBuilder();
  444. for (int i = 0; i < exp.length(); i++)
  445. if (inONP.indexOf(exp.charAt(i)) >= 0)
  446. b.append(exp.charAt(i));
  447.  
  448. if (b.toString().length() == 0)
  449. {
  450. System.out.println();
  451. continue;
  452. }
  453.  
  454.  
  455. ONPToInfix onp = new ONPToInfix(b.toString());
  456. System.out.print("INF: ");
  457. onp.transToInfix();
  458. System.out.println();
  459.  
  460. }
  461.  
  462. }
  463.  
  464. }
  465. }
  466. // testy z strony informatyki
  467. /*
  468. ONP: error
  469. ONP:AB+
  470. INF: error
  471. INF:ab+$#^%$ ^^*
  472. ONP: error
  473. ONP:ab/#@!&
  474. INF: error
  475. ONP:ab+AB-=
  476. INF: error
  477. ONP: ab+cd* /
  478. INF: (a+b)/(c*d)
  479. INF: ; p^a..*(x{+ 'y)
  480. ONP: pa^xy+*
  481. INF: a +b
  482. ONP: ab+
  483. ONP: a A b +
  484. INF: a+b
  485.  
  486. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement