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.print("W ONP: ");
- for(int i = 0; i < stos.length; i++)
- if(wyjscie[i] != null)
- System.out.print(wyjscie[i] + " ");
- }
- public static void main(String[] args) {
- //dlugosc dzialania
- int dlugosc = 0;
- while(args[0].charAt(dlugosc) != '=')
- dlugosc++;
- //Wypisanie dzialania
- System.out.print("Działanie: ");
- for(int i = 0; i < dlugosc; i++)
- System.out.print(args[0].charAt(i));
- System.out.println("");
- //Tablice stosu wejscia i wyjscia
- String[] stos = new String[dlugosc];
- String[] wyjscie = new String[dlugosc];
- double[] wejscie = new double[dlugosc];
- //przechowywanie indexu gdzie mozna wstawic element w tablicach stos i wyjscie
- int tmp_st = 0;
- int tmp_wy = 0;
- for(int i = 0; i < dlugosc; )
- {
- //sprawdzanie czy jest liczba
- if(Character.isDigit(args[0].charAt(i)))
- {
- wyjscie[tmp_wy] = Character.toString(args[0].charAt(i));
- i++;
- //szukanie wielocyfrowych oraz liczb typu double
- while(i<dlugosc && (Character.isDigit(args[0].charAt(i)) || args[0].charAt(i) == '.'))
- {
- wyjscie[tmp_wy] += args[0].charAt(i);
- i++;
- }
- tmp_wy++;
- }
- else //jesli nie jest, to moze byc operatorem lub nawiasem
- {
- if(args[0].charAt(i) == '(') //jesli nawias otwierajacy to po prostu na stos
- {
- stos[tmp_st] = Character.toString(args[0].charAt(i));
- tmp_st++;
- }
- else if(args[0].charAt(i) == ')') //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], Character.toString(args[0].charAt(i))))
- {
- wyjscie[tmp_wy] = stos[tmp_st - 1];
- tmp_wy++;
- stos[tmp_st - 1] = Character.toString(args[0].charAt(i));
- }
- else
- {
- stos[tmp_st] = Character.toString(args[0].charAt(i));
- tmp_st++;
- }
- }
- else
- {
- stos[tmp_st] = Character.toString(args[0].charAt(i));
- tmp_st++;
- }
- }
- i++;
- }
- }
- //przepisanie na wyjscie stosu
- for(;tmp_st !=0; tmp_st--)
- {
- wyjscie[tmp_wy] = stos[tmp_st - 1];
- stos[tmp_st - 1] = "brak";
- tmp_wy++;
- }
- //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] = Double.parseDouble(wyjscie[i]);
- tmp_wy++;
- tmp_st++;
- }
- else //znaczy ze operator, czyli wykonanie dzialania
- {
- 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.println("");
- System.out.print("WYNIK: " + wejscie[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement