data = rbind(c("가", "나", 1),c("가", "다", 3),c("가", "라", 2),c("나", "다", 3),c("나", "라", 1),c("다", "라", 2))

uniq = sort(unique(c(data[,1], data[,2])))

res_mat = matrix(0, length(uniq), length(uniq))
dimnames(res_mat)[[1]] = uniq
dimnames(res_mat)[[2]] = uniq

for(i in 1:dim(data)[[1]])
{
    res_mat[which(dimnames(res_mat)[[1]] == data[i,1]), which(dimnames(res_mat)[[2]] == data[i,2])] = data[i,3]
}

res_mat = t(res_mat)
res_mat = data.frame(res_mat)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jaeseong
 */

/******************************************************************************
 *  Compilation:  javac Polynomial.java
 *  Execution:    java Polynomial
 *
 *  Polynomials with integer coefficients.
 *
 *  % java Polynomial
 *  zero(x) =     0
 *  p(x) =        4x^3 + 3x^2 + 2x + 1
 *  q(x) =        3x^2 + 5
 *  p(x) + q(x) = 4x^3 + 6x^2 + 2x + 6
 *  p(x) * q(x) = 12x^5 + 9x^4 + 26x^3 + 18x^2 + 10x + 5
 *  p(q(x))     = 108x^6 + 567x^4 + 996x^2 + 586
 *  0 - p(x)    = -4x^3 - 3x^2 - 2x - 1
 *  p(3)        = 142
 *  p'(x)       = 12x^2 + 6x + 2
 *  p''(x)      = 24x + 6
 *
 ******************************************************************************/

public class Polynomial {
    private int[] coef;  // coefficients
    private int deg;     // degree of polynomial (0 for the zero polynomial)
   
    // a * x^b
    public Polynomial(int a, int b) {
        coef = new int[b+1];
        coef[b] = a;
        deg = degree();
    }

    Polynomial() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    // return the degree of this polynomial (0 for the zero polynomial)
    public int degree() {
        int d = 0;
        for (int i = 0; i < coef.length; i++)
            if (coef[i] != 0) d = i;
        return d;
    }

    // return c = a + b
    public Polynomial plus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // return (a - b)
    public Polynomial minus(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
        for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
        for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
        c.deg = c.degree();
        return c;
    }

    // return (a * b)
    public Polynomial times(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, a.deg + b.deg);
        for (int i = 0; i <= a.deg; i++)
            for (int j = 0; j <= b.deg; j++)
                c.coef[i+j] += (a.coef[i] * b.coef[j]);
        c.deg = c.degree();
        return c;
    }

    // return a(b(x))  - compute using Horner's method
    public Polynomial compose(Polynomial b) {
        Polynomial a = this;
        Polynomial c = new Polynomial(0, 0);
        for (int i = a.deg; i >= 0; i--) {
            Polynomial term = new Polynomial(a.coef[i], 0);
            c = term.plus(b.times(c));
        }
        return c;
    }


    // do a and b represent the same polynomial?
    public boolean eq(Polynomial b) {
        Polynomial a = this;
        if (a.deg != b.deg) return false;
        for (int i = a.deg; i >= 0; i--)
            if (a.coef[i] != b.coef[i]) return false;
        return true;
    }


    // use Horner's method to compute and return the polynomial evaluated at x
    public int evaluate(int x) {
        int p = 0;
        for (int i = deg; i >= 0; i--)
            p = coef[i] + (x * p);
        return p;
    }

    // differentiate this polynomial and return it
    public Polynomial differentiate() {
        if (deg == 0) return new Polynomial(0, 0);
        Polynomial deriv = new Polynomial(0, deg - 1);
        deriv.deg = deg - 1;
        for (int i = 0; i < deg; i++)
            deriv.coef[i] = (i + 1) * coef[i + 1];
        return deriv;
    }

    // convert to string representation
    public String toString() {
        if (deg ==  0) return "" + coef[0];
        if (deg ==  1) return coef[1] + "x + " + coef[0];
        String s = coef[deg] + "x^" + deg;
        for (int i = deg-1; i >= 0; i--) {
            if      (coef[i] == 0) continue;
            else if (coef[i]  > 0) s = s + " + " + ( coef[i]);
            else if (coef[i]  < 0) s = s + " - " + (-coef[i]);
            if      (i == 1) s = s + "x";
            else if (i >  1) s = s + "x^" + i;
        }
        return s;
    }

    // test client
    public static void main(String[] args) {
        Polynomial zero = new Polynomial(0, 0);

        Polynomial p1   = new Polynomial(4, 3);
        Polynomial p2   = new Polynomial(3, 2);
        Polynomial p3   = new Polynomial(1, 0);
        Polynomial p4   = new Polynomial(2, 1);
        Polynomial p    = p1.plus(p2).plus(p3).plus(p4);   // 4x^3 + 3x^2 + 1

        Polynomial q1   = new Polynomial(3, 2);
        Polynomial q2   = new Polynomial(5, 0);
        Polynomial q    = q1.plus(q2);                     // 3x^2 + 5


        Polynomial r    = p.plus(q);
        Polynomial s    = p.times(q);
        Polynomial t    = p.compose(q);

        System.out.println("zero(x) =     " + zero);
        System.out.println("p(x) =        " + p);
        System.out.println("q(x) =        " + q);
        System.out.println("p(x) + q(x) = " + r);
        System.out.println("p(x) * q(x) = " + s);
        System.out.println("p(q(x))     = " + t);
        System.out.println("0 - p(x)    = " + zero.minus(p));
        System.out.println("p(3)        = " + p.evaluate(3));
        System.out.println("p'(x)       = " + p.differentiate());
        System.out.println("p''(x)      = " + p.differentiate().differentiate());
   }

}

/*********************************************************************************
* File Name : thread_server.c
*---------------------------------------------------------------------------------
* 간단한 쓰레드 서버
*---------------------------------------------------------------------------------
* gcc -o thread_server thread_server.c -lthread -lsocket -lnsl
*********************************************************************************/
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <thread.h>
#include <string.h>
#include <errno.h>
 
union sock
{
    struct sockaddr s;
    struct sockaddr_in i;
};
 
#define SMAX 15
 
struct scorebd
{
    time_t   tevent;
    thread_t thread;
    int sig;
} scorebd[SMAX];
 
mutex_t sb_lock;
thread_t thr_master;
 
void *handling(void *);
void handto();
void *timing(void *);
void timer();
void master();
 
void reqto(int);
int sigtype();
void thr_reg();
void thr_unreg();
 
main(int argc,char *argv[])
{
    int port;
    union sock sock,work;
    int wsd,sd;
    int addlen;
    
    struct sigaction siginfo;
    
    int i,rv,nrv;
    
    if(argc != 2) exit(1);
    else port = atoi(argv[1]);  /* 첫번째 아규먼트로 포트 설정 */
    
    sigemptyset(&(siginfo.sa_mask));
    sigaddset(&(siginfo.sa_mask),SIGHUP);
    sigaddset(&(siginfo.sa_mask),SIGALRM);
    sigprocmask(SIG_SETMASK,&(siginfo.sa_mask),NULL);
    
    if(thr_create(NULL,0,timing,NULL,0,NULL) == -1)
    {
        perror("Creating timing thread");
        exit(2);
    }
    
    thr_master = thr_self();
    
    sigemptyset(&(siginfo.sa_mask));
    sigaddset(&(siginfo.sa_mask),SIGUSR1);
    thr_sigsetmask(SIG_UNBLOCK,&(siginfo.sa_mask),NULL);
    siginfo.sa_handler = master;
    siginfo.sa_flags = 0;
    sigaction(SIGUSR1,&siginfo,NULL);
    
    if((sd = socket(AF_INET,SOCK_STREAM,0)) == -1)
    {
        perror("No socket");
        exit(1);
    }
    sock.i.sin_family = AF_INET;
    sock.i.sin_port = port;
    sock.i.sin_addr.s_addr = INADDR_ANY;
    
    if(bind(sd,&(sock.s),sizeof(struct sockaddr)) == -1)
    {
        perror("Bad Bind");
        exit(1);
    }
    if(listen(sd,5) == -1)
    {
        perror("Bad listen");
        exit(1);
    }
    
    while(1)
    { 
        wsd = accept(sd,&(work.s),&addlen);
        thr_create(NULL,0,handling,&wsd,0,NULL);
    }
}
 

void *timing(void *arg)
{
    struct sigaction siginfo;
    sigemptyset(&(siginfo.sa_mask));
    sigaddset(&(siginfo.sa_mask),SIGALRM);
    sigaddset(&(siginfo.sa_mask),SIGHUP);
    siginfo.sa_handler = timer;
    siginfo.sa_flags = 0; 
    thr_sigsetmask(SIG_UNBLOCK,&(siginfo.sa_mask),NULL);
    sigaction(SIGALRM,&siginfo,NULL);
    sigaction(SIGHUP,&siginfo,NULL);
    
    alarm(1);
    
    while(1)
    {
        pause();
        thr_yield();
    }
}
 
void timer(int sig)
{
    int i,n;
    time_t now;
    time(&now);
    mutex_lock(&sb_lock);
    
    for(i=0;i<SMAX;i++)
    {
        if(scorebd[i].thread)
        {
            if(sig == SIGHUP)
            {
                scorebd[i].sig = SIGHUP;
                thr_kill(scorebd[i].thread,SIGUSR1);
            }
            else if(sig == SIGALRM)
            {
                if(scorebd[i].tevent < now)
                {
                    scorebd[i].sig = SIGALRM;
                    thr_kill(scorebd[i].thread,SIGUSR1);
                }
            }
        }
    }
    if(sig == SIGHUP) thr_kill(thr_master,SIGUSR1);
    
    mutex_unlock(&sb_lock); 
    alarm(1);
    return;
}
 
void *handling(void *sd)
{
    int nrv,wsd;
    int i;
    char buff[BUFSIZ];
    FILE *logfp = NULL;
    struct sigaction siginfo;
    thread_t me;
    
    wsd = *(int *)sd;
    
    me = thr_self();
    
    sigemptyset(&(siginfo.sa_mask));
    sigaddset(&(siginfo.sa_mask),SIGUSR1);
    thr_sigsetmask(SIG_UNBLOCK,&(siginfo.sa_mask),NULL);
    siginfo.sa_handler = handto;
    siginfo.sa_flags = 0;
    sigaction(SIGUSR1,&siginfo,NULL);
    
    thr_reg();
    
    while(1)
    {
        reqto(20);
        nrv = read(wsd,buff+i,BUFSIZ-i);
        
        if(nrv > 0)
        {
            char *crlfp;
            int fraglen;
 
            i += nrv;
            buff[i] = '\0';
            if(crlfp=strstr(buff,"\r\n"))
            {
                *crlfp = '\0';
                if(logfp == NULL)
                {
                    logfp = fopen(buff,"w");
                    if(logfp == NULL)
                    {
                        close(wsd);
                        return;
                    }
                }
                else fprintf(logfp,"%s\n",buff);
                fraglen = (buff+i)-(crlfp+2);
                if(fraglen)
                {
                    strcpy(buff,crlfp+2);
                    i = fraglen;
                }
                else i = 0;
            } 
            
        }
        else if (nrv == 0) 
        {
            close(wsd);
            fclose(logfp);
            thr_unreg();
            return;
        }
        else if(nrv < 0)
        { 
            switch(sigtype(me))
            {
                case SIGALRM : 
                write(wsd,"Timed out - bye\r\n",17);
                break;
                case SIGHUP :
                write(wsd,"Closing down - bye\r\n",20);
                break;
            }
            close(wsd);
            fclose(logfp);
            thr_unreg();
            return;
        }
        
    } /* while */
}
 
void reqto(int dt)
{
    time_t now;
    thread_t me;
    int i;
    time(&now);
    me = thr_self();
    mutex_lock(&sb_lock);
    for(i=0;i<SMAX;i++)
    {
        if(scorebd[i].thread == me)
        {
            scorebd[i].tevent = now + dt;
            break;
        }
    }
    if(i == SMAX) abort();
    mutex_unlock(&sb_lock);
}
 
