include <malloc.h> #include <stdio.h> #include <ctype.h>//å¤ææ¯å¦ä¸ºå符çå½æ°ç头æ件 #define maxsize 100 typedef int elemtype; typedef struct sqstack sqstack;//ç±äºsqstackä¸æ¯ä¸ä¸ªç±»å èstruct sqstackææ¯ char ch[7]=;//æ符å·è½¬æ¢æä¸ä¸ªå符æ°ç» int f1[7]=;//æ å
å
ç´ ä¼å
级 int f2[7]=;//æ å¤çå
ç´ ä¼å
级 struct sqstack { elemtype stack[maxsize]; int top; }; void Initstack(sqstack *s) { s->top=0; } void Push(sqstack *s,elemtype x) { if(s->top==maxsize-1) printf("Overflow\n"); else { s->top++; s->stack[s->top]=x; } } void Pop(sqstack *s,elemtype *x) { if(s->top==0) printf("underflow\n"); else { *x=s->stack[s->top]; s->top--; } } elemtype Gettop(sqstack s) { if(s.top==0) { printf("underflow\n"); return 0; } else return s.stack[s.top]; } elemtype f(char c) { switch(c) { case '+': return 0; case '-': return 1; case '*': return 2; case '/': return 3; case '(': return 4; case ')': return 5; default: return 6; } } char precede(char c1,char c2) { int i1=f(c1); int i2=f(c2);//æå符åææ°å if(f1[i1]>f2[i2])//éè¿åæ¥è®¾å®æ¾å°ä¼å
级 return '>'; else if(f1[i1]<f2[i2]) return '<'; else return '='; } int Operate(elemtype a,elemtype theta,elemtype b) { int sum; switch(theta) { case 0: sum=a+b; break; case 1: sum=a-b; break; case 2: sum=a*b; break; default: sum=a/b; } return sum; } EvaluateExpression() { char c; int i=0,sum=0; int k=1,j=1;//设置äºå¼å
³åé elemtype x,theta,a,b; sqstack OPTR,OPND; Initstack(&OPTR); Push(&OPTR,f('#'));//0åå
¥æ Initstack(&OPND); c=getchar(); if(c==ch[2]||c==ch[3]||c==ch[5]||c==ch[6])//å
对ï¼å-çæ
åµå¿½ç¥åå·¦æ¬å·çæ
åµ { printf("é误1 \n"); k=0; return 0; } if(c==ch[0]) c=getchar();//å¦ææ¯ï¼ï¼æå®è¦ç if(c==ch[1]) { j=0; c=getchar();//ä¹æï¼å·è¦ç } while(c!='#'||ch[Gettop(OPTR)]!='#') { if(isdigit(c)) { sum=0; while(isdigit(c)) { if(!j) { sum=sum*10-(c-'0');//å®ç°äºæ°å串åé¢æè´å·ï¼ä¹åæ¯ï¼sum=-(sum*10)-(c-'0')ç»ææ¯ï¼12ï¼13ï¼21ï¼ } else sum=sum*10+(c-'0'); c=getchar(); } Push(&OPND,sum);//å¦æè¿æ¯æ°åå
ä¸åæ ï¼ææ°å串转åæåè¿å¶æ°åååæ j=1; } else if(k) { switch(precede(ch[Gettop(OPTR)],c)) { case'<': Push(&OPTR,f(c));//æå®ä»¬æ´åå c=getchar(); if(c==ch[0]||c==ch[1]||c==ch[2]||c==ch[3]||c==ch[5]||c=='\n')//è¦é¤å»ä¸ä¸ªæ¯âï¼âçæ
åµ ä¹æ以è¿ç®ç¬¦å½å°è¿éæ¥ { printf("åºé2\n"); k=0; return 0;//å äºå¼å
³åéåè¿å0çå¼ä½¿ç¨åºæ´ä»¥æä½ } break; case'=': Pop(&OPTR,&x); c=getchar(); if(c==ch[0]||c==ch[1]||c==ch[2]||c==ch[3]||c==ch[5]||c=='\n')//æch[6]çæ
åµä¹å¿½ç¥äºä½æ¤æ¶å¹¶æ²¡æ注æå°å³æ¬å·åé¢å³è¿ç®ç¬¦çæ
åµ { printf("åºé2\n"); k=0; return 0; } break; case'>': Pop(&OPTR,&theta); Pop(&OPND,&b); Pop(&OPND,&a);//注æè¿éæ¯è°å
åºæ Push(&OPND,Operate(a,theta,b)); break; } } }//å¨è¿éå¤ææ¯å¦ä»¥è¿ç®ç¬¦ç»ææ¯ä¸å¯¹ç return(Gettop(OPND)); } main() { int result; printf("è¾å
¥ä½ çç®æ¯è¡¨è¾¾å¼ï¼\n"); result=EvaluateExpression(); printf("ç»ææ¯ :%d\n",result); return 0; } : æ¬è®¡ç®å¨å©ç¨å æ æ¥å®ç°ã 1ãå®ä¹åç¼å¼è®¡ç®å¨çå æ ç»æ å 为éè¦åå¨çåå
ä¸å¤ï¼è¿é使ç¨é¡ºåºæ ï¼å³ç¨ä¸ç»´æ°ç»æ¥æ¨¡æå æ ï¼ #define MAX 100 int stack[MAX]; int top=0; å æ¤ç¨åºä¸å®ä¹äºé¿åº¦ä¸ºMAXçä¸ç»´æ°ç»ï¼è¿éMAXç¨å®å®ä¹ä¸ºå¸¸æ°100ï¼æ们å¯ä»¥ä¿®æ¹å®å®ä¹èéæ°å®ä¹å æ ç大å°ã æ´åæ°æ®top为æ 顶æ示ï¼ç±äºç¨åºå¼å§æ¶å æ ä¸å¹¶æ ä»»ä½æ°æ®å
ç´ ï¼å æ¤top被åå§å为0ã 2ãåå¨åç¼å¼è®¡ç®å¨çè¿ç®æ° æ们å®ä¹äºå æ stack[MAX]åï¼å°±å¯ä»¥å©ç¨å
¥æ æä½åå¨å
åè¾å
¥ç两个è¿ç®æ°ã ä¸é¢çä¸ä¸æ¯å¦ä½å®ç°çï¼ int push(int i) /*åå¨è¿ç®æ°ï¼å
¥æ æä½*/ { if(top<MAX) { stack[++top]=i; /*å æ ä»æ空é´ï¼æ 顶æ示ä¸ç§»ä¸ä¸ªä½ç½®*/ return 0; } else /*å æ 已满ï¼ç»åºé误信æ¯ï¼è¿ååºéæ示*/ { printf("The stack is full"); return ERR; } } æ们å¨è°ç¨å½æ°pushæ¶ï¼å¦æå®çè¿åå¼ä¸º0ï¼è¯´æå
¥æ æä½æåï¼å¦åï¼è¥è¿åå¼ä¸ºERR(å¨ç¨åºä¸è¯´æ为-1)ï¼è¯´æå
¥æ æä½å¤±è´¥ã 3ãä»å æ ä¸ååºè¿ç®æ° å½ç¨åºä¸è¯»å®äºååè¿ç®ç¬¦åï¼æ们就å¯ä»¥ä»å æ ä¸ååºå·²ç»åå
¥ç两个è¿ç®æ°ï¼ææ表达å¼ï¼è®¡ç®åºç»æãååºè¿ç®æ°çå½æ°éç¨çæ£æ¯åºæ ç®æ³ãå¨æ¬ä¾ä¸ï¼å®ç°è¯¥ç®æ³çå½æ° 为pop(): int pop(); /*ååºè¿ç®æ°ï¼åºæ æä½*/ { int var; /*å®ä¹å¾
è¿åçæ 顶å
ç´ */ if(top!=NULL) /*å æ ä¸ä»ææ°æ®å
ç´ */ { var=stack[top--]; /*å æ æ示ä¸ç§»ä¸ä¸ªä½ç½®*/ return var; } else /*å æ 为空ï¼ç»åºé误信æ¯ï¼å¹¶è¿ååºéè¿åå¼*/ printf("The stack is cmpty!\n"); return ERR; } åæ ·ï¼å¦æå æ ä¸ä¸ºç©ºï¼pop()å½æ°è¿åå æ 顶端çæ°æ®å
ç´ ï¼å¦åï¼ç»åºæ 空æ示ï¼å¹¶è¿åé误è¿åå¼ERRã 4ã设计å®æ´çåç¼å¼è®¡ç®å¨ æäºå æ åå¨è¿ç®æ°ï¼åç¼å¼è®¡ç®å¨ç设计就å¾ç®åäºãç¨åºé¦å
æ示ç¨æ·è¾å
¥ç¬¬ä¸ä¸ªè¿ç®æ°ï¼è°ç¨push()å½æ°åå
¥å æ ä¸ï¼èåæ示ç¨æ·è¾å
¥ç¬¬äºä¸ªè¿ç®æ°ï¼åæ ·è°ç¨push()å½æ°åå
¥å æ ä¸ãæ¥ä¸æ¥ï¼ç¨åºæ示ç¨æ·è¾å
¥+,-,*,/åç§è¿ç®ç¬¦çä¸ç§ï¼ç¨åºéè¿switch_caseç»æå¤æè¾å
¥è¿ç®ç¬¦çç§ç±»ï¼è½¬èæ§è¡ä¸åçå¤ç代ç ã以é¤æ³ä¸ºä¾ï¼è¯´æç¨åºçæ§è¡æµç¨ï¼ case '/': b=pop(); a=pop(); c=a/b; printf("\n\nThe result is %d\n",c); printf("\n"); break; ç¨åºå¤æç¨æ·è¾å
¥çæ¯é¤å·åï¼å°±æ§è¡ä¸è¿°ä»£ç ãé¦å
æ¥è¿ä¸¤æ¬¡è°ç¨pop()å½æ°ä»å æ ä¸è¯»åºå
åè¾å
¥çè¿ç®æ°ï¼åå
¥æ´åæ°aåbä¸ï¼ç¶åæ§è¡é¤æ³è¿ç®ï¼ç»æåå
¥åå
cä¸ãè¿æ¶éè¦èè究ç«è°æ¯è¢«é¤æ°ï¼è°æ¯é¤æ°ãç±äºå¼å§æ们å
å°è¢«é¤æ°å
¥æ ï¼æ ¹æ®å æ âå
è¿ååºâçååï¼è¢«é¤æ°åºè¯¥æ¯ç¬¬äºæ¬¡è°ç¨pop()å½æ°å¾å°çè¿åå¼ãèé¤æ°åæ¯ç¬¬ä¸æ¬¡è°ç¨pop()å½æ°å¾å°çè¿åå¼ã æåç¨åºæå°åºè¿ç®ç»æï¼å¹¶ç¤ºæ示ç¨æ·æ¯å¦ç»§ç»è¿è¡ç¨åºï¼ printf("\t Continue?(y/n):"); l=getche(); if(l=='n') exit(0); å¦æç¨æ·åçæ¯"n"ï¼é£ä¹ç»æç¨åºï¼å¦å继ç»å¾ªç¯ã å®æ´çç¨åºä»£ç å¦ä¸ï¼ #include<stdio.h> #include<conio.h> #include<stdlib.h> #define ERR -1 #define MAX 100 /*å®ä¹å æ ç大å°*/ int stack[MAX]; /*ç¨ä¸ç»´æ°ç»å®ä¹å æ */ int top=0; /*å®ä¹å æ æ示*/ int push(int i) /*åå¨è¿ç®æ°,å
¥æ æä½*/ { if(top<MAX) { stack[++top]=i; /*å æ ä»æ空é´ï¼æ 顶æ示ä¸ç§»ä¸ä¸ªä½ç½®*/ return 0; } else { printf("The stack is full"); return ERR; } } int pop() /*ååºè¿ç®æ°ï¼åºæ æä½*/ { int var; /*å®ä¹å¾
è¿åçæ 顶å
ç´ */ if(top!=NULL) /*å æ ä¸ä»æå
ç´ */ { var=stack[top--]; /*å æ æ示ä¸ç§»ä¸ä¸ªä½ç½®*/ return var; /*è¿åæ 顶å
ç´ */ } else printf("The stack is empty!\n"); return ERR; } void main() { int m,n; char l; int a,b,c; int k; do{ printf("\tAriothmatic Operate simulator\n"); /*ç»åºæ示信æ¯*/ printf("\n\tPlease input first number:"); /*è¾å
¥ç¬¬ä¸ä¸ªè¿ç®æ°*/ scanf("%d",&m); push(m); /*第ä¸ä¸ªè¿ç®æ°å
¥æ */ printf("\n\tPlease input second number:"); /*è¾å
¥ç¬¬äºä¸ªè¿ç®æ°*/ scanf("%d",&n); push(n); /*第äºä¸ªè¿ç®æ°å
¥æ */ printf("\n\tChoose operator(+/-/*//):"); l=getche(); /*è¾å
¥è¿ç®ç¬¦*/ switch(l) /*å¤æè¿ç®ç¬¦ï¼è½¬èæ§è¡ç¸åºä»£ç */ { case '+': b=pop(); a=pop(); c=a+b; printf("\n\n\tThe result is %d\n",c); printf("\n"); break; case '-': b=pop(); a=pop(); c=a-b; printf("\n\n\tThe result is %d\n",c); printf("\n"); break; case '*': b=pop(); a=pop(); c=a*b; printf("\n\n\tThe result is %d\n",c); printf("\n"); break; case '/': b=pop(); a=pop(); c=a/b; printf("\n\n\tThe result is %d\n",c); printf("\n"); break; } printf("\tContinue?(y/n):"); /*æ示ç¨æ·æ¯å¦ç»æç¨åº*/ l=getche(); if(l=='n') exit(0); }while(1); } : #include <stdio.h> #include <conio.h> #include <malloc.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; #define STACK_INIT_SIZE 100 //åå§åé
é #define STACKINCREMENT 10 //åå¨ç©ºé´çåé
å¢é typedef char ElemType; typedef ElemType OperandType; //æä½æ° typedef char OperatorType; typedef struct { ElemType *base; ElemType *top; int stacksize; }SqStack; Status InitStack(SqStack &S) { //æé ä¸ä¸ªç©ºæ S S.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if(!S.base) exit (OVERFLOW); S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } Status GetTop(SqStack S){ ElemType e; if (S.top == S.base) return ERROR; e = *(S.top-1); return e; } Status Push (SqStack &S,ElemType e) { //æå
¥å
ç´ e为æ°çæ 顶å
ç´ if (S.top - S.base >= S.stacksize){ S.base = (ElemType *) realloc ( S.base, (S.stacksize + STACKINCREMENT) * sizeof(ElemType)); if(!S.base) exit (OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK; } Status Pop (SqStack &S,ElemType &e){ //è¥æ ä¸ç©ºï¼åå é¤Sçæ 顶å
ç´ ï¼ç¨eè¿åå
¶å¼ï¼å¹¶è¿åOKï¼å¦åè¿åERROR if(S.top == S.base) return ERROR; e = * --S.top; return OK; } char In(char c,char OP[]) { if(c>=35 && c<=47) return 1; else return 0; } char OP[8]=; int m[7][7]={1,1,2,2,2,1,1, 1,1,2,2,2,1,1, 1,1,1,1,2,1,1, 1,1,1,1,2,1,1, 2,2,2,2,2,0,-1, 1,1,1,1,-1,1,1, 2,2,2,2,2,-1,0};//1 > 2 < 0 = -1 ä¸åå¨ char Precede(char i,char j) { int a,b; char *p; for(p=OP,a=0;*p!='\0';p++,a++) if(*p==i) break; for(p=OP,b=0;*p!='\0';p++,b++) if(*p==j) break; if(m[a][b]==1) return '>'; else if(m[a][b]==2) return '<'; else if(m[a][b]==0) return '='; else return 'O'; } char Operate(char a,char theta,char b) { if(a>47) a=atoi(&a); if(b>47) b=atoi(&b); switch(theta) { case '+': return a+b; break; case '-': return a-b; break; case '*': return a*b; break; case '/': return a/b; break; } } OperandType EvaluateExpression() { SqStack OPTR,OPND; OperandType a,b,c; OperatorType theta; InitStack(OPTR); Push(OPTR,'#'); InitStack(OPND); c=getchar(); while (c!='#' || GetTop(OPTR)!='#') { if (!In(c,OP)) else switch(Precede(GetTop(OPTR),c)) { case '<' : Push(OPTR,c); c = getchar(); break; case '=' : Pop(OPTR,c); c = getchar(); break; case '>' : Pop(OPTR,theta); Pop(OPND,b); Pop(OPND,a); Push(OPND,Operate(a,theta,b)); break; } } return GetTop(OPND); } void main() { printf("(以#为ç»æ符)\n"); printf("请è¾å
¥ï¼\n"); int a; a=(int)EvaluateExpression(); printf("%d",a); getch(); } : lsé½æ£ç¡® : C++ In Actionè¿æ¬ä¹¦éé¢æ表达å¼æ±å¼ç详ç»é¡¹ç®åæ. : æ°æ®ç»æç书éé¢é½æç,ä»ç»çä¸ä¸ : studyall123çåªè½å¯¹0å°9çæ°åè¿ç®æææ,对äº10以ä¸çæ°åå°±ä¸è¡!ä¸ç¥éæ没ææ´å¥½çæ¹æ³! : ç°å¨ç人ï¼è¿googleä¸ä¸é½æå : å®é
ä¸æ¯æç
§éæ³¢å
°å¼ç顺åºè®©è¾å
¥ç表达å¼å
¥æ ï¼åæ ¹æ®è¿ç®ç¬¦ä¼å
级æ¥è®¡ç®ã : lenrning!
温馨提示:内容为网友见解,仅供参考