ÇÁ·Î±×·¡¹Ö

 3206, 1/161 ȸ¿ø°¡ÀÔ  ·Î±×ÀΠ 
   leebk1124
   c¾ð¾î segmentation fault:11 ¿À·ù Áú¹®µå¸³´Ï´Ù!

http://www.hackerschool.org/HS_Boards/zboard.php?id=QNA_programming&no=6628 [º¹»ç]


#include <stdio.h>
#include <ctype.h>
#define STACK_SIZE 100

char token_stack[STACK_SIZE];
int token_top = -1;
void infix_to_postfix();
int precedence(char op);
void push_token(char sym);
char pop_token();
void print_stack();
int is_empty();
double cal_postfix(char expr[]);

int main()
{
    infix_to_postfix();
}

void infix_to_postfix()
{
    char expr[100], sym, token, postfix[100];
    int cnt = 0;
    int pos = 0;
    int i = 0;

    printf("Enter the expression :: ");
    scanf("%s", expr);

    for(int i = 0; expr[i] != '\0'; i++) {
        printf("%c", expr[i]);
    }

    while ((token = expr[pos++]) != '\0')
    {
        if(token >= '0' && token <= '9') {
            do{
                postfix[cnt++] = token;
            }while((token >= '0' && token <= '9'));
            
            postfix[cnt++] = ' ';
            //print_stack();
        }
        
        else if(token == '(') {
            push_token(token);
            //print_stack();
        }

        else if(token == ')') {
            while((sym = pop_token()) != '(') {
                postfix[cnt++] = sym;
                postfix[cnt++] = ' ';
            }
           // print_stack();
        }

        else
        {
            while (precedence(token_stack[token_top]) >= precedence(token) && token_top != -1) {
                postfix[cnt++] = pop_token();
                postfix[cnt++] = ' ';
            }
            push_token(token);
            
            //print_stack();
        }
        

        //else
        //    pos++;
    }

    while (token_top >= 0)
    {
        postfix[cnt++] = pop_token();
        postfix[cnt++] = ' ';
       // print_stack();
    }
    //print_stack();

    
    postfix[cnt] = '\0';

    //while(postfix[i++] != '\0') {
    //    printf("%c\n", postfix[i]);
    //}

    //cal_postfix(postfix);

}

int precedence(char op)
{
    if (op == '(')
        return 0;
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == '/' || op == '%')
        return 2;
    else
        return ' ';
}

void push_token(char sym)
{
    if (token_top < STACK_SIZE -1) {
        token_stack[token_top] = sym;
        token_top++;
    }
    else
        printf("token stack full\n");
}

char pop_token()
{
    if (token_top >= 0)
        return token_stack[token_top--];

    printf("token stack empty\n");
    return ' ';
}

void print_stack()
{
    if(!is_empty()) {
        for(int i = 0; i <= token_top; i++) {
            printf("%c", token_stack[i]);
        }
        printf("\n");
    }
}

int is_empty() {
    if(token_top == -1) {
        printf("Stack is empty.\n");
        return 1;
    }
    return 0;
}

double cal_postfix(char expr[])
{
    char pos;
    double val0, val1, val2;
    int i = 0;

    while(expr[i] != '\0')
    {
        pos = expr[i++];

        if(pos >= '0' && pos <= '9')
        {
            val0 = pos - '0';
            push_token(val0);
        }

        else if(pos == '+' || pos == '-' || pos == '*' || pos == '/')
        {
            val2 = pop_token();
            val1 = pop_token();

            switch(pos)
            {
                case '+' : push_token(val1 + val2); break;
                case '-' : push_token(val1 - val2); break;
                case '*' : push_token(val1 * val2); break;
                case '/' : push_token(val1 / val2); break;
            }
        }
    }
    return pop_token();
}


infix¸¦ postfix·Î ¹Ù²Ù°í °è»êÀ» ÇÏ´Â ÇÁ·Î±×·¥ÀÔ´Ï´Ù.
±×·±µ¥ °£´ÜÇÏ°Ô 24+3¸¸ ÀÔ·ÂÇصµ segmentatino fault¿À·ù°¡ ¹ß»ýÇÕ´Ï´Ù.
¹è¿­À» ÀԷ¹ÞÀº ÈÄ printf¹®µµ ½ÇÇàÀÌ µÇÁö ¾Ê½À´Ï´Ù.
¾îµð¼­ ¹®Á¦°¡ ¹ß»ýÇÑ°ÇÁö µµÀúÈ÷ ¸øã°Ú¾î¼­ Áú¹®µå¸³´Ï´Ù. °¨»çÇÕ´Ï´Ù!

  Hit : 1945     Date : 2021/05/21 01:03



    
cd80 ÀÔ·ÂÈÄ ¹Ù·Î³ª¿À´Â printf´Â \nÀÌ ¾ø¾î¼­ flush°¡ µÇÁö ¾Ê¾Æ¼­ Ãâ·ÂÀÌ ¾ÈµÇ´Â°Å±¸¿ä
Å©·¡½¬ ³ª´Â°Å´Â
if(token >= '0' && token <= '9') {
do{
postfix[cnt++] = token;
}while((token >= '0' && token <= '9'));
¿©±â¼­ ¹«ÇÑ·çÇÁ¸¦ µ¹¾Æ¼­ Å©·¡½¬°¡ ¹ß»ýÇÕ´Ï´Ù
2021/05/21  
leebk1124 ¿Í Çæ ±×·¯³×¿ä!! Á¤¸» °¨»çÇÕ´Ï´Ù!! 2021/05/22