i Struct int productcode char roductdescription20 Float productprice

  • Slides: 55
Download presentation

i 재고 자료를 위한 구조체 배열 선언 예 Struct { int product_code; char roduct_description[20]

i 재고 자료를 위한 구조체 배열 선언 예 Struct { int product_code; char roduct_description[20] Float product_price; } inventory_data[50]; 2

3 i C에 의한 구조체 배열 선언 예 struct { int product_code; char product_description[20];

3 i C에 의한 구조체 배열 선언 예 struct { int product_code; char product_description[20]; real product_price; } inventory_data[50]; i 구조체 배열의 초기화 예 inventory_data[0]. product_code=754; strcpy(inventory_data[0]. product_description, “carburetor”); inventory_data[0]. product_price=12. 95;

i 레코드를 위한 구조체 선언 struct product { char code[5]; char description[15]; int quantity_on_hand;

i 레코드를 위한 구조체 선언 struct product { char code[5]; char description[15]; int quantity_on_hand; float unit_price; int reorder_point; int outstanding_order_size; } inventory[10000]; i 구조체 배열 원소의 크기 항목 code description quantity_on_hand unit_price reorder_point outstanding_order_size 바이트 크기 5 15 2 4 2 2 ----30 % 단 하나의 문자는 1 바이트, 정수는 2 바이트, 실수는 4 바이트의 주기억장치를 사용한 다고 가정함. 9

10 [예제 2. 2 : 학생 신상 정보] 대학생들에 관한 자료를 저장하고자 한다. 학생

10 [예제 2. 2 : 학생 신상 정보] 대학생들에 관한 자료를 저장하고자 한다. 학생 한 명에 대한 자료를 학생의 이름, 주소, 전 화번호로 구성하고자 한다. i 레코드를 위한 구조체 선언 (방법 1) struct student { char int } one_student; (방법 2) struct addresses { char }; name[31]; address[50]; phone_number; home[50]; campus[50]; struct student { char name[31]; struct addresses address; int phone_number; } one_student;

11 (방법 3) struct student { char name[31]; struct address { char home[50]; char

11 (방법 3) struct student { char name[31]; struct address { char home[50]; char campus[50]; }; int phone_number; } one_student; % 주소 참조 : one_student. address. home one_student. address. campus

12 (방법 4) struct addresses { char home[50]; char campus[50]; }; struct phone {

12 (방법 4) struct addresses { char home[50]; char campus[50]; }; struct phone { int area_code; int exchange; int number; }; struct classes { char course_number[6]; char room_number[4]; int meeting_time; }; struct student { char name[31]; struct addresses address; struct phone_number; struct classes class[6]; } one_student; % 수강 정보 참조 : one_student. class[i]. course_number one_student. class[i]. room_number one_student. class[i]. meeting_time

15 학생 레코드 예 i 구조체의 경우 struct addresses { char }; i home[50];

15 학생 레코드 예 i 구조체의 경우 struct addresses { char }; i home[50]; campus[50]; 가변 레코드의 경우 struct home { char }; home[50]; campus[50]; union addresses { char one_address[50]; struct home two_address; }

16 [예제 3 : 공용체] i 문제 : 사람을 묘사하는 레코드는 고정 항목과 혼인상태를

16 [예제 3 : 공용체] i 문제 : 사람을 묘사하는 레코드는 고정 항목과 혼인상태를 나타내는 가변 항목으로 구성된 다. 만약 그 사람이 결혼을 했으면 배우자의 이름을 저장하며, 결혼하지 않은 사람은 그 항 목에 다른 값(이혼 날짜, 사망 일짜)을 저장한다. i 레코드의 형식 정의 : enum sex_type {FEMALE, MALE}; enum martial_status{DIVORCED, MARRIED, SINGLE, WIDOWED}; union status_desc{ char married_name[25]; char date_of_divorced[6]; char date_of_death[6]; }; struct person { char enum char int enum union }; name[25]; sex_type sex; date_of_birth[6]; number_of_dependents; martial_status; status_descrip;

2. 2. 3 프로그램 [프로그램 2. 1] #include <stdio. h> struct student { int

2. 2. 3 프로그램 [프로그램 2. 1] #include <stdio. h> struct student { int exam_1 score, exam 2_score; char identification[4]; int program_score, total_score; }; char Find_Grade(int score, float average) { if ( score >= ( 1. 13 * average ) ) return ‘A’; else if ( score > average ) return ‘B’; 19

20 else if ( score > ( average - ( 0. 19*average) ) )

20 else if ( score > ( average - ( 0. 19*average) ) ) return ‘C’; else if ( score > ( average - (0. 26*average) ) ) return ‘D’; else return ‘F’; } main() { struct student class[200]; int count, i, number_of_students, sum; char grade; float total_average; printf(“%5 s%12 s%10 s%11 s”, ”ID”, ”EXAM 1”, ”EXAM 2”, ”PROGRAMS”); printf(“%9 s%8 snn”, ”TOTAL”, ”GRADE”);

printf(“%9 s%8 snn”, ”TOTAL”, ”GRADE”); count = 0; sum = 0; while (count <

printf(“%9 s%8 snn”, ”TOTAL”, ”GRADE”); count = 0; sum = 0; while (count < 200 ) { count++; for ( i = 0 ; i < 4; i++) scanf(%c”, &class[count]. identification[i]); scanf(%d”, &class[count]. exam_1_score); scanf(%d”, &class[count]. exam_2_score); scanf(%d”, &class[count]. program_score); class[count]. total_score = 0; if ( class[count]. exam_1_score != -1 ) class[count]. total_score + = class[count]. exam_1_score; if ( class[count]. exam_2_score != -1 ) class[count]. total_score + = class[count]. exam_2_score; if ( class[count]. program_1_score != -1 ) class[count]. total_score + = class[count]. program_score; sum += class[count]. total_score; } 21

} number_of_students = count; total_average = sum/count; for ( count = 0; count <

} number_of_students = count; total_average = sum/count; for ( count = 0; count < number_of_students ; count++) { grade = Find_Grade(class[count]. total_score, total_average); printf(“n %4 s “, class[count]. identification); printf(“%d “, class[count]. exam_1_score); printf(“%d “, class[count]. exam_2_score); printf(“%d “, class[count]. program_score); printf(“%d “, class[count]. total_score); printf(“%c “, grade); } } 22

파일읽기 구성도 OS user program buffer memory File 23

파일읽기 구성도 OS user program buffer memory File 23

File type & Open FILE *fp; fp = fopen(“a. txt”, ”r”); user program variables

File type & Open FILE *fp; fp = fopen(“a. txt”, ”r”); user program variables ki int char *ptr (Buffer) char *base int flag int file kim yosuk princes _iobuf[3] 25 File

파일 열기 및 폐쇄 /* r : 단지 읽기 위하여 열기. */ #include <stdio.

파일 열기 및 폐쇄 /* r : 단지 읽기 위하여 열기. */ #include <stdio. h> main() { FILE *fp; int c; fp = fopen(“a. dat”, ”r”); while(( c = getc(fp))!=EOF) putchar(c); fclose(fp); } 27 /* w+ : 읽고 쓰기 위한 열기. 없으면 생성, 있으면 무시 */ #include <stdio. h> main() { FILE *fp 1, *fp 2; int c; fp 1=fopen(“a. dat”, ”r”); fp 2= fopen(“b. dat”, ”w+”); while(( c = getc(fp 1))!=EOF) putc(c, fp 2); fclose(fp 1); fclose(fp 2); }

29 getw(), putw() : get word, put word i화일로부터 i형식 i예 한 워드(word)를 읽거나

29 getw(), putw() : get word, put word i화일로부터 i형식 i예 한 워드(word)를 읽거나 쓸 경우에 사용하는 함수 int FILE getw(stream) *stream; /* 화일 포인터*/ void int FILE putw(w, stream) w; *stream; /*출력할 단어*/ /* 화일 포인터*/ : 정수형 화일 score_int를 읽어서 temp_int 화일에 복사하는 프로그램이다. #include <stdio. h> main() { FILE *input_buf, *output_buf; int a_word; if (( input_buf = fopen(“score_int”, “r”) == NULL ) { printf(“Can’t open file n”); exit(1); } output_buf = fopen(“temp_int”, “w+b”); while (( a_word = getw(input_buf)) !=EOF) putw(a_word, output_buf); fclose(input_buf); fclose(output_buf); }

i예 : 화일의 내용을 화면에 보여주는 프로그램이다. #include <stdio. h> main() { FILE *input_buf;

i예 : 화일의 내용을 화면에 보여주는 프로그램이다. #include <stdio. h> main() { FILE *input_buf; char string[80]; if (( input_buf = fopen(“exam_scores”, “r”) == NULL ) { printf(“Can’t open file n”); exit(1); } while ( fgets(string, 80, input_buf) != NULL) fputs(string, stdout); fclose(input_buf); } 31

32 ungetc() : undo get character i i 읽은 문자를 화일에 되돌려 주는 함수

32 ungetc() : undo get character i i 읽은 문자를 화일에 되돌려 주는 함수 형식 int char FILE ungetc(c, stream) c; *stream; /*반환할 문자*/ /* 화일 포인터*/ M ungetc(), fgets() Munyudo getc() Aegichungso is related to Munyudo.

i예 : 블럭 단위로 쓰는 프로그램 #include <stdio. h> main() { FILE *output_buf; float

i예 : 블럭 단위로 쓰는 프로그램 #include <stdio. h> main() { FILE *output_buf; float f = 12. 23; if (( output_buf = fopen(“test”, “wb”) == NULL ) { printf(“Can’t open file n”); exit(1); } fwrite ( &f, sizeof(float), 1, output_buf); fclose(output_buf); } 34

fscanf(), fprintf() : file scanf, file printf i i 버퍼형 화일을 대상으로 지정한 양식에

fscanf(), fprintf() : file scanf, file printf i i 버퍼형 화일을 대상으로 지정한 양식에 따라 문자나 숫자를 읽거나 쓰는 함수 형식 int FILE char fscanf (stream, format, argument list) *stream; /* 화일 포인터*/ *format /*입력 양식*/ int FILE char fprintf (stream, format, argument list) *stream; /* 화일 포인터*/ *format /*출력 양식*/ 35

i예 : 키보드로부터 자료를 읽어 버퍼형 화일에 그 자료를 기록하는 프로그램 #include <stdio. h>

i예 : 키보드로부터 자료를 읽어 버퍼형 화일에 그 자료를 기록하는 프로그램 #include <stdio. h> main() { FILE *input_buf, *output_buf; char string[80]; int t; if (( output_buf = fopen(“test”, “w”) == NULL ) { printf(“Can’t open file n”); exit(1); } fscanf ( stdin, ”%s%d”, string, &t); fprintf ( output_buf, “%s %d”, string, t); fclose (output_buf); if (( input_buf = fopen(“test”, “r”) == NULL ) { printf(“Can’t open file n”); exit(1); } fscanf ( input_buf, ”%s%d”, string, &t); fprintf ( stdout, “%s %d”, string, t); fclose (input_buf); } 36

37 fseek() : file seek i i 버퍼형 입출력 시스템에서 임의의 위치에서 읽고 쓰기

37 fseek() : file seek i i 버퍼형 입출력 시스템에서 임의의 위치에서 읽고 쓰기 위해 화일 현재 포인터의 위치를 임 의로 변경하는 함수 형식 int fseek( stream, offset, mode) FILE *stream; /* 화일 포인터*/ long int offset; /*이동할 바이트 수*/ int mode; /* 0: 처음, 1: 현재, 2: 끝*/ /* record size 50 bytes */. . . fseek(fp, 0 L, 0); fseek(fp, 100 L, 1); fseek(fp, -200 L, 2); … c = getc(fp); /* c = ? ? ? */ buffer pointer 012345678 abcdefghijk 가나다라마

2. 3 화일 40 i 사례 문제 : 10, 000개의 학생 레코드를 화일에 저장하고

2. 3 화일 40 i 사례 문제 : 10, 000개의 학생 레코드를 화일에 저장하고 한번에 하나의 레코드를 접근하는 프로그램 i [프로그램 2. 2] #include <stdio. h> struct Names { char last[15]; char first[15]; char middle; }; struct Addresses { char home[50]; char campus[50]; };

struct Phone { int area_code; int exchange; int number; }; struct Class { char

struct Phone { int area_code; int exchange; int number; }; struct Class { char course_number[6]; char room_number[4]; int meeting_time; }; struct Student { struct Names name; struct Addresses address; struct Phone_number; struct Class classes[6]; }; 41

main() { FILE *std_file; FILE *out_std_file; struct Student one_student; int ch, i, j, f;

main() { FILE *std_file; FILE *out_std_file; struct Student one_student; int ch, i, j, f; out_std_file = fopen("out_std_file. c", "w"); if((std_file = fopen("std_file. c", "r")) !=NULL) { while ((ch=getc(std_file)) != EOF) { f = 0; for(i = 0; i<15; i++) one_student. name. last[i] = getc(std_file); one_student. name. last[14] = ''; for(i = 0; i<15; i++) one_student. name. first[i] = getc(std_file); one_student. name. first[14] = ''; one_student. name. middle = getc(std_file); for(i = 0; i<50; i++) one_student. address. home[i] = getc(std_file); one_student. address. home[49] = ''; 42

for(i = 0; i<50; i++) one_student. address. campus[i] = getc(std_file); one_student. address. campus[49] =

for(i = 0; i<50; i++) one_student. address. campus[i] = getc(std_file); one_student. address. campus[49] = ''; fscanf(std_file, "%d ", &one_student. Phone_number. area_code); fscanf(std_file, "%d", &one_student. Phone_number. exchange); for(i=0; i<6; i++) { for(j=0; j<6; j++) one_student. classes[i]. course_number[j] = getc(std_file); one_student. classes[i]. course_number[5] = ''; for(j=0; j<4; j++) one_student. classes[i]. room_number[j] = getc(std_file); one_student. classes[i]. room_number[3] = ''; fscanf(std_file, "%d ", &one_student. classes[i]. meeting_time); } fputs(one_student. name. last, out_std_file); 43

fputs(one_student. name. first, out_std_file); fprintf(out_std_file, "%c", one_student. name. middle); fputs(one_student. address. home, out_std_file); fputs(one_student.

fputs(one_student. name. first, out_std_file); fprintf(out_std_file, "%c", one_student. name. middle); fputs(one_student. address. home, out_std_file); fputs(one_student. address. campus, out_std_file); fprintf(out_std_file, "%d", one_student. Phone_number. area_code); fprintf(out_std_file, "%d", one_student. Phone_number. exchange); for(f=0; f < 6; f++) { fputs(one_student. classes[f]. course_number, out_std_file); fputs(one_student. classes[f]. room_number, out_std_file); fprintf(out_std_file, "%d", one_student. classes[f]. meeting_time); } } fclose(std_file); fclose(out_std_file); } } else { printf("n. Warning : std_file. c does not existn"); exit(0); } 44

i [프로그램 2. 3] student 화일에 대한 백업 화일을 생성하는 프로그램 #include <stdio. h>

i [프로그램 2. 3] student 화일에 대한 백업 화일을 생성하는 프로그램 #include <stdio. h> struct Names { char last[15]; char first[15]; char middle; }; struct Addresses { char home[50]; char campus[50]; }; struct Phone { int area_code; int exchange; int number; }; 45

struct Class { char course_number[6]; char room_number[4]; int meeting_time; }; struct Student { struct

struct Class { char course_number[6]; char room_number[4]; int meeting_time; }; struct Student { struct Names name; struct Addresses address; struct Phone_number; struct Class classes[6]; }; main(int argc, char* argv[]) { FILE *out_std_file, *std_file; struct Student one_student; int i, j, f=0, g=0, h=0; 46

47 if(argc < 3) { printf("n. Usage : BACKUP Source_File_Name Backup_File_Namen"); exit(0); } else

47 if(argc < 3) { printf("n. Usage : BACKUP Source_File_Name Backup_File_Namen"); exit(0); } else if(comp(argv[1], argv[2]) == 0) { printf("n. Warning : Source_File_Name must differ from Backup_File_Namen"); exit(0); } out_std_file = fopen(argv[2], "w"); if((std_file = fopen(argv[1], "r")) != NULL) { while (getc(std_file) != EOF) { for(i=0; i<15; i++) fscanf(std_file, "%d", &one_student. phone_number);

for(i=0; i<6; i++) { for(i=0; i<6; i++) one_student. classes[i]. course_number[j] = getc(std_file); for(j=0; j<4;

for(i=0; i<6; i++) { for(i=0; i<6; i++) one_student. classes[i]. course_number[j] = getc(std_file); for(j=0; j<4; j++) one_student. classes[i]. room_number[j] = getc(std_file); fscanf(std_file, "%d", &one_student. classes[i]. meeting_time); } fputs(one_student. name. last, out_std_file); fputs(one_student. name. first, out_std_file); fprintf(out_std_file, "%d", one_student. name. middle); fputs(one_student. address. home, out_std_file); fputs(one_student. address. campus, out_std_file); fprintf(out_std_file, "%d", one_student. phone_number. area_code); fprintf(out_std_file, "%d", one_student. phone_number. exchange); fprintf(out_std_file, "%d", one_student. phone_number); while(f++ < 6){ fputs(one_student. classes[f]. course_number, out_std_file); 48

49 fputs(one_student. classes[f]. room_number, out_std_file); fprintf(out_std_file, "%d", one_student. classes[f]. meeting_time); } fclose(std_file); } }

49 fputs(one_student. classes[f]. room_number, out_std_file); fprintf(out_std_file, "%d", one_student. classes[f]. meeting_time); } fclose(std_file); } } }

솔루션 I/O Package in Java 50 Object File. Descripter Input. Stream Reader Output. Stream

솔루션 I/O Package in Java 50 Object File. Descripter Input. Stream Reader Output. Stream Object. Input. Stream File. Output. Stream File. Input. Stream Filter. Output. Stream Filter. Input. Stream Buffered. IOnput. Stream Data. Input. Stream Data. Onput. Stream Print. Stream writer

솔루션 Input. Stream Class n n 추상클래스로서 Java. io Package의 입력스트림들의 기본 클래스 Methods

솔루션 Input. Stream Class n n 추상클래스로서 Java. io Package의 입력스트림들의 기본 클래스 Methods – public Input. Stream() – public abstract int read() throws IOException • 0 <= Getchar()<= 255, return – 1 for eol – Public int read(char buf[]) //입력스트림으로 부터 buffer배열 크기만큼의 문자를 읽어서 buffer 에 저장 – Public int read(char buff[], int offset, int len) throws … – Public long skip(long count) throws … : 건너뛴 바이트 수 반환 – Public int available() throws … : 연속판독가능 바이트 수 반환 – Public void close() throws … : 입력 스트림 닫음 51

솔루션 Input. Stream Class(2) 52 // read a file from stdin and print the

솔루션 Input. Stream Class(2) 52 // read a file from stdin and print the contents to stdout import java. io. *; 2 class input 1 { 3 public static void main(String args[]) throws IOException { 4 File. Input. Stream in = new File. Input. Stream(args[0]); 5 Print. Stream out = new Print. Stream(System. out); 6 int ascii. Num; 7 for (; (ascii. Num = in. read())!= -1; ){ 8 out. print((char)ascii. Num); 9 } 10 out. println( "Done"); }

솔루션 Input. Stream Class(3) n Example Input. Stream in = System. in; … int

솔루션 Input. Stream Class(3) n Example Input. Stream in = System. in; … int code; int[7] bunho; int[10] tel; code = in. read(); … in. read(bunho); in. skip(1); in. read(tel, 2, 4); … 53 1 kim. In. Mun 237 1 007011 skip code 1 name k i m I n. Mu n bunho 2 3 7 tel 0070

솔루션 Output. Stream Class n n Input. Stream과 비슷 Methods – public Output. Stream()

솔루션 Output. Stream Class n n Input. Stream과 비슷 Methods – public Output. Stream() – public abstract void write(int b) throws … – public void write(char buf []) – public void write(char buf [], int offset, int count) throws… – public void flush() throws IOException – public void close() throws IOException 54