Function malloc void mallocsizet size Allocate memory block
요구사항 분석 � � � 입력으로 텍스트 파일을 사용한다. 소문자와 대문자를 구별하지 않고 영문자의 수를 센다. 영문자의 수를 출력한다. Function malloc() void* malloc(size_t size); Allocate memory block Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced. 충북대학교 소프트웨어학과 4
문자 코드 (ASCII Code) b 3 b 2 b 1 b 0 000(0) 0000(0) NULL 0001(1) SOH 0010(2) STX 0011(3) ETX 0100(4) EOT 0101(5) ENQ 0110(6) ACK 0111(7) BEL 1000(8) BS 1001(9) HT 1010(A) LF 1011(B) VT 1100(C) FF 1101(D) CR 1110(E) SO 1111(F) SI 001(1) DLE DC 1 DC 2 DC 3 DC 4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 010(2) SP ! “ # $ % & ‘ ( ) * + , . / char ch = ‘A’; printf(“%c, %d, %x”, ch, ch); b 6 b 5 b 4 011(3) 100(4) 0 @ 1 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I : J ; K < L = M > N ? O 101(5) P Q R S T U V W X Y Z [ ] ^ _ 110(6) ` a b c d e f g h i j k l m n o 111(7) p q r s t u v w x y z { | } ~ DEL // A, 65, 41 충북대학교 소프트웨어학과 5
deallocate(), print_words() 구현 1. 2. 3. 4. 5. 6. 7. void deallocate() { int n; for (n=0; n<nword; n++) if (pstrarray[n] != NULL) free(pstrarray[n]); } // nword 까지 // 문자열이 연결되어 있다면 // 기억 공간을 해제한다. 8. void print_words() 9. { 10. int n; 11. printf("n정렬 후 출력: 단어 수=%dn", nword); 12. for (n=0; n<nword; n++) 13. puts(pstrarray[n]); 14. } 충북대학교 소프트웨어학과 24
단어 정렬 구현 1. 2. 3. 4. void sort_string() { int n, m, minindex; char *temp; 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. } // selection_sort(int array[]) 복사하여 수정한다. // 문자열을 교체하기 위하여 임시 변수를 추가한다. // for (n=0; n<SIZE-1; n++) // 배열의 크기를 nword로 변경한다. for (n=0; n<nword-1; n++) { minindex = n; //for (m=n; m<SIZE; m++) // 배열의 크기를 nword로 변경한다. for (m=n; m<nword; m++) //if (array[minindex] > array[m]) // 문자열 비교 함수로 교체한다. if (strcmp(pstrarray[minindex], pstrarray[m]) > 0) minindex = m; // swap(&array[minindex], &array[n]); // 직접 교체한다. temp = pstrarray[minindex]; pstrarray[minindex] = pstrarray[n]; pstrarray[n] = temp; } 충북대학교 소프트웨어학과 26
구조체 사용 구조체 정의 구조체 변수 선언 struct 구조체이름 { 구조체 멤버 목록; }; struct 구조체이름 변수이름; struct STUDENT_INFO { char name[20]; // 이름 char ID[20]; int english; int math; }; struct STUDENT_INFO chulsoo; struct STUDENT_INTO class[30]; struct STUDENT_INFO *sptr; 구조체 변수 사용 // 학번 // 영어 성적 // 수학 성적 구조체변수. 멤버변수 구조체포인터->멤버변수 scanf(“%s”, chulsoo. name); chulsoo. english = 90; printf(“%s”, class[0]. ID); class[1]. math = 85; sptr = &chulsoo; sptr->english = 55; 충북대학교 소프트웨어학과 31
기억장소 할당 및 해제 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. #include <stdlib. h> #include <string. h> // malloc(), free() 선언 선언 // memset() 함수 선언 void intialize() { words = (struct WORDCOUNT *)malloc(sizeof(struct WORDCOUNT)*MAXWORD); memset(words, 0, sizeof(struct WORDCOUNT)*MAXWORD); nwords = 0; // 저장된 단어 수 = 0 } void deallocate() { free(words); } 충북대학교 소프트웨어학과 36
소문자 변환 // tolower() 함수 정의 추가 1. #include <ctype. h> 2. void convert_lower(char *str); // 함수 선언 추가 3. void read_file() 4. { 5. …. 6. while (token!= NULL) 7. { // 함수 호출 추가 8. convert_lower(token): 9. printf("%sn", token); 10. token = strtok(NULL, " , . !? tn"); } 11. 12. } 13. void convert_lower(char *str) 14. { 15. while (*str != NULL) 16. { // 문자열의 끝이 아니면 17. *str = tolower(*str); // 소문자로 변환한다. 18. str++; // 다음 문자를 처리한다. } 19. 20. } 충북대학교 소프트웨어학과 42
단어 출력 함수 구현 1. 2. 3. 4. 5. 6. void print_words() { int n; for (n=0; n<nword; n++) printf("%3 d. %-18 s: %dn", n+1, words[n]. str, words[n]. count); } 충북대학교 소프트웨어학과 50
- Slides: 51