所有原创文章转载请注明作者及链接
// blackboycpp(AT)gmail.com
// QQ群: 135202158
/******************************************************************** File: ListStack.h Author: blackboy, [email protected] Purpose: 栈ADT,链表实现 Created: 2011-03-31 Modified: 2011-03-31 09:02 *********************************************************************/ #ifndef __LIST_STACK_H__ #define __LIST_STACK_H__ typedef int ElementType; struct _Node; typedef struct _Node Node; typedef Node* PtrToNode; typedef PtrToNode Stack; struct _Node { ElementType Val; PtrToNode Next; }; ////////////////////////////////////////////////////////////////////////// Stack CreateStack(); void DisposeStack(Stack); void MakeEmpty(Stack); void Push(ElementType, Stack); void Pop(Stack); ElementType Top(Stack); int IsEmpty(Stack); #endif
#include <stdlib.h> #include "ListStack.h" Stack CreateStack() { Stack S = (Node*)malloc(sizeof(Node)); S->Next = NULL; return S; } void DisposeStack(Stack S) { MakeEmpty(S); free(S); S = NULL; } void MakeEmpty(Stack S) { while(!IsEmpty(S)) Pop(S); } void Push(ElementType X, Stack S) { PtrToNode p; p = (Node*)malloc(sizeof(Node)); p->Val = X; p->Next = S->Next; S->Next = p; } void Pop(Stack S) { PtrToNode p; if(IsEmpty(S)) return; p = S->Next; S->Next = S->Next->Next; free(p); p = NULL; } ElementType Top(Stack S) { if(IsEmpty(S)) return 0; return S->Next->Val; } int IsEmpty(Stack S) { return S->Next == NULL; }
/******************************************************************** File: calc.c Author: blackboy, [email protected] Purpose: 将中缀表达式转换为后缀表达式,并求出它的值。 只考虑1位数字(0-9)操作数,及+,*,(,)4个操作符 Created: 2011-04-01 Modified: 2011-04-01 15:37 *********************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include "ListStack.h" // Stack中元素类型为int #define MAX_INPUT 100 void InfixToPostfix(char*); // 将中缀表达式(将标准的算术表达式)转换为后缀表达式 int CalcPostfix(const char*); // 计算后缀表达式 void PrintStack(Stack); // 将栈内容打印到标准输出 int main() { char input[MAX_INPUT] = {0}; //char input[] = "a+b*c+(d*e+f)*g"; //char input[] = "2+3*2+(5*3+7)*2"; // 52 printf("input infix expression(MAX: %d):/n", MAX_INPUT); gets(input); printf("infix: %s/n", input); InfixToPostfix(input); printf("postfix: %s/n", input); printf("result: %d/n", CalcPostfix(input)); system("PAUSE"); return 0; } int CalcPostfix(const char* input) { Stack S; int i, a, b; char tmp; S = CreateStack(); for(i=0; i<(int)strlen(input); ++i) { if( isdigit(input[i]) ) { tmp = input[i]; Push(atoi(&tmp), S); } else { a = Top(S); Pop(S); b = Top(S); Pop(S); if(input[i] == '+') Push(a+b, S); else if(input[i] == '*') Push(a*b, S); } //PrintStack(S); } i = Top(S); DisposeStack(S); return i; } void InfixToPostfix(char* input) { char* tmp = (char*)calloc(strlen(input) + 1, sizeof(char)); Stack S; int i, j; S = CreateStack(); j = 0; for(i=0; i<(int)strlen(input); ++i) { if( isdigit(input[i]) ) { tmp[j++] = input[i]; } else if( input[i] == '+' ) { while(!IsEmpty(S)) { if(Top(S) == '+' || Top(S) == '*') { tmp[j++] = Top(S); Pop(S); } else break; } Push(input[i], S); } else if( input[i] == '*' ) { while(!IsEmpty(S)) { if(Top(S) == '*') { tmp[j++] = Top(S); Pop(S); } else break; } Push(input[i], S); } else if( input[i] == '(' ) { Push(input[i], S); } else if( input[i] == ')' ) { while(Top(S) != '(') { tmp[j++] = Top(S); Pop(S); } Pop(S); } } while(!IsEmpty(S)) { tmp[j++] = Top(S); Pop(S); } DisposeStack(S); strcpy(input, tmp); } void PrintStack(Stack S) { PtrToNode p = S->Next; while(p != NULL) { printf("%d ", p->Val); p = p->Next; } printf("/n"); }