• 검색 결과가 없습니다.

가지의 상황이 발생핛 수 있다

문서에서 스택 (페이지 57-65)

오류의 처리

모두 6 가지의 상황이 발생핛 수 있다

 성공의 경우 1 가지, 오류의 경우 5가지

 [실습 07-1]에서는 성공/실패 여부만 있었다.

[실습 2] 앆정화된 수식계산 57

 대책은?

 관찰

 모든 오류는 Postfix_eval()에서 인식된다

 오류가 발생하면, 그 상황을 Postfix_eval()을 call핚 main()으로 젂달핛 필요가 있다.

이를테면 우리의 경우 main() 으로

 모두 6 가지의 상황이 발생핛 수 있다.

성공의 경우 1 가지, 오류의 경우 5가지

[실습 07-1]에서는 성공/실패 여부만 있었다.

 상황의 경우를 코드화: Postfix_eval() 의 return 값으로 하자

int Postfix_eval (Postfix * postfix)

0 : 성공

1 : “[오류] 수식이 너무 길어 처리가 불가능합니다”

2 : “[오류] 연산값에 비해 연산자의 수가 많습니다”

3 : “[오류] 연산값에 비해 연산자의 수가 적습니다”

4 : “[오류] 수식에 알 수 없는 연산자가 있습니다”

5 : “[오류] 나눗셈의 분모가 0 입니다”

© 2011, J.-H.Kang, CNU

 오류 코드의 의미를 알기 쉽게

#define ERROR_NO 0

#define ERROR_ExpressionTooLong 1

#define ERROR_OperatorsTooMany 2

#define ERROR_OperatorsTooLess 3

#define ERROR_UndefinedOperator 4

#define ERROR_DivideByZero 5

[실습 2] 앆정화된 수식계산 59

 오류가 인식되는 곳에 이렇게! [1]

 boolean Postfix_eval (Postfix * postfix)

{ int operand, operand1, operand2, calculated ; STACK * s = STACK_new() ;

int i = 0 ;

while ( postfix->expression[i] != „\0‟) {

if (postfix->expression[i] >=„0‟ && postfix->expression[i] <= „9‟) { // token is an operand. Push it into stack

operand = (postfix->expression[i] – „0‟) ; if ( STACK_isFull(s) ) {

return ERROR_ExpressionTooLong ; // 수식이 너무 길어 처리가 불가능합니다.

} else {

STACK_push(s, operand) ; } else { // The token is an operator }

if ( postfix->expression[i] == „+‟) { ...

} else if ( postfix->expression[i] == „-‟) { ...

} else if ( postfix->expression[i] == „*‟) { ...

} else if ( postfix->expression[i] == „/‟) { ...

} else if ( postfix->expression[i] == „%‟) { ...

} else {

return ERROR_UndefinedOperator ; // [오류] 수식에 알 수 없는 연산자가 있습니다.

} }

Postfix_showTokenAndStack(char postfix->expression[i], s) ; i++ ;

} // end of while

if ( STACK_length(s)==1 ) {

// At this point, the result is on top of stack postfix->computedValue = STACK_pop(s) ; } else if ( STACK_length(s) > 1) {

return ERROR_OperatorsTooMany ; // [오류] 연산값에 비해 연산자의 수가 적습니다.

} STACK_free(s) ; return TRUE ; }

© 2011, J.-H.Kang, CNU

 오류가 인식되는 곳에 이렇게![2]

if ( postfix->expression[i] == „+‟) { if ( STACK_length(s) >= 2 ) {

operand2 = STACK_pop(s) ; operand1 = STACK_pop(s) ;

calculated = operand1 + operand2 ;

STACK_push(s, calculated) ; // 2 개 pop() 했으므로, 스택에 하나 push() 핛 여유는 있음. isFull() 검사 불필요 } else {

return ERROR_OperatorsTooLess ; // [오류] 연산값에 비해 연산자의 수가 적습니다.

} else if ( postfix->expression[i] == „-‟) { } if ( STACK_length(s) >= 2 ) {

operand2 = STACK_pop(s) ; operand1 = STACK_pop(s) ;

calculated = operand1 - operand2 ; STACK_push(s, calculated) ;

} else {

return ERROR_OperatorsTooLess ; // [오류] 연산값에 비해 연산자의 수가 적습니다.

} else if ( postfix->expression[i] == „*‟) { } if ( STACK_length(s) >= 2 ) {

operand2 = STACK_pop(s) ; operand1 = STACK_pop(s) ;

calculated = operand1 * operand2 ; STACK_push(s, calculated) ;

} else {

return ERROR_OperatorsTooLess ; // [오류] 연산값에 비해 연산자의 수가 적습니다.

} else if ( postfix->expression[i] == „/‟) { } if ( STACK_length(s) >= 2 ) {

operand2 = STACK_pop(s) ; operand1 = STACK_pop(s) ; if ( operand2 == 0 )

return ERROR_DivideByZero ; // [오류] 나눗셈의 분모가 0 입니다.

calculated = operand1 / operand2 ; STACK_push(s, calculated) ; } else {

return ERROR_OperatorsTooLess ; // [오류] 연산값에 비해 연산자의 수가 적습니다.

} else if ( postfix->expression[i] == „%‟) { } if ( STACK_length(s) >= 2 ) {

operand2 = STACK_pop(s) ; operand1 = STACK_pop(s) ;

calculated = operand1 % operand2 ; STACK_push(s, calculated) ;

} else {

return ERROR_OperatorsTooLess ; // [오류] 연산값에 비해 연산자의 수가 적습니다.

} }

[실습 2] 앆정화된 수식계산 61

값의 종류가 5가지나 되므로, boolean으로 불가능

 main()에서 오류메시지를 출력한다.

#define MAXNUMEBROFTOKENS 200

#define MSG_StartingMessage “<Postfix 수식을 계산합니다>\n”

#define MSG_EndingMessage “\n<계산을 종료합니다>\n”

void main(void)

{ char expression[MAXNUMEBROFTOKENS] ; Postfix * postfix ;

boolean isInputEnded ; ; int returnCode ;

printMessage(MSG_StartingMessage) ; postfix = Postfix_new() ;

isInputEnded = inputPostfix(expression) ; while ( ! isInputEnded ) {

Postfix_setExpression(postfix, expression) ; returnCode = Postfix_eval(postfix) ; if ( returnCode == ERROR_NONE ) {

printf(“계산값 : %d\n”, Postfix_computedValue(postfix)) ; } else {

outputErrorMessage(returnCode) ; } isInputEnded = inputPostfix(expression) ; } Postfix_free(postfix) ;

printMessage(MSG_EndingMessage) ; }

#define ErrorMsg_ExpresionTooLong “[오류] 수식이 너무 길어 처리가 불가능합니다.“

#define ErrorMsg_OperatorsTooMany “[오류] 연산값에 비해 연산자의 수가 많습니다.”

#define ErrorMsg_OperatorsTooLess “[오류] 연산값에 비해 연산자의 수가 적습니다.”

#define ErrorMsg_UndefinedOperator “[오류] 수식에 알 수 없는 연산자가 있습니다.”

#define ErrorMsg_DivideByZero “[오류] 나눗셈의 분모가 0 입니다.”

void outputErrorMessage (int errorCode)

{ if ( errorCode == ERROR_ExpressionTooLong ) { printf(ErrorMSG_ExpresionTooLong ;

} else if ( errorCode == ERROR_OperatorsToomany ) { printf(ErrorMsg__OperatorsTooMany) ;

} else if ( errorCode == ERROR_OperatorsTooLess) { printf(ErrorMsg_OperatorsTooLess) ;

} else if ( errorCode == ERROR_UndefinedOperator ) { printf(ErrorMsg_UndefinedOperator) ;

} else if ( errorCode == ErrorMsg_DivideByZero ) { printf(ErrorMsg_ DivideByZero) ;

}

}

© 2011, J.-H.Kang, CNU

 오류 코드를 사용하는 곳

main()

Posfix_eval()

outputErrorMessage()

[실습 2] 앆정화된 수식계산 63

 생각해 볼 점

오류 처리의 중요성은?

휴대폰을 쓰다가 작동이 멈추어 버린 경험이 있는 가?

 왜 멈추었다고 생각되는가?

“P 회사 휴대폰은 잘 죽지 않는데, A 회사 휴대폰은

잘 죽더라” 라는 식의 대화를 하거나, 또는 그런 이

야기를 들은 적이 있는가?

© 2011, J.-H.Kang, CNU [실습 2] 앆정화된 수식계산 65

문서에서 스택 (페이지 57-65)

관련 문서