Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package lab3;
  2.  
  3. import java.math.*;
  4.  
  5. public class onp {
  6. static  int top=0;
  7. static int m2=0;
  8.  
  9. static String stos2[]=new String[20];
  10.     public static int push (String stos[],String dana)
  11.     {
  12.         if(top>20)
  13.         {
  14.             System.out.println("Brak miejsca");
  15.             return 0;
  16.         }
  17.         else
  18.         {
  19.             stos[top]=dana;
  20.             top++;
  21.            
  22.             return 1;
  23.         }
  24.     };
  25.    
  26.     public static int pop (String stos[])
  27.     {
  28.        
  29.         if(top==0)
  30.         {
  31.             System.out.println();
  32.             return 0;
  33.         }
  34.        
  35.         else
  36.         {
  37.             if(stos[top-1].equals("(")==true)
  38.             {top--;
  39.             }
  40.             else
  41.             {System.out.print(stos[top-1]);
  42.             stos2[m2]=stos[top-1];
  43.             m2++;
  44.             top--;}
  45.             return 1;
  46.         }
  47.        
  48.        
  49.        
  50.     };
  51.    
  52.     public static void wynik (String stos[])
  53.     { int s=0; double x; int m=0;
  54.         while(stos2[s]!=null)
  55.         {
  56.             if(stos2[s].equals("^")==true)
  57.             {
  58.                 //System.out.println(m);
  59.                 x=Math.pow(Double.parseDouble(stos[m-2]), Double.parseDouble(stos[m-1]));
  60.                 stos[m-2]=Double.toString(x);
  61.                 m--;
  62.             }
  63.             else
  64.             {
  65.                 if(stos2[s].equals("x")==true)
  66.                 {
  67.                     x=Double.parseDouble(stos[m-1])*Double.parseDouble(stos[m-2]);
  68.                     stos[m-2]=Double.toString(x);
  69.                     m--;
  70.                        
  71.                 }
  72.                 else
  73.                 {
  74.                     if(stos2[s].equals("/")==true)
  75.                     {
  76.                         x=Double.parseDouble(stos[m-2])/Double.parseDouble(stos[m-1]);
  77.                         stos[m-2]=Double.toString(x);
  78.                         m--;
  79.                     }
  80.                     else
  81.                     {
  82.                         if(stos2[s].equals("+")==true)
  83.                         {
  84.                             x=Double.parseDouble(stos[m-2])+Double.parseDouble(stos[m-1]);
  85.                             stos[m-2]=Double.toString(x);
  86.                             m--;
  87.                         }
  88.                         else
  89.                         {
  90.                             if(stos2[s].equals("-")==true)
  91.                             {
  92.                                 x=Double.parseDouble(stos[m-2])-Double.parseDouble(stos[m-1]);
  93.                                 stos[m-2]=Double.toString(x);
  94.                                 m--;
  95.                             }
  96.                             else
  97.                             { //System.out.println(m);
  98.                                 stos[m]=stos2[s];
  99.                                
  100.                                 m++;
  101.                             }
  102.                         }
  103.                     }
  104.                 }
  105.             }
  106.            
  107.            
  108.             s++;
  109.         }
  110.         System.out.println(stos[0]);
  111.     }
  112.    
  113.     public static void main(String[] args) {
  114.         // TODO Auto-generated method stub
  115.    
  116.         int n = 0;
  117.         int i;
  118.         String Stos[]=new String[20];
  119.         while(n < args.length) {   
  120.             String rownanie = args[n];
  121.             i=0;
  122.         while(rownanie.charAt(i) != '=') { 
  123.                    
  124.             if(rownanie.charAt(i) == '(')
  125.             {
  126.                 push(Stos,"(");
  127.             }
  128.             else
  129.                 {
  130.                 if(rownanie.charAt(i) == ')')
  131.                         {
  132.                         while(Stos[top-1].equals("(")==false)
  133.                         {
  134.                             pop(Stos);
  135.                         }
  136.                             top--;
  137.                         }
  138.                 else
  139.                 {
  140.                     if(rownanie.charAt(i) == 'x' || rownanie.charAt(i) == '/')
  141.                     {
  142.                         while(top!=0 &&(Stos[top-1].equals("x")==true || Stos[top-1].equals("/")==true || Stos[top-1].equals("^")==true))
  143.                         {
  144.                             pop(Stos);
  145.                            
  146.                         }
  147.                        
  148.                             push(Stos,String.valueOf(rownanie.charAt(i)));
  149.                        
  150.                     }
  151.                     else
  152.                     {
  153.                         if(rownanie.charAt(i) == '+' || rownanie.charAt(i) == '=')
  154.                         {
  155.                             while(top!=0 && (Stos[top-1].equals("x")==true || Stos[top-1].equals("/")==true || Stos[top-1].equals("+")==true || Stos[top-1].equals("-")==true||Stos[top-1].equals("^")==true))
  156.                                 {
  157.                                 pop(Stos);
  158.                            
  159.                                 }
  160.                             push(Stos,String.valueOf(rownanie.charAt(i)));
  161.                         }
  162.                         else
  163.                         {
  164.                             if(rownanie.charAt(i) == '^')
  165.                             {
  166.                                 while(top!=0 && Stos[top-1].equals("^")==true)
  167.                                 {
  168.                                     pop(Stos);
  169.                                 }
  170.                                 push(Stos,String.valueOf(rownanie.charAt(i)));
  171.                             }
  172.                             else
  173.                             {
  174.                             System.out.print(rownanie.charAt(i));
  175.                             stos2[m2]=String.valueOf(rownanie.charAt(i));
  176.                             m2++;
  177.                             }
  178.                         }
  179.                     }
  180.                 }
  181.                 }
  182.            
  183.             i++;
  184.        
  185.         }
  186.         n++;
  187.         while(pop(Stos)==1)
  188.         {
  189.            
  190.         }
  191.         int q=0;
  192.         while(q<15)
  193.         {
  194.             //System.out.print(stos2[q]);
  195.             q++;
  196.         }
  197.         wynik(Stos);
  198.             }
  199.  
  200.         }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement