Advertisement
majczel23000

[Java] Calc with ONP

Mar 5th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 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.  
  48.         System.out.println("Wartosc wyrazenia w ONP: ");
  49.         for(int i = 0; i < stos.length; i++)
  50.         {
  51.             System.out.print(wyjscie[i] + " ");
  52.         }
  53.        
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.         // TODO Auto-generated method stub
  58.         //System.out.println(args.length);
  59.         //wypisz(args,args);
  60.         //Tablice
  61.         String[] stos = new String[args.length];
  62.         String[] wyjscie = new String[args.length];
  63.         int[] wejscie = new int[args.length];
  64.        
  65.         //przechowywanie indexu gdzie mozna wstawic element w tablicach stos i wyjscie
  66.         int tmp_st = 0;
  67.         int tmp_wy = 0;
  68.        
  69.         for(int i = 0; i < args.length; i++)
  70.         {
  71.             //sprawdzanie czy jest liczba
  72.             if(Character.isDigit(args[i].charAt(0)))
  73.             {
  74.                 wyjscie[tmp_wy] = args[i];
  75.                 tmp_wy++;
  76.             }
  77.             else    //jesli nie jest, to moze byc operatorem lub nawiasem
  78.             {
  79.                 if(args[i].equals("(")) //jesli nawias otwierajacy to po prostu na stos
  80.                 {
  81.                     stos[tmp_st] = args[i];
  82.                     tmp_st++;
  83.                 }
  84.                 else if(args[i].equals(")"))    //jesli zamykajacy to na wyjscie wszystko az do napotkania otwierajacego
  85.                 {
  86.                     while(!stos[tmp_st - 1].equals("("))
  87.                     {
  88.                         wyjscie[tmp_wy] = stos[tmp_st - 1];
  89.                         tmp_wy++;
  90.                         stos[tmp_st - 1] = "brak";
  91.                         tmp_st--;
  92.                     }
  93.                     stos[tmp_st - 1] = "brak";
  94.                     tmp_st--;
  95.                 }
  96.                 else    //sprawdzanie priorytetu
  97.                 {
  98.                     if(tmp_st>0)
  99.                     {
  100.                         if(!stos[tmp_st-1].equals("(") && sprawdz_priorytet(stos[tmp_st - 1], args[i]))
  101.                         {
  102.                             wyjscie[tmp_wy] = stos[tmp_st - 1];
  103.                             tmp_wy++;
  104.                             stos[tmp_st - 1] = args[i];
  105.                         }
  106.                         else
  107.                         {
  108.                             stos[tmp_st] = args[i];
  109.                             tmp_st++;
  110.                         }
  111.                     }
  112.                     else
  113.                     {
  114.                         stos[tmp_st] = args[i];
  115.                         tmp_st++;
  116.                     }
  117.                    
  118.                 }
  119.             }
  120.         }
  121.         for(;tmp_st !=0; tmp_st--)
  122.         {
  123.             wyjscie[tmp_wy] = stos[tmp_st - 1];
  124.             stos[tmp_st - 1] = "brak";
  125.             tmp_wy++;
  126.         }
  127.         //TUTAJ SKONCZYLO SIE WYPISYWANIE WYJSCIA, CZYLI POSTACI ONP
  128.         //TERAZ WEJSCIE I OBLICZENIE WARTOSCI WYRAZENIA
  129.         tmp_st = 0;
  130.         int k = tmp_wy;
  131.         tmp_wy = 0;
  132.         wypisz(stos,wyjscie);
  133.        
  134.         int tmp_we = 0;
  135.         for(int i = 0; i < k; i++)
  136.         {
  137.             //sprawdzanie czy jest liczba
  138.             if(Character.isDigit(wyjscie[i].charAt(0)))
  139.             {
  140.                 wejscie[tmp_st] = Integer.parseInt(wyjscie[i]);
  141.                 tmp_wy++;
  142.                 tmp_st++;
  143.             }
  144.             else    //znaczy ze operator
  145.             {
  146.                 if(wyjscie[i].equals("+"))
  147.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] +wejscie[tmp_st - 1];
  148.                 else if(wyjscie[i].equals("-"))
  149.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] - wejscie[tmp_st - 1];
  150.                 else if(wyjscie[i].equals("x"))
  151.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] * wejscie[tmp_st - 1];
  152.                 else if(wyjscie[i].equals("/"))
  153.                     wejscie[tmp_st - 2] = wejscie[tmp_st - 2] / wejscie[tmp_st - 1];
  154.                 tmp_st--;
  155.             }
  156.         }
  157.         System.out.print("WYNIK: " + wejscie[0]);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement