Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  1. import java.util.Scanner;
  2. class apka {
  3. class Stack {
  4. private int maxsize;
  5. private char elem[];
  6. private int top;
  7. private int size;
  8.  
  9. public Stack(int s) {
  10. maxsize = s;
  11. elem = new char[maxsize];
  12. top = -1;
  13. size = 0;
  14. }
  15.  
  16. public Stack() {
  17. maxsize = 0;
  18. elem = new char[maxsize];
  19. top = -1;
  20. size = 0;
  21. }
  22.  
  23. public void push(char ch) {
  24. top = top + 1;
  25. ;
  26. elem[top] = ch;
  27. size = size + 1;
  28. }
  29.  
  30. public char pop() {
  31. top = top - 1;
  32. size = size - 1;
  33. return elem[top + 1];
  34. }
  35.  
  36. public boolean isEmpty() {
  37. if (top == -1) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43.  
  44. public char top() {
  45. return elem[top];
  46. }
  47.  
  48. public int size() {
  49. return size;
  50. }
  51. }
  52.  
  53. class Wyrazenie {
  54. public String operator;
  55. public int priorytet;
  56. public String wyraz;
  57.  
  58.  
  59. int OperatorNaPrioryet(String op) // jak w nazwie, podaje priorytet operatora.
  60. {
  61. switch (op) {
  62. case "=":
  63. return 0;
  64.  
  65. case "<":
  66. case ">":
  67. return 1;
  68.  
  69. case "+":
  70. case "-":
  71. return 2;
  72.  
  73. case "*":
  74. case "/":
  75. case "%":
  76. return 3;
  77.  
  78. case "^":
  79. return 4;
  80.  
  81. case "~":
  82. return 5;
  83.  
  84. default: // operand
  85. return 6;
  86. }
  87.  
  88. }
  89.  
  90. public Wyrazenie(String wyraz_a, String wyraz_b, String op) {
  91. operator = op;
  92. wyraz = wyraz_a + op + wyraz_b;
  93.  
  94. priorytet = OperatorNaPrioryet(op);
  95. }
  96.  
  97. public Wyrazenie(String wyraz_a, String op) // dla unarnych (czyli tylko ~)
  98. {
  99. operator = op;
  100.  
  101. priorytet = 5;
  102.  
  103. wyraz = "~" + wyraz_a;
  104. }
  105.  
  106. public Wyrazenie(String lit) // dla operandow
  107. {
  108. priorytet = 6;
  109. wyraz = lit;
  110. operator = "";
  111. }
  112.  
  113. }
  114.  
  115. class StacktoInfix {
  116. private int maxsize;
  117. private Wyrazenie elem[];
  118. private int top;
  119. private int size;
  120.  
  121. public StacktoInfix(int s) {
  122. maxsize = s;
  123. elem = new Wyrazenie[maxsize];
  124. top = -1;
  125. size = 0;
  126. }
  127.  
  128. public StacktoInfix() {
  129. maxsize = 0;
  130. elem = new Wyrazenie[maxsize];
  131. top = -1;
  132. size = 0;
  133. }
  134.  
  135. public void push(Wyrazenie w) {
  136. top = top + 1;
  137. elem[top] = w;
  138. size = size + 1;
  139. }
  140.  
  141. public Wyrazenie pop() {
  142. top = top - 1;
  143. size = size - 1;
  144. return elem[top + 1];
  145. }
  146.  
  147. public boolean isEmpty() {
  148. if (top == -1) {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154.  
  155. public Wyrazenie top() {
  156. return elem[top];
  157. }
  158.  
  159. public int size() {
  160. return size;
  161. }
  162. }
  163.  
  164. class ONPToInfix {
  165. private StacktoInfix theStacktoInfix;
  166. private String input;
  167. private String output = "";
  168.  
  169. public ONPToInfix(String in) {
  170. input = in;
  171. int stacktoInfixSize = input.length();
  172. theStacktoInfix = new StacktoInfix(stacktoInfixSize);
  173. }
  174.  
  175. public void transToInfix() {
  176. for (int j = 0; j < input.length(); j++) {
  177. String ch = input.charAt(j) + "";
  178.  
  179. // if(!theStacktoInfix.isEmpty())
  180. // System.out.println(theStacktoInfix.top().wyraz);
  181.  
  182. int prio_obecny = Wyrazenie.OperatorNaPrioryet(ch);
  183.  
  184. if (prio_obecny == 6) // czyli operand
  185. {
  186. theStacktoInfix.push(new Wyrazenie(ch));
  187. continue;
  188. }
  189.  
  190. if (ch.equals("~")) // jedyny unarny, więc rozpatrujemy go osobno
  191. {
  192. Wyrazenie a = theStacktoInfix.pop();
  193.  
  194. if (a.priorytet < prio_obecny)
  195. theStacktoInfix.push(new Wyrazenie("(" + a.wyraz + ")", ch));
  196. else
  197. theStacktoInfix.push(new Wyrazenie(a.wyraz, ch));
  198.  
  199. continue;
  200. }
  201.  
  202. //czas na binarne
  203.  
  204. Wyrazenie b = theStacktoInfix.pop();
  205. Wyrazenie a = theStacktoInfix.pop();
  206.  
  207. String tymczasowa_lewa, tymczasowa_prawa;
  208.  
  209. if (a.priorytet < prio_obecny || (a.priorytet == prio_obecny && ch == "^")) {
  210. tymczasowa_lewa = "(" + a.wyraz + ")";
  211. } else {
  212. tymczasowa_lewa = a.wyraz;
  213. }
  214.  
  215. if (b.priorytet < prio_obecny || (b.priorytet == prio_obecny && ch != "^")) {
  216. tymczasowa_prawa = "(" + b.wyraz + ")";
  217. } else {
  218. tymczasowa_prawa = b.wyraz;
  219. }
  220.  
  221. theStacktoInfix.push(new Wyrazenie(tymczasowa_lewa, tymczasowa_prawa, ch));
  222. // System.out.println(theStacktoInfix.top().wyraz);
  223. }
  224.  
  225. //if (theStacktoInfix.size() == 1) {
  226. Wyrazenie koniec = theStacktoInfix.pop();
  227. System.out.print(koniec.wyraz);
  228.  
  229. //}
  230.  
  231.  
  232. }
  233. }
  234.  
  235. class InfixToONP {
  236. private Stack theStack;
  237. private String input;
  238. private String output = "";
  239.  
  240. public InfixToONP(String in) {
  241. input = in;
  242. int stackSize = input.length();
  243. theStack = new Stack(stackSize);
  244. }
  245.  
  246. public String transToOnp() { // wejscie
  247. for (int j = 0; j < input.length(); j++) {
  248. char ch = input.charAt(j);
  249. switch (ch) {
  250. case '=':
  251. gotOper(ch, 0);
  252. break;
  253. case '<':
  254. case '>':
  255. gotOper(ch, 1);
  256. break;
  257. case '+':
  258. case '-':
  259. gotOper(ch, 2);
  260. break;
  261. case '*':
  262. case '/':
  263. case '%':
  264. gotOper(ch, 3);
  265. break;
  266. case '^':
  267. gotOper(ch, 4);
  268. break;
  269. case '~':
  270. gotOper(ch, 5);
  271. break;
  272. case '(':
  273. theStack.push(ch);
  274. break;
  275. case ')':
  276. gotParen(ch);
  277. break;
  278. default:
  279. output = output + ch;
  280. break;
  281. }
  282. }
  283. while (!theStack.isEmpty()) {
  284. output = output + theStack.pop();
  285. }
  286. return output;
  287. }
  288.  
  289. public void gotOper(char opThis, int prec1) { // tu mowie o gorze stosu
  290. String strona_wyjscia;
  291. String stronaczubka = " ";
  292. if (opThis == '=' || opThis == '^' || opThis == '~') {
  293. strona_wyjscia = "prawy";
  294. } else {
  295. strona_wyjscia = "lewy";
  296. }
  297. while (!theStack.isEmpty()) {
  298. char opTop = theStack.pop();
  299. if (opTop == '(') {
  300. theStack.push(opTop);
  301. break;
  302. } else {
  303. int prec2 = 0;
  304. if (opTop == '=') {
  305. prec2 = 0;
  306. stronaczubka = "prawy";
  307. }
  308. if (opTop == '<' || opTop == '>') {
  309. prec2 = 1;
  310. stronaczubka = "lewy";
  311. }
  312. if (opTop == '+' || opTop == '-') {
  313. prec2 = 2;
  314. stronaczubka = "lewy";
  315. }
  316. if (opTop == '*' || opTop == '/' || opTop == '%') {
  317. prec2 = 3;
  318. stronaczubka = "lewy";
  319. }
  320. if (opTop == '^') {
  321. prec2 = 4;
  322. stronaczubka = "prawy";
  323. }
  324. if (opTop == '~') {
  325. prec2 = 5;
  326. stronaczubka = "prawy";
  327. }
  328. if ((stronaczubka == "lewy" && prec2 >= prec1) || (stronaczubka == "prawy" && prec2 > prec1)) {
  329. output = output + opTop;
  330. } else {
  331. theStack.push(opTop);
  332. break;
  333. }
  334. }
  335. }
  336. theStack.push(opThis);
  337. }
  338.  
  339. public void gotParen(char ch) {
  340. while (!theStack.isEmpty()) {
  341. char chx = theStack.pop();
  342. if (chx == '(')
  343. break;
  344. else
  345. output = output + chx;
  346. }
  347. }
  348. }
  349. public static Scanner in = new Scanner(System.in);
  350.  
  351. public class Main {
  352. public static void main(String args[]) {
  353. int ilosc = in.nextInt();
  354. in.nextLine();
  355. while (ilosc-- != 0) {
  356. String line = in.nextLine();
  357. String type = line.substring(0, 4);
  358. String exp = line.substring(5);
  359. String inONP = "qwertyuiopasdfghjklzxcvbnm=<>+-*/%^~"; //dozwolone znaki w ONP
  360. String inINF = inONP + "()"; // w INF to samo, ale jeszcze nawiasy
  361.  
  362. if (type.equals("INF:")) {
  363. StringBuilder b = new StringBuilder();
  364. for (int i = 0; i < exp.length(); i++)
  365. if (inINF.indexOf(exp.charAt(i)) >= 0)
  366. b.append(exp.charAt(i));
  367.  
  368. InfixToONP inf = new InfixToONP(b.toString());
  369. System.out.print("ONP: ");
  370. inf.transToOnp();
  371. System.out.println();
  372. continue;
  373.  
  374. }
  375.  
  376. if (type.equals("ONP:")) {
  377. StringBuilder b = new StringBuilder();
  378. for (int i = 0; i < exp.length(); i++)
  379. if (inONP.indexOf(exp.charAt(i)) >= 0)
  380. b.append(exp.charAt(i));
  381.  
  382. ONPToInfix onp = new ONPToInfix(b.toString());
  383. System.out.print("INF: ");
  384. onp.transToInfix();
  385. System.out.println();
  386.  
  387. }
  388.  
  389. }
  390.  
  391. }
  392. }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement