Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Karol Szawlis
- import java.util.Scanner;
- // implementacja stosu do ONP
- 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;
- }
- }
- // koniec implementaacji stosu do ONP
- class Wyrazenie {
- public String operator;
- public int priorytet;
- public String wyraz;
- public static int OperatorNaPrioryet(String op)
- {
- 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)
- {
- operator = op;
- priorytet = 5;
- wyraz = "~" + wyraz_a;
- }
- public Wyrazenie(String lit)
- {
- 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
- {
- if(theStacktoInfix.isEmpty())
- {
- System.out.print("error");
- return;
- }
- 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
- if(theStacktoInfix.size() < 2)
- {
- System.out.print("error");
- return;
- }
- Wyrazenie b = theStacktoInfix.pop();
- Wyrazenie a = theStacktoInfix.pop();
- String tymczasowa_lewa, tymczasowa_prawa;
- if (a.priorytet < prio_obecny ){//|| (a.priorytet == prio_obecny && (ch.equals("^") ))){//|| ch.equals("=") ) )) {
- tymczasowa_lewa = "(" + a.wyraz + ")";
- } else {
- tymczasowa_lewa = a.wyraz;
- }
- if (b.priorytet <= prio_obecny){ // || (b.priorytet == prio_obecny && (!ch.equals("^")))){ // && !ch.equals("=")) )) {
- tymczasowa_prawa = "(" + b.wyraz + ")";
- } else {
- tymczasowa_prawa = b.wyraz;
- }
- theStacktoInfix.push(new Wyrazenie(tymczasowa_lewa, tymczasowa_prawa, ch));
- }
- if(theStacktoInfix.size() != 1)
- {
- System.out.print("error");
- return;
- }
- 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 Boolean SprawdzInfix() {
- int nawiasy = 0;
- boolean czynastepnyoperand = true;
- boolean operand = true;
- for (int i = 0; i < input.length(); i++) {
- if (input.charAt(i) == '(') nawiasy++;
- else if (input.charAt(i) == ')') {
- if(nawiasy==0)return false;
- if(opwnawiasie(i)) nawiasy--;
- }
- else if (czynastepnyoperand && czyOperand(input.charAt(i))) {
- czynastepnyoperand = false;
- operand = true;
- } else if (!czynastepnyoperand && czyOperand(input.charAt(i))) return false;
- else if ((czynastepnyoperand && input.charAt(i) != '~') || (!operand && input.charAt(i) != '~'))
- return false;
- else if (input.charAt(i) == '~') operand = false;
- else
- czynastepnyoperand = true;
- }
- return (nawiasy == 0 && !czynastepnyoperand);
- }
- boolean opwnawiasie(int i){
- if(i>0&&input.charAt(i-1)=='~')return false;
- while(--i >=0&&input.charAt(i)!='(')
- if(
- czyOperand(input.charAt(i)))return true;
- return false;
- }
- public boolean czyOperand(char operand){
- return operand >= 'a' && operand <= 'z';
- }
- public void gotOper(char opThis, int prec1) {
- 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 class Karola {
- public static Scanner in = new Scanner(System.in);
- 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=<>+-*/%^~";
- String inINF = inONP + "()"; //
- 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: ");
- if (b.toString().length() == 0)
- {
- System.out.println();
- continue;
- }
- if(!inf.SprawdzInfix() )
- {
- System.out.println("error");
- continue;
- }
- System.out.println(inf.transToOnp());
- 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));
- if (b.toString().length() == 0)
- {
- System.out.println();
- continue;
- }
- ONPToInfix onp = new ONPToInfix(b.toString());
- System.out.print("INF: ");
- onp.transToInfix();
- System.out.println();
- }
- }
- }
- }
- // testy z strony informatyki
- /*
- ONP: error
- ONP:AB+
- INF: error
- INF:ab+$#^%$ ^^*
- ONP: error
- ONP:ab/#@!&
- INF: error
- ONP:ab+AB-=
- INF: error
- ONP: ab+cd* /
- INF: (a+b)/(c*d)
- INF: ; p^a..*(x{+ 'y)
- ONP: pa^xy+*
- INF: a +b
- ONP: ab+
- ONP: a A b +
- INF: a+b
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement