6 4 1 int a10 10 30 50

  • Slides: 64
Download presentation

예제 6. 4 1차원 배열의 초기화 및 선언 예 ⑴ int a[10] = {

예제 6. 4 1차원 배열의 초기화 및 선언 예 ⑴ int a[10] = { 10, 30, 50, 70, 100, 40, 30, 55, 223, 765 }; ⑵ int b[] = { 10, 30, 50, 70, 100, 40, 30, 55, 223, 765 }; ⑶ int c[10] = { 10, 30, 50, 70, 100, 40, 30, }; /*숫자 뒤에 콤마가 있다. */ ⑷ int d[3] = { 1, 10, 5, 7, 8 }; ⑸ char ch[5] = "Test"; ⑹ int numbers[10]; ⑺ static int numbers[10] = { 34, 27, 16 }; ⑻ static int numbers[] = { 2, -3, 45, 79, -14, 5, 9, 28, -1, 0}; ⑼ static char text[] = "Welcome to Korea. "; ⑽ static float radix[12] = { 134. 362, 1913. 248 }; ⑾ double radians[1000]; YES C 제 6 장 배열과 문자열 10

예제 6. 5 배열 선언시 초기화한 후, 출력하는 프로그램. #include <stdio. h> void main(void)

예제 6. 5 배열 선언시 초기화한 후, 출력하는 프로그램. #include <stdio. h> void main(void) { int x; static int values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; static char word[] ={ 'H', 'e', 'l', 'o' }; for( x = 0; x < 9; ++x ) printf("values[%d] = %dn", x, values[x]); for( x = 0; x < 5; ++x ) printf("word[%d] = %dn", x, word[x]); } YES C 제 6 장 배열과 문자열 11

예제 6. 6 1차원 배열을 이용한 합계계산 프로그램 #include <stdio. h> void main(void) {

예제 6. 6 1차원 배열을 이용한 합계계산 프로그램 #include <stdio. h> void main(void) { int i, data[] = {78, 55, 99, 75, 84, 39, 67, 98, 87, 100}; /*배열 선언*/ long int sum = 0; /* 초기 값을 0으로 둔다. */ float ave; for (i = 0; i < 10; i++) sum += data[i]; ave = (float)sum / 10. 0; printf("Total = %ld Average = %. 2 fn", sum, ave); } YES C 제 6 장 배열과 문자열 12

예제 6. 11 10명의~ • • #include <stdio. h> #include <conio. h> • •

예제 6. 11 10명의~ • • #include <stdio. h> #include <conio. h> • • • • void main(void) { int jumsu[10], sum=0, avg=0, i; for(i=0; i<10; i++) { printf("input(jumsu %d )=>", i+1); scanf("%d", &jumsu[i]); printf("n"); } for(i=0; i<10; i++) sum += jumsu[i]; printf("total = %d , avg = %dn", sum/i); } YES C 제 6 장 배열과 문자열 19

예제 6. 16 2차원 배열의 초기화하는 예제 • #include <stdio. h> • • void

예제 6. 16 2차원 배열의 초기화하는 예제 • #include <stdio. h> • • void main(void) { int array[4][3] = {{100, }, {0, } , {111, 222}, }; int i, j; • • for(i = 0; i < 4; i++) { for(j = 0; j < 3; j++) printf("array[%d] = %dn", i, j, array[i][j]); putchar('n'); } } • • • YES C 제 6 장 배열과 문자열 31

예제 6. 18 행과 열의~ • • • /* 2차원 배열 array[3][5]의 각 행의

예제 6. 18 행과 열의~ • • • /* 2차원 배열 array[3][5]의 각 행의 합과 전체 합을 계산하는 프로그 램 */ #include <stdio. h> #define ROW 3 #define COLUMN 5 void main(void) { int array[ROW][COLUMN]; int i, j; long temp, total = 0; YES C 제 6 장 배열과 문자열 33

예제 6. 18 행과 열의~(계속) • • • • • • for(i = 0;

예제 6. 18 행과 열의~(계속) • • • • • • for(i = 0; i < ROW; i++) { temp = 0; for(j = 0; j < COLUMN; j++) { printf("Input data in array[%d] : ", i, j); scanf("%d", &array[i][j]); temp += array[i][j]; } switch( i ) /* 각 행의 합을 출력 */ { case 0: printf("narray[%d] = %ldnn", i, temp); break; case 1: printf("narray[%d] = %ldnn", i, temp); break; case 2: printf("narray[%d] = %ldnn", i, temp); } /* end of switch */ total += temp; } /* end of for statement */ printf("The total of array is %ld n", total); } YES C 제 6 장 배열과 문자열 34

예제 6. 19 주어진 2차원 배열에서, 모든 요소들의 합 을 계산하고, total을 출력하는 프로그램을

예제 6. 19 주어진 2차원 배열에서, 모든 요소들의 합 을 계산하고, total을 출력하는 프로그램을 작성하라. #include <stdio. h> void main(void) { static int m[][] = { {10, 5, -3}, {9, 0, 0}, {32, 20, 1}, {0, 0, 8} }; int row, column, sum; sum = 0; for( row = 0; row < 4; row++ ) for( column = 0; column < 3; column++ ) sum = sum + m[row][column]; printf("The total is %dn", sum ); } 실행결과 The total is 82 YES C 제 6 장 배열과 문자열 35

(3) 문자열 상수의 초기화 • const char str[10]="String"; • const char str[]="String"; YES C

(3) 문자열 상수의 초기화 • const char str[10]="String"; • const char str[]="String"; YES C 제 6 장 배열과 문자열 40

예제 6. 21 특수문자 처리하는 방법. #include <stdio. h> void main(void) { printf("C Programmingn");

예제 6. 21 특수문자 처리하는 방법. #include <stdio. h> void main(void) { printf("C Programmingn"); printf("C b. Programmingn"); printf("'C' Programmingn"); printf(""C Programming"n"); printf("C t. Programmingn"); printf("C r. Programmingn"); printf("Cnt. Programmingn"); printf("C Programmingan"); } YES C 제 6 장 배열과 문자열 43

6. 4. 2 문자분류 및 문자변환 표준함수 함수 설명 해당문자 isalnum(int c) 알파벳 또는

6. 4. 2 문자분류 및 문자변환 표준함수 함수 설명 해당문자 isalnum(int c) 알파벳 또는 숫자인가? A~Z, a~z, 0~9 isalpha(int a) 알파벳인가? A~Z, a~z isascii(int a) ASCII문자인가? iscntrl(int a) 제어문자인가? BEL, BS, CR, FF, HT, NL, VT isdigit(int a) 10진수인가? 0~9 isgraph(int a) 그래픽 문자인가? isalnum or ispunct islower(int a) 소문자인가? a~z isprintf(int a) 출력 가능한 문자인가? isgraph or isspace ispunct(int a) 구두점 문자인가? !#%&(); <=>? []+ , . /: ^_{|} isspace(int a) 공백문자인가? CR, FF, HT, NL, VT, space isupper(int a) 대문자인가? A~Z isxdigit(int a) 16진수인가? 0~9, A~F, a~f YES C 제 6 장 배열과 문자열 44

예제 6. 22 toupper()를 사용하여 문자열을 한번에 한 문자 씩 대문자로 변환하는 예제. #include

예제 6. 22 toupper()를 사용하여 문자열을 한번에 한 문자 씩 대문자로 변환하는 예제. #include <stdio. h> #include <ctype. h> void main(void) { char name[80]; int loop; printf("Enter in a name in lowercasen"); scanf( "%s", name ); for( loop = 0; name[loop] != 0; loop++ ) name[loop] = toupper( name[loop] ); printf("The name in uppercase is %s", name); } YES C 제 6 장 배열과 문자열 46

예제 6. 23 예제 6. 22를 strupr함수를 사용하여 한꺼번에 대문자로 바꾸기 #include <stdio. h>

예제 6. 23 예제 6. 22를 strupr함수를 사용하여 한꺼번에 대문자로 바꾸기 #include <stdio. h> #include <ctype. h> void main(void) { char name[80]; /*declare an array of characters 0 -79*/ printf("Enter in a name in lowercasen"); scanf( "%s", name ); strupr( name ); printf("The name is uppercase is %sn", name ); strlwr( name ); printf("The name is lowercase is %sn", name ); } YES C 제 6 장 배열과 문자열 47

표 6. 4 보이는 문자(visible graphic character) 구분 alphabet digit underscore 해당 문자 A~Z,

표 6. 4 보이는 문자(visible graphic character) 구분 alphabet digit underscore 해당 문자 A~Z, a~z 0~9 _ ! " # % & ' ( ) * + , - punctutation. / : ; < = > ? [ ] ^ { | } ~ YES C 제 6 장 배열과 문자열 48

표 6. 5 추가 그래픽 문자 (Additional graphic characters) 문자 space BEL BS FF

표 6. 5 추가 그래픽 문자 (Additional graphic characters) 문자 space BEL BS FF NL CR HT VT 기능 Blank space Bell(경고음) Back space Form feed New line Carriage return Horizontal tab Vertical tab YES C 제 6 장 배열과 문자열 49

표 6. 7 숫자를 이용한 제어 문자 (numerical escape sequence) 표시 d or ddd

표 6. 7 숫자를 이용한 제어 문자 (numerical escape sequence) 표시 d or ddd 기능 10진수 escape sequence xh or xhh 16진수 escape sequence oo or ooo 8진수 escape sequence YES C 제 6 장 배열과 문자열 51

예제 6. 24 문자처리함수를 이용한 프로그램이다. 그 결 과를 분석해보자 #include <stdio. h> #include

예제 6. 24 문자처리함수를 이용한 프로그램이다. 그 결 과를 분석해보자 #include <stdio. h> #include <ctype. h> #include <conio. h> void main(void) { int c; printf("n isalpha : n"); for(c=0; c<127; c++) if(isalpha(c))putchar(c); printf("n isdigit : n"); for(c=0; c<127; c++) if(isdigit(c))putchar(c); printf("n islower : n"); YES C 제 6 장 배열과 문자열 52

예제 6. 24(계속) for(c=0; c<127; c++) if(islower(c))putchar(c); printf("n ispunct : n"); for(c=0; c<127; c++)

예제 6. 24(계속) for(c=0; c<127; c++) if(islower(c))putchar(c); printf("n ispunct : n"); for(c=0; c<127; c++) if(ispunct(c))putchar(c); printf("n isupper : n"); for(c=0; c<127; c++) if(isupper(c))putchar(c); getch(); } YES C 제 6 장 배열과 문자열 53

6. 4. 3 데이터 변환 함수 기능 double atof(char *string) 스트링을 부동소수점으로 변환 int

6. 4. 3 데이터 변환 함수 기능 double atof(char *string) 스트링을 부동소수점으로 변환 int atoi(char *string) 스트링을 long integer로 변환 char *itoa(int value, char *string, int radix) 정수를 주어진 기수법으로 스트링으 로 변환 char *ltoa(long value, char long 정수를 주어진 기수법으로 스트 *string, int radix) 링으로 변환 double strtod(char *string, 스트링을 double 부동소수점으로 변 char *endptr) 환 long strtol(char *string, 스트링을 주어진 기수법으로 long 정 char *endptr, int radix) 수로 변환 unsigned long strtoul(char *string, char *endptr, int 스트링을 unsigned long으로 변환 radix) 54 YES C 제 6 장 배열과 문자열

6. 4. 4 스트림 입출력 함수 기능 int fgetc(Fl. LE *file_pointer) 스트림으로부터 한 문자

6. 4. 4 스트림 입출력 함수 기능 int fgetc(Fl. LE *file_pointer) 스트림으로부터 한 문자 가져오기 char *fgets(char *string, int maxchar, FILE *file_pointer) 파일로부터 스트링 읽기 int fputc(int c, FILE *file_pointer) 스트림에 한 문자 쓰기 int fputchar(int c) stdout에 한 문자 쓰기 int fputs(char *string, FILE *file_pointer) 스트림에 스트링 쓰기 int getc(Fl. LE *file_pointer) 스트림으로부터 한 문자 읽기 int getchar(void) stdin으로부터 한 문자 읽 기 char *gets(char *buffer) stdin으로부터 한 줄을 읽 어서 버퍼에 넣기 int printf(const char *format, args); YES C 제 6 장 배열과 문자열 stdout으로 형식화된 출 57 력 쓰기

6. 4. 4 스트림 입출력(계속) 함수 int putc(int c, FILE *file_pointer) 기능 한 문자를

6. 4. 4 스트림 입출력(계속) 함수 int putc(int c, FILE *file_pointer) 기능 한 문자를 스트림에 쓰기 int putchar(int c) stdout에 한 문자 쓰기 int puts(char *string) stdout에 스트링 쓰기 int scanf(char *format_string, args) stdin으로부터 형식화된 입력 읽기 int sprintf(char *string, 스트링에 형식화된 출력 char *format_string, args) 쓰기 int sscanf(char *buffer, char *format_string, args) YES C 제 6 장 배열과 문자열 스트링으로부터 형식화된 입력 읽기 58

6. 4. 5 문자열 처리 함수 기능 char *strcat(char *s 1, const char s

6. 4. 5 문자열 처리 함수 기능 char *strcat(char *s 1, const char s 1에 s 2를 추가하고 s 1을 *s 2); return char *strchr(const char *s, int s에서 c가 발생하는 첫 번째 c); 위치 찾기 int strcmp (const char *s 1, const char *s 2) s 1과 s 2를 비교, 두 문자열 이 같으면 0, s 1이 크면 양 수, 아니면 음수를 return int strcoll (const char *s 1, const char *s 2); s 1과 s 2비교, s 1이 크면 양 수, 같으면 0, 아니면 음수를 return char* strcpy(char *s 1, const char s 2에서 s 1로 스트링 복사 *s 2) size_t strcspn (const char *s 1, const char *s 2); YES C 제 6 장 배열과 문자열 1에서 s 2와 같은 문자열 검 색, 문자열이 있으면 위치 return 59

예제 6. 28 문자열 처리 함수를 이용한 프로그램이다. #include <stdio. h> #include <conio. h>

예제 6. 28 문자열 처리 함수를 이용한 프로그램이다. #include <stdio. h> #include <conio. h> #include <string. h> void main(void) { char a[30], b[30], pass[10], s 1[15], s 2[15]; printf("/*strcat()=string 1+string 2 */nn"); printf("input string 1: "); gets(a); printf("input string 2: "); gets(b); printf("nn string 1 + string 2 = %s n", strcat(a, b)); printf("nn/* strcmp() = compare string 1 with string 2 */ n"); printf("nnif your password is. . h 6614 n"); YES C 제 6 장 배열과 문자열 63

예제 6. 28(계속) do{ printf("Your Password : "); gets(pass); if(strcmp(pass, "h 6614")) printf("invalid passwordn");

예제 6. 28(계속) do{ printf("Your Password : "); gets(pass); if(strcmp(pass, "h 6614")) printf("invalid passwordn"); else break; } while(1); printf("n O. K"); getch(); printf("n/*strcpy(s 1, s 2)=Copy s 2 to s 1*/ n"); printf("input string s 2 : "); gets(s 2); strcpy(s 1, s 2); printf("s 1 = %sn", s 1); getch(); 64 } YES C 제 6 장 배열과 문자열