Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kalkulatorONP;
- public class Calc {
- public static boolean sprawdz_priorytet(String stos, String args)
- {
- int p1 = 0, p2 = 0;
- switch(stos)
- {
- case "+":
- p1 = 1;
- break;
- case "-":
- p1 = 1;
- break;
- case "x":
- p1 = 2;
- break;
- case"/":
- p1 = 2;
- break;
- default:
- break;
- }
- switch(args)
- {
- case "+":
- p2 = 1;
- break;
- case "-":
- p2 = 1;
- break;
- case "x":
- p2 = 2;
- break;
- case"/":
- p2 = 2;
- break;
- default:
- break;
- }
- return (p1 >= p2);
- }
- public static void wypisz(String[] stos, String[] wyjscie)
- {
- System.out.println("Wartosc wyrazenia w ONP: ");
- for(int i = 0; i < stos.length; i++)
- {
- System.out.print(wyjscie[i] + " ");
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //System.out.println(args.length);
- //wypisz(args,args);
- //Tablice
- String[] stos = new String[args.length];
- String[] wyjscie = new String[args.length];
- int[] wejscie = new int[args.length];
- //przechowywanie indexu gdzie mozna wstawic element w tablicach stos i wyjscie
- int tmp_st = 0;
- int tmp_wy = 0;
- for(int i = 0; i < args.length; i++)
- {
- //sprawdzanie czy jest liczba
- if(Character.isDigit(args[i].charAt(0)))
- {
- wyjscie[tmp_wy] = args[i];
- tmp_wy++;
- }
- else //jesli nie jest, to moze byc operatorem lub nawiasem
- {
- if(args[i].equals("(")) //jesli nawias otwierajacy to po prostu na stos
- {
- stos[tmp_st] = args[i];
- tmp_st++;
- }
- else if(args[i].equals(")")) //jesli zamykajacy to na wyjscie wszystko az do napotkania otwierajacego
- {
- while(!stos[tmp_st - 1].equals("("))
- {
- wyjscie[tmp_wy] = stos[tmp_st - 1];
- tmp_wy++;
- stos[tmp_st - 1] = "brak";
- tmp_st--;
- }
- stos[tmp_st - 1] = "brak";
- tmp_st--;
- }
- else //sprawdzanie priorytetu
- {
- if(tmp_st>0)
- {
- if(!stos[tmp_st-1].equals("(") && sprawdz_priorytet(stos[tmp_st - 1], args[i]))
- {
- wyjscie[tmp_wy] = stos[tmp_st - 1];
- tmp_wy++;
- stos[tmp_st - 1] = args[i];
- }
- else
- {
- stos[tmp_st] = args[i];
- tmp_st++;
- }
- }
- else
- {
- stos[tmp_st] = args[i];
- tmp_st++;
- }
- }
- }
- }
- for(;tmp_st !=0; tmp_st--)
- {
- wyjscie[tmp_wy] = stos[tmp_st - 1];
- stos[tmp_st - 1] = "brak";
- tmp_wy++;
- }
- //TUTAJ SKONCZYLO SIE WYPISYWANIE WYJSCIA, CZYLI POSTACI ONP
- //TERAZ WEJSCIE I OBLICZENIE WARTOSCI WYRAZENIA
- tmp_st = 0;
- int k = tmp_wy;
- tmp_wy = 0;
- wypisz(stos,wyjscie);
- int tmp_we = 0;
- for(int i = 0; i < k; i++)
- {
- //sprawdzanie czy jest liczba
- if(Character.isDigit(wyjscie[i].charAt(0)))
- {
- wejscie[tmp_st] = Integer.parseInt(wyjscie[i]);
- tmp_wy++;
- tmp_st++;
- }
- else //znaczy ze operator
- {
- if(wyjscie[i].equals("+"))
- wejscie[tmp_st - 2] = wejscie[tmp_st - 2] +wejscie[tmp_st - 1];
- else if(wyjscie[i].equals("-"))
- wejscie[tmp_st - 2] = wejscie[tmp_st - 2] - wejscie[tmp_st - 1];
- else if(wyjscie[i].equals("x"))
- wejscie[tmp_st - 2] = wejscie[tmp_st - 2] * wejscie[tmp_st - 1];
- else if(wyjscie[i].equals("/"))
- wejscie[tmp_st - 2] = wejscie[tmp_st - 2] / wejscie[tmp_st - 1];
- tmp_st--;
- }
- }
- System.out.print("WYNIK: " + wejscie[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement