간단히 a[i]의 문자가 있고 int형 변수 b가 있다고 치자.

그럼

b=a[i]-'0'

로 값을 넣어주면 바로 int형으로 변환되어 있는걸 확인 할 수 있다

정말 간단하네요잉

'Programing > C++' 카테고리의 다른 글

구조체로 구현한 Invader  (0) 2015.06.09
클래스 template  (0) 2015.04.27
함수 Template  (0) 2015.04.27
이차원 동적 배열 입력 받아 생성하기  (0) 2015.02.10
Tree 구현하기  (0) 2015.02.10
Posted by kimmayer

scanf로 문자형 입력을 받을 때 

%c로 입력받으면 제일 앞의 문자를 입력 받을 수 있게 되고 %s로 받으면 하나의 단어를 입력 받을 수 있다. 예를들어 apple의 경우 %c는 'a'가 %s의 경우 'apple'을 전달인자로 받게 된다.

 

scanf로 문자를 입력을 받을 땐 일반 int형 변수의 경우는 앞에 &를 붙여서 받지만 문자열은 그렇지 않다.

 

ex) scanf("%d", &ex1) int형 변수 ex1을 받는 경우

ex) scanf("%s", ex2) char형 변수 ex2를 받는 경우

'Programing > C' 카테고리의 다른 글

socket operation on non-socket 에러  (0) 2015.12.08
간단한 스택 구현  (0) 2015.09.12
2차원 배열 함수 인자로 넘기기 예제  (0) 2015.02.10
전치행렬 구현  (0) 2015.02.10
피보나치 수열 재귀 함수, For loop  (0) 2015.02.10
Posted by kimmayer

//

//  File.c

//  Example

//

//  Created by 김 일호 on 13. 3. 7..

//  Copyright (c) 2013년 김 일호. All rights reserved.

//

 

#include <stdio.h>

 

#define ROW 3

#define COL 4

void transfer(int array[][COL], int rows, int cols);

 

int main()

{

    int matrix[ROW][COL];

    

    int i = 0;

    int j = 0;

    

    for(i = 0; i<ROW ; i++){

        for(j = 0 ; j<COL ;j++)

            scanf("%d", &matrix[i][j]);

    }

    

    

   transfer(matrix, ROW, COL);

    

    return 0;

}

 

void transfer(int array[][COL], int rows, int cols)

{

    int i = 0;

    int j = 0;

    

    for(i = 0; i<ROW; i++){

        for(j = 0; j<COL; j++)

            printf("%d ", array[i][j]);

        printf("\n");

    }

    

}

 

 

코드 작성은 제가 했으나 다음 블로그를 참조 하였습니다.

http://mwultong.blogspot.com/

 

문제가 될 시 삭제 하겠습니다.

'Programing > C' 카테고리의 다른 글

간단한 스택 구현  (0) 2015.09.12
scanf를 %s와 %c로 받는 차이  (0) 2015.02.10
전치행렬 구현  (0) 2015.02.10
피보나치 수열 재귀 함수, For loop  (0) 2015.02.10
십진수 127까지 이진화  (0) 2015.02.10
Posted by kimmayer

2015. 2. 10. 18:05 Programing/C

전치행렬 구현

//

//  File.c

//  Example

//

//  Created by 김 일호 on 13. 3. 7..

//  Copyright (c) 2013년 김 일호. All rights reserved.

//

 

#include <stdio.h>

 

#define ROW 3

#define COL 4

void transfer(int array[][COL], int rows, int cols);

 

int main()

{

    int matrix[ROW][COL];

    

    int i = 0;

    int j = 0;

    

    for(i = 0; i<ROW ; i++){

        for(j = 0 ; j<COL ;j++)

            scanf("%d", &matrix[i][j]);

    }

    

    for(i = 0; i<ROW; i++){

        for(j = 0; j<COL; j++)

            printf("%d ", matrix[i][j]);

        printf("\n");

    }

    

     printf("\n");

 

    

   transfer(matrix, ROW, COL);

    

    return 0;

}

 

void transfer(int array[][COL], int rows, int cols)

{

    int i = 0;

    int j = 0;

    int t_matrix[COL][ROW];

    

    for(i = 0; i<COL; i++){

        for(j = 0; j<ROW; j++){

            t_matrix[j][i] = array[j][i];

        }

    }

    

    for(i = 0; i<COL; i++){

        for(j = 0; j<ROW; j++)

            printf("%d ", t_matrix[i][j]);

        printf("\n");

    }

    

}

'Programing > C' 카테고리의 다른 글

간단한 스택 구현  (0) 2015.09.12
scanf를 %s와 %c로 받는 차이  (0) 2015.02.10
2차원 배열 함수 인자로 넘기기 예제  (0) 2015.02.10
피보나치 수열 재귀 함수, For loop  (0) 2015.02.10
십진수 127까지 이진화  (0) 2015.02.10
Posted by kimmayer

#include <stdio.h>

 

unsigned int Fibonacci(unsigned int n)

{

    if(n<2)

        return n;

    else

        return Fibonacci(n-1)+Fibonacci(n-2);

}

 

 

int main()

{

    int i;

    int j;

    scanf ("%d", &j);

    for( i=0; i<=j; i++)

        printf("Fibonacci(%d) = %d\n", i, Fibonacci(i));

    

    return 0;

}

 

j가 40대를 넘어서 부터 시간이 오래 걸리더군요... 재귀를 사용하지 않으면 더 빠른걸로 알고 있습니다만...

 

(http://slow-down.tistory.com/118 를 참조하였습니다. 문제시 삭제 하겠습니다.)

 

 

//

//  File.c

//  example2

//

//  Created by 김 일호 on 13. 4. 11..

//  Copyright (c) 2013년 김 일호. All rights reserved.

//

 

#include <stdio.h>

 

 

int main()

{

    int a=0;

    unsigned int i=0;

    unsigned int j=1;

    unsigned int q=0;

    unsigned int k;

    scanf("%d", &k);

    

    for(a=0; a<k; a++)

    {

    printf("Fibonacci(%d) %5d\n",a+1, j);

        q = i + j;

        i = j;

        j = q;

    }

    return 0;

}

 

이건 문제 없다만 unsigend int 형 범위를 벗어나 버린다.

'Programing > C' 카테고리의 다른 글

간단한 스택 구현  (0) 2015.09.12
scanf를 %s와 %c로 받는 차이  (0) 2015.02.10
2차원 배열 함수 인자로 넘기기 예제  (0) 2015.02.10
전치행렬 구현  (0) 2015.02.10
십진수 127까지 이진화  (0) 2015.02.10
Posted by kimmayer

#include <stdio.h>

 

int main()

{

 int depth[7] = { 1, 6, 7, 6, 1, 4, 5 };

 int sum=0;

 int i = 0;

 int j = 6;

 int count = 0;

 int number = 0;

 int temp = 0;

 int binary[128][7] = { 0 }; 

 

 

 

 for (i = 0; i < 128; i++){

  temp = number;

  while (temp>0)

   {

    binary[i][j] = temp % 2;

    temp = temp / 2;

    j -= 1;

   }

   number += 1;

   j = 6;

 }

 

 for (i = 0; i < 128; i++){

  for (j = 0; j < 7; j++){

   printf("%d ", binary[i][j]);

  }

  printf("\n");

 }

 system("pause");

 

}

'Programing > C' 카테고리의 다른 글

간단한 스택 구현  (0) 2015.09.12
scanf를 %s와 %c로 받는 차이  (0) 2015.02.10
2차원 배열 함수 인자로 넘기기 예제  (0) 2015.02.10
전치행렬 구현  (0) 2015.02.10
피보나치 수열 재귀 함수, For loop  (0) 2015.02.10
Posted by kimmayer

2015. 2. 10. 14:17 Programing/JAVA

This Keyword

c++ 에서도 한번 스쳐 지나간 적이 있는 this 키워드,

이번에 자세히 개념이해를 하려고 공부를 해봤는데

생각보다 간단하면서도 이해가 되지 않는다.


책에서 보면 this의 사용은 객체 변수나 생성자, 메소드의 매개 변수의 이름을 의미적으로

정확하게 하기 위해서 사용한다고 쓰여 있다(being java 154p).


이것만으로는 무언가 부족하다!

그런데 예제 하나를 보고 이해했다.


public class Box {

    int length;

    int width;

    int height;

    public Box(int length, int width, int height) {

        this.length = length;

        this.width = width;

        this.height = height;

    }

}


Box 생성자에서 length = length 라고 썼으면 위에서 선언한 length 변수 인지

아니면 생성자에서 사용되는 length 인지 이해하기 힘들 것인데

this를 사용 함으로써 생성자에서 사용되는 length 라고 알려주고 있는것!


단순히 이거 뿐일까?

더 알아보고 추가되는 내용이 있으면 추가 해야겠다.



'Programing > JAVA' 카테고리의 다른 글

Netty 인코더 디코더의 아주 간단한 개념  (0) 2016.01.11
Netty  (0) 2016.01.11
접근 한정자 (member access)  (0) 2015.02.10
접근 한정자 예제(객체지향 예제)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
Posted by kimmayer

public class test1{

  public int a; //public 멤버변수 선언

  int b;  //접근한정자를 지정하지 않고 선언

  private int c; //private 멤버변수 선언



public void method1() {} //public 메소드 선언

void method2() {}  // 접근한정자 지정하지 않음

private void method3 () {} private 메소드 선언

}


public class SamePackage{

Test t1 = new Test1();

t1.a = 3; //접근 가능

t1.b = 5; //접근 가능

t1.c = 7; //접근 불가능

t1.method (); //접근 가능

t2.method (); //접근 가능

t3.method (); //접근 불가능

}



public class OtherPackage{

Test1 t2  = new Test1();

t1.a = 3; //접근 가능

t1.b = 5; //접근 불가능

t1.c = 7; //접근 불가능

t1.method (); //접근 가능

t2.method (); //접근 불가능

t3.method (); //접근 불가능

}



같은 패키지 내에서는 지정되지 않은 지역에서는 접근 가능하나

다른 패키지에서는 지정되지 않은 한정자 들은 접근 불가능으로 나온다.

'Programing > JAVA' 카테고리의 다른 글

Netty  (0) 2016.01.11
This Keyword  (0) 2015.02.10
접근 한정자 예제(객체지향 예제)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
배열과 객체생성 잊지 말아야 할 점!  (0) 2015.02.10
Posted by kimmayer

class Fruit {

 int apple;

 int straw;

 int grapes;

 int sum;

 

 Fruit(int apple, int straw, int grapes){

  this.apple = apple;

  this.straw = straw;

  this.grapes = grapes;

 }

 

 public int Count(){

  sum = apple + straw + grapes;

  return sum;

 }

}



public class MethodDemo1 {

 public static void main(String args[]){

  int total;

  Fruit f1 = new Fruit(30, 30, 40);

  total = f1.Count();

  System.out.println("객체 f1의 총 갯수 = " + total);

  System.out.println("객체 f1의 apple 개수 = "+ f1.apple);

  System.out.println("객체 f1의 straw 개수 = "+ f1.straw);

  System.out.println("객체 f1의 graphs 개수 = "+ f1.grapes);

 }


}


여기까지 예제는 따로 클래스화 시키지 않은 내용들인데

사실 객체지향을 위해서라면 아래 코딩된 예시가 더욱 올바른 것이라고 한다.



class Fruit{

 private int a;

 private int b;

 private int c;

 private int sum;

 Fruit (int apple, int straw, int grapes){

  a = apple;

  b = straw;

  c = grapes;

  this.count();

 }

 

 private void count(){

  sum = a + b + c;

 }

 public int gettotal(){

  return sum;

 }

 public int getapple(){

  return a;

 }

 public int getstraw(){

  return b;

 }

 public int getgrapes(){

  return c;

 }

}


public class MethodDemo2 {

 public static void main(String args[]){

  int total;

  Fruit f1 = new Fruit(30,30,40);

  total = f1.gettotal();

  System.out.println("객체 f1의 총 개수 =" + total);

  System.out.println("객체 f1의 apple 개수 = "+ f1.getapple());

  System.out.println("객체 f1의 straw 개수 = "+ f1.getstraw());

  System.out.println("객체 f1의 graphs 개수 = "+ f1.getgrapes());

 }


}


각각의 값을 return 해 주는 클래스 메소드가 따로 있다.


'Programing > JAVA' 카테고리의 다른 글

This Keyword  (0) 2015.02.10
접근 한정자 (member access)  (0) 2015.02.10
인스턴스 복사  (0) 2015.02.10
배열과 객체생성 잊지 말아야 할 점!  (0) 2015.02.10
Setter와 Getter  (0) 2015.02.10
Posted by kimmayer

Book b = new Book();

Book c = new Book();


Book 레퍼런스 두 개를 성성하고 새로운 객체 Book 두 개를 생성한다.

그리고 생성한 Book 객체를 레퍼런스 변수에 대입한다.


결국 Book b와 c가 Book 객체 1, 2를 참조하고 있다.



Book d = c;


새로운 Book 레퍼런스 d가 생성되고

c와 똑같은 객체를 참조 한다.



c = b;


b와 c는 똑같은 객체를 참조한다.

Posted by kimmayer
이전버튼 1 2 3 4 5 6 이전버튼

블로그 이미지
IT 기술들 정리, 독후감을 주로 남깁니다!
kimmayer

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함