User:Marphy2k

postfix solution


 * 1) include
 * 2) include
 * 3) include
 * 4) include

void main

{ clrscr; char infix[50],postfix[50],stack[50],counter='1' ; int i,j,k,length;

gets(infix); length=strlen(infix); infix[length]='$'; i=0,j=0,k=1,stack[0]='#';

while(1) { if(infix[i]=='$') break; else if(infix[i]>='a'&&infix[i]<='z') { postfix[j]=infix[i]; j=j+1; i=i+1;

} else if(infix[i]=='(') { stack[k]=infix[i];  k++;  i++; } else if(infix[i]==')') { while(1) { if(stack[k-1]=='(') {  k--;  break; }

else {

k--; postfix[j]=stack[k]; j++; } } i++; }

else { if(infix[i]=='+' || infix[i]=='-') { if(stack[k-1]=='+' || stack[k-1]=='-' || stack[k-1]=='/'|| stack[k-1]=='*') { k--; postfix[j]=stack[k]; j++; stack[k]=infix[i]; i++; k++; } else {

stack[k]=infix[i] ; i++; k++; } } else if(infix[i]=='^')  //'^' { stack[k]=infix[i] ; i++; k++;

} else if (infix[i]=='/'||infix[i]=='*')

{ if (stack[k-1]=='/'||stack[k-1]== '*')

{

k--; postfix[j]=stack[k];

j++; stack[k]=infix[i];

i++; k++;

}  else {  stack[k]=infix[i]; k++; i++; }  }   }   }   while(1)

{  if( stack[k-1]=='#') break; else {    k--; postfix[j]=stack[k]; j++; }    }     postfix[j]='$'; i=0; while(1) {    if( postfix[i]=='$'  ) break; else {    cout<< postfix[i]; }    i++; }    cout<='a'&&postfix[j]<='z') {    stack [k]=postfix[j]; k++; j++; }    else {    if(stack[k-2]>='A'&& stack[k-1]>='A') {    cout<<"T"<='A') {    cout<<"T"<='A'&& stack[k-1]<'A') {    cout<<"T"<<counter<<"="<<stack[k-2]<<postfix[j]<<"T"<<stack[k-1]<<endl<<endl; k=k-2; stack[k]=counter; k++; j++; counter++;

}    else {    cout<<"T"<<counter<<"="<<"T"<<stack[k-2]<<postfix[j]<<"T"<<stack[k-1]<<endl<<endl; k=k-2; stack[k]=counter; k++; j++; counter++; }    }     }     getch;

}