1 / 14

컴퓨터의 기초 제 5 강 - 함수 , 문자열

컴퓨터의 기초 제 5 강 - 함수 , 문자열. 2006 년 4 월 17 일. 함수 사용법. 기본형태 return_type function_name(parameter_lists) { statement; …; return Return_Value; } 예시 int myFunc (int a, int b) { int sum = a + b; return sum; }. 변수의 사용 범위. int a, b; int function1(int x, int y) { x++; y--;

lucius
Download Presentation

컴퓨터의 기초 제 5 강 - 함수 , 문자열

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 컴퓨터의 기초 제 5강- 함수, 문자열 2006년 4월 17일

  2. 함수 사용법 • 기본형태 return_type function_name(parameter_lists) { statement; …; return Return_Value; } • 예시 int myFunc (int a, int b) { int sum = a + b; return sum; }

  3. 변수의 사용 범위 int a, b; int function1(int x, int y) { x++; y--; return x+y; } int function2() { int x=1; y=1; a = x*2; b=y+2; return a+b; } void main() { int x , y; x = 10; y = 20; function1(x, y); function2(); }

  4. 재귀 함수 • ‘recursion’ int power1(int x, int n) { int y; if ( n == 1 ) return x; else { y = power1(x, n/2); if( n % 2 == 1 ) return x*y*y; else return y*y; } }

  5. 재귀 함수 • ‘recursion’ x=5, n=3 int power1(5,3) { int y; if ( 3 == 1 ) return 5; else { y = power1(5, 3/2); <-recursion if( 3 % 2 == 1 ) return 5*y*y; else return y*y; } }

  6. 재귀 함수 • ‘recursion’ x=5, n=1 int power1(5,1) { int y; if ( 1 == 1 ) return 5; else { y = power1(5, 3/2); if( 3 % 2 == 1 ) return 5*y*y; else return y*y; } }

  7. 재귀 함수 • ‘recursion’ x=5, n=3 int power1(5,3) { int y; if ( 3 == 1 ) return 5; else { y = 5; <-recursion if( 3 % 2 == 1 ) return 5*5*5; else return y*y; } }

  8. 반복법 • ‘iteration’ int power2(int x, int n) { int y; while( n > 1) { x *= n; n--; } return x; }

  9. 문자열 • 문자열의 끝 C에서는 문자열이 NULL로 종료됨 char str[10]; str[0] = ‘L’; str[1] = ‘e’; str[2] = ‘t’; str[3] = ‘ ’; str[4] = ‘i’; str[5] = ‘t’; str[6] = NULL; str[7] = ‘b’; str[8] = ‘e’; str[9] = NULL; printf(“%s”, str);

  10. 문자열 관련 함수 • 문자열 입력 scanf, gets, getch, getchar • 문자열 조작 strcpy, strlen, strcmp, strlen, strcat, strcmp • 문자열 변환 tolower, toupper, isupper, islower, isdigit, isalpha • 문자열 처리 strchr, strtok

  11. 기타 유용한 함수 • 형변환함수 atoi, atof, itoa, itof • 수치계산함수 - <math.h> log, sin, cos, tan, pow • 강제종료함수 exit • 랜덤함수 rand

  12. Homework • Assignment #5 • 본 PPT에 나온 recursion과 iteration예제는 sound한 프로그램이 아니다. 어떠한 integer입력이 오더라도 맞는 결과를 출력할 수 있는 함수를 작성하시오. • 위에서 만든 함수를 이용하여 유저의 입력을 받아 정수 2개를 받아서 지수계산을 하는 프로그램을 작성하시오. 예) x=5, n=-2 -> 0.04

  13. Homework • Assignment #6 • 10글자 이내의 영단어를 입력받아, 입력받은 것과 순서가 반대이고, 대소문자도 반대인 문자열을 출력하는 프로그램을 작성하시오. TooHard -> DRAhOOt • 4글자 이내의 영단어를 입력받아, 글자 순서의 모든 조합을 출력하는 프로그램을 작성하시오. sun -> sun, snu, usn, uns, nsu, nus • 영단어 3개를 입력받아, 알파벳 순서대로 정렬하여 출력하는 프로그램을 작성하시오. love, lovely, like -> like, love, lovely

  14. Any Questions?

More Related