Download ppt - Reverse Polish Calculator

Transcript

Reverse Polish Calculator

• Reverse Polish Notation 연산자가 피연산자의 뒤에 온다 . 예 ) (1 – 2) * (4 + 5) 1 2 – 4 5 + * * 수식에서 괄호가 요구되지 않는다 . ( 수식이 애매하지 않다 )

• 구현 방법 1) 피연산자는 stack(LIFO) 상에 push 2) 연산자가 입력되면 적절한 수의 피연산자를 pop

시킨다 . ( 이항연산자 2 개 ) 3) 연산자를 pop 된 피연산자에 적용하고 , 그 계산

결과를 stack 상에 push

예 ) 1 2 – 4 5 + * 1) 1 과 2 는 stack 상에 push 2) – 를 만나면 stack 상의 2 개 자료를 pop 3) 이들의 차를 구한 (-1) 다음 이 결과값을 stack 에 push

• 1’) 4 와 5 를 stack 상에 push 2’) + 를 만날 때 , stack 상의 2 개의 자료를 pop 3’) 4 와 5 를 더한 다음 그 결과 9 를 stack 상에 push 2’’) * 를 만나면 stack 상의 2 개의 자료를 pop (-1 과 9) 3’’) –1 과 9 를 곱한 다음 그 결과 – 9 를 stack 에 push * newline 을 만나면 stack 상의 자료를 pop 한 다음 출력한다 .

• 실행 과정 1 2 – 4 5 + *

stack

0sp 1

2

sp

-1

sp

-1

4

5

sp

-1

9

sp

-9

sp

sp

개략적인 프로그램 구성While (next operator or operand is not EOF)

if (number)push it

else if (operator)pop operandsdo operationpush result

else if (newline)pop and print top of stack

elseerror

#define MAXOP 100#define NUMBER '0'

int getop(char []);void push(double);double pop(void);

/* reverse polish calculator */main() {

int type;double op2;char s[MAXOP];

while ((type = getop(s)) != EOF) {switch (type) {case NUMBER:

push(atof(s));break;

case '+':push(pop() + pop());break;

case '*':push(pop() * pop());break;

case '-':op2 = pop();push(pop() - op2);break;

case '/':op2 = pop();if (op2 != 0.0)

push(pop() / op2);else

printf("erroe: zero division\n");break;

case '\n':printf("\t%.8g\n",pop());break;

default:printf("error: unknown command %s\n",s);

break;}

}return 0;

}

#define MAXVAL 100int sp = 0;double val[MAXVAL];

void push(double f) {if (sp < MAXVAL )

val[sp++] = f;else

printf("error: stack full, can't push %g\n", f);

}

double pop(void) {if (sp > 0)

return val[--sp];else {

printf("error: stack empty\n");return 0.0;

}}

#include <ctype.h>

int getch(void);void ungetch(int);

/* getop: get next operator or numeric operand */

int getop(char s[]) {int i, c;

while ((s[0] = c = getch()) == ' ' || c == '\t');

s[1] = '\0';if (!isdigit(c) && c != '.')

return c;i = 0;if (isdigit(c))

while (isdigit(s[++i] = c = getch()));

if (c == '.')while (isdigit(s[++i] = c = getch()))

;

s[i] = '\0';if (c != EOF)ungetch(c);return NUMBER;

}#define BUFSIZE 100

char buf[BUFSIZE];int bufp = 0;

int getch(void) {return (bufp > 0) ? buf[--bufp] : getchar();

}

void ungetch(int c) {if (bufp >= BUFSIZE)

printf("ungetch: too many characters\n");else

buf[bufp++] = c;}

Scope rule( 유효범위 규칙 )main() { …}

/**********************************/

int sp = 0;

double val[MAXVAL];

void push(double f) { … }

double pop(void) { …}

sp, val 변수의 scope : 선언된 위치부터 파일의 끝까지 (push, pop 에서 ok, main 에서 X, main 내에서 push 나 pop 도 X)

• 전역변수가 정의되기 전에 사용될 필요가 있거나 , 다른 파일에서 정의된다면 extern 선언이 반드시 요구됨 .

• 선언 (declaration) vs 정의 (definition) 선언 : 변수의 속성 ( 타입 ) 을 지시 정의 : 기억장소 할당 예 ) 정의int sp;double val[MAXVAL];

예 ) 선언extern int sp;extern double val[];

• 예

In file1:

extern int sp;

extern double val[];

void push(double f) { … }

double pop(void) { … }

In file2:

int sp = 0;

double val[MAXVAL];

Separate Compile

• Let us now consider dividing the calculator program into several source files.

• 1st file : main (main.c)• 2nd file : push, pop (stack.c)• 3rd file : getop (getop.c)• 4th file : getch, ungetch (getch.c)

#define NUMBER ‘0’

void push(double);

double pop(void);

int getop(char []);

int getch(void);

void ungetch(int);

calc.h

Separate Compile

#include <stdio.h>

#inlcude <math.h>

#include “calc.h”

#define MAXOP 100

main() { … }

main.c

getop.c

#include <stdio.h>

#include <ctype.h>

#include “calc.h”

int getop() { … }

stack.c

#include <stdio.h>

#include “calc.h”

#define MAXVAL 100

int sp = 0;

double val[MAXVAL];

void push(double) { …}

double pop(void) { … }

Separate Compile

Separate Compile

getch.c

#include <stdio.h>

#define BUFSIZE 100

char buf[BUFSIZE];

int bufp = 0;

int getch(void) { … }

void ungetch(int c) { … }

Turbo-C( 분리컴파일 )

• Create the file rcalc.prj and put into it the name of each file that contains definitions of functions used in the program. The names may be separated by white space, commas, or semicolons.

• In file rcalc.prj: main.c (calc.h) getop.c (calc.h) stack.c (calc.h) getch.c(calc.h)

• Select the Project menu from main menu line.

• Then in turn select Project name and type in the name rcalc.prj

• Now press Alt-r to make and run the program.

• Turbo-C project facility will recompile only those files that have been changed since the last time rcalc.exe was updated.

Static 변수 사용 예• Static 변수의 선언은 컴파일되는

파일의 나머지 부분에 대해서만 scope를 제약하는 기능

• 예 static char buf[BUFSIZE]; static int bufp = 0; int getch(void) { … }

C 전처리기1. File inclusion #include “filename” #include <filename>2. Macro 대치 #define name replacement text #define forever for(;;) #define max(A,B) ((A) > (B) ? (A) : (B)) #define square(x) ( (x) * (x) )