15 Implementation 1 Machine language assembly language 2
프로그래밍 언어의 발전 (1/5) 구현 (Implementation) 제 1세대 언어: Machine language, assembly language 제 2세대 언어: Fortran, Cobol, Algol 60, Basic 제 3세대 언어 • 범용: PL/1, Pascal, Modula-2, C, Ada, C++, SIMULA, Smalltalk, Java, … • 특수: CHILL, RPG, Lisp, Prolog, APL, … 제 4세대 언어(4 GL, non-procedural language): MANTIS, IDEAL, RAMIS II, SQL, … Page 5 Software Engineering by Yang-Sae Moon
프로그래밍 언어의 발전 (2/5) 구현 (Implementation) Assembly language 예제 Intel Chip (Linux) Page 6 Software Engineering by Yang-Sae Moon
프로그래밍 언어의 발전 (2/5) 구현 (Implementation) Assembly language 예제 Intel Chip (Linux) Sparc Chip (Solaris) Page 7 Software Engineering by Yang-Sae Moon
프로그래밍 언어의 발전 (3/5) 구현 (Implementation) Fortran 프로그램 예제 Page 8 Software Engineering by Yang-Sae Moon
프로그래밍 언어의 발전 (4/5) 구현 (Implementation) Pascal 프로그램 예제 Page 9 Software Engineering by Yang-Sae Moon
프로그래밍 언어의 발전 (5/5) 구현 (Implementation) C 프로그램 예제 Page 10 Software Engineering by Yang-Sae Moon
언어의 문형(구문) (1/2) 구현 (Implementation) 일관적이고 원시 코드의 명료성을 증진시킬 수 있어야 함 ① 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에서 endif, 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 Page 17 Software Engineering by Yang-Sae Moon
언어의 문형(구문) (2/2) 구현 (Implementation) ③ dangling else �else가 어디에 해당? if condition. A then if condition. B then action 1 else action 2 if condition. A then if concition. B then action 1 endif else action 2 endif Page 18 Software Engineering by Yang-Sae Moon
제어 구조 (Control Structure) (1/2) 구현 (Implementation) 기본적인 제어 구문: if-then-else 선택 구조(case 구조) • case 선택자의 타입은 가능하면 제한이 없어야 • case 문장의 레이블은 범위를 표시할 수 있어야 • 선택자가 가질 수 있는 모든 값을 열거하도록 강요해서는 안 된다 case Person of when Newborn | Infant => Infant_Seat; when Toddler. . Child => Lap_Belt; when Others => Shoulder_and_Lap_Belt; end case; Page 19 Software Engineering by Yang-Sae Moon
자료형 (Data Type) 구현 (Implementation) Strong typed language • 타입 검사가 매우 강한 특성을 가진다. (“정수 변수 : = 실수”는 컴파일 시 에러 발생) • Ada, Modula-2, Pascal Dynamic typed language • 수행 중간에 변수의 형이 변할 수 있다. • Lisp, APL, PHP 단순 자료형 • 실수, 정수, 논리형, 문자형 • 포인터형(Ada, C, Modula-2, Pascal) 사용자 정의 타입 type Ages=(Infant, Toddler, Preschool, Child, Teenager, Adult); var Person: Ages; Page 21 Software Engineering by Yang-Sae Moon
구조 자료형 (1/2) 구현 (Implementation) (일반적으로) 배열(array)과 구조체(structure)를 의미함 정적 배열 • 프로그램 수행 초기(혹은 컴파일 타임)에 배열의 크기가 결정됨 • Fortran, Pascal, Modula-2 동적 배열 • 프로그램 수행 중간에 (메모리 할당 등을 통하여) 배열의 크기가 결정됨 • C, Ada type Puzzle is array (integer range <>, integer range <>) of character; subtype Sunday. Puzzle is Puzzle(1. . 50, 1. . 50); Page 22 Software Engineering by Yang-Sae Moon
구조 자료형 (2/2) 구현 (Implementation) 구조(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; Page 23 Software Engineering by Yang-Sae Moon
추상 자료형 (Abstract Data Type) 구현 (Implementation) 자세한 내용이 encapsulation되어 있어, 프로그래머는 자료 값이나 오퍼 레이션이 어떻게 구현되어 있는지 자세히 알 필요가 없는 자료형 (일반적으로) Object-Oriented 언어에서 클래스 형태로 사용함 class Bank. Account { public: Bank. Account(int won, double rate); Bank. Account(); void update(); double get_balance(); double get_rate(); void output(); private: double balance; interest_rate; } Page 24 Software Engineering by Yang-Sae Moon
함수/프로시저 (2/3) 구현 (Implementation) An example of “call by value” Page 27 Software Engineering by Yang-Sae Moon
함수/프로시저 (3/3) 구현 (Implementation) An example of “call by reference” Page 28 Software Engineering by Yang-Sae Moon
재사용 (Reuse) 구현 (Implementation) 특정 자료형이나 목적에 맞도록 구현하는 것이 아니라, 일반적 자료형 및 목적에 맞도록 구현 �재사용 측면에서 유리 Ada example: 모든 자료형을 지원하는 스택의 구현 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); Page 29 Software Engineering by Yang-Sae Moon
구조적 프로그래밍 (3/3) 구현 (Implementation) Goto文은 구조적인 제어 흐름을 해치지 않는 범위에서 사용해야 함 �가능하면 마음 속에서, 머리 속에서 “goto”를 지우는 것이 바람직 함 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의 사용 Page 32 Software Engineering by Yang-Sae Moon
Focus 프로그램 예 구현 (Implementation) TABLE FILE SALES HEADING CENTER ‘SAMPLE SALES REPORT’ SUM SALES BY REGION ACROSS MONTH BY YEAR ON YEAR SUMMARIZE ON YEAR PAGE-BREAK END 어떤 의미인지 정확하지는 않지만… 아마도 SALES라는 파일에서 영역, 연도별로, 각 달에 걸쳐서 매출 평균을 구하는 … Page 35 Software Engineering by Yang-Sae Moon
비주얼 베이직 예제 (1/2) 구현 (Implementation) Visual Basic의 경우 ① 원하는 윈도우를 그린다 ② 버튼, 텍스트 박스의 속성을(properties)를 설정 Page 39 Software Engineering by Yang-Sae Moon
비주얼 베이직 예제 (2/2) 구현 (Implementation) ③ 연계(관련)된 이벤트에 대해서 코드를 작성한다. Page 40 Software Engineering by Yang-Sae Moon
Visual Studio 구현 (Implementation) Page 41 Software Engineering by Yang-Sae Moon
Java – Eclipse (Android 개발) Page 42 구현 (Implementation) Software Engineering by Yang-Sae Moon
스타일 1: 명확하게 작성하라. (1/3) 구현 (Implementation) 부제: 너무 똑똑한 체 하지 말 것 int i, j; float v[N][N]; . . for(i=1; i <= N; i++) for(j=1; j <= N; j++) v[i-1][j-1] = (i/j)*(j/i); int i, j; float v[N][N]; . . for(i=0; i < N; i++) for(j=0; j < N; j++) if(i == j) v[i][j] = 1; else v[i][j] = 0; Page 45 Software Engineering by Yang-Sae Moon
스타일 1: 명확하게 작성하라. (2/3) 구현 (Implementation) 일반적으로 짧을수록 명확해 진다. power[1] power[2] power[3] power[4] power[5] power[6] power[7] power[8]. . . = = = = base; base*base*base; base*base*base*base*base; base*base*base*base*base*base; power[1] = base; for(i=2; i < N; i++) power[i] = power[i-1]*base; Page 46 Software Engineering by Yang-Sae Moon
스타일 1: 명확하게 작성하라. (3/3) 구현 (Implementation) 짧으면 항시 명확하다? �꼭 그렇지만은 않다. int Integer. From. Hex(char Hex. Digit) { if(Hex. Digit < 58) return(Hex. Digit – 48); else return(Hex. Digit – 55); // ‘A’ = 65 } // ‘ 0’ = 48 int Integer. From. Hex(char Hex. Digit) { switch(Hex. Digit) { case ‘ 0’: return 0; break; case ‘ 1’: return 1; break; . . . case ‘ 9’: return 9; break; case ‘A’: return 10; break; . . . case ‘F’: return 15; break; } } Page 47 Software Engineering by Yang-Sae Moon
스타일 2: 간결하고 직접적으로 표현하라. (1/2) 구현 (Implementation) 최소값을 구하는 예제 (x = y, or y = x인 경우, 바른 값을 찾지 못한다. if(x < y) { if(x < z) if(x > z) } if(x > y) { if(y < z) if(y > z) } small = x; small = z; small = y; small = z; small = x; if(y < small) small = y; if(z < small) small = z; Page 48 Software Engineering by Yang-Sae Moon
스타일 2: 간결하고 직접적으로 표현하라. (2/2) 구현 (Implementation) 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(); } Page 49 Software Engineering by Yang-Sae Moon
스타일 3: 임시 변수 사용을 피하라. 구현 (Implementation) 임시 변수(temporary variable)의 사용은 가급적 피한다. t 1 = x 1 –(x 2 + x 2); t 2 = 7 – x 2; Y = t 1 + t 2; y = 2*(x 1 – 2*x 2) + 2*(7 – x 2); Page 50 Software Engineering by Yang-Sae Moon
스타일 4: 혼돈을 초래치 않는 변수 명을 사용하라. 구현 (Implementation) 혼돈하기 쉬운 글자들의 예 • 1 과 l a 1 al • 0 과 O term 0 term. O • 5 와 S TEXT 5 TEXTS • I 와 l Iist list • m 과 n 의 연속 사용 mnnm mnmn • u 와 v 의 연속 사용 vuvu uuvu 변수 수정의 예제 • positionx, positiony � xpos, ypos • n, nnn � n, nsq, ncube � nunit, nsqur, ncube Page 51 Software Engineering by Yang-Sae Moon
스타일 5: 일관성 있는 변수 명을 사용하라. 구현 (Implementation) Hungarian notation: 변수 이름의 처음 몇 자는 변수의 타입이나 범위를 나타내도록 한다. 예) ptr_to_date_key (포인터) 일관성 있는 이름을 사용한다. char void int buffer[500], mssge[80]; read_instance(void), Save. Current(void); get_line(void), write_line(void); index 1, index 2; char void int buffer[500], message[80]; read_instance(void), save_current(void); read_line(void), write_line(void) i, j; Page 52 Software Engineering by Yang-Sae Moon
스타일 6: 문장 그룹이 명확하도록 { }와 들여쓰기를 사용하라. 구현 (Implementation) for(i=0; i < N; i++) { k = a[i]; if(k>100) a[i] = i*3; else if(j ==N). . . } for(i=0; i < N; i++) { k = a[i]; if(k > 100) a[i] = i * 3; else if(j == N). . . } Page 53 Software Engineering by Yang-Sae Moon
스타일 7: 한 가지 선택이면 if 만, 두 가지이면 if-else 구현 (Implementation) if(swctl == ‘ 1’) goto conti; else { dvictl += 10; swctl = ‘ 1’; } conti; if(swctl != ‘ 1’) { dvictl += 10; swctl = ‘ 1’; } Page 54 Software Engineering by Yang-Sae Moon
스타일 8: if 다음의 if 구조, null else는 피한다. 구현 (Implementation) if 다음의 if나 null else는 가독성을 크게 떨어뜨리며, 오해의 소지가 있다. if(qty > 10) // A if(qty > 200) if(qty >= 500) bill_a += 1. 00; else bill_a += 0. 50; else; // B else bill_a = 0. 0; // B // C // A if(qty >= 500. 0) bill_a += 1. 00; if(qty < 500 && qty > 200) bill_a += 0. 5; if(qty <= 10) bill_a = 0. 0; Page 55 Software Engineering by Yang-Sae Moon
스타일 9: 문장의 반복은 최소화한다. 구현 (Implementation) 반복되는 문장은 가급적 반복문을 사용하여 제거한다. tkn = get_token(); if(tkn == T_END) return; if(tkn == T_START) start_proc(); while(ntokens < T_LIMIT) { process_token(); add_token(); tkn = get_token(); if(tkn == T_END) return; if(tkn == T_START) start_proc(); } for(; ; ) { tkn = get_token(); if(tkn == T_END) return; if(tkn == T_START) start_proc(); if(ntokens >= T_LIMIT) break; process_token(); add_token(); } Page 56 Software Engineering by Yang-Sae Moon
프로그램에 관한 몇 가지 격언 (1/2) 구현 (Implementation) 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. (먼저 자료 구조를 잘 잡는다면, 프로그램은 저절로 써지게 될 것이다. ) Page 62 Software Engineering by Yang-Sae Moon
프로그램 헤더 주석의 예제 (1/2) 구현 (Implementation) /*************************************** - EXAM SCORE SUMMARY PROGRAM - WRITTEN BY : HONG, KIL DONG - RELEASE DATE : April 1, 2010 - 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 Page 67 Software Engineering by Yang-Sae Moon
프로그램 헤더 주석의 예제 (2/2) 구현 (Implementation) - 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 ************************************/ Page 68 Software Engineering by Yang-Sae Moon
- Slides: 68