Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.42 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.db;
  2.  
  3. import java.util.Stack;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Mark {
  8.     String column[]={"name" , "age" , "email" ,"success"};
  9.     Class Types[]={String.class,int.class,String.class,boolean.class};
  10.     Object Data[]={"ahmed",15,"a@g.com",true};
  11.     private String Expression =" name = \"ahmed\" And age = 15 or email = \"sfsdafas\" And success";
  12.     private Boolean allowForData=true;
  13.     private Boolean allowForOperation=false;
  14.   //  Object operation[]={'(',5,'-',10,'=',-6,')'};
  15.     Object operation[]={'(','(','(',5,'+',7,'<',3,')',"or",'(','(',6,'-',3,')','*',3,'=',6,')',')',"and","not",'(',"not",'(',"not",'(',"not",'(',"ahmed",'=',"ahmed",')',')',')',')',')'};
  16.     char primaryOperation[]={'+','-','*','/'};
  17.     Stack OperationStack=new Stack();
  18.     Stack DataStack=new Stack();
  19.     Mark(){
  20.           //  Mark();
  21.             Calc();
  22.             System.out.println(DataStack.peek());
  23.     }
  24.  
  25.     private void Calc() {
  26.         for(int i=0;i<operation.length;i++){
  27.             Distibute(operation[i]);
  28.         }
  29.         DeathStack();
  30.     }
  31.  
  32.     private void Distibute(Object o)
  33.     {
  34.         if(PrimaryOperation(o)||comparitionOperation(o))
  35.         {
  36.             AppendOperation(o);
  37.         }else if(logicOperation(o)){
  38.             AppendLogic(o);
  39.         } else if(TRUEFALSE(o)){
  40.            RemoveNot((Boolean.parseBoolean(o.toString())));
  41.             allowForData=false;
  42.             allowForOperation=true;
  43.         }else if(o.getClass()==Character.class&&(char)o=='('){
  44.              if(allowForData){
  45.                  OperationStack.push(o);
  46.              }else throw new NumberFormatException();
  47.         }else if(o.getClass()==Character.class&&(char)o==')'){
  48.             DeathPrantetes();
  49.             allowForOperation=true;
  50.             allowForData=false;
  51.         }
  52.         else  {
  53.            if(allowForData)
  54.            {
  55.                DataStack.push(o);
  56.                allowForData=false;
  57.                allowForOperation=true;
  58.            }
  59.            else{
  60.                System.out.println(o);
  61.                throw new NumberFormatException();
  62.            }
  63.         }
  64.     }
  65.  
  66.     private void DeathPrantetes() {
  67.         while (!OperationStack.empty()&&(OperationStack.peek().getClass()!=Character.class||(char)OperationStack.peek()!='(')){
  68.             Object o=OperationStack.peek();
  69.             if(PrimaryOperation(o))RemovePrimaryOperation();
  70.             else if(comparitionOperation(o))RemoveComparition();
  71.             else  if(((String)o).equals("and"))RemoveAnd();
  72.             else if(((String)o).equals("or")){
  73.                 RemoveOR();
  74.             }
  75.             else  throw new NumberFormatException();
  76.  
  77.         }
  78.  
  79.         if((char)OperationStack.peek()!='('){
  80.             throw new NumberFormatException();
  81.         }
  82.          OperationStack.pop();
  83.          if(!OperationStack.empty()&&OperationStack.peek().equals("not")){
  84.              if(DataStack.peek().getClass()==Boolean.class)
  85.              RemoveNotDeath();
  86.              else throw new NumberFormatException();
  87.          }
  88.     }
  89.  
  90.     private boolean TRUEFALSE(Object o) {
  91.         if(o.getClass()!=String.class){
  92.             return false;
  93.         }
  94.             String str=(String)o;
  95.             return str.equalsIgnoreCase("true")||str.equalsIgnoreCase("false");
  96.     }
  97.  
  98.     private void AppendLogic(Object o) {
  99.         String str=(String)o;
  100.         if(str.equalsIgnoreCase("and")){
  101.             if(!allowForOperation)throw new NumberFormatException();
  102.             allowForOperation=false;
  103.             allowForData=true;
  104.             while (DataStack.peek().getClass()!=Boolean.class&&PrimaryOperation(OperationStack.peek())){
  105.                RemovePrimaryOperation();
  106.             }
  107.             RemoveComparition();
  108.             OperationStack.push("and");
  109.  
  110.         }else if (str.equalsIgnoreCase("or")){
  111.             if(!allowForOperation)throw new NumberFormatException();
  112.             allowForOperation=false;
  113.             allowForData=true;
  114.  
  115.             while (DataStack.peek().getClass()!=Boolean.class&&PrimaryOperation(OperationStack.peek())){
  116.                 RemovePrimaryOperation();
  117.             }
  118.             RemoveComparition();
  119.             while (!OperationStack.empty()&&OperationStack.peek().equals("and")){
  120.                     RemoveAnd();
  121.                 }
  122.             OperationStack.push("or");
  123.  
  124.         }
  125.         else {
  126.             allowForOperation=false;
  127.             allowForData=true;
  128.       //      if(!comparitionOperation(OperationStack.peek()))throw new NumberFormatException();
  129.             if(!OperationStack.empty()&&OperationStack.peek().equals("not")){
  130.                 OperationStack.pop();
  131.                 return;
  132.             }
  133.             OperationStack.push("not");
  134.         }
  135.     }
  136.  
  137.  
  138.  
  139.     private void RemoveComparition() {
  140.         if(DataStack.peek().getClass()==Boolean.class)return;
  141.         Object y=DataStack.pop();
  142.         Object x=DataStack.pop();
  143.         char C=(char)OperationStack.pop();
  144.         switch (C){
  145.             case '>':
  146.                 if(isString(y,x)){
  147.                     RemoveNot(((String)x).compareTo((String) y)>0);
  148.                 }else {
  149.                     RemoveNot((int)x>(int)y);
  150.                 }
  151.                 return;
  152.             case '<':
  153.                 if(isString(y,x)){
  154.                     RemoveNot(((String)x).compareTo((String) y)<0);
  155.                 }else {
  156.                     RemoveNot((int)x<(int)y);
  157.                 }
  158.                 return;
  159.             case '=':
  160.                 if(isString(y,x)){
  161.                     RemoveNot(((String)x).compareTo((String) y)==0);
  162.                 }else {
  163.                     RemoveNot((int)x==(int)y);
  164.                 }
  165.                 return;
  166.  
  167.         }
  168.     }
  169.     private void RemoveAnd() {
  170.         boolean x=(boolean)DataStack.pop();
  171.         boolean y=(boolean)DataStack.pop();
  172.         OperationStack.pop();
  173.         RemoveNot(x&&y);
  174.     }
  175.  
  176.     private void RemoveNot(boolean b){
  177.  
  178.         if(!OperationStack.empty()&&OperationStack.peek().equals("not")){
  179.             OperationStack.pop();
  180.             DataStack.push(!b);
  181.             return;
  182.         }
  183.         DataStack.push(b);
  184.     }
  185.     private void RemoveOR() {
  186.         boolean x=(boolean)DataStack.pop();
  187.         boolean y=(boolean)DataStack.pop();
  188.         OperationStack.pop();
  189.        RemoveNot(x||y);
  190.     }
  191.  
  192.     boolean isString(Object o , Object o1){
  193.         if(o.getClass()==String.class){
  194.             if(o1.getClass()==String.class){
  195.                 return true;
  196.             }
  197.             throw new NumberFormatException();
  198.         }
  199.         if(o.getClass()==Integer.class){
  200.             if(o1.getClass()==Integer.class){
  201.                 return false;
  202.             }
  203.             throw new NumberFormatException();
  204.         }
  205.         throw new NumberFormatException();
  206.     }
  207.  
  208.     private boolean logicOperation(Object o) {
  209.         return (o.getClass()==String.class&&(((String)o).toLowerCase().equals("and")||
  210.                 ((String)o).toLowerCase().equals("or")||
  211.                 ((String)o).toLowerCase().equals("not")
  212.                 ));
  213.     }
  214.  
  215.     private boolean comparitionOperation(Object o) {
  216.         if(o.getClass()!=Character.class)return false;
  217.         if((char)o=='>'||(char)o=='<'||(char)o=='=')return true;
  218.         return false;
  219.     }
  220.  
  221.     private void AppendOperation( Object object) {
  222.         if(!allowForOperation)throw new NumberFormatException();
  223.         allowForOperation=false;
  224.         allowForData=true;
  225.  
  226.         if(OperationStack.empty()||!PrimaryOperation(OperationStack.peek())){
  227.             OperationStack.push(object);
  228.             return;
  229.         }
  230.  
  231.         if(((char)object=='*'||(char)object=='/'))
  232.              while (!OperationStack.empty()&&((char)OperationStack.peek()=='*'||(char)OperationStack.peek()=='/'))
  233.              RemovePrimaryOperation();
  234.         else if((char)object=='+'||(char)object=='-'||(char)object=='>'||(char)object=='<'||(char)object=='='){
  235.             while (!OperationStack.empty()&&PrimaryOperation(OperationStack.peek()))
  236.             RemovePrimaryOperation();
  237.         }
  238.         OperationStack.push(object);
  239.     }
  240.  
  241.     private void RemovePrimaryOperation() {
  242.         int y=(int)DataStack.peek();
  243.         DataStack.pop();
  244.         int x=(int)DataStack.peek();
  245.         DataStack.pop();
  246.         char c =(char)OperationStack.peek();
  247.         OperationStack.pop();
  248.         switch (c){
  249.             case '+':
  250.                 DataStack.push(x+y);
  251.                 return;
  252.             case '-':
  253.                 DataStack.push(x-y);
  254.                 return;
  255.             case '*':
  256.                 DataStack.push(x*y);
  257.                 return;
  258.             case '/':
  259.                 DataStack.push(x/y);
  260.                 return;
  261.         }
  262.  
  263.     }
  264.     boolean PrimaryOperation(Object object)
  265.     {
  266.  
  267.         for(char c : primaryOperation) {
  268.             try {
  269.                 if (c == (char) object) return true;
  270.             }catch (Exception e){}
  271.         }
  272.         return false;
  273.  
  274.     }
  275.     void DeathStack(){
  276.         while (!OperationStack.empty()){
  277.             Object o=OperationStack.peek();
  278.             if(PrimaryOperation(OperationStack.peek()))RemovePrimaryOperation();
  279.             else if(comparitionOperation(o))RemoveComparition();
  280.             else  if((OperationStack.peek()).equals("and"))RemoveAnd();
  281.             else if((OperationStack.peek()).equals("or"))RemoveOR();
  282.             else if((OperationStack.peek()).equals("not"))RemoveNotDeath();
  283.             else throw new NumberFormatException();
  284.         }
  285.         if(DataStack.empty()||DataStack.peek().getClass()!=Boolean.class)
  286.             throw new NumberFormatException();
  287.     }
  288.  
  289.     private void RemoveNotDeath() {
  290.         Boolean b=(boolean)DataStack.pop();
  291.         OperationStack.pop();
  292.         DataStack.push(!b);
  293.     }
  294.  
  295.  
  296.     void Mark()
  297.     {
  298.         System.out.println(Expression);
  299.         for(int i=0;i<column.length;i++)
  300.         {
  301.             if(Types[i]==String.class){
  302.                 Replace(" "+column[i],"\""+Data[i]+"\"");
  303.             }else {
  304.                 Replace(" "+column[i],String.valueOf(Data[i]));
  305.             }
  306.         }
  307.         System.out.println(Expression);
  308.     }
  309.     private void Replace(String ReplaceIt,String ReplaceWith)
  310.     {
  311.         Pattern pattern=Pattern.compile(ReplaceIt);
  312.         Matcher matcher=pattern.matcher(Expression);
  313.         while (matcher.find())
  314.         {
  315.             int begin=matcher.start();
  316.             int end=matcher.end();
  317.             boolean FindBefore=before(begin);
  318.             boolean FindAfter=after(end);
  319.             boolean bound=false;
  320.             if(end== Expression.length()|| Expression.charAt(end)==' ')
  321.             {
  322.                 bound=true;
  323.             }
  324.             if(FindBefore&&FindAfter&&bound)
  325.             {
  326.                 Expression = new StringBuilder(Expression).replace(begin+1,end,ReplaceWith).toString();
  327.             }
  328.         }
  329.     }
  330.  
  331.     private boolean after(int x) {
  332.         for(int i = x; i< Expression.length(); i++){
  333.             if(Expression.charAt(i)==' ')continue;
  334.             if(Expression.charAt(i)=='\"')return false;
  335.             return true;
  336.         }
  337.         return true;
  338.     }
  339.  
  340.     boolean before(int x){
  341.            for(int i=x-1;i>=0;i--){
  342.                if(Expression.charAt(i)==' ')continue;
  343.                if(Expression.charAt(i)=='\"')return false;
  344.                return true;
  345.            }
  346.         return true;
  347.     }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement