Coding q 1 m q 2 m FORTRAN






























![코딩 스타일 <올바른 예> search(board, i, j, row, col, l) int board[8][8], i, j, 코딩 스타일 <올바른 예> search(board, i, j, row, col, l) int board[8][8], i, j,](https://slidetodoc.com/presentation_image/abdc73860a37083ad6b79598c5aaf191/image-31.jpg)






- Slides: 37
구현 (Coding)
프로그래밍 언어의 발전 q 제 1 세대 언어(저급언어) m 기계어, 어셈블리 언어 q 제 2 세대 언어 m FORTRAN, COBOL, ALGOL 60, BASIC q 제 3 세대 언어 범용: PL/1, Pascal, Modula-2, C, Ada, C++, SIMULA, Smalltalk m 특수: CHILL, RPG, Lisp, Prolog, APL m q 제 4 세대 언어(4 GL) non-procedural language m MANTIS, IDEAL, RAMIS II, SQL m 6
언어의 구문 q 일관적이고 원시코드의 명료성을 증진 <예> ① Pascal의 경우 if Total. Sales > Bonus. Level then Bonus : = Commission + (Bonus. Percent * Total. Sales); if Total. Sales > Bonus. Level then begin Bonus : = Commission + (Bonus. Percent * Total. Sales); Bonus. Months : = Bonus. Months + 1 end ② Ada에서 end. If, endwhile의 사용 if Total. Sales > Bonus. Level then Bonus : = Commission + (Bonus. Percent * Total. Sales) endif if Total. Sales > Bonus. Level then Bonus : = Commission + (Bonus. Percent * Total. Sales); Bonus. Months : = Bonus. Months + 1 endif 12
언어의 구문 ③ dangling else if condition. A then if concition. B then action 1 else action 2 if condition. A then if concition. B then action 1 endif else action 2 endif 13
자료형(Data Type) q Strong typed language m Ada, Modula-2, Pascal q Dynamic type language m Lisp, APL q 단순 자료형 실수, 정수, 논리형, 문자형 m 포인터형(Ada, C, Modula-2, Pascal) m q 사용자 정의 타입 <예> type Ages=(Infant, Toddler, Preschool, Child, Teenager, Adult); var Person: Ages; 15
구조 자료형 q 정적 배열 m FORTRAN, Pascal, Modula-2 q 동적 배열 m C 언어, Ada <예> type Puzzle is array (integer range <>, integer range <>) of character; subtype Sunday. Puzzle is Puzzle(1. . 50, 1. . 50); q 구조(structure) <예> type Subscriber. Type = record Name: array[1. . 50] of char; IDNumber: 10000. . 99999; Issues. Sent: 0. . 104; Issues. Remaining: 0. . 104; Subscription. Type: (New, Renewal, Free, Lifetime) end; 16
재사용(Ada Package) generic type Stack. Item is private; package Stack is procedure Push(Element: in Stack. Item); function Pop return Stack. Item; . . end Stack; package Char. Stack is new Stack(character); package Integer. Stack is new Stack(integer); 18
재사용(C++ 클래스) class Employee { private: char* position. Type int. . . public: promote(. . . ); } name; position; salary; Employee Young. Hee(); 또는 Employee* Young. Hee = Employee(); 19
Goto 문의 사용 q 구조적인 제어흐름을 해치지 않는 범위에서 사용 50 60 70 80 DO 50 I=1, COUNT. IF (ERROR 1) GO TO 60. IF (ERROR 2) GO TO 70. CONTINUE {Code for Error 1 handling} GO TO 80 {Code for Error handling} CONTINUE I=1 while I <= Table. Size and Table(I) <> Target do I=I+1 if I > Table. Size then {code for target not found} else { code for Target found} (a) 구조적 코딩 for I = 1 to Table. Size do if Table(I) = Target then goto Found Not. Found: {code for Target not found} Found: {code for Target found} (b) goto의 사용 22
코딩 스타일 q 스타일 2: 수식에서 의미하는 바를 간결하고 직접적으로 표현하라 <잘못된 예> if ( x < y ) { if ( x < z ) small = x; if ( x > z ) small = z; } if ( x > y ) { if ( y < z ) small = y; if ( y > z ) small = z; } m <올바른 예> small = x; if ( y < small ) small = y; if ( z < small ) small = z; If. . . then. . . else에서는 짧은 선택구조를 먼저 <잘못된 예> if (in_user_code) { in_user_code = FALSE; r 2 = r; reset_pharlap(); send_sig_segv(); } else revert(); <올바른 예> if (!in_user_code) revert(); else { in_user_code = FALSE; r 2 = r; reset_pharlap(); send_sig_segv(); } 24
코딩 스타일 q 스타일 5: 일관성 있는 변수명을 사용하라 m Hungarian notation <예> m ptr_to_date_key(포인터), Pi(상수), Person(구조체) 일관성 있는 이름 <잘못된 char void int <올바른 char void int 예> buffer[500], mssge[80]; read_instnance(void), Save. Current(void); get_line(void), write_line(void); index 1, index 2; dirctry, vsble; 예> buffer[500], message[80]; read_instnance(void), save_current(void); read_line(void), write_line(void); i, j; drctry, vsble; 26
코딩 스타일 q 스타일 8: if 다음에 if가 따라오는 구조나 null else는 피할 것 <잘못된 예> if ( qty > 10 ) if ( qty > 200 ) if ( qty >= 500 ) bill_a += 1. 00; else bill_a += 0. 50; else bill_a = 0. 0; /* /* /* A */ B */ C */ B */ A */ if ( qty >= 500 ) bill_a += 1. 0; else if ( qty > 200 ) bill_a += 0. 5; else if ( qty <= 10) bill_a = 0. 0; <올바른 예> if ( qty >= 500. 0 ) bill+a += 1. 0; if ( qty < 500 && qty > 200 ) bill_a += 0. 5; if ( qty <= 10 ) bill_a = 0. 0; 28
코딩 스타일 q 스타일 9: 문장의 반복은 최소화한다. <잘못된 예> get_token(); if (tkn == T_END) return; if (tkn == T_START) start_proc(); while (ntokens < T_LIMIT) { process_token(); add_token(); get_token(); if (tkn == T_END) return; if (tkn == T_START) start_proc(); } <올바른 예> for ( ; ; ) { get_token(); if (tkn == T_END) return; if (tkn == T_START) start_proc(); if (T_LIMIT <= ntokens) break; process_token(); add_entry(); } 29
코딩 스타일 q 스타일 8: 모듈화하라. 서브루틴을 사용하라 <잘못된 예> search( board, i, j, movrow, movcol, l) int board[8][8], i, j, movrow[4], movcol[4], *l; { /* 체커 말에는 다음과 같이 코드를 부여한다. 1 - 백기사, 2 - 백왕, 3 - 흑기사, 4 - 흑왕 말판에 서 말이 없는 곳은 0, 있는 곳은 말의 코드가 표시 된다. movrow, movcol은 가능한 움직임을 나타 내기 위한 배열이며 l은 가능한 움직임의 갯수이 다. */ int k; if (board[i][j] == 0) exit(0); *l = 0; k = board[i][j]; if (k == 3) goto back_right; if (i == 7) goto exit_test; if (j == 7) goto for_left; if (board[i+1][j+1] != 0) goto for_left; moverow[*1] = i + 1; movecol[*l] = j + 1; *l++; for_left: if (j == 0) goto exit_test; moverow[*l] = i+1; movecol[*l] = j-1; *l++; exit_test: if (k == l) return; back_right: if (i == 0) return; if (j == 7) goto back_left; if (board[i-1][j+1] != 0) goto back_left; moverow[*l] = i-1; movecol[*l] = j+1; *l++; back_left: if (j == 0) return; if (board[i-1][j-1] != 0) return; moverow[*l] = i-1; movecol[*l] = j-1; *l++; } 30
코딩 스타일 <올바른 예> search(board, i, j, row, col, l) int board[8][8], i, j, row[4], col[4], *l; { /* board[I][j] = 0 => 빈 공 간 1 => 백기사, 2=> 백왕 3 => 흑기사, 4=> 흑왕 i는 앞이 증가 방향, j는 오른쪽이 증가방 향 l은 움직임이 저장되는 row, col의 카운 터로 사용됨 */ int k; void store(); if (i < 0 || i > 7 || j < 0 || j > 7) error("out of board"); k = board[i][j]; *l = 0; if (k < l || k >4) error("misplaced knight"); *l = 0; if (k != 3) store(board, i+1, j+1, row, col, l); if (k != 3) store(board, i+1, j-1, row, col, l); if (k != 1) store(board, i-1, j+1, row, col, l); if (k != 1) store(board, i-1, j-1, row, col, l); } void store(board, ic, jc, row, col, l) int board[8][8], ic, jc, row[4], col[4], *l; { if (ic < 0 || ic > 7 }} jc < 0 || jc > 7) return; if (board[ic][jc] != 0) return; *l++; row[*l] = ic; col[*l] = jc; } 31
프로그램에 관한 격언들 m m m The sooner you start to code, the longer the program will take. If you can't write it down in English, you can't code it. If the code and the comments disagree, then both are probably wrong. If you have too many special cases, you are doing it wrong. Get your data structures correct first, and the rest of the program will write itself. Don't debug standing up. It cuts your patience in half, and you need all you can muster. Each new user of a new system uncovers a new class bugs. Whenever possible, steal code. Always do the hard part first. If the hard part is impossible, why waste time on the easy part? If you lie to the computer, it will get you. One person's constant is another person's variable. 34
원시코드의 문서화(예) /***************************************** - EXAM SCORE SUMMARY PROGRAM - WRITTEN BY : HONG, KIL DONG - RELEASE DATE : April 1, 1994 - PURPOSE : This program produces a variety of statistics on a group of student's exam scores. For each score that is entered, the score and a row of stars representing the magnitute of the score is printed out. In addition, the number of scores, the average score, the highest score and the lowest score are printed out. - DESIGN : The program is composed of the following modules: Main module - The Main, controlling module Validate Input - Gets and validates the user's input Process Valid Score - Prints score and row of stars for one score Update Statistics - Updates global statistics Print Summary Report - Prints summary statistics 36
원시코드의 문서화(예) - MAJOR VARIABLES : Sum. Of. Score (subrange of integer, 0. . 2000) The sum of all score entered Num. Of. Students (subrange of integer, 0. . 100) The total number of score entered Largest. Score (subrange of integer, 0. . 20) The largest score in the set of scores Smallest. Score (subrange of integer, 0. . 20) The smallest score - FILES USED: Input - contains integer scores, one to a line Output - contains a one-line histogram for each score read, followed on the next line by the number of students and their average score, then on the next line by the top score, and concluding on the next line with the lowest score. - EXTERNAL REFERENCES: None **************************************/ 37





































