'C'에 해당되는 글 116건

  1. 2007.11.03 포인터 by 청웨일
  2. 2007.11.02 문자열의 배열 by 청웨일
  3. 2007.11.02 배열의 초기화 by 청웨일
  4. 2007.11.02 다차원 배열 by 청웨일
  5. 2007.11.02 문자열의 사용 by 청웨일

포인터

C : 2007. 11. 3. 11:33

*포인터변수 - 다른 객체의 메모리 주소를 저장하는 변수이다. 메모리에 저장.

                    이진트리와 연결 리스트를 구현하는데 사용된다.


type *변수 이름


& : 뒤에 오는 변수의 주소를 반환

* : 뒤에 오는 주소에 저장된 값을 반환


p.197
#include <stdio.h>
int main(void)
{
 int *p, q;

 q = 199;
 p = &q;        //199가 저장된 q의 주소를 포인터변수 p에 저장한다.

 printf("%d", *p);      //포인터를 이용해 q에 저장된 값을 출력한다.
}


p.198
#include <stdio.h>
int main(void)
{
 int *p, q;

 p = &q;          //포인터 p와 변수 q를 연결한다.
 *p = 199;       //포인터 p가 가리키는 주소에 199를 치환한다.

 printf("q's value is %d", q);

 return 0;
}

-----------

int q;

double *fp;        //포인터의 기초형은 일치해야 한다.


fp = &q;

-----------

int형은 double 보다 실이가 짧기 때문에 q에 인접한 메모리까지 치환된다.

q에 할당된 바이트외에도 인접한 바이트를 사용함으로써 에러를 야기시킬 수 있다.


Posted by 청웨일

문자열의 배열

C : 2007. 11. 2. 16:40
 

* 문자열의 배열 - 문자열 테이블(string table)

* char names[10][40]   -   10개의 문자열을 포함할수 있는 테이블을 지정하며,
                                      각 문자열은 널문자를 포함하여 40개까지의 문자를 갖는다.

                                  -   테이블내의 문자열을 접근하려면,
                                      첫번째 색인만 지정해주면 된다.

* 문자열 저장 - gets(names[2]);

* 문자열 출력 - printf(names[0]);


p.189_1
#include <stdio.h>

int main(void)
{
 char text[10][80];
 int i;
 for(i=0; i<10; i++) {
  printf("%d: ", i+1);
  gets(text[i]);
 }

 do {
  printf("Enter number of string (1-10) : ");
  scanf("%d", &i);
  i--;
  if(i>=0 && i<=9) printf("%s\n", text[i]);
 } while(i>=0);
 return 0;
}


p.190_2
#include <stdio.h>
#include <string.h>

char words[][2][40] = {
 "dog", "Hund",
  "no", "nein",
  "year", "Jahr",
  "child", "Kind",
  "I", "Ich",
  "drive", "fahren",
  "house", "Haus",
  "to", "zu",
  " ", " "
};


int main(void)
{
 char english[80];
 int i;

 printf("Enter English woed: ");
 gets(english);

 i = 0;

 while(strcmp(words[i][0], " ")) {
  if(!strcmp(english, words[i][0])) {
   printf("German translation: %s", words[i][1]);
   break;
  }
  i++;
 }
 if(!strcmp(words[i][0], " ")) printf("Not in dictionary\n");

 return 0;
}


p.191_3
#include <stdio.h>
int main(void)
{
 char text[][80] = {
  "when", "in", "the",
  "course", "of", "human",
  "events", ""
 };

 int i, j;

 for(i=0; text[i][0]; i++) {
  for(j=0; text[i][j]; j++) printf("%c", text[i][j]);
  printf(" ");
 }
 return 0;
}

p.191_연습1
#include <stdio.h>
#include <conio.h>

int main(void)
{
 char words[10][80] = {"hi", "when", "in", "the", "of",
  "human", "events", "dog", "Hund", "no"};                            //배열의 초기화
 char ch;

 while(1) {
  printf("Enter number of string (0-9) quit is 'q': ");
  ch = getche();
  if(ch == 'q') break;                  //q를 누르면 종료한다
  ch = ch - '0';                             //문자형 '
1'은 아스키코드값 49이므로
                                                   
 '
0'(48)을 빼서 정수형1이 되도록 한다.
  if(ch>=0 && ch<=9) printf("\nword is %s\n", words[ch]);
 }
 printf("\n\n");
 return 0;
}

Posted by 청웨일

배열의 초기화

C : 2007. 11. 2. 16:39
 

* 자료형 변수이름[크기] = {배열 원소}

* 언사이즈드 배열 -> int pwr[] = {..............}
                             초기화 리스트의 크기를 변경할 때마다
                             배열의 크기가 자동으로 조정



p.186_1
#include <stdio.h>
int main(void)
{
 int ServerUsers[5][2] = {
  1, 14,
  2, 28,
  3, 19,
  4, 8,
  5, 15
 };

 int server;
 int i;

 printf("Enter the server number: ");
 scanf("%d", &server);

 for(i=0; i<5; i++)
  if(server == ServerUsers[i][0]) {
   printf("There are %d users on server %d.\n",
    ServerUsers[i][1], server);
   break;
  }
  if(i==5) printf("Server not listed.\n");

  return 0;
}

p.187 예제 2
#include <stdio.h>
#include <string.h>

int main(void)
{
 char str[80] = "I like C";   //str에 초기값
 strcpy(str, "hello");       //str에 hello를 복사
 printf(str);                        //str 출력

 return 0;
}

p.188_3
#include <stdio.h>

int main(void)
{
 int i;
 int number[10][3] = {
  1, 1, 1,
  2, 4, 8,
  3, 9, 27,
  4, 16, 64,
  5, 25, 125,                             //배열의 초기화
  6, 36, 216,
  7, 49, 343,
  8, 64, 512,
  9, 81, 729,
  10, 100, 1000
 };


 printf("enter number : ");
 scanf("%d", &num);

 for(i=0; i<10; i++) {
  if(num == number[i][2])
   printf("%d, %d\n", number[i][0], number[i][1]);
 }
 return 0;
}

Posted by 청웨일

다차원 배열

C : 2007. 11. 2. 16:38
 

int count[10][12] = 2차원의 정수배열


p.181
#include <stdio.h>

int main(void)
{
 int twod[4][5];
 int i, j;
 
 for(i=0; i<4; i++) {   //변수에 수를 적용하기 위한 for문
  for(j=0; j<5; j++){
   twod[i][j] = i*j;
        }
 }
 
  

    for(i=0; i<4; i++) {         //출력하기 위한 for문
        for(j=0; j<5; j++) printf("%d", twod[i][j]);
     printf("\n");
 }
 return 0;
}

p.183 예제1
#include <stdio.h>
int main(void)
{
 int bball[4][5];  //2차원 배열
 int i, j;

 for(i=0; i<4; i++)
  for(j=0; j<5; j++) {
   printf("Quater %d, player %d, ", i+1, j+1);
   printf("enter number of points: ");
   scanf("%d", &bball[i][j]);
  }
 for(i=0; i<4; i++)
  for(j=0; j<5; j++) {
   printf("Quater %d, player %d, ", i+1, j+1);
   printf("%d\n", bball[i][j]);
  }
 return 0;
}

p.183 연습1
#include <stdio.h>
int main(void)
{
 int bball[3][3][3];         //3차원 배열
 int i, j, k;

 for(i=0; i<3; i++)
  for(j=0; j<3; j++)
   for(k=0; k<3; k++) {
    printf("a %d, b %d, c %d ", i+1, j+1, k+1);
    printf("enter number of points: ");
    scanf("%d", &bball[i][j][k]);
   }
 for(i=0; i<3; i++)
  for(j=0; j<3; j++)
   for(k=0; k<3; k++) {
    printf("a %d, b %d, c %d - ", i+1, j+1, k+1);
    printf("%d\n", bball[i][j][k]);
   }
 return 0;
}

p.183 연습 2
#include <stdio.h>
int main(void)
{
 int bball[3][3][3];
 int i, j, k, sum =0;

 for(i=0; i<3; i++)
  for(j=0; j<3; j++)
   for(k=0; k<3; k++) {
    printf("a %d, b %d, c %d ", i+1, j+1, k+1);
    printf("enter number of points: ");
    scanf("%d", &bball[i][j][k]);
   }
 for(i=0; i<3; i++)
  for(j=0; j<3; j++)
   for(k=0; k<3; k++) {
    printf("a %d, b %d, c %d - ", i+1, j+1, k+1);
    printf("%d\n", bball[i][j][k]);
    sum = sum + bball[i][j][k];
   }
 printf("%d", sum);
 return 0;
}

Posted by 청웨일

문자열의 사용

C : 2007. 11. 2. 16:36
* 문자열 - 널 종료 문자 배열

* 널(null) == 0(거짓)

*gets();     - 사용자가 enter를 입력할때까지 문자들을 읽는다.


@<string.h> 헤더파일을 사용하는 함수

*strcpy(to, from);  - from을 to에 복사  
*strcat(to, from);  - from을 to 뒤에 추가    
*strcmp(s1, s2);    - 비교, 같으면 0 반환  
                              s1<s2 - 0보다 작은 값.  
                              s1>s2 - 0보다 큰 값.

*strlen(str);       - 문자열의 길이/문자수를 반환.


p.178_1
#include <stdio.h>
#include <string.h>

int main(void)
{
 char str1[80], str2[80];
 int i;


 printf("Enter the first string: ");
 gets(str1);
 printf("Enter the second string: ");
 gets(str2);


//문자열의 길이 출력

 printf("%s is %d chars long\n", str1, strlen(str1));
 printf("%s is %d chars long\n", str2, strlen(str2));


//문자열 비교

 i= strcmp(str1, str2);
 if(!i) printf("The strings are equal.\n");
 else if(i<0) printf("%s is less then %s\n", str1, str2);
 else printf("%s is greater then %s\n", str1, str2);


//공간이 충분할때 str1끈에 str2를 연결

 if(strlen(str1)+ strlen(str2)<80) {
  strcat(str1, str2);
  printf("%s\n", str1);
 }


//str2를 str1에 복사

 strcpy(str1, str2);
 printf("%s %s\n", str1, str2);

 return 0;
}

p.179_2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
 char command[80], temp[80];
 int i, j;
 for( ; ;) {
  printf("Operation? \n add\n subtract\n divide\n multiply\n quit\n : ");
  gets(command);

  if(!strcmp(command, "quit")) break;
  printf("Enter first number: ");
  gets(temp);
  i = atoi(temp);                         //문자열 인수가 표현하는 수와 동일한 정수값을 반환

  printf("Enter second number: ");
  gets(temp);
  j = atoi(temp);

  if(!strcmp(command, "add")) printf("%d\n", i+j);
  else if(!strcmp(command, "subtract")) printf("%d\n", i-j);
  else if(!strcmp(command, "divide")) {
   if(j) printf("%d\n", i/j);
  }
  else if(!strcmp(command, "multiply")) printf("%d\n", i*j);
  else printf("Unknown command. \n");
 }

 return 0;
}

Posted by 청웨일