operand 사이 공백 넣는 방법

스택을 이용한 한 자리수 계산기를 두자리로 변환해보는 중인데
strncat이나 atoi를 이용하여 operand뒤 operator앞에 공백을 넣으면 될 줄 알았더니
space연산자를 넣어보기도 하고 함수를 써보기도 했지만 뭘 해도 한 자리수 이상은
맨 뒤에 숫자를 제외하고는 처리가 안되더라구요,, 어떻게 수정해야 할까요 고수님들 ㅠ
#include
#define MAX_STACK_SIZE 100
#define MAX_EXPR_SIZE 100
//atoi함수 등을 사용하여 숫자 뒤 공백을 넣어 두자리 수를 구별해보자
typedef enum {lparen, rparen, plus, minus, times, divide, mod, eos, operand} precedence;
int stack [MAX_STACK_SIZE];
char iexpr[MAX_EXPR_SIZE];
char pexpr[MAX_EXPR_SIZE];
static int isp[] = {0,19,12,12,13,13,13,0};
static int icp[] = {20,19,12,12,13,13,13,0};
int top;
precedence get_token(char expr[], char *symbol, int *n) {
*symbol= expr[*n]; (*n)++;
switch(*symbol) {
case '(' : return lparen;
case ')' : return rparen;
case '+' : return plus;
case '-' : return minus;
case '*' : return times;
case '/' : return divide;
case '%' : return mod;
case '\0' : return eos;
default : return operand;
}
}
char print_token(precedence token, int *p)
{
if (token == plus) pexpr[(*p)++]=' +';
else if (token == minus) pexpr[(*p)++]=' -';
else if (token == times) pexpr[(*p)++]=' *';
else if (token == divide) pexpr[(*p)++]=' /';
else if (token == mod) pexpr[(*p)++]=' %';
}
int eval(void) {
precedence token;
int op1, op2, n= 0, top= -1;
char symbol;
init_stack();
token= get_token(pexpr, &symbol, &n);
while(token != eos) {
if(token==operand) push(&top, symbol- '0');
else {
op2= pop(&top); op1= pop(&top);
switch(token) {
case plus: push(&top, op1+op2); break;
case minus: push(&top, op1-op2); break;
case times: push(&top, op1*op2); break;
case divide: push(&top, op1/op2); break;
case mod: push(&top, op1%op2);
}
}
token= get_token(pexpr,&symbol, &n);
} // while
return pop(&top);
}
void infix_to_postfix (void) {
char symbol;
precedence token;
int n=0, top= 0;
int p =0;
stack[top]= eos;
init_stack();
for(token=get_token(iexpr, &symbol, &n); token != eos; token=get_token(iexpr, &symbol, &n)) {
if(token == operand)
//printf("%c", symbol);
pexpr[p++] = symbol;
else if(token == rparen) {
while(stack[top] != lparen) print_token(pop(&top),&p);//print_token(pop(&top));
pop(&top); // 왼쪽괄호를 버림
}
else {
while(isp[stack[top]] >= icp[token])
print_token(pop(&top),&p);//print_token(pop(&top));
push(&top, token);
} // else
} // for
while((token= pop(&top)) != eos)
print_token(token,&p);//print_token(token);
//print_token('\0');
pexpr[p] = '\0';
}
void init_stack(void)
{
top =-1;
}
void push(int *ptop, int item){
if(*ptop >= MAX_STACK_SIZE -1){
printf("Stack is Full\n");
return;
}
stack[++*ptop] = item;
}
int pop(int *ptop){
int token;
if(*ptop == -1)
printf("Stack is Empty");
token = stack[(*ptop)--];
return token;
}
void main()
{
int i,result;
loop2:
printf("입력 스트링 : ");
gets(iexpr);
for (i = 0; iexpr[i] != '\0'; i++)
if (iexpr[i] == 'E')
goto loop3;
if (iexpr[i] == '$')
goto loop1;
loop1:
infix_to_postfix();
//출력
printf("Postfix 형태 : ");
for (i = 0; pexpr[i] != '\0'; i++)
{
if (pexpr[i] == '$')
pexpr[i] = pexpr[i - 1];
else
printf("%c", pexpr[i]);
}
printf("\n");
result = eval();
printf("계산 결과 : %d\n\n", result);
goto loop2;
loop3:
//끝
printf("END-OF-OUTPUT");
}
lexer, 즉 귀하의 코드에서 get_token
lexer, 즉 귀하의 코드에서
get_token
부분이 똑똑해져야 합니다.한 글자만 달랑 보고 판단하고 반환할 게 아니라, 연속된 digit sequence를 보고 숫자 하나로 합쳐서 반환해야지요.
구현하려면 여러 군데 고치셔야 되서 번거롭겠지만 개념상으로는 딱 그 정도면 됩니다.
댓글 달기