ÇÁ·Î±×·¡¹Ö

 3206, 3/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 : 2004     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  
3166     [re] Àú±â,.,, ¹¹°¡ À߸ø µÇ¾ú´ÂÁö,,,     rootguy
09/14 1916
3165     [re] char ÇüÀÇ º¯¼ö¿¡ ¹®ÀÚ ´ëÀÔÇϴ¹æ¹ýÁ»..(±×¿Ü µîµî)     oes2
08/26 1917
3164   C¾ð¾î ¿¡¼­[1]     jh31829
11/21 1919
3163   Æ®·¹ÀÌ´×2¿¡¼­ 3À¸·Î ¾îÄÉ °¡ÁÒ?[3]     killerit
03/09 1923
3162     [re] ÀÌ ´Ü¾î Á»¿ä ¤Ð¤Ð(Áߺ¹¤¸¤µ)     hackcool
07/31 1924
3161   [ÃʱÞ] ÀÚ·áÇü°ü·Ã[4]     íÆå¨éë
03/15 1929
3160   óÀ½ÀÇ ½ÃÀÛ¿¡ ´ëÇؼ­ Áú¹® µå·Á¿ä..¤Ì.¤Ð..¤¾[1]     manilaina
02/01 1937
3159   Çб³ °úÁ¦Áß ¸·Çô¼­¿ä...Áú¹®µå¸³´Ï´Ù...[7]     kamijyo
10/17 1943
3158   ÄÄÇ»ÅÍ ÇÁ·Î±×·¡¹ÖÀ» ÇÒ·Á°íÇϴµ¥..[1]     okko80
08/29 1943
3157     [re] ÀÚ¹Ù·Î °ÔÀÓÀ» Á¦ÀÛÇÏ°í ½ÍÀºµ¥...     zetrhee
04/15 1961
3156     [re] ÇÁ·Î±×·¡¹ÖÀ» ¹è¿öº¸·ÁÇÕ´Ï´Ù.[1]     rootguy
10/14 1966
3155   ÇѽÉÇÏ°ÚÁö¸¸¼­µµ ¼º½ÇÇÑ ´äº¯ ºÎŹµå¸³´Ï´Ù[2]     dragon2044
10/22 1967
3154   ´Ù½Ã Àü¹®ÀûÀ¸·Î °øºÎ½ÃÀÛÇغ¸°í½Í½À´Ï´Ù.     philomylove
11/03 1971
3153     [re] ÀÌ°Å º¸½Ã°í °íÃÄÁÖ¼¼¿ä[1]     rain48
10/15 1972
3152   C¾ð¾î °ü·Ã¹®ÀÇ[4]     radical31
03/13 1981
3151   vi Áú¹®Á»ÇÒ°Ô¿ä.[7]     power602
11/11 1988
3150   [ÃʱÞ] ´©Àû°è»ê½Ã °á°ú°ªÀÌ»ó[2]     íÆå¨éë
03/16 1995
3149     [re] c¾ð¾î¿¡¼­¿ä...     namco1
10/26 1996
  c¾ð¾î segmentation fault:11 ¿À·ù Áú¹®µå¸³´Ï´Ù![2]     leebk1124
05/21 2003
3147       [re] [re] ¶Ç Áú¹®Çϳ׿ä.. ±¸Á¶Ã¼ÀÇ Æ÷ÀÎÅÍ¿¡ °üÇÑ Áú¹®ÀÔ´Ï´Ù..[3]     °ËÀº¿ù¾Æ
12/11 2015
[1][2] 3 [4][5][6][7][8][9][10]..[161]

Copyright 1999-2024 Zeroboard / skin by Hackerschool.org / Secure Patch by Hackerschool.org