清華大佬耗費三個月吐血整理的幾百G的資源,免費分享!....>>>
#include <stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define OVERFLOW 0 typedef int SElemType; char m[20]; typedef enum aa{FALSE,TRUE} Status; typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack; int N; unsigned n; Status InitStack(SqStack &S) { S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S.base) return FALSE; S.top=S.base; S.stacksize=STACK_INIT_SIZE; return TRUE; } Status Pop(SqStack &S, SElemType &e) { if(S.top==S.base) return FALSE; e = *--S.top; return TRUE; } Status Push(SqStack &S, SElemType e) { if(S.top-S.base >= S.stacksize) { S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT)*sizeof(SElemType)); if(!S.base) return FALSE; S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return TRUE; } Status GetTop(SqStack S, SElemType &e) { if(S.top > S.base) { e = *(S.top-1); return TRUE; } else return FALSE; } Status StackEmpty(SqStack S) { if(S.top == S.base) return TRUE; else return FALSE; } Status In(SElemType c) //判斷c是否為運算符 { switch(c) { case'+': case'-': case'*': case'/': case'(': case')': case'#': return TRUE; default: return FALSE; } } char Precede(SElemType t1, SElemType t2) //判斷兩符號的優(yōu)先關(guān)系 { SElemType f; switch(t2) { case'+': case'-': if(t1=='('||t1=='#') f='<'; else f='>'; break; case'*': case'/': if(t1=='*'||t1=='/'||t1==')') f='>'; else f='<'; break; case'(': if(t1==')') { printf("括號不匹配\n"); exit(OVERFLOW); } else f='<'; break; case')': switch(t1) { case'(': f='='; break; case'#': printf("缺乏左括號\n"); exit(OVERFLOW); default: f='>'; } break; case'#': switch(t1) { case'#': f='='; break; case'(': printf("缺乏右括號\n"); exit(OVERFLOW); default: f='>'; } } return f; } SElemType Operate(SElemType a,SElemType theta,SElemType b) //做四則運算a theta b,返回運算結(jié)果 { SElemType c; switch(theta) { case'+': return a+b; case'-': return a-b; case'*': return a*b; } return a/b; } SElemType EvaluateExpression() { SqStack OPTR, OPND; SElemType a, b, c, x; int i=0; InitStack(OPTR); InitStack(OPND); Push(OPTR, '#'); c=m[i++]; GetTop(OPTR, x); while(c != '#' || x!='#') { if(In(c)) switch(Precede(x, c)) { case '<': Push(OPTR, c); c=m[i++]; break; case '=': Pop(OPTR, x); c=m[i++]; break; case '>': Pop(OPTR, x); Pop(OPND, b); Pop(OPND, a); Push(OPND, Operate(a, x, b)); break; } else if(c>='0' && c<='9') { Push(OPND, c-48); c=m[i++]; } else { printf("出現(xiàn)非法字符\n"); exit(OVERFLOW); } GetTop(OPTR, x); } Pop(OPND, x); if(!StackEmpty(OPND)) { printf("表達(dá)式不正確\n"); exit(OVERFLOW); } return x; } void main() { int x; while(1) { printf("1.輸入表達(dá)式\n2.輸出表達(dá)式\n3.判斷表達(dá)式的括號是否匹配并計算表達(dá)式的值\n4.退出\n"); printf("請選擇1-4\n"); scanf("%d",&x); if(x==1) { printf("請輸入算式表達(dá)式(輸入的值要為0-9),中間運算值和輸出結(jié)果為-128~127\n"); scanf("%s", m); } else if(x==2) { printf("輸出表達(dá)式\n"); printf("%s\n", m); } else if(x==3) { printf("判斷表達(dá)式的括號是否匹配并計算表達(dá)式的值\n"); printf("%d\n",EvaluateExpression()); } else if(x==4) { break; } else { printf("輸入的為非法字符"); break; } } }