[카테고리:] 컴퓨터

  • sequences in GEB

    Goedel, Escher, Bach 에 나오는 함수들을 만들어 보았다.

    G sequence

    #include

    #include

    #include

    int gfunction(int);

    int main(int argc, char* argv[])

    {

    int i;

    if (argc==1){

    printf(“Error. There are no options.\n”);

    exit(1);

    }

    i=atoi(argv[1]);

    printf(“%d”,gfunction(i));

    }

    int gfunction(int i)

    {

    if (i<0) {
    printf(“Error. Parameter is negative.\n”);

    }

    else if (i==0) {

    return 0;

    }

    else {

    return (i-gfunction(gfunction(i-1)));

    }

    }

    H sequence

    #include

    #include

    #include

    int hfunction(int);

    int main(int argc, char* argv[])

    {

    int i;

    if (argc==1){

    printf(“Error. There are no options.\n”);

    exit(1);

    }

    i=atoi(argv[1]);

    printf(“%d”,hfunction(i));

    }

    int hfunction(int i)

    {

    if (i<0) {
    printf(“Error. Parameter is negative.\n”);

    }

    else if (i==0) {

    return 0;

    }

    else {

    return (i-hfunction(hfunction(hfunction(i-1))));

    }

    }

    M and F sequence

    #include

    #include

    #include

    int mfunction(int);

    int ffunction(int);

    int main(int argc, char* argv[])

    {

    int i;

    if (argc==1){

    printf(“Error. There are no options.\n”);

    exit(1);

    }

    i=atoi(argv[1]);

    printf(“%d\n”,mfunction(i));

    printf(“%d\n”,ffunction(i));

    }

    int mfunction(int i)

    {

    if (i<0) {
    printf(“Error. Parameter is negative.\n”);

    }

    else if (i==0) {

    return 0;

    }

    else {

    return (i-ffunction(mfunction(i-1)));

    }

    }

    int ffunction(int i)

    {

    if (i<0) {
    printf(“Error. Parameter is negative.\n”);

    }

    else if (i==0) {

    return 1;

    }

    else {

    return (i-mfunction(ffunction(i-1)));

    }

    }

    Q sequence

    #include

    #include

    #include

    int hfunction(int);

    int main(void)

    {

    int n;

    for(n=1;n<100;n++)
    printf(“%d, “,qfunction(n));

    }

    int qfunction(int i)

    {

    if (i<0) {
    printf(“Error. Parameter is negative.\n”);

    }

    else if (i==2 || i==1) {

    return 1;

    }

    else {

    int q=qfunction(i-qfunction(i-1))+qfunction(i-qfunction(i-2));

    return q;

    }

    }

    그냥 작동하기만 할 뿐인 프로그램들이다.

    컴파일은 DevC++ 4.9.9.1 with MinGW 에서 해서 작동하는 것을 확인하였다.

  • 거리와 기울기가 주어지면

    거리 T, 기울기 M, 주어진 점 (px, py)일 때, 주어진 점을 지나면서 기울기가 M인 직선 위에 있는 점 
    중에서, 주어진 점과 거리가 T만큼 떨어져 있는 점 찾기
    몬테카를로 방법.

    error1 = 0.01 // 거리 오차값
    error2 = 0.01 // 기울기 오차값
    (x,y)=(px,py) // 처음엔 거기서 출발합니다

    while(1){ // 될때까지 무한반복
    (ran_x,ran_y)=(rand(),rand()) //적당히 랜덤한 좌표 하나를 만듭니다
    (x_backup, y_backup)=(x,y) // 일단 백업해두고
    (x,y)=(x,y)+(ran_x,ran_y) //그쪽으로 갑니다
    if abs(distance((x,y),(px,py)) - T) < abs(distance((x_backup,y_backup),(px,py)) - T)
    || abs(tangent((x,y),(px,py)) - M) < abs(tangent((x_backup,y_backup),(px,py)) - M)
    then (x,y)=(x_backup,y_backup) // 비교해서 더 조건이 나빠졌으면 원래대로 돌아감.
    if abs(distance((x,y),(px,py)) - T) < error1 && abs(tangent((x,y),(px,py)) - M)
    < error2 then { //비교해서 기준치를 만족하면 탈출
    print((x,y))
    exit
    }
    }

    rand() //난수를 발생시킵니다. 적당한 함수 정의를 통하여 0부터 T사이의 난수만
    발생시키기로 합니다.
    abs() //입력받은 실수의 절대값을 반환합니다
    distance() //두 점 사이의 거리를 실수값으로 알려줍니다
    tangent() //두 점이 만드는 직선의 기울기를 알려줍니다
    print() //입력받은 점을 출력합니다
  • GSL 사용하기 ~ 설명서 2장 4절까지

    GSL?

    GNU Scientific Libraries

    과학/공학/수학 연구에 사용되는 많은 함수들을 다 만들어 둔 패키지다.

    아래 링크에서 구할 수 있다.


    http://www.gnu.org/software/gsl/

    예전에 공짜로 얻었던 컴퓨터에 리눅스를 설치하고 이걸 갖고 뭘할까 하다가 GSL을 공부해 보기로 마음먹었다.

    CPU의 속력이 866MHz밖에 되지 않는 구시대의 유물이지만, 내가 갖고 놀기에는 충분해 보인다.

    다음과 같은 것들을 지원한다고 한다.

    복소수 방정식의 근
    특수 함수들
    벡터와 행렬
    순열, 조합
    정렬
    기본 선형대수학
    선형 대수학
    고유값 시스템
    빠른 푸리에 변환
    Quadrature 난수
    준-난수 수열
    난수 분포
    통계 히스토그램
    N-Tuple
    몬테 카를로 적분
    Simulated Annealing 미분 방정식
    보간법
    미분
    체비셰프 보간법
    Series Acceleration
    이산 한켈 변환
    근 찾기
    최소화
    최소 제곱법
    물리 상수
    IEEE Floating-Point
    이산 웨이블릿 변환
    Basis splines

    그래서. 프로젝트를 정해야 하는데…

    그건 나중에 정하고. 아마 그때그때 예제를 조금 응용하는 정도가 되지 않을까…

    사용설명서를 읽어보았다.

    2.1절에 보면 예제가 있다.

    //gsl_example.c
    #include
    #include

    int main (void)
    {
    double x = 5.0;
    double y = gsl_sf_bessel_J0 (x);
    printf ("J0(%g) = %.18e\n", x, y);
    return 0;
    }

    참고로, GSL의 메뉴얼은 GFDL에 의해서 내가 퍼와도 된다.

    위의 소스를 실행시키기 위해서

    snowall@snowall-desktop:~/projects/gsl_example gcc -o gsl_example gsl_example.c

    /tmp/cc3a6993.o: In function `main’:

    gsl_example.c:(.text+0x1d): undefined reference to `gsl_sf_bessel_J0′

    collect2: ld returned 1 exit status

    이렇게 에러가 나왔다.

    그래서 메뉴얼을 살펴봤다.

    2.2 Compling and Linking 을 살펴봤더니



    gcc -Wall -I/usr/local/include -c example.c

    이렇게 하면 된다고 한다. 그래서 했더니, 바이너리 파일이 만들어지긴 했는데 out 파일이 아니라 o 파일이다. 당연히 실행될리가 없다.

    그래서, 다음장인 2.3 Shared Libraries 를 살펴봤다.
    gcc -static example.o -lgsl -lgslcblas -lm

    이렇게 하라고 시킨다. 그래서 그렇게 했더니 lgslblas가 뭔지 모르겠다고 에러를 뱉었다.
    근데, 난 Blas를 설치를 안했으니 당연히 에러가 뜰 것이다. 그래서 lgslblas 옵션을 빼고 컴파일 했다.
    된다. ㅋㅋ

    2.4 절을 읽어보니 Ansi C와 잘 맞는다고 되어 있어서 그러려니 하면 될 것 같다.

  • VBA 스크립트 2탄

    Sub Distance()

    Dim i As Integer

    Dim j As Integer

    Dim k As Integer

    Dim l As Integer

    Dim n As Integer

    l = 2

    j = 2

    For i = 1 To 7

    n = 3

    For k = 1 To 7

    If 1# > (Worksheets(“Sheet1”).Cells(i, j).Value) * (Worksheets(“Sheet1”).Cells(i, j).Value) – (Worksheets(“Sheet2”).Cells(k, l).Value) * (Worksheets(“Sheet2”).Cells(k, l).Value) Then

    Worksheets(“Sheet1”).Cells(i, j + n).Value = Worksheets(“Sheet2”).Cells(k, 1).Value

    n = n + 1

    End If

    Next

    Next

    End Sub

    이놈은 대체 뭐하는 놈일까요? -_-;

  • VBA스크립트

    Sub FindAndChange()

    Dim i As Integer

    Dim j As Integer

    For i = 1 To 10

    For j = 1 To 20

    If Worksheets(“Sheet1”).Cells(i, 2).Value = Worksheets(“Sheet2”).Cells(j, 2).Value Then

    Worksheets(“Sheet2”).Cells(j, 1).Value = Worksheets(“Sheet1”).Cells(i, 1).Value

    End If

    Next j

    Next i

    End Sub

    음…

    이젠 VBA 엑셀 스크립트까지 건드리게 되었다.

    이 코드에 대한 상세한 설명은 댓글 달리면 추가할 예정임.

  • gnuplot italic greek letter

    gnuplot에서 그리스 문자의 이탤릭(기울임꼴) 넣는 방법. 검색하면 일반 문자의 기울임꼴 넣는 방법도 나온다. 사실은 Symbol-Oblique 를 Times-Italic 등으로 바꾸면 된다는…

    —–

    set xlabel ‘{/Symbol-Oblique a} Temperature’

    This is documented, albeit obscurely, in “help set term postscript”:

    In addition to the standard postscript fonts, an oblique version of the

    Symbol font, useful for mathematics, is defined. It is called

    “Symbol-Oblique”.

    That won’t help you for terminal types other than PostScript, however.

    In general the answer to all font questions is “it depends on the fonts

    you have installed on your system”.



    Ethan A Merritt

  • 자기참조 구조체 예제

    왜 이 프로그램을 만들었는지 묻지 말자.

    이 프로그램은 public domain 라이센스이다. 출처를 밝히지 않고도 누구나 자유롭게 이용할 수 있고, 상업적 목적으로 사용해도 무방하다.

    #include

    #include

    #include

    struct person{

    char name[30];

    char tel[14];

    int hour;

    int payment;

    struct person *next;

    };

    main()

    {

    FILE *data;

    struct person dummy;

    struct person *start=&dummy;

    struct person *wkdata;

    struct person *wp;

    struct person *best;

    char name[30]=” “;

    char buf[20];

    char tel_input[14]=” “;

    int hour_input=0;

    int payment_input=0;

    int i=0;

    int totalpay=0;

    start=&dummy;

    start->next=NULL;

    printf(“put your data in\n”);

    while(1){

    printf(“name:”);

    gets(name);

    if(strcmp(name,””)==0) break;

    printf(“phone:”);

    gets(tel_input);

    printf(“work hour:”);

    gets(buf);

    hour_input=atoi(buf);

    printf(“pay per hour:”);

    gets(buf);

    payment_input=atoi(buf);

    wkdata=(struct person *)malloc(sizeof(struct person));

    if(wkdata==NULL){

    printf(“Memory allocation cannot be done\n”);


    exit(1);

    }

    strcpy(wkdata->name,name);

    strcpy(wkdata->tel,tel_input);

    wkdata->hour=hour_input;

    wkdata->payment=payment_input;

    for(wp=start; wp->next != NULL; wp=wp->next){

    if(payment_input*hour_input>wp


    ->next->payment*wp->next->hour

    ){

    wkdata->next = wp->next;

    wp->next = wkdata;

    break;

    }

    }

    if(wp->next==NULL){

    wkdata->next=NULL;

    wp->next=wkdata;

    }

    }

    data=fopen(”

    data

    .txt”,”w”);

    fprintf(data,”Name Phone Working

    Hour pay/hour Total

    Pay\n=========================

    ==============================

    ====\n”);

    fclose(data);

    for(wp=start->next;wp!=NULL;wp

    =wp->next){

    data=fopen(”

    data

    .txt”,”a+”);

    fprintf(data,”%-30s%-14s\t%12d

    \t%8d\t%8d\n”,

    wp->name,wp->tel,wp->hour,wp-

    >payment,wp->hour*wp->payment)

    ;

    fclose(data);

    i++;

    totalpay+=wp->hour*wp->payment


    ;

    }

    data=fopen(”

    data

    .txt”,”a”);

    fprintf(data,”================

    ==============================

    =============\n”);

    fclose(data);

    best=start;

    for(wp=start->next;wp!=NULL;wp

    =wp->next){

    if(wp->payment>best->payment)

    best=wp;

    }

    printf(“\nTotal workers are %d\n”, i);

    printf(“The highest paid person of %dwon is %s

    .\n”,start->next->payment

    ,start->next->name);

    printf(“Sum of total payment is %d.\n”,totalpay);

    }












    물론, 어디서 많이 보던 프로그램이다 싶은 사람들은 특정 사람들이겠지만…;

  • 중앙대학교 학위 논문 스타일 파일

    드디어 90%쯤 완성된 버전을 공개한다. 이 이상은 내 능력의 한계이다. 나중에 TeX의 도사가 되면 처리하도록 하자.

    1. 겉표지, 속표지, 인정서에서 제목과 소속/이름/연도 부분 사이의 간격 조절을 위해 반드시 클래스 파일의 주석을 참고하여
    수정하여야 한다. 안그러면 하단 여백이 붕 떠버리거나 너무 밑으로 내려가는 사태가 발생한다. 이렇게 되는 이유는 vfill
    명령을 이용하면 하단 여백까지 잘 맞춰 내려가도록 설정되는데, 하단 여백이 6cm인지라, 그냥 내리면 너무 많이 뜨고,
    vspace{3cm}명령을 이용해서 적당히 맞춰서 내려줘야만 했다. 참고로, 하단 종이 끝에서 학위 취득 연도까지의 거리는
    3cm로 규정되어 있다.

    2. pdflatex을 이용하면 겉표지, 속표지, 인정서 부분이 여전히 a4로 출력된다.

    3. 본문 하단 여백에서, 하단 종이 끝에서 페이지 번호까지 2cm가 되어야 하는데 4cm쯤 된다. 역시 해결하지 못하였다.

    뭐, 원래, 진짜 남자라면 백업따위 안하고 웹에 올려서 남들이 백업받게 해야 한다고 했던가.

  • 성적조회기

    #include

    #include

    #include

    typedef struct {

    char name[10];

    double s[3];

    } data;

    double average(data);

    void best(data *,int);

    void nameprinting(data,int);

    void score(char*,double);

    void namesearch(data*,char*);

    int rank(data *,int);

    int main(int argc, char **argv){

    FILE *inputdata;

    inputdata=fopen(argv[1],”r”);

    data a[10];

    int i;char temp[20];

    for(i=0;i<10;i++){
    fscanf(inputdata,”%s\n%lf,%lf,%lf\n”,a[i].name,&a[i].s[0],&a[i].s[1],&a[i].s[2]);

    }

    if(atoi(argv[2])==1){

    char n[10];strcpy(n,argv[3]);

    namesearch(a,n);

    }

    else if(atoi(argv[2])==2){

    best(a,atoi(argv[3]));

    }

    else{

    printf(“error!\n”);

    }

    return 0;

    }

    void namesearch(data *a, char *n){

    int i;

    for(i=0;i<10;i++){
    if(strcmp(a[i].name,n)==0){

    nameprinting(a[i],rank(a,i));

    }

    }

    }

    void nameprinting(data a,int ranking){

    int f;

    char a1[2],a2[2],a3[2];

    score(a1,a.s[0]);

    score(a2,a.s[1]);

    score(a3,a.s[2]);

    printf(“%s : %lf %s\t%lf %s\t%lf %s\t%lf\t%d\n”,a.name,a.s[0],a1,a.s[1],a2,a.s[2],a3,average(a),ranking);

    }

    void score(char *sco,double a){

    if(a<60.0){
    strcpy(sco,”F”);

    return;

    }

    else if(a<70.0){
    strcpy(sco,”D”);

    return;

    }

    else if(a<80.0){
    strcpy(sco,”C”);

    return;

    }

    else if(a<90.0){
    strcpy(sco,”B”);

    return;

    }

    else {

    strcpy(sco,”A”);

    }

    return;

    }

    double average(data a){

    return (a.s[0]+a.s[1]+a.s[2])/3.0;

    }

    int rank(data *a,int i){

    int ranking=1;

    int j;

    for(j=0;j<10;j++){
    if(average(a[j])>average(a[i])){

    ranking++;

    }

    }

    return ranking;

    }

    void best(data *a,int i){

    int j;

    data temp=a[0];

    for(j=1;j<10;j++){
    if(a[j].s[i]>temp.s[i]){

    temp=a[j];

    }

    }

    nameprinting(temp,1);

    }