컴퓨터의 기초 제 5 강 - 함수 , 문자열
DESCRIPTION
컴퓨터의 기초 제 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--; - PowerPoint PPT Presentation Copyright Complaint Adult Content Flag as Inappropriate Report This 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - Presentation Transcript 컴퓨터의 기초 제 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; }TRANSCRIPT
컴퓨터의 기초 제 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--;
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(); }
재귀 함수 ‘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;}
}
재귀 함수 ‘recursion’ x=5, n=3
int power1(5,3) {int y;if ( 3 == 1 ) return 5;else {
y = power1(5, 3/2); <-recursionif( 3 % 2 == 1 )
return 5*y*y;else
return y*y;}
}
재귀 함수 ‘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;}
}
재귀 함수 ‘recursion’ x=5, n=3
int power1(5,3) {int y;if ( 3 == 1 ) return 5;else {
y = 5; <-recursionif( 3 % 2 == 1 )
return 5*5*5;else
return y*y;}
}
반복법 ‘iteration’
int power2(int x, int n) {int y;while( n > 1) {
x *= n;n--;
}return x;
}
문자열 문자열의 끝
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);
문자열 관련 함수 문자열 입력
scanf, gets, getch, getchar
문자열 조작strcpy, strlen, strcmp, strlen, strcat, strcmp
문자열 변환tolower, toupper, isupper, islower, isdigit, isalpha
문자열 처리strchr, strtok
기타 유용한 함수 형변환함수
atoi, atof, itoa, itof
수치계산함수 - <math.h>log, sin, cos, tan, pow
강제종료함수exit
랜덤함수rand
Homework
Assignment #5 본 PPT 에 나온 recursion 과 iteration
예제는 sound 한 프로그램이 아니다 . 어떠한 integer 입력이 오더라도 맞는 결과를 출력할 수 있는 함수를 작성하시오 .
위에서 만든 함수를 이용하여 유저의 입력을 받아 정수 2 개를 받아서 지수계산을 하는 프로그램을 작성하시오 .예 ) x=5, n=-2 -> 0.04
Homework
Assignment #6 10 글자 이내의 영단어를 입력받아 , 입력받은 것과
순서가 반대이고 , 대소문자도 반대인 문자열을 출력하는 프로그램을 작성하시오 .TooHard -> DRAhOOt
4 글자 이내의 영단어를 입력받아 , 글자 순서의 모든 조합을 출력하는 프로그램을 작성하시오 .sun -> sun, snu, usn, uns, nsu, nus
영단어 3 개를 입력받아 , 알파벳 순서대로 정렬하여 출력하는 프로그램을 작성하시오 .love, lovely, like -> like, love, lovely
Any Questions?