Soft Ware/자료구조 및 알고리즘

중위 순위를 후위순위로 바꾸는 소스입니다

달려가보자 2012. 4. 15. 14:49

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum {FALSE=0,TRUE};

typedef struct Operation_Stack
{
 char Data;
 struct Operation_Stack * beforeStack;
}Operation_Stack;


typedef struct Number_Stack
{
 char Data;
 struct Number_Stack * NextStack;
}Number_Stack;


typedef struct Top
{
 struct Operation_Stack * OperationTop;
 struct Number_Stack * NumberHead;
}Top;


void OperationPop(Top ** top);
int NumberPush(Top ** top, char nData);
int OperationPush(Top ** top, char cData);
void Push(Top ** top, char * string);
void NuberPrint(Top ** top);

int main()
{
 Top Data ;
 Top *Top = &Data;

 char String[100]={0,};

 Top->NumberHead = NULL;
 Top->OperationTop = NULL;

 printf("Data input : ");
 scanf("%s", String);
 
 Push(&Top,String);

 while(Top->OperationTop!= NULL)
 {
  OperationPop(&Top);
 }

 NuberPrint(&Top);
 

 return 0;
}

int NumberPush(Top ** top, char nData)
{
  Number_Stack * Node = NULL;
  Number_Stack * tail = NULL;

 Node = (Number_Stack *)calloc(1, sizeof(Number_Stack));
 
 Node->Data = nData;
 Node->NextStack = NULL;

 if(((*top)->NumberHead) == NULL) (*top)->NumberHead = Node;
 
 else
 {
  for(tail = (*top)->NumberHead; tail->NextStack != NULL; tail = tail->NextStack);

  tail->NextStack = Node;
 }

 return TRUE;
}

int OperationPush(Top ** top, char cData)
{
 Operation_Stack * Node = NULL;

 Node = (Operation_Stack *)calloc(1,sizeof(Operation_Stack));

 Node->Data = cData;
 Node->beforeStack = NULL;

 if((*top)->OperationTop == NULL) (*top)->OperationTop = Node;

 else
 {
  if((*top)->OperationTop->Data == '*' || (*top)->OperationTop->Data == '/')
  {
   OperationPop(top);
  }
  Node->beforeStack = (*top)->OperationTop;
  (*top)->OperationTop = Node;
 }

 return TRUE;
}

void Push(Top ** top, char * string)
{
 int Len = 0;
 int count = 0;

 Len = strlen(string);

 for(count = 0; count < Len; count++,string++)
 {
  if((*string) != '*' && (*string) !='/' && (*string) != '+' && (*string) != '-')
  {
   if(NumberPush(top,(*string))) printf("NumberPush succese!!\n");
  }
  else
  {
   if(OperationPush(top,(*string))) printf("OperationPush succese!!\n");
  }
 }
}

void OperationPop(Top ** top)
{
 char cData=0;

 if((*top)->OperationTop == NULL)
 {
  printf("No Data!!\n");
  exit(0);
 }

 cData = (*top)->OperationTop->Data;
 (*top)->OperationTop = (*top)->OperationTop->beforeStack;
 
 NumberPush(top,cData);
}

void NuberPrint(Top ** top)
{
 for(;(*top)->NumberHead != NULL;(*top)->NumberHead= (*top)->NumberHead->NextStack)
 {
  printf("%c",(*top)->NumberHead->Data);
 }
 
}