Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class apka {
- class Stack {
- private int maxsize;
- private char elem[];
- private int top;
- private int size;
- public Stack(int s) {
- maxsize = s;
- elem = new char[maxsize];
- top = -1;
- size = 0;
- }
- public Stack() {
- maxsize = 0;
- elem = new char[maxsize];
- top = -1;
- size = 0;
- }
- public void push(char ch) {
- top = top + 1;
- ;
- elem[top] = ch;
- size = size + 1;
- }
- public char pop() {
- top = top - 1;
- size = size - 1;
- return elem[top + 1];
- }
- public boolean isEmpty() {
- if (top == -1) {
- return true;
- } else {
- return false;
- }
- }
- public char top() {
- return elem[top];
- }
- public int size() {
- return size;
- }
- }
- class Wyrazenie {
- public String operator;
- public int priorytet;
- public String wyraz;
- int OperatorNaPrioryet(String op) // jak w nazwie, podaje priorytet operatora.
- {
- switch (op) {
- case "=":
- return 0;
- case "<":
- case ">":
- return 1;
- case "+":
- case "-":
- return 2;
- case "*":
- case "/":
- case "%":
- return 3;
- case "^":
- return 4;
- case "~":
- return 5;
- default: // operand
- return 6;
- }
- }
- public Wyrazenie(String wyraz_a, String wyraz_b, String op) {
- operator = op;
- wyraz = wyraz_a + op + wyraz_b;
- priorytet = OperatorNaPrioryet(op);
- }
- public Wyrazenie(String wyraz_a, String op) // dla unarnych (czyli tylko ~)
- {
- operator = op;
- priorytet = 5;
- wyraz = "~" + wyraz_a;
- }
- public Wyrazenie(String lit) // dla operandow
- {
- priorytet = 6;
- wyraz = lit;
- operator = "";
- }
- }
- class StacktoInfix {
- private int maxsize;
- private Wyrazenie elem[];
- private int top;
- private int size;
- public StacktoInfix(int s) {
- maxsize = s;
- elem = new Wyrazenie[maxsize];
- top = -1;
- size = 0;
- }
- public StacktoInfix() {
- maxsize = 0;
- elem = new Wyrazenie[maxsize];
- top = -1;
- size = 0;
- }
- public void push(Wyrazenie w) {
- top = top + 1;
- elem[top] = w;
- size = size + 1;
- }
- public Wyrazenie pop() {
- top = top - 1;
- size = size - 1;
- return elem[top + 1];
- }
- public boolean isEmpty() {
- if (top == -1) {
- return true;
- } else {
- return false;
- }
- }
- public Wyrazenie top() {
- return elem[top];
- }
- public int size() {
- return size;
- }
- }
- class ONPToInfix {
- private StacktoInfix theStacktoInfix;
- private String input;
- private String output = "";
- public ONPToInfix(String in) {
- input = in;
- int stacktoInfixSize = input.length();
- theStacktoInfix = new StacktoInfix(stacktoInfixSize);
- }
- public void transToInfix() {
- for (int j = 0; j < input.length(); j++) {
- String ch = input.charAt(j) + "";
- // if(!theStacktoInfix.isEmpty())
- // System.out.println(theStacktoInfix.top().wyraz);
- int prio_obecny = Wyrazenie.OperatorNaPrioryet(ch);
- if (prio_obecny == 6) // czyli operand
- {
- theStacktoInfix.push(new Wyrazenie(ch));
- continue;
- }
- if (ch.equals("~")) // jedyny unarny, więc rozpatrujemy go osobno
- {
- Wyrazenie a = theStacktoInfix.pop();
- if (a.priorytet < prio_obecny)
- theStacktoInfix.push(new Wyrazenie("(" + a.wyraz + ")", ch));
- else
- theStacktoInfix.push(new Wyrazenie(a.wyraz, ch));
- continue;
- }
- //czas na binarne
- Wyrazenie b = theStacktoInfix.pop();
- Wyrazenie a = theStacktoInfix.pop();
- String tymczasowa_lewa, tymczasowa_prawa;
- if (a.priorytet < prio_obecny || (a.priorytet == prio_obecny && ch == "^")) {
- tymczasowa_lewa = "(" + a.wyraz + ")";
- } else {
- tymczasowa_lewa = a.wyraz;
- }
- if (b.priorytet < prio_obecny || (b.priorytet == prio_obecny && ch != "^")) {
- tymczasowa_prawa = "(" + b.wyraz + ")";
- } else {
- tymczasowa_prawa = b.wyraz;
- }
- theStacktoInfix.push(new Wyrazenie(tymczasowa_lewa, tymczasowa_prawa, ch));
- // System.out.println(theStacktoInfix.top().wyraz);
- }
- //if (theStacktoInfix.size() == 1) {
- Wyrazenie koniec = theStacktoInfix.pop();
- System.out.print(koniec.wyraz);
- //}
- }
- }
- class InfixToONP {
- private Stack theStack;
- private String input;
- private String output = "";
- public InfixToONP(String in) {
- input = in;
- int stackSize = input.length();
- theStack = new Stack(stackSize);
- }
- public String transToOnp() { // wejscie
- for (int j = 0; j < input.length(); j++) {
- char ch = input.charAt(j);
- switch (ch) {
- case '=':
- gotOper(ch, 0);
- break;
- case '<':
- case '>':
- gotOper(ch, 1);
- break;
- case '+':
- case '-':
- gotOper(ch, 2);
- break;
- case '*':
- case '/':
- case '%':
- gotOper(ch, 3);
- break;
- case '^':
- gotOper(ch, 4);
- break;
- case '~':
- gotOper(ch, 5);
- break;
- case '(':
- theStack.push(ch);
- break;
- case ')':
- gotParen(ch);
- break;
- default:
- output = output + ch;
- break;
- }
- }
- while (!theStack.isEmpty()) {
- output = output + theStack.pop();
- }
- return output;
- }
- public void gotOper(char opThis, int prec1) { // tu mowie o gorze stosu
- String strona_wyjscia;
- String stronaczubka = " ";
- if (opThis == '=' || opThis == '^' || opThis == '~') {
- strona_wyjscia = "prawy";
- } else {
- strona_wyjscia = "lewy";
- }
- while (!theStack.isEmpty()) {
- char opTop = theStack.pop();
- if (opTop == '(') {
- theStack.push(opTop);
- break;
- } else {
- int prec2 = 0;
- if (opTop == '=') {
- prec2 = 0;
- stronaczubka = "prawy";
- }
- if (opTop == '<' || opTop == '>') {
- prec2 = 1;
- stronaczubka = "lewy";
- }
- if (opTop == '+' || opTop == '-') {
- prec2 = 2;
- stronaczubka = "lewy";
- }
- if (opTop == '*' || opTop == '/' || opTop == '%') {
- prec2 = 3;
- stronaczubka = "lewy";
- }
- if (opTop == '^') {
- prec2 = 4;
- stronaczubka = "prawy";
- }
- if (opTop == '~') {
- prec2 = 5;
- stronaczubka = "prawy";
- }
- if ((stronaczubka == "lewy" && prec2 >= prec1) || (stronaczubka == "prawy" && prec2 > prec1)) {
- output = output + opTop;
- } else {
- theStack.push(opTop);
- break;
- }
- }
- }
- theStack.push(opThis);
- }
- public void gotParen(char ch) {
- while (!theStack.isEmpty()) {
- char chx = theStack.pop();
- if (chx == '(')
- break;
- else
- output = output + chx;
- }
- }
- }
- public static Scanner in = new Scanner(System.in);
- public class Main {
- public static void main(String args[]) {
- int ilosc = in.nextInt();
- in.nextLine();
- while (ilosc-- != 0) {
- String line = in.nextLine();
- String type = line.substring(0, 4);
- String exp = line.substring(5);
- String inONP = "qwertyuiopasdfghjklzxcvbnm=<>+-*/%^~"; //dozwolone znaki w ONP
- String inINF = inONP + "()"; // w INF to samo, ale jeszcze nawiasy
- if (type.equals("INF:")) {
- StringBuilder b = new StringBuilder();
- for (int i = 0; i < exp.length(); i++)
- if (inINF.indexOf(exp.charAt(i)) >= 0)
- b.append(exp.charAt(i));
- InfixToONP inf = new InfixToONP(b.toString());
- System.out.print("ONP: ");
- inf.transToOnp();
- System.out.println();
- continue;
- }
- if (type.equals("ONP:")) {
- StringBuilder b = new StringBuilder();
- for (int i = 0; i < exp.length(); i++)
- if (inONP.indexOf(exp.charAt(i)) >= 0)
- b.append(exp.charAt(i));
- ONPToInfix onp = new ONPToInfix(b.toString());
- System.out.print("INF: ");
- onp.transToInfix();
- System.out.println();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement