Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab3;
- import java.math.*;
- public class onp {
- static int top=0;
- static int m2=0;
- static String stos2[]=new String[20];
- public static int push (String stos[],String dana)
- {
- if(top>20)
- {
- System.out.println("Brak miejsca");
- return 0;
- }
- else
- {
- stos[top]=dana;
- top++;
- return 1;
- }
- };
- public static int pop (String stos[])
- {
- if(top==0)
- {
- System.out.println();
- return 0;
- }
- else
- {
- if(stos[top-1].equals("(")==true)
- {top--;
- }
- else
- {System.out.print(stos[top-1]);
- stos2[m2]=stos[top-1];
- m2++;
- top--;}
- return 1;
- }
- };
- public static void wynik (String stos[])
- { int s=0; double x; int m=0;
- while(stos2[s]!=null)
- {
- if(stos2[s].equals("^")==true)
- {
- //System.out.println(m);
- x=Math.pow(Double.parseDouble(stos[m-2]), Double.parseDouble(stos[m-1]));
- stos[m-2]=Double.toString(x);
- m--;
- }
- else
- {
- if(stos2[s].equals("x")==true)
- {
- x=Double.parseDouble(stos[m-1])*Double.parseDouble(stos[m-2]);
- stos[m-2]=Double.toString(x);
- m--;
- }
- else
- {
- if(stos2[s].equals("/")==true)
- {
- x=Double.parseDouble(stos[m-2])/Double.parseDouble(stos[m-1]);
- stos[m-2]=Double.toString(x);
- m--;
- }
- else
- {
- if(stos2[s].equals("+")==true)
- {
- x=Double.parseDouble(stos[m-2])+Double.parseDouble(stos[m-1]);
- stos[m-2]=Double.toString(x);
- m--;
- }
- else
- {
- if(stos2[s].equals("-")==true)
- {
- x=Double.parseDouble(stos[m-2])-Double.parseDouble(stos[m-1]);
- stos[m-2]=Double.toString(x);
- m--;
- }
- else
- { //System.out.println(m);
- stos[m]=stos2[s];
- m++;
- }
- }
- }
- }
- }
- s++;
- }
- System.out.println(stos[0]);
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int n = 0;
- int i;
- String Stos[]=new String[20];
- while(n < args.length) {
- String rownanie = args[n];
- i=0;
- while(rownanie.charAt(i) != '=') {
- if(rownanie.charAt(i) == '(')
- {
- push(Stos,"(");
- }
- else
- {
- if(rownanie.charAt(i) == ')')
- {
- while(Stos[top-1].equals("(")==false)
- {
- pop(Stos);
- }
- top--;
- }
- else
- {
- if(rownanie.charAt(i) == 'x' || rownanie.charAt(i) == '/')
- {
- while(top!=0 &&(Stos[top-1].equals("x")==true || Stos[top-1].equals("/")==true || Stos[top-1].equals("^")==true))
- {
- pop(Stos);
- }
- push(Stos,String.valueOf(rownanie.charAt(i)));
- }
- else
- {
- if(rownanie.charAt(i) == '+' || rownanie.charAt(i) == '=')
- {
- 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))
- {
- pop(Stos);
- }
- push(Stos,String.valueOf(rownanie.charAt(i)));
- }
- else
- {
- if(rownanie.charAt(i) == '^')
- {
- while(top!=0 && Stos[top-1].equals("^")==true)
- {
- pop(Stos);
- }
- push(Stos,String.valueOf(rownanie.charAt(i)));
- }
- else
- {
- System.out.print(rownanie.charAt(i));
- stos2[m2]=String.valueOf(rownanie.charAt(i));
- m2++;
- }
- }
- }
- }
- }
- i++;
- }
- n++;
- while(pop(Stos)==1)
- {
- }
- int q=0;
- while(q<15)
- {
- //System.out.print(stos2[q]);
- q++;
- }
- wynik(Stos);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement