ÇÁ·Î±×·¡¹Ö

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

http://www.hackerschool.org/HS_Boards/zboard.php?desc=desc&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 : 2124     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  
84   Á¤¸» ±ÞÇÕ´Ï´Ù. °í¼ö´Ôµé µµ¿ÍÁֽʽÿä[4]     muhonbunk
06/26 2114
83   net send Àε¥¿ä µµ¿ÍÁÖ¼¼¿©...[5]     maxdongjin
10/25 2114
82   ¸®½ºÇÁ »ç¿ëÇϽôºРÀÖ³ª¿ä?[1]     my1004
12/25 2113
81     [re] cÇÁ·Î±×·¡¹Ö Áú¹®ÀÔ´Ï´Ù..     moongchiza
12/21 2113
80   Àú±â¿ä Á¦°¡ Áß2°¡ µÇ´ÂÇлýÀε¥...[7]     okko80
08/29 2112
79   trainer8¿¡¼­[1]     deathnote
01/18 2102
78   ´äÁ»°¡¸£ÃÄÁÖ¼¼¿ä.[1]     nb01122
08/28 2094
77     [re] ÇØÅ·Çϴµ¥¿¡ À־..     moongchiza
07/31 2090
76     [re] gcc...     xaero77
10/28 2086
75   [Áú¹®] c ÇÔ¼ö ¸Þ´º¾ó ±¸ÇÒ¼ö Àִ°÷???[2]     golRyun
03/31 2085
74     [re] ¿ÏÀüÈ÷ Ãʺ¸Àε¥, ¸î°¡Áö Áú¹®Á» µå¸±²²¿ä.[3]     goya
02/23 2082
73   File Descriptor °ü·Ã Áú¹®ÀÔ´Ï´Ù.     tjdalstjr938
01/22 2078
72     [re] ÀڷᱸÁ¶¿¡¼­ Èñ¼ÒÇà·Ä¿¡ ´ëÇؼ­ ¼³¸íÇØÁֽǺÐ...ºÎŹµå¸®°Ú½À´Ï´Ù     tonoticing
11/19 2070
71   ÆÄƼ¼Ç Áú¹®ÀÌ¿ä..[3]     kjh4697
07/30 2070
70   C¾ð¾î¸¦ °øºÎÇϸ鼭...Áú¹®[2]     tegi83
02/11 2068
69     [re] ¿¡..¸®´ª½ºÇØÅ·À» ÇÒ·Á¸é ¹» ¹è¿öµÖ¾ß ÇÒ±î¿ä     idl0521
10/06 2067
68     [re] ¾Æ¹«°Íµµ¸ô¶ó¿ä///.¤Ð¤Ð     twinz
08/25 2062
67   cmd⸻°í[5]     piousdo
02/13 2061
66     [re] ÀÌ°Í Âü...     dfutn626
02/09 2060
65     [re] ¹è¿­¿¡ ´ëÇØ Áú¹®ÀÖ½À´Ï´Ù     vamalboro
10/05 2059
[1]..[151][152][153][154][155][156] 157 [158][159][160]..[161]

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