Advertisement
majczel23000

[Java] Calc with ONP v2

Mar 24th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package kalkulatorONP;
  2.  
  3. public class Calc {
  4.    
  5.     public static boolean sprawdz_priorytet(String stos, String args)
  6.     {
  7.         int p1 = 0, p2 = 0;
  8.         switch(stos)
  9.         {
  10.             case "+":
  11.                 p1 = 1;
  12.                 break;
  13.             case "-":
  14.                 p1 = 1;
  15.                 break;
  16.             case "x":
  17.                 p1 = 2;
  18.                 break;
  19.             case"/":
  20.                 p1 = 2;
  21.                 break;
  22.             default:
  23.                 break;
  24.         }
  25.         switch(args)
  26.         {
  27.             case "+":
  28.                 p2 = 1;
  29.                 break;
  30.             case "-":
  31.                 p2 = 1;
  32.                 break;
  33.             case "x":
  34.                 p2 = 2;
  35.                 break;
  36.             case"/":
  37.                 p2 = 2;
  38.                 break;
  39.             default:
  40.                 break;
  41.         }
  42.         return (p1 >= p2);
  43.     }
  44.    
  45.     public static void wypisz(String[] stos, String[] wyjscie)
  46.     {
  47.         System.out.print("W ONP: ");
  48.         for(int i = 0; i < stos.length; i++)
  49.             if(wyjscie[i] != null)
  50.                 System.out.print(wyjscie[i] + " ");
  51.     }
  52.  
  53.     public static void main(String[] args) {
  54.         //dlugosc dzialania
  55.         int dlugosc = 0;
  56.         while(args[0].charAt(dlugosc) != '=')
  57.             dlugosc++;
  58.         //Wypisanie dzialania
  59.         System.out.print("Działanie: ");
  60.         for(int i = 0; i < dlugosc; i++)
  61.             System.out.print(args[0].charAt(i));
  62.         System.out.println("");
  63.        
  64.         //Tablice stosu wejscia i wyjscia
  65.         String[] stos = new String[dlugosc];
  66.         String[] wyjscie = new String[dlugosc];
  67.         double[] wejscie = new double[dlugosc];
  68.        
  69.         //przechowywanie indexu gdzie mozna wstawic element w tablicach stos i wyjscie
  70.         int tmp_st = 0;
  71.         int tmp_wy = 0;
  72.        
  73.         for(int i = 0; i < dlugosc; )
  74.         {
  75.             //sprawdzanie czy jest liczba
  76.             if(Character.isDigit(args[0].charAt(i)))
  77.             {
  78.                 wyjscie[tmp_wy] = Character.toString(args[0].charAt(i));
  79.                 i++;
  80.                 //szukanie wielocyfrowych oraz liczb typu double
  81.                 while(i<dlugosc && (Character.isDigit(args[0].charAt(i)) || args[0].charAt(i) == '.'))
  82.                 {
  83.                     wyjscie[tmp_wy] += args[0].charAt(i);
  84.                     i++;
  85.                 }
  86.                 tmp_wy++;
  87.             }
  88.             else    //jesli nie jest, to moze byc operatorem lub nawiasem
  89.             {
  90.                 if(args[0].charAt(i) == '(')    //jesli nawias otwierajacy to po prostu na stos
  91.                 {
  92.                     stos[tmp_st] = Character.toString(args[0].charAt(i));
  93.                     tmp_st++;
  94.                 }
  95.                 else if(args[0].charAt(i) == ')')   //jesli zamykajacy to na wyjscie wszystko az do napotkania otwierajacego
  96.                 {
  97.                     while(!stos[tmp_st - 1].equals("("))
  98.                     {
  99.                         wyjscie[tmp_wy] = stos[tmp_st - 1];
  100.                         tmp_wy++;
  101.                         stos[tmp_st - 1] = "brak";
  102.                         tmp_st--;
  103.                     }
  104.                     stos[tmp_st - 1] = "brak";
  105.                     tmp_st--;
  106.                 }
  107.                 else    //sprawdzanie priorytetu
  108.                 {
  109.                     if(tmp_st>0)
  110.                     {
  111.                         if(!stos[tmp_st-1].equals("(") && sprawdz_priorytet(stos[tmp_st - 1], Character.toString(args[0].charAt(i))))
  112.                         {
  113.                             wyjscie[tmp_wy] = stos[tmp_st - 1];
  114.                             tmp_wy++;
  115.                             stos[tmp_st - 1] = Character.toString(args[0].charAt(i));
  116.                         }
  117.                         else
  118.                         {
  119.                             stos[tmp_st] = Character.toString(args[0].charAt(i));
  120.                             tmp_st++;
  121.                         }
  122.                     }
  123.                     else
  124.                     {
  125.                         stos[tmp_st] = Character.toString(args[0].charAt(i));
  126.                         tmp_st++;
  127.                     }
  128.                 }
  129.                 i++;
  130.             }
  131.         }
  132.         //przepisanie na wyjscie stosu
  133.         for(;tmp_st !=0; tmp_st--)
  134.         {
  135.             wyjscie[tmp_wy] = stos[tmp_st - 1];
  136.             stos[tmp_st - 1] = "brak";
  137.             tmp_wy++;
  138.         }
  139.        
  140.         //TERAZ WEJSCIE I OBLICZENIE WARTOSCI WYRAZENIA
  141.         tmp_st = 0;
  142.         int k = tmp_wy;
  143.         tmp_wy = 0;
  144.         wypisz(stos,wyjscie);
  145.        
  146.         int tmp_we = 0;
  147.         for(int i = 0; i < k; i++)
  148.         {
  149.             //sprawdzanie czy jest liczba
  150.             if(Character.isDigit(wyjscie[i].charAt(0)))
  151.             {
  152.                 wejscie[tmp_st] = Double.parseDouble(wyjscie[i]);
  153.                 tmp_wy++;
  154.                 tmp_st++;
  155.             }
  156.             else    //znaczy ze operator, czyli wykonanie dzialania
  157.             {
  158.                 if(wyjscie[i].equals("+"))
  159.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] + wejscie[tmp_st - 1];
  160.                 else if(wyjscie[i].equals("-"))
  161.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] - wejscie[tmp_st - 1];
  162.                 else if(wyjscie[i].equals("x"))
  163.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] * wejscie[tmp_st - 1];
  164.                 else if(wyjscie[i].equals("/"))
  165.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] / wejscie[tmp_st - 1];
  166.                 tmp_st--;
  167.             }
  168.         }
  169.         System.out.println("");
  170.         System.out.print("WYNIK: " + wejscie[0]);
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement