java jcp 테스트

분류없음 2018.04.10 22:24

package jcp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;

public class test01 {

 public void print(Object x) {
  System.out.print(x);
 }
 
 public void println(Object x) {
  System.out.println(x);
 }
 
 public void println() {
  System.out.println();
    }
 
 public void printf(String format, Object ... args) {
  System.out.printf(format,args);
    }
 
 /*public void printArray(String[][] args) {
  
  int i, j;
  int x, y;
  
  x = args.length;
  y = args[0].length;
  
  for(i = 0 ; i < x ; i++) {
   
   for(j = 0 ; j < y ; j++) {
    printf("%2d ",args[i][j]);
   }
  
   println();
  }
  
 }
 
 public void printArray(int[][] args) {
  
  int i, j;
  int x, y;
  
  x = args.length;
  y = args[0].length;
  
  for(i = 0 ; i < x ; i++) {
   
   for(j = 0 ; j < y ; j++) {
    printf("%2d ",args[i][j]);
   }
  
   println();
  }
  
 }*/
 
 public void printArray(int[][] arr)
    {
        for( int i = 0; i < arr.length; i++ )
        {
            for( int j = 0; j < arr[i].length; j++ ) {
             print(arr[i][j] + "\t");
            }
               
            println();
        }
        println();
    }
 
 public void printArray(Object[][] arr)
    {
        for( int i = 0; i < arr.length; i++ )
        {
            for( int j = 0; j < arr[i].length; j++ ) {
             print(arr[i][j] + "\t");
            }
               
            println();
        }
        println();
    }
    
 public void sortArray(Object[][] arr)
    {
  sortArray(arr, 1);
  
    }
 
 public void sortArray(Object[][] arr, final int compareIdx)
    {
        Arrays.sort(arr, new Comparator<Object[]>() {
            public int compare(Object[] arr1, Object[] arr2) {
             
             // 동일부서일 경우 오름차순
             if (arr1[compareIdx].equals(arr2[compareIdx])) {
              
              if( ((Comparable)arr1[0]).compareTo(arr2[0]) < 0 )
                        return -1;
                    else
                        return 1;
              
             } else {
             
              if( ((Comparable)arr1[compareIdx]).compareTo(arr2[compareIdx]) < 0 )
                        return -1;
                    else
                        return 1;
              
             }
               
            }
   
        });
  
    }

 
 public int[][] Array_Turn90(int[][] array) {
  
  int i, j;
  int k = 1;
  int x,y;
  
  x = array.length;
  y = array[0].length;
  
  /*println();
  println("# 원본");
  
  printArray(array);*/
  
  println();
  println("# 회전");
  
  int[][] array2 = new int[x][y];
  
  for(i = 0 ; i < x ; i++) {
   
   for(j = 0 ; j < y ; j++) {

    array2[i][j] = array[(x - 1) - j][i];
    
   }
  }
  
  printArray(array2);
  
  return array2;
 }
 
 /**
  * 배열 회전
  */
 
 public void test01() {
  
  
  int[][] array = new int[3][3];
  int arrayValue = 0;
  
  for (int i=0; i < array.length; i++) {
   
   for (int j=0; j < array[i].length; j++) {
    arrayValue++;
    array[i][j] = arrayValue;
   }
  }
  
  int[][] array2 = Array_Turn90(array);
  
  Array_Turn90(array2);
  
 }
 
 /**
  *  부서명 매핑 및 부서, 사번별 정렬
  */
 public void test02() {
  
  
  String[][] department = {
     {"10", "영업팀"}
    ,{"20", "구매팀"}
    ,{"30", "개발팀"}
    ,{"40", "운영팀"}
    };
  
  ArrayList arrayList = new ArrayList<>(Arrays.asList(department));
  
  Map map = new HashMap();
  for (int i=0; i < department.length; i++) {
   map.put(department[i][0], department[i][1]);
  }
  
  System.out.println(map.get("30"));
  
  String[][] employee = {
     {"59545", "최선명", "10"}
    ,{"23561", "박선주", "20"}
    ,{"37123", "김길동", "10"}
    ,{"33777", "이기주", "30"}
    };
  
//  sortArray(employee, 2);
  
  Arrays.sort(employee, new Comparator<Object[]>() {
            public int compare(Object[] arr1, Object[] arr2) {
             
             // 동일부서일 경우 오름차순
             if (arr1[2].equals(arr2[2])) {
              
              if( ((Comparable)arr1[0]).compareTo(arr2[0]) < 0 )
                        return -1;
                    else
                        return 1;
              
             } else {
             
              if( ((Comparable)arr1[2]).compareTo(arr2[2]) < 0 )
                        return -1;
                    else
                        return 1;
              
             }
               
            }
   
        });
  
  
  printArray(employee);
  
  for (int i=0; i < employee.length; i++) {
   println(map.get(employee[i][2]) +"," + employee[i][0] + "," + employee[i][1]);
  }
  
  
 }
 
 /**
  * Dense Ranking
  */
 public void test03() {
  
  String[][] studentPoint = {
     {"10210", "80"}
    ,{"10211", "77"}
    ,{"10212", "55"}
    ,{"10213", "66"}
    ,{"10214", "77"}
    ,{"10215", "99"}
    ,{"10216", "30"}
    ,{"10217", "10"}
    };
  
  
  Arrays.sort(studentPoint, new Comparator<Object[]>() {
           public int compare(Object[] arr1, Object[] arr2) {
            
            if( ((Comparable)arr1[1]).compareTo(arr2[1]) < 0 )
                   return 1;
               else
                   return -1;
           }
       });
  
  printArray(studentPoint);
 }
 
 /**
  * 달팽이 배열
  */
 public void test04() {
  
  int num,n,i,j,k;
  
  num = 10;
  
  int array[][]= new int[num][num];
       
        int element =1;
        j=0;
       
               
        for(n = num; n > 0; n -= 2) {
         
            for(i = 0;i < n;i++) {
                array[j][j+i] = element;
                element++;
            }
           
            for(k = 1;k < n; k++) {
                array[j+k][num-j-1] = element;
                element++;
            }
           
            for(i = 1;i < n;i++) {
                array[j+k-1][num-1-j-i] = element;
                element++;
            }
           
            for(k=1; k < n-1; k++){
                array[num-1-j-k][j]=element;
                element++;
            }
               
            j++;
        }
       
        // SHOW
       
        for(i=0;i<num;i++){
            for(j=0;j<num;j++){
                System.out.printf("%3d",array[i][j]);
            }
            System.out.println("");
        }


 }
 
 /**
  * 패킷 복원 (25분)
  */
 public void test05() {
  
//  String[] packets = {"1I_LOVE", "3G_CNS", "4CNS!!", "2VE_L"};
  String[] packets = {"bbb", "bbb", "aaa", "ccc"};
  
  if(packets  ==  null  ||  packets.length  ==  0)  {
   System.out.println("error");
  } else {
  
   LinkedHashSet<String>  hSet  =  new  LinkedHashSet<String>();

            for(String  str  :  packets){
             hSet.add(str);
            }
           
            /*Iterator<String>  it  =  hSet.iterator();
            while(it.hasNext()){
             //("Str  :  "  +  it.next());
             System.out.println(it.next());
            }*/
           
            String[] retArr =  (String [])hSet.toArray(new String[hSet.size()]);
           
            for (int i=0; i < retArr.length; i++) {
             System.out.println(retArr[i]);
            }
           
   
  }
     
     
  
 }
 
 public String initScriptValue(String evalStr) throws ScriptException {
  
  // JavaScript를 호출
  // JavaScript의 ScriptEngine을 구함
  ScriptEngineManager sem = new ScriptEngineManager();
  ScriptEngine se = sem.getEngineByName("JavaScript");
  
  return se.eval(evalStr).toString();

 }
  
 public void test06() {
  
  String str = "1*8+3+6+5/2*5-9";
//  String str = "1*8-3-6+5/2*6-9";
  String str2 = "";
  int i1= 0;
  
  try {
   
   for (int i=0; i < str.length(); i++) {
    
    if ("/".equals(str.charAt(i)+"")) {
     
//     System.out.println("--" + str.charAt(i-1) + str.charAt(i) + str.charAt(i+1));
     
     String a = initScriptValue("Math.floor("+str.charAt(i-1) + str.charAt(i) + str.charAt(i+1)+")");
     
     
//     System.out.println();
     str2 = str2.substring(0, str2.length()-1);
     str2 += "(" + a +")";
     i++;
    } else {
     str2 += str.charAt(i);
    }
   }
   
//   System.out.println(str2);
  
   // eval 함수로 평가. JavaScript를 실행
   int c = (int) Double.parseDouble((String)initScriptValue("Math.floor(" + str2 + ")"));
   
   System.out.println(c);
   
  } catch(Exception e) {
   e.printStackTrace();
  }
  
   
 }
 
 /**
  * Compressor
  */
 public void test07() {
  
  String str = "aaAaBbBBcDdddaaA";
//  String str = "abc";
  
  str = str.toLowerCase();
  
  boolean isChage = false;
  
  char[] charArr = str.toCharArray();
        char prevChar=' ';
        StringBuffer sb= new StringBuffer();
        int charArrLength = charArr.length;
        int dupCnt=1;

        for(int i=0; i<charArrLength; i++){
            if(prevChar==charArr[i]){
                dupCnt++;
            } else {
                if(i!=0){ // 중복값에 대한 처리
                    sb.append(prevChar);
                    sb.append(dupCnt);
                }
                dupCnt=1;
            }
            prevChar = charArr[i];
            if(i==charArrLength-1){  // 마지막 값에 대한 처리
                sb.append(prevChar);
                sb.append(dupCnt);
               
                if (dupCnt > 1) {
                 isChage = true;
                }
            }          
        }
       
        if (isChage) {
         System.out.println(sb.toString());
        } else {
         System.out.println(str);
        }
 }
 
 public void test08() {
  
  String str = "BAC";
  test08_01("", str);
 }
 
 public void test08_01(String prefix, String str) {
  
  int n = str.length();
     if (n == 0) System.out.println(prefix);
     else {
         for (int i = 0; i < n; i++)
          test08_01(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
     }
 }
 
 
 
 public static void main(String[] args) {
  
  test01 t01 = new test01();
  
//  t01.println("test");  
//  t01.print("test");
  
//  t01.test01();
//  t01.test02();
//  t01.test03();
//  t01.test04();
//  t01.test05();
//  t01.test06();
//  t01.test07();
  t01.test08();
  
  
  
 }
 
}

Posted by 은후아빠^^

안녕하세요 이문세의 별밤이 너무 그리워 검색하다가 들어와 글 남깁니다.

제 유년시절의 깃들어져 있어 너무 듣고 싶은데

별밤 녹음테이프 변환 진행중이신가요?

혹 공유하실 생각이 있으신지요? ^^

 

Posted by 은후아빠^^

sybtaxhighlight 적용

기타 2012.09.27 18:53

sybtaxhighlight 적용


 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'기타' 카테고리의 다른 글

sybtaxhighlight 적용  (0) 2012.09.27
Posted by 은후아빠^^