2017-09-27 13 views
1

infix 표현식을 후행으로 변환하는 c로 코딩하고 있습니다. 오류없이 실행했지만 올바른 출력을 내고있는 것은 아닙니다. 출력은 형태로 될 것으로 생각된다프로그래밍에서 접미사 - 접미사 변환 (A^B^C)/(D * E)

(A^B^C)/(D*E) = ABC^^DE*/하지만 난 내 질문에 전체 코드를 삽입 할 수없는 이유를 모르겠어요 출력이 AB^C^DE*/

The convert method is as follows in the comment 

답변

0

입니다 이 내 가득 코드

#include <stdio.h> 
#include <conio.h> 
#include <ctype.h> 
#define SIZE 50 
char s[SIZE]; 
int top=-1;  
push(char elem) 
{      
    s[++top]=elem; 
} 

char pop() 
{      
    return(s[top--]); 
} 

int pr(char elem) 
{     
    switch(elem) 
    { 
    case '#': return 0; 
    case '(': return 1; 
    case '+': 
    case '-': return 2; 
    case '*': 
    case '/': return 3; 
    } 
} 

void main() 
{       
    char infx[50],pofx[50],ch,elem; 
    int i=0,k=0; 
    clrscr(); 
    printf("\n\nRead the Infix Expression ? "); 
    scanf("%s",infx); 
    push('#'); 
    while((ch=infx[i++]) != '\0') 
    { 
    if(ch == '(') push(ch); 
    else 
    if(isalnum(ch)) pofx[k++]=ch; 
     else 
    if(ch == ')') 
    { 
     while(s[top] != '(') 
     pofx[k++]=pop(); 
     elem=pop(); 
    } 
    else 
    {  
     while(pr(s[top]) >= pr(ch)) 
     pofx[k++]=pop(); 
     push(ch); 
    } 
    } 
    while(s[top] != '#')  
    pofx[k++]=pop(); 
    pofx[k]='\0';   
    printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infx,pofx); 
    getch(); 
}