Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. import java.awt.TextArea;
  2. import java.util.LinkedList;
  3.  
  4. import javax.swing.JTextField;
  5.  
  6. public class Input {
  7.    
  8.  
  9.     public static void trajtoInputin(JTextField input, TextArea log, boolean buli){
  10.        
  11.         String str = input.getText(); // marrim inputin nga perdoruesi
  12.         str = str.replaceAll("\\s+",""); // heqim hapesirat
  13.         int pozBaraz = str.indexOf("="); // pozicioni i shenjes se barazimit
  14.         if(pozBaraz != -1) {
  15.             String f1 = str.substring(0,pozBaraz); // funksioni i majte
  16.             String f2 = str.substring(pozBaraz+1, str.length()); // funksioni i djathte
  17.            
  18.             if (!f1.matches(".*[^A-Za-z+!()01].*") && !f2.matches(".*[^A-Za-z+!()01].*") ) {
  19.                 // kontrollo te dy funksionet nese kane karaktere te lejueshme
  20.                 //      A-Z     a-z     +   !   (   )   0   1
  21.                
  22.                 log.append(kontrolloSintaksen(f1)+" "+ buli +" " +kontrolloSintaksen(f2));//TO DO
  23.                
  24.             }
  25.             else {
  26.                 log.append("\n\nINPUT ERROR\n\n");
  27.             }
  28.            
  29.         }
  30.         else {
  31.             log.append("\n\nINPUT ERROR\n\n");         
  32.         }
  33.        
  34.        
  35.     }
  36.    
  37.     public static String kontrolloSintaksen(String str) {
  38.        
  39.        
  40.         //Do kontrollojme per karakterin '+'
  41.        
  42.         LinkedList<Integer> list = new LinkedList<Integer>();
  43.         int temp;
  44.         char character = '+';
  45.         for(int i = 0; i < str.length(); i++){
  46.             if(str.charAt(i) == character){
  47.                list.add(i);
  48.             }
  49.         }
  50.        
  51.         if(list.size() < 4) { // Nese ka max 3 karaktere '+'    dmth a + b + c + d      =>  4 terma
  52.            
  53.             for(int j = 0; j < list.size(); j++) {
  54.                
  55.                 temp = list.get(j);
  56.                
  57.                 if(temp == 0 || temp == str.length()-1 ) { // nese karakteri '+' ndodhet ne fillim ose ne fund
  58.                     return "\n\nINPUT ERROR\n\n";                              
  59.                 }
  60.                 else {
  61.                    
  62.                     if(str.charAt(temp-1) == '+' || str.charAt(temp-1) == '(' || str.charAt(temp-1) == '!') {
  63.                         //nese karakteri perpara '+' eshte '+' ose '(' ose '!'
  64.                         return "\n\nINPUT ERROR\n\n";                              
  65.                     }
  66.                     else {
  67.                        
  68.                         if(str.charAt(temp+1) == '+' || str.charAt(temp+1) == ')') {
  69.                             //nese karakteri pas '+' eshte '+' ose ')'
  70.                             return "\n\nINPUT ERROR\n\n";                              
  71.                         }
  72.                         //else vazhdon kodi me poshte
  73.                        
  74.                     }
  75.                    
  76.                 }
  77.                
  78.             }
  79.            
  80.         }
  81.         else {
  82.             return "\n\nINPUT ERROR\n\n";                              
  83.         }
  84.        
  85.        
  86.        
  87.         //Do kontrollojme per karakteret '(' dhe ')'
  88.        
  89.         list = new LinkedList<Integer>();//pozicionet e kllapave '('
  90.         LinkedList<Integer> pozicioneKllapash = new LinkedList<Integer>();//pozicionet e cifteve te kllapave
  91.         char kllapM = '(';
  92.         char kllapD = ')';
  93.         for(int i = 0; i < str.length(); i++){
  94.             if(str.charAt(i) == kllapD){
  95.                
  96.                 if(list.size() == 0){
  97.                     return "\n\nINPUT ERROR\n\n";//ka nje kllape ')' teper                 
  98.                 }
  99.                 else {
  100.                    
  101.                     pozicioneKllapash.add(list.removeLast());//hiq kllapen '(' nga lista dhe shtoje te tjetra
  102.                     pozicioneKllapash.add(i);// shto kllapen ')' te lista
  103.                    
  104.                 }
  105.                
  106.             }
  107.             else if(str.charAt(i) == kllapM){
  108.                 list.add(i);
  109.             }
  110.        
  111.         }
  112.        
  113.         for(int i = 0; i< pozicioneKllapash.size(); i+=2) {//kontrollo nqs ka vtm nje karakter ose asnje midis kllapave, nqs po return "error"
  114.            
  115.             if(pozicioneKllapash.get(i) + 1 == pozicioneKllapash.get(i+1) || pozicioneKllapash.get(i) + 2 == pozicioneKllapash.get(i+1)){
  116.                 return "\n\nINPUT ERROR\n\n";                              
  117.             }
  118.            
  119.         }
  120.        
  121.        
  122.         //minimizojme '!' qe jane me shume se nje here
  123.        
  124.        
  125.             //TO DO
  126.        
  127.        
  128.        
  129.         // Duhet te hiqen kllapat para se te behet return
  130.        
  131.        
  132.        
  133.             //TO DO
  134.        
  135.        
  136.         return str;// funksioni i formatuar
  137.        
  138.        
  139.        
  140.     }
  141.        
  142.    
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement