#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);
}
}
'Soft Ware > 자료구조 및 알고리즘' 카테고리의 다른 글
정렬 !! -선택정렬- (0) | 2012.04.16 |
---|---|
유클리드 호제법을 이용한 최대공약수 구하기 (0) | 2012.04.16 |
동적할당과 스택 구조를 이용하여 후위 연산해보기!! (0) | 2012.04.15 |
원형 리스트 !!! (0) | 2011.08.09 |
연결리스트 한번 공부해보자!!! (0) | 2011.08.08 |