max 1 int maxint x int y return
- Slides: 28
예제: 중복 부프로그램 max #1 int max(int x, int y) { return x>y ? x : y; } 다음 max() 호출의 의미는? ü max(2, 3) ü max(2. 1, 3. 2) ü max(1, 3, 2) ü max(2. 1, 3) max #2 double max(double x, double y) { return x>y ? x : y; } max #3 int max(int x, int y, int z) { return x>y ? (x>z ? x : z) : (y > z ? y : z); } 5
예제: 중복 부프로그램 • 함수 호출의 의미는? max #1 int max(int x, int y) { return x>y ? x : y; } max #2 max(2. 1, 3); double max(double x, double y) { return x>y ? x : y; } max #3 int max(int x, int y, int z) { return x>y ? (x>z ? x : z) : (y > z ? y : z); } 7
중복 부프로그램 C++ int=>double, double =>int의 강제 변환 모두 허용 max #1 int max(int x, int y) { return x>y ? x : y; } max #2 max(2. 1, 3); double max(double x, double y) { return x>y ? x : y; } max #3 int max(int x, int y, int z) { return x>y ? (x>z ? x : z) : (y > z ? y : z); } 10
중복 부프로그램 Java 확장 변환의 경우만 강제변환 허용 max #1 int max(int x, int y) { return x>y ? x : y; } max #2 max(2. 1, 3); double max(double x, double y) { return x>y ? x : y; } max #3 int max(int x, int y, int z) { return x>y ? (x>z ? x : z) : (y > z ? y : z); } 11
중복 부프로그램 Ada 타입 강제 변환을 허용하지 않음 max #1 int max(int x, int y) { return x>y ? x : y; } max #2 max(2. 1, 3); double max(double x, double y) { return x>y ? x : y; } max #3 int max(int x, int y, int z) { return x>y ? (x>z ? x : z) : (y > z ? y : z); } 12
C++ 포괄형 함수 template<class TYPE> void copy(TYPE a[], TYPE b[], int n) { for (int i=0; i < n; i++) a[i] = b[i]; } double a 1[10], a 2[10]; char b 1[10], b 2[10]; int c 1[30], c 2[30]; … copy(a 1, a 2, 10); copy(b 1, b 2, 10); void copy(double a[], double b[], int n) { for (int i=0; i < n; i++) a[i] = b[i]; } 사 례 화 void copy(char a[], char b[], int n) { for (int i=0; i < n; i++) a[i] = b[i]; } copy(c 1, c 2, 30); copy(a 1, b 1, 10); void copy(int a[], int b[], int n) { for (int i=0; i < n; i++) a[i] = b[i]; } 20
C++ 포괄형 함수 template<class TYPE 1, TYPE 2> void copy(TYPE 1 a[], TYPE 2 b[], int n) { for (int i=0; i < n; i++) a[i] = b[i]; } 포괄형 매개변수를 2개 갖는 템플리트 함수 정의 double a 1[10], a 2[10]; char b 1[10], b 2[10]; int c 1[30], c 2[30]; … 사 례 화 void copy(int a[], char b[], int n) { for (int i=0; i < n; i++) a[i] = b[i]; } copy(a 1, b 1, 10); 21
C++ 포괄형 함수 • generic_sort() 함수 작성 template <class Type> void generic_sort(Type list[], int len) { int i, j; Type temp; for (i = 0; i < len - 1; top++) for (j = i + 1; j < len - 1; j++) if (list[i] > list[j]) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } int a[100]; float b[100]; … generic_sort(a, 100); generic_sort(b, 100); 22
Java 포괄형 메소드 • 포괄형 매개변수는 클래스에 제한 – 기본 타입은 허용하지 않음 – 클래스 범위 제한 public static <T> T min(T[] a) T가 compart. To()를 포함하는 것을 어떻 게 아는가? { if (a == null || a. length ==0) return null; T smallest = a[0]; for (int i=1; i <a. length; i++) if (smallest. compare. To(a[i]) > 0) smallest = a[i]; return smallest; } 24
Java 포괄형 메소드 public static <T> T min(T[] a) { if (a == null || a. length ==0) return null; T smallest = a[0]; for (int i=1; i <a. length; i++) if (smallest. compare. To(a[i]) > 0) smallest = a[i]; return smallest; } T를 Comparable를 구현하는 클래스로 제한 public static <T extends Comparable> T min(T[] a) {…} 25
Java 포괄형 메소드 • 포괄형 타입을 이용한 generic_sort() public static <T extends Comparable> void generic_sort(T[] a) { for (int i = 0; i<a. lenghth; i++) for (int j= i+1; j <a. length; j++) if (a[i]. Compared. To(a[j]) > 0) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; } } 26
- Int max(int x int y)
- Sum0
- Maxint
- C maxint
- Int max int min c++
- Inheritance calculator
- Public void drawsquare(int x, int y, int len)
- Divideint
- Absolute maximum and minimum
- Css inline vs external
- Void
- Int factorial(int n)
- Int main int argc char argv
- Interface myinterface int foo(int x)
- Int main int num 4
- Arduino int to const char
- Int main(int argc, char** argv)
- Void swap(int a int b)
- Is methane lighter than air
- Unlevered return on assets
- Conveyor guarding
- Swapd fee calculator
- Return on sales formula
- Npv exercises
- Nip guards on conveyor belts
- How to find expected return
- Lesson 4 parameters and return make
- Separation initiation return