請編寫程式實現表示式求值
阿新 • • 發佈:2019-02-07
#include <stdio.h> #include <stdlib.h> #include <string.h> #define error 0 #define ok 1 #define overflow -1 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define OPSETSIZE 7 char OPSET[OPSETSIZE]={'+','-','*','/','(',')','#'}; unsigned char Prior[7][7] = { '>','>','<','<','<','>','>', '>','>','<','<','<','>','>', '>','>','>','>','<','>','>', '>','>','>','>','<','>','>', '<','<','<','<','<','=',' ', '>','>','>','>',' ','>','>', '<','<','<','<','<',' ','=' }; typedef int Status; template <typename T> struct SqStack { T *top; T *base; int stacksize; }; template <typename T1,typename T2> Status InitStack(T1 &S) { S.base=(T2 *)malloc(STACK_INIT_SIZE*sizeof(T2)); if(!S.base) exit (overflow); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return ok; }//初始化棧函式模板 template <typename T1,typename T2> Status Push(T1 &S,T2 e) { if(S.top-S.base>=S.stacksize) { S.base=(T2 *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(T2)); if(!S.base) exit (overflow); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return ok; } template <typename T1,typename T2> Status Pop(T1 &S,T2 &e) { if(S.top==S.base) return error; e=*--S.top; return ok; } template <typename T1,typename T2> T2 GetTop(T1 S) { if(S.top==S.base) return error; else return *(S.top-1); } Status In(char Test,char* TestOp) { bool Find=false; for (int i=0; i< OPSETSIZE; i++) { if (Test == TestOp[i]) Find= true; } return Find; } float Operate(float a,unsigned char theta, float b) { switch(theta) { case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': return a/b; default : return 0; } } int ReturnOpOrd(char op,char* TestOp) { int i; for(i=0; i< OPSETSIZE; i++) { if (op == TestOp[i]) return i; } return 0; } char precede(char Aop, char Bop) { return Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)]; } float EvaluateExpression() { SqStack<char> OPTR; SqStack<float> OPND; char TempData[20]; float Data,a,b; char theta,c,x,Dr[2]; InitStack<SqStack<char>,char> (OPTR); Push (OPTR, '#'); InitStack <SqStack<float>,float>(OPND); strcpy(TempData,"\0"); c=getchar(); while (c!= '#' || GetTop<SqStack<char>,char>(OPTR)!= '#') { if (!In(c, OPSET)) { Dr[0]=c; Dr[1]='\0'; strcat(TempData,Dr); c=getchar(); if(In(c,OPSET)) Data=(float)atof(TempData); Push(OPND, Data); strcpy(TempData,"\0"); } else { switch (precede(GetTop<SqStack<char>,char>(OPTR), c)) { case '<': Push(OPTR, c); c=getchar(); break; case '=': Pop(OPTR, x); c=getchar(); break; case '>': Pop(OPTR, theta); Pop(OPND, b); Pop(OPND, a); Push(OPND, Operate(a, theta, b)); break; } } } return GetTop<SqStack<float>,float>(OPND); } int main() { printf("請輸入表示式(end #):\n"); printf("%f\n",EvaluateExpression()); return 0; }