#include <stdio.h> #include <stdlib.h> #define MAXSIZE 25 typedef enum{OK=1,ERROR=0}Status; typedef double ElmType; typedef struct{ ElmType data[MAXSIZE]; int top; }SqStack; Status Push(SqStack* s, ElmType e) { if (s->top == MAXSIZE - 1) { return ERROR;//满 } s->top++; s->data[s->top] = e; return OK; } ElmType Pop(SqStack* s, ElmType e) { if (s->top == -1) { return ERROR;//栈空 } e = s->data[s->top]; s->top--; return e; } ElmType com(char* zfc) { ElmType a, b, c, d, e=0; SqStack* s=(SqStack*)malloc(sizeof(SqStack)); s->top = -1; while(*zfc!='\0'){ switch (*zfc){ case'+':{ a= Pop(s, a); b= Pop(s, b); c = b + a; Push(s, c); break; } case'-':{ a= Pop(s, a); b= Pop(s, b); c = b - a; Push(s, c); break; } case'*':{ a= Pop(s, a); b= Pop(s, b); c = b * a; Push(s, c); break; } case'/':{ a= Pop(s, a); b= Pop(s, b); if (a!= 0){ c = b/a; Push(s, c); break; } else{ printf("除零错误\n"); return ERROR; } } default:{ while(*zfc>= '0' && '9'>=*zfc) { d = *zfc-'0'; Push(s, d); break; } //while的 } //default的 } // switch 的 zfc++; } //while return Pop(s,e); } int main() { ElmType a; char zfc[]=""; gets(zfc); a=com(zfc); printf("表达式的值:%f\n", a); return 0; }