#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
typedef enum { ADD = 0, SUBT = 0, MULT = 1, DIVD = 1, LEFTB = -1, RIGHTB =-1, NLL = -2, } Operator;
typedef char ElementType; #define Node ptrNode #define Stack ptrNode typedef struct _Node // node defination { ElementType data; struct _Node *next; } * ptrNode;
void CheckStack(Stack S) { if (S == NULL) { printf("stack is invaild!\n"); } }
int IsEmpty(Stack S) { CheckStack(S); return S->next == NULL; }
Stack CreateStack(void) { Stack S = (Stack)malloc(sizeof(struct _Node)); CheckStack(S); S->next = NULL; return S; }
ElementType Pop(Stack S) { int temp; CheckStack(S); Node tmepNode = S->next; if (tmepNode != NULL) { temp = tmepNode->data; S->next = tmepNode->next; free(tmepNode); return temp; } else { return -1; } }
void MakeEmpty(Stack S) { CheckStack(S); while (S->next != NULL) { Pop(S); } } void DisposeStack(Stack S) { CheckStack(S); MakeEmpty(S); free(S); }
void Push(ElementType X, Stack S) { CheckStack(S); Node tempNode = S->next; Node newNode = (Node)malloc(sizeof(struct _Node)); newNode->data = X; if (tempNode != NULL) { newNode->next = tempNode; } else { newNode->next = NULL; } S->next = newNode; }
ElementType Top(Stack S) { CheckStack(S); if (S->next != NULL) { return S->next->data; } else { return -1; } }
void PrintOutPut(Stack S) { Node tep = S->next; char *str=(char*)malloc(30*sizeof(char)); int cnt=0; CheckStack(S); while (tep != NULL) { *(str+(cnt++))=tep->data; tep = tep->next; } while(cnt--){ printf("%c",*(str+cnt)); } free(str); }
void PrintStack(Stack S) { Node tep = S->next; while (tep != NULL) { printf("addr=0x%3lX data=%d nextaddr=0x%3lX\n", (unsigned long int)tep & 0xFFF, tep->data, (unsigned long int)tep->next & 0xFFF); tep = tep->next; } }
Operator ChToOpt(char C) { Operator temp; switch (C) { case '+': temp = ADD; break; case '-': temp = SUBT; break; case '*': temp = MULT; break; case '/': temp = DIVD; break; case '(': temp = LEFTB; break; case ')': temp = RIGHTB; break; default: temp = NLL; break; } return temp; }
void printfCovt(Stack stack, Stack out, int i) { printf("-------STEP %d---------\n", i); printf("stack : "); PrintOutPut(stack); printf(" <--\n"); printf("output: "); PrintOutPut(out); printf(" <--\n"); printf("-----------------------\n\n"); }
int main(int argc, char const *argv[]) { int tempint; Stack MyStack, output = NULL; uint8_t i = 0; char *func = "1+2*3+(4*5+6)*7"; Operator curOp; Operator TopOp; MyStack = CreateStack(); output = CreateStack(); for (i = 0; i < strlen(func); i++) {
switch (*(func + i)) { case '+': case '-': case '*': case '/': do { curOp = ChToOpt(*(func + i)); TopOp = ChToOpt(Top(MyStack)); if (TopOp >= curOp) { Push(Pop(MyStack), output); TopOp = ChToOpt(Top(MyStack)); } } while (TopOp >= curOp); Push(*(func + i), MyStack); break; case '(': Push(*(func + i), MyStack); break; case ')': while (Top(MyStack) != '(') { Push(Pop(MyStack), output); } Pop(MyStack); break; default: Push(*(func + i), output); break; } printfCovt(MyStack, output, i); }
while (!IsEmpty(MyStack)) { Push(Pop(MyStack), output); } printfCovt(MyStack, output, i++);
return 0; }
|