1 / 21

Computer Graphics (Part 1: C/C++ Programming)

Computer Graphics (Part 1: C/C++ Programming). HyunKi Hong Dept. of Image Eng., GSAIM ChungAng Univ. Contents. 상수와 변수 수식과 연산자 제어 명령문 파생자료형 ( 배열 , 포인터 , 구조체 등 ) C++ 함수 클래스와 객체. 포인터 변수 배열. 포인터 변수 대신 포인터 배열 변수 를 사용하여 문자배열 참조 데이타형 *배열명 [ 첨자 ]; char *ch[5];

luisa
Télécharger la présentation

Computer Graphics (Part 1: C/C++ Programming)

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. Computer Graphics(Part 1: C/C++ Programming) HyunKi Hong Dept. of Image Eng., GSAIM ChungAng Univ.

  2. Contents 상수와 변수 수식과 연산자 제어 명령문 파생자료형(배열, 포인터, 구조체 등) C++ 함수 클래스와 객체

  3. 포인터 변수 배열 • 포인터 변수 대신 포인터 배열 변수를 사용하여 문자배열 참조 • 데이타형 *배열명[첨자]; • char *ch[5]; • 5개의 임의의 길이의 문자열 사용

  4. 포인터 변수 배열 예 int i; char *ch[4]={"ABC", "Image", "GSAIM", "CAU" }; for(i=0;i<4;i++) printf("ch[%d]= %d %c %s\n",i, ch[i], *ch[i], ch[i]); ch[0]는 문자열 “ABC”가 저장되어 있는 시작 주소 4 /21

  5. 배열 인자 #include <stdio.h> void array(int array1[], int number) { int i; for(i = 0; i <number; i++) printf("%d vs. %d \n", array1[i], *(array1+i)); for (i = 0; i <number; i++) printf("%d vs. %d \n", &(array1[i]), (array1+i)); } void main(void) { int little[5] = {6,7,8,9,10 }; array(little, 5); } int *array1 5 /21

  6. 배열 인자 #include <iostream.h> void values(int array[], int number) { int i; for (i = 0; i <number; i++) { cout << "input value" << i << '\n'; cin >> array[i]; cout << array[i] << '\n'; } } void main(void) { int num[3]; int i; values(num, 3); for (i = 0; i <3; i++) cout << num[i] << "\n"; } 6 /21

  7. 변수/배열/구조체 • 변수 • 단독주택 • int x; //4평(byte)짜리 단독주택 1가구 • 배열 • 같은 평수로 구성된 아파트 • int x[5]; //4평짜리 5가구 • 동일한 형(type)의 데이타들을 하나의 단위로 취급 • 구조체(변수/배열/구조체들의 모임) • 다른 형(type)의 데이타들을 하나의 단위로 취급

  8. 지금까지 주로 사용한 자료형 • 숫자를 저장 • int • double • 문자를 저장 • char • 문자열 저장 • char * 구조체를 이용해서 다양한 자료형 사용!!! - 사람(문자형) - 성적(숫자형) …….

  9. 구조체(structure) • 다양한 자료들이 한 묶음으로 된 새로운 자료형을 만들면? • 학생정보 • 이름: 홍길동(char name[10]) • 나이: 28(int age) • 몸무게:74.5(double weight) • 성적처리 struct Student{ char name[10]; int age; double weight; }; 100명 학생의 성적처리 char name[100][10]; int num[100], s[100][4]; double avg1[100];

  10. 예:struct man형 구조체 형틀(template) 선언 • struct man이라는 새로운 데이터 형 • 구조체 형틀 자체는 변수나 배열 등을 메모리에 만드는 것이 아니고, 구조체를 설계하는 지침만을 컴파일러에게 알려 준다. structman{ char name[7]; //이름 자료형1 변수이름1 char mf; //성별 자료형2 변수이름2 int age; //나이 …… } ; struct man hhk; struct man형 선언 구조체 태그 구조체 멤버 man형 객체(object) 정의

  11. C/C++에서 구조체 선언 struct PERSON // Declare PERSON struct type { int age; // Declare member types long ss; float weight; char name[25]; } family_member; // Define object of type PERSON struct PERSON sister; // C style structure declaration PERSON brother; // C++ style structure declaration sister.age = 13; // assign values to members brother.age = 7;

  12. 구조체 선언 방법 • 성적레코드 struct score{ char hakbun[9]; char name[15]; int kor,eng,tot; double ave; }; score hkd; struct score{ char hakbun[9]; char name[15]; int kor,eng,tot; double ave; }hkd; typedef struct { char hakbun[9]; char name[15]; int kor,eng,tot; double ave; } score; score hkd; 객체

  13. 구조체 멤버 참조 • 구조체를 구성하는 원소(멤버)들에 대한 대입 또는 연산 처리 수행 시, 일반 변수와는 다르게 구조체 멤버 연산자(structure member operator) (.)을 반드시 구조체 이름과 멤버이름 사이에 넣어서 연결시켜야 함. 객체이름.멤버이름 typedef struct { char hakbun[9]; char name[15]; int kor, eng, tot; double ave; }SUNGJUK; SUNKJUK hkd; hkd.kor=80; hkd.eng=90; hkd.tot=hkd.kor+hkd.eng; printf(“%c”,hkd.hakbun[1]); printf(“%s”,hkd.name);

  14. 구조체 변수의 초기화(1) #include <iostream.h> struct movie { char name[20]; int stars; }; void main() { movie titles[2] = {{"삼총사", 4}, {"사선에서",2}}; for(int i=0; i<2; i++) cout<<titles[i].name<<" , "<<titles[i].stars<<endl; } 14/21

  15. 구조체 변수의 초기화(2) #include <stdio.h> typedef struct{ // SCORE형 구조체 선언 char hakbun[9]; char name[15]; int kor, eng, tot; double ave; }SCORE; void main() { SCORE h={"9812000", "홍길동",80,90}; // SCORE형 변수(객체) h 정의 h.tot=h.kor+h.eng; printf("%s %s %d %d %d",h.hakbun,h.name,h.kor,h.eng,h.tot); } 구조체변수의 기억 부류는 구조체 형틀이 선언되어 있는 위치와 관계 없고, 그 변수가 정의되어 있는 위치에 따른다. 15/21

  16. 구조체 변수의 대입 #include <stdio.h> typedef struct{ char hakbun[8]; char name[10]; int kor,eng,tot; double ave; }SCORE; void main() { SCORE j,h={"9812000", "홍길동",80,90}; h.tot=h.kor+h.eng; j=h; printf("%s %s %d %d %d\n",h.hakbun,h.name,h.kor,h.eng,h.tot); printf("%s %s %d %d %d",j.hakbun,j.name,j.kor,j.eng,j.tot); } 16/21

  17. 구조체 배열 • 성적 처리할 학생의 수가 많을 경우 구조체를 이용해도 프로그램이 복잡해진다. • 동일한 구조를 가진 대량의 레코드를 처리할 경우 구조체 배열을 적용하면 효과적인 프로그램 작성 가능 int i, j; for(i=0;i<2;i++) for(j=i+1;j<3;j++){ if(man[i].w<man[j].w){ temp=man[i]; man[i]=man[j]; man[j]=temp; } } printf("\n 이름 \t체중"); for(i=0;i<3;i++) printf("\n %s %5.1f",man[i].name,man[i].w); } #include <stdio.h> typedef struct{ char name[10]; double w; }WEIGHT; void main() { WEIGHT man[3]={{"홍길동",54.5}, {"엄청군",125.6}, {"갈비양",35.7}}; WEIGHT temp; 17/21

  18. 구조체 포인터 • 구조체가 너무 커지면 구조체 전체를 다른 함수로 전달 시, 그 크기 만큼의 버퍼를 확보해야 하는 등의 오버헤드 발생 • 구조체 자체를 전달하는 것 보다 포인터를 전달하는 것이 용량이나 실행 속도면에서 효율적 struct score{ char hakbun[8]; char name[10]; int kor,eng,tot; double ave; }; score *hkd; 포인터를 이용한 멤버의 접근 (*hkd).kor=90; hkd->kor=90; //구조체 포인터 hkd가 가르키고 있는 구조체의 멤버 kor -> : 구조체포인터 연산자 연산자 우선순위

  19. 구조체 포인터 예제 #include <stdio.h> #include <string.h> struct person{ char name[10]; int birth; }; void main() { person man,*boy; boy=&man; strcpy(man.name, "홍길동"); boy->birth=880709; printf("%s %d",boy->name, man.birth); } 포인터 객체 int x, *px; px=&x; 19/21

  20. 구조체 배열과 포인터 예제 #include <stdio.h> #include <string.h> struct person{ char name[10]; int birth; }; void main() { struct person man[3]={{"홍길동",980119}, {"아무게",920612}, {"모모군",860808}}; struct person *boy; boy=man; printf("%d %d %d",man[1].birth,(*(boy+1)).birth,(boy+1)->birth); } int x[3], *px; px=x; 20/21

  21. 구조체 변수의 매개변수 전달 call by value call by reference #include <stdio.h> struct score{ int kor,eng; }; int hap(score hkd); void main() { score jumsu={80,90}; int sum; sum=hap(jumsu); printf("%d",sum); } int hap(score hkd) { return(hkd.eng+hkd.kor); } #include <stdio.h> struct score{ int kor,eng; }; int hap(score *hkd); void main() { score jumsu={80,90}; int sum; sum=hap(&jumsu); printf("%d",sum); } int hap(score *hkd) { return(hkd->eng+hkd->kor); } 21/21

More Related