Lab 6 1 void main int AROWSCOLS 1
Lab 6 -1: 행렬연산 � 행렬의 합, 차 구하기 void main() { int A[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int B[ROWS][COLS] = {{11, 12, 13}, {14, 15, 16}, {17, 18, 19}}; int C[ROWS][COLS] = {0}; printf("A = n"); print_matrix(A); printf("B = n"); print_matrix(B); add_matrix(A, B, C); printf("A + B = n"); print_matrix(C); sub_matrix(B, A, C); printf("B - A = n"); print_matrix(C); } � 함수 구현 ◦ void print_matrix(int m[ROWS][COLS]) ◦ void add_matrix(int a[ROWS][COLS], int b[ROWS][COLS], int c[ROWS][COLS]) ◦ void sub_matrix(int a[ROWS][COLS], int b[ROWS][COLS], int c[ROWS][COLS])
Lab 6 -2: 행렬 ADT 사용 � 행렬을 단순 배열이 아닌 구조체를 이용해 정의 #define MAXROWS 100 #define MAXCOLS 100 typedef struct matrix { int rows, cols; int data[MAXROWS][MAXCOLS]; } Matrix; � Lab ◦ ◦ ◦ 6 -1 A, B, C, 와 모든 함수 재 작성 Matrix A = {3, 3, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}} Matrix B = {3, 3, {{11, 12, 13}, {14, 15, 16}, {17, 18, 19}}} Matrix C print_matrix(Matrix *m) add_matrix(Matrix *a, Matrix *b, Matrix *c) sub_matrix(Matrix *a, Matrix *b, Matrix *c)
Lab 6 -3: 구조체 연습 � 복소수 ◦ 정의 : x = real + img*i typedef struct complex { int real; int img; } COMPLEX; ◦ 연산함수 작성 �COMPLEX add(COMPLEX x, COMPLEX y) � z = x + y z = add(x, y) �COMPLEX sub(COMPLEX x, COMPLEX y) � z = x – y z = sub(x, y) ◦ 출력함수 작성 �print_complex(COMPLEX *x) � (a + bi) 식으로 출력 �img 가 음수인 경우 처리 주의 � (-2 + -2 i) X � (-2 – 2 i) O void main() { COMPLEX x, y, z; scanf("%d %d", &x. real, &x. img); scanf("%d %d", &y. real, &y. img); z = add(x, y); print_complex(&x); printf(" + "); print_complex(&y); printf(" = "); print_complex(&z); printf("n"); z = sub(x, y); print_complex(&x); printf(" - "); print_complex(&y); printf(" = "); print_complex(&z); printf("n"); }
Lab 6 -4: 구조체 선언과 입출력 � 선언 typedef struct student { char name[20]; int year; char no[20]; char phone[20]; } Student; Student *data; scanf("%d", &count); data = (Student *)malloc(sizeof(Student)*count); � 입출력 ◦ Input. Student(Student *s) ◦ Print. Student(Student *s) � 찾기 ◦ scanf("%s", name); ◦ if (strcmp(name, data[i]. name)==0) �Print. Student(&data[i]);
#include <string. h> // 이름 찾기 위해 필요 typedef struct student { char name[20]; int year; char no[20]; char phone[20]; } Student; void Print. Student(Student *s) // 완성할것 { } void Input. Student(Student *s) // 완성할것 { } void main(void) { int i, count, found; char name[20]; // 찾을 이름 입력 용 Student *data; printf("몇명 : "); scanf("%d", &count); data = (Student *)malloc( ); // 완성할것 for (i=0; i<count; i++) { // 입력 부분 완성 printf("n자료번호 %d 입력n", i+1); Input. Student( ); // 완성할것 } for (i=0; i<count; i++) // 출력 Print. Student( ); printf("찾을 이름: "); scanf("%s", name); // 이름 입력 found = 0; for (i=0; i<count; i++) { // 자료 찾기 // 같은 이름을 찾아서 맞으면 break if (strcmp ) // 찾으면 break; } if (i<count) // 찾았다. Print. Student( ); // 완성할것 else printf("찾는 데이타가 없습니다. n"); }
Lab 6 -5: 문자열 처리(배열) � reverse(char � to[], char from[]) 완성 reverse() 한글을 고려한다면? � 한글 조건 ((unsigned char)str[i]>=0 x 80 && (unsigned char)str[i+1]>=0 x 80) #include <string. h> void reverse(char to[], char from[]) { } void main() { char str 1[100] = "Hansung"; char str 2[100] = "University"; char str 3[100] = "Computer"; char str 4[100] = "Engineering"; char str 5[100]; char str 6[100]; int ret; ret = strlen(str 1); printf("strlen(%s)=%dn", str 1, ret); ret = strcmp(str 1, str 1); printf("strcmp(%s, %s) = %dn", str 1, ret); ret = strcmp(str 1, str 2); printf("strcmp(%s, %s) = %dn", str 1, str 2, ret); ret = strcmp(str 1, str 3); printf("strcmp(%s, %s) = %dn", str 1, str 3, ret); strcpy(str 5, str 1); printf("strcpy str 5 = %sn", str 5); strcat(str 5, " "); strcat(str 5, str 2); printf("strcat str 5, str 2 = %sn", str 5); reverse(str 6, str 5); printf("reverse str 5 = %sn", str 6); } ca
Lab 6 -6: 문자열 처리(포인터) � Lab 6 -4. c를 포인터를 사용해서 재작성 ◦ strlen(), strcpy(), strcat(), strcmp(), 대신 ◦ mystrlen(), mystrcpy(), mystrcat(), mystrcmp() ◦ Reverse() 는 한글을 고려 char *mystrcpy(char *to, char *from) {} int mystrlen(char *str) {} int mystrcmp(char *s 1, char *s 2) {} char *mystrcat(char *to, char *from) {} void reverse(char *to, char *from) {} void main() { ret = mystrlen(str 1); printf("mystrlen(%s)=%dn", str 1, ret); ret = mystrcmp(str 1, str 1); printf("mystrcmp(%s, %s) = %dn", str 1, ret); ret = mystrcmp(str 1, str 2); printf("mystrcmp(%s, %s) = %dn", str 1, str 2, ret); ret = mystrcmp(str 1, str 3); printf("mystrcmp(%s, %s) = %dn", str 1, str 3, ret); mystrcpy(str 5, str 1); printf("mystrcpy str 5 = %sn", str 5); mystrcat(str 5, " "); mystrcat(str 5, str 2); printf("mystrcat str 5 = %sn", str 5); reverse(str 6, str 5); printf("reverse str 5 = %sn", str 6); } rcat
Lab 6 -7: FILE *fp 이용 입/출력 � 파일 입/출력 ◦ FILE *fp; � fp 는 입/출력을 위한 매개 변수 ◦ 기본 함수 � fopen, fprintf, fscanf, fclose, fwrite, fread ◦ 입/출력 예 FILE *fp; // 파일에 출력(write) fp = fopen("data. txt", "w"); // w를 write mode, a는 추가(append) fprintf(fp, "%dn", id); fprintf(fp, "%sn", name); fprintf(fp, "%sn", phone); fclose(fp); // 파일에서 입력(read) printf("입력 파일 명 : "); scanf("%s", filename); fp = fopen(filename, "r"); // r은 read mode fscanf(fp, "%d", &id); printf("학번=%dn", id); fscanf(fp, "%s", buf); printf("이름=%sn", buf); fscanf(fp, "%s", buf); printf("전화=%sn", buf); fclose(fp);
Lab 6 -10 : 전화번호 관리 � 읽기/쓰기 mode 로 open ◦ ◦ � Menu ◦ � � (1) 목록, (2) 찾기, (3) 추가, (4) 종료 getnumberofdata(fp) ◦ Data 수 계산 : filesize / data size filesize 구하기 � fseek(fp, 0, SEEK_END); // 끝으로 이동 � filesize = ftell(fp); // 현재 위치를 byte로 계산 ndata = filesize / sizeof(struct Phone) ◦ ◦ ◦ 처음으로 이동 : fseek(fp, 0, SEEK_SET); ndata = getnumberofdata(fp); while (ndata--) fread(), printf() ◦ ◦ 처음으로 이동 : fseek(fp, 0, SEEK_SET); fread() 하고 strcmp() ◦ ◦ 끝으로 이동 : fseek(fp, 0, SEEK_END); fwrite(&data, sizeof(data), 1, fp); ◦ 변경, 삭제 ◦ ◦ � fopen(filename, “r+b”); + 는 read/write mode 의미 전체목록 찾기 추가 추가하기 (숙제)
Lab 6 -ACM: Candy War 중간 과정을 출력
- Slides: 16