Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[6167] infix수식을prefix수식좀알려주세요~
조민혁 [superbsoul] 3031 읽음    2008-04-02 23:25
infix수식을prefix수식으로 나타냈는데 while문에서 에러가;;;;
그리고복잡도가뭔지설명좀해주세요~

#include <stdio.h>
#include <string.h>
#define MAX_STACK_SIZE 100
#define MAX_EXPR_SIZE 100

typedef enum {lparen, rparen, plus, minus, times, divide,
              mod , eos, operand} precedence;

int isp[]={19,0,12,12,13,13,13,0};
int icp[]={19,20,12,12,13,13,13,0};
//사용자 정의 함수
void prefix();
precedence get_token(char *symbol, int *n);
char print_token(precedence token);
precedence u_delete(int *top);
void u_add(int *top, precedence token);

char expr[MAX_EXPR_SIZE];
char buffer[MAX_EXPR_SIZE];
precedence stack[MAX_STACK_SIZE];

precedence token;
precedence t_token;
char symbol;
int n=0;
int top=0;
int btop=0;

int main(void)
{
    int intSelect;

     while()
    {
       
        printf("Infix -> Pretfix 바꾸기 : ");
      
        scanf("%d",&intSelect);

        if(intSelect)
        {
            n=0;//전역 변수 값 리셋
            top=0;
            btop=0;
            prefix();
            return 0;
        }


    }//end of while
  
}//end of main

void prefix()
{
      int length,j;

      //precedence stack[MAX_STACK_SIZE];
      //precedence token;

      printf("\n수식을 입력해 주세요\n");
      printf("infix  >> ");
      scanf("%s",expr); //infix : char 배열

      length=strlen(expr);
      n=length-1; //수식의 길이 값을 구하여 수식의 크기만큼 반복하기 위한 제어변수
      /*for(i=0 ; i<=length ; i++)
      {
          stack[i]=expr[i]//문자열을 열거형 스택에 치환
      }*/

      stack[0]=eos;
      printf("prefix >> ");
      for(j=0; j<length; j++)//token=get_token(&symbol, &n); token!=eos; token=get_token(&symbol, &n))
      {
              token=get_token(&symbol, &n);

              if(token==operand)
              {
                  buffer[btop]=symbol;
                  btop++;
                  //printf("%c", symbol);
              }
             
              else if(token==lparen)
              {
                  while(stack[top]!=rparen)
                  {
                      buffer[btop]=print_token(u_delete(&top));
                      btop++;
                  }
                  u_delete(&top);
              }

              else
              {
                  while(isp[stack[top]]>icp[token]) //전위에선 우선 순위가 같아도 나오지 않는다.즉 클 때만 나온다.
                  {
                      buffer[btop]=print_token(u_delete(&top));
                      btop++;
                  }
               u_add(&top, token);
              }
      }//enf of for
      while ((token=u_delete(&top))!=eos)
      {
           buffer[btop]=print_token(token);
           btop++;
      }

      for(j=length-1; j>=0; j--)
      {
          printf("%c",buffer[j]);
      }
      printf("\n");
}

precedence get_token(char *symbol, int *n)
{
    //배열의 오른쪽 부터 왼쪽으로 수식을 읽어 나간다.(n=length)
    *symbol=expr[(*n)];
    (*n)--;
    switch(*symbol)
    {
        case '(' : return lparen;
        case ')' : return rparen;
        case '+' : return plus;
        case '-' : return minus;
        case '/' : return divide;
        case '*' : return times;
        case '%' : return mod;
        case ' ' : return eos;
        default  : return operand;
    }
}

char print_token(precedence token)
{
    switch(token)
    {
        case lparen: return '(';
            //break;
        case rparen: return ')';
            //break;
        case plus  : return '+';
            //break;
        case minus : return '-';
            //break;
        case divide: return '/';
            //break;
        case times : return '*';
            //break;
        case mod   : return '%';
            //break;
    }
    return 0;
}

precedence u_delete(int *top)
{
    //precedence t_token;

    t_token = stack[*top];
    (*top)--;
    return t_token;
}

void u_add(int *top, precedence token)
{
    (*top)++;
    stack[*top] = token;
}

+ -

관련 글 리스트
6167 infix수식을prefix수식좀알려주세요~ 조민혁 3031 2008/04/02
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.