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 부분이 똑똑해져야 합니다.

한 글자만 달랑 보고 판단하고 반환할 게 아니라, 연속된 digit sequence를 보고 숫자 하나로 합쳐서 반환해야지요.

구현하려면 여러 군데 고치셔야 되서 번거롭겠지만 개념상으로는 딱 그 정도면 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.