標籤:des style 使用 os 2014 io
棧的應用有很多,四則運算是一個比較常見的應用。對於四則運算,括弧內的要先運算,而且還要先乘除後加減,又要涉及到負數和浮點數,看上去簡簡單單的式子,其實暗藏殺機。
常用的方法是利用尾碼運算式(逆波蘭)進行計算。主要分為兩步:
(1)將中綴運算式轉化為尾碼運算式(棧用來進出運算的符號):
從左至右遍曆中綴運算式的每一個數字和符號,若是數字就輸出,既成為尾碼運算式的一部分,若是符號,則判斷其與棧頂符號的優先順序,是右括弧或優先順序低於棧頂符號(乘除優先加減),則棧頂元素依次出棧並輸出,並將當前符號進棧,一直到最終輸出尾碼運算式為止。
(2)將尾碼運算式進行運算得出結果(棧用來進出運算的數字)
從左至右遍曆運算式的每個數字和符號,遇到是數字就進棧,遇到是符號,就將處於棧頂兩個數字出棧,進行運算,運算結果進棧,一直到最終獲得結果。
使用C語言實現的演算法:
/*============================================================== * FileName: expression_calc.c * Desc: 使用棧完成四則運算式的計算,支援大中小括弧以及負數,不支援浮點數 * Author: Hu Chunxu * Email: [email protected] * Version: 0.0.1 * LastChange: 2014-07-15 10:07:48 * History: *=============================================================*/#include <stdio.h>#include <stdlib.h>#include <string.h>#define OK 1 #define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 50typedef int Status;typedef int SElemType;//定義一個順序儲存棧typedef struct{ SElemType data[MAXSIZE]; int top;}SqStack;/*******************棧的基本操作********************************/Status init_stack(SqStack *s){ s->top = -1; return OK;}Status clear_stack(SqStack *s){ s->top = -1; return OK;}Status stack_empty(SqStack s){ if(s.top == -1) return TRUE; else return FALSE;}int stack_length(SqStack *s){ return s->top+1;}Status push(SqStack *s, SElemType e){ if(s->top == MAXSIZE-1) return ERROR; s->top++; s->data[s->top] = e; return OK;}Status pop(SqStack *s, SElemType *e){ if(s->top == -1) return ERROR; *e = s->data[s->top]; s->top--; return OK;}/*******************中序運算式轉換為後續運算式********************************/Status infix_to_postfix(char *infix, char *postfix){ SqStack s; int e = 0; int i = 0, j = 0; int flag = 0; if(init_stack(&s) != OK) return ERROR; while(infix[i]!=‘\0‘) { while(infix[i]>=‘0‘ && infix[i]<=‘9‘) //如果是數字則輸出 { if(flag) { flag = 0; postfix[j++] = ‘-‘; } postfix[j++] = infix[i]; i++; if(infix[i]<‘0‘ || infix[i]>‘9‘) postfix[j++] = ‘ ‘; } if(infix[i]==‘)‘ || infix[i]==‘]‘ || infix[i]==‘}‘) //如果符號,則進行棧操作 { pop(&s, &e); while(e!=‘(‘ && e!=‘[‘ && e!=‘{‘) { postfix[j++] = e; postfix[j++] = ‘ ‘; pop(&s, &e); } } else if(infix[i]==‘+‘ || infix[i]==‘-‘) { if(infix[i] == ‘-‘ && (i==0 || (i!=0 && (infix[i-1]<‘0‘ || infix[i-1]>‘9‘)))) //當‘-‘號處於第一位,或前面是符號時,為負號標誌 flag = 1; else if(stack_empty(s)) push(&s, infix[i]); else { do { pop(&s, &e); if(e==‘(‘ || e==‘[‘ || e== ‘{‘) push(&s, e); else { postfix[j++] = e; postfix[j++] = ‘ ‘; } }while(!stack_empty(s) && e!=‘(‘ && e!=‘[‘ && e!=‘{‘); push(&s, infix[i]); } } else if(infix[i]==‘*‘ || infix[i]==‘/‘ || infix[i]==‘(‘ || infix[i]==‘[‘ || infix[i] == ‘{‘) push(&s, infix[i]); else if(infix[i] == ‘\0‘) break; else return ERROR; i++; } while(!stack_empty(s)) { pop(&s,&e); postfix[j++] = e; postfix[j++] = ‘ ‘; } clear_stack(&s); return OK;}/*******************根據後續運算式計算結果********************************/Status calculate(char *postfix, int *result){ SqStack s; char *op; //存放尾碼運算式中的每個因數或運算子 char *buf=postfix; //聲明bufhe saveptr兩個變數,是strtok_r函數的需要。 char *saveptr=NULL; int d,e,f; if(init_stack(&s) != OK) return ERROR; while((op = strtok(buf, " ")) != NULL) { buf = NULL; switch(op[0]) { case ‘+‘: pop(&s, &d); pop(&s, &e); f = d+e; push(&s, f); break; case ‘-‘: if(op[1]>=‘0‘ && op[1]<=‘9‘) //是負號而不是減號 { d = atoi(op); push(&s, d); break; } pop(&s, &d); pop(&s, &e); f = e-d; push(&s, f); break; case ‘*‘: pop(&s, &d); pop(&s, &e); f = e*d; push(&s, f); break; case ‘/‘: pop(&s, &d); pop(&s, &e); f = e/d; push(&s, f); break; default: d = atoi(op); push(&s, d); break; } } pop(&s, result); clear_stack(&s);}void main(){ char infix[MAXSIZE] = {0}; char postfix[MAXSIZE] = {0}; int result = 0; scanf("%s", infix); infix_to_postfix(infix, postfix); calculate(postfix, &result); printf("%d\n",result);}