int sigtype()
{
    thread_t me;
    int i,rv = 0;
    me = thr_self();
    mutex_lock(&sb_lock);
    for(i=0;i<SMAX;i++)
    {
        if(scorebd[i].thread == me)
        {
            rv = scorebd[i].sig;
            scorebd[i].sig = 0;
            break;
        }
    }
    mutex_unlock(&sb_lock);
    return rv;
}
 

void thr_reg()
{
    thread_t me;
    int i;
    me = thr_self();
    mutex_lock(&sb_lock);
    for(i=0;i<SMAX;i++)
    {
        if(scorebd[i].thread == 0)
        {
            scorebd[i].thread = me;
            break;
        }
    }
    if(i==SMAX) abort();
    mutex_unlock(&sb_lock);
    return;
}
 
void thr_unreg()
{
    thread_t me;
    int i;
    me = thr_self();
    mutex_lock(&sb_lock);
    for(i=0;i<SMAX;i++)
    {
        if(scorebd[i].thread == me)
        {
            scorebd[i].thread = 0;
            break;
        }
    }
    if(i == SMAX) abort();
    mutex_unlock(&sb_lock);
    return;
}
 
void handto(int sig)
{
    return;
}
 
void master(int sig)
{
    int i;
    mutex_lock(&sb_lock);
    for(i=0;i<SMAX;i++)
    {
        if(scorebd[i].thread) thr_join(scorebd[i].thread,NULL,NULL);
    }
    mutex_unlock(&sb_lock);
    exit(0);
}
package json;

import net.sf.json.*;
import java.io.*;
import java.net.*;

public class json_test {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String getS = getRequest();
        JSONArray json = JSONArray.fromObject(getS);
        
        for( int i=0; i<json.size(); i++ ) {
            JSONObject timeline = (JSONObject)json.get(i);
            //JSONArray
            JSONObject user = timeline.getJSONObject("user");
            
            System.out.println("No : " + (i+1) +
                                ", Source : " +
                                timeline.get("source") +
                                ", Retweeted : " +
                                timeline.get("retweeted") +
                                ", Time_zone : " +
                                user.get("time_zone") +
                                ", Retweet Count : " +
                                timeline.get("retweet_count"));
        }
    }
    
    public static String getRequest() throws IOException {
        String result = "";
        String getRequest = "GET /1/statuses/public_timeline.json HTTP/1.1\r\n" +
                            "Host: api.twitter.com\r\n" +
                            "Connection: close\r\n" +
                            "\r\n";
        
        InetAddress inetAddress = InetAddress.getByName("api.twitter.com");
        Socket tcpSocket = new Socket(inetAddress.getHostAddress(), 80);
        
        OutputStream os_socket = tcpSocket.getOutputStream();
        InputStream is_socket = tcpSocket.getInputStream();
        
        BufferedWriter bufferW = new BufferedWriter(new OutputStreamWriter(os_socket));
        BufferedReader bufferR = new BufferedReader(new InputStreamReader(is_socket));
        
        bufferW.write(getRequest);
        bufferW.flush();
        
        String str = null;
        while( (str=bufferR.readLine()) != null ){
            if( str.startsWith("[") ) result += str;
        }
        
        bufferW.close();
        bufferR.close();
        tcpSocket.close();
        
        return result;
    }

}

'노트 > 소스 코드' 카테고리의 다른 글

[R] Co-occurrence Matrix  (0) 2016.02.09
[Java] Polynomials with integer coefficients  (0) 2016.01.18
[C] Multi Thread Server Example  (0) 2011.12.28
[C++] Dijkstra Algorithm (NOT BRUSH)  (0) 2011.12.28
[C] English Morpheme Analyzer (With Lex)  (0) 2011.12.28
[Java] Simple Thread Example - Horse Race  (0) 2011.12.28
[C] Char, Word, Line Count (With Lex)  (0) 2011.12.28
[JavaScript] Layer Popup Example  (0) 2011.12.28
[Java] Notepad  (0) 2011.12.28
[Java] RSS Reader  (0) 2011.12.28

+ Recent posts