棧在表達式計算過程中的應用 :建立操作數棧和運算符棧。運算符有優先級。
規則:
自左至右掃描表達式,凡是遇到操作數一律進操作數棧。
當遇到運算符時,如果它的優先級比運算符棧棧頂元素的優先級高就進棧。反之,取出棧頂運算符和操作數棧棧頂的連續兩個操作數進行運算,并將結果存入操作數棧,然后繼續比較該運算符與棧頂運算符的優先級。
左括號一律進運算符棧,右括號一律不進運算符棧,取出運算符棧頂運算符和操作數棧頂的兩個操作數進行運算,并將結果壓入操作數棧,直到取出左括號為止。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100enum link{PUSH, PUSH_NO};typedef struct // 運算數
{int num[MAX];int top;
}OP_num;typedef struct // 運算符
{char str[MAX];int top;
}OP_ch;// 運算數置空棧
void SETNULL_num (OP_num* s)
{s->top = -1;
}// 運算符置空棧
void SETNULL_ch (OP_ch* s)
{s->top = -1;
}// 判斷是否是數字,是返回1 不是返回0
int is_num (char ch)
{if (ch >= '0' && ch <= '9'){return 1;}else{return 0;}
} // 數字入棧
int PUSH_num (OP_num *s, int data)
{if ((MAX - 1) == s->top){return 0;}else{ s->num[++s->top] = data;}
}// 運算符入棧
int PUSH_ch (OP_ch* s, char ch)
{if ((MAX - 1) == s->top){return 0;}else{s->str[++s->top] = ch;}
}// 判斷是否將運算符入棧
int jud (OP_ch* s, char ch)
{if (-1 == s->top) // 判斷是否是空棧{return PUSH;}else{switch (s->str[s->top]) // 根據棧頂運算判斷是否進棧{case '+': // 當棧頂是'+-'時,只有‘+-)’不進棧case '-':{if (ch == '+' || ch == '-' || ch == ')'){return PUSH_NO;}else{return PUSH;}break;}case '*':case '/':{if ('(' == ch){return PUSH;}else{return PUSH_NO;}break;}case '(':{return PUSH;break;}}}
}// 數字出棧
int Pop_num (OP_num* s)
{return (s->num[s->top--]);
}// 運算符出棧
void Pop_ch (OP_ch* s)
{s->top--;
}// 進行運算
void operate (OP_ch* s_ch, OP_num* s_sum)
{int a = Pop_num(s_sum); // 取第一個數int b = Pop_num(s_sum); // 取第二個數int result;// 根據當前運算符棧頂的符號來判斷進行何種運算switch (s_ch->str[s_ch->top]){case '+':result = a + b;break;case '-':result = b - a;break;case '*':result = a * b;break;case '/':result = b / a;break;} PUSH_num (s_sum, result); // 將運算結果入棧
}int main()
{OP_num sdata;OP_ch soper;SETNULL_num (&sdata);SETNULL_ch (&soper);int i = 0, len_str, t;char str[MAX];char str_num[MAX]; // 存放要轉化的數字gets (str); // 輸入表達式len_str = strlen (str); // 獲取表達式長度while (str[i] != '\0') // 遍歷表達式{if (is_num(str[i])) // 判斷是否是數字{t = 0;while (is_num(str[i])){str_num[t++] = str[i++];//將表達式中的數字進行保存,用于轉化為對應的整形數}str_num[t] = '\0';PUSH_num (&sdata, atoi(str_num));// 遇到算數符號的時候讓符號前面的數進棧}else{if (PUSH == jud(&soper, str[i])){PUSH_ch (&soper, str[i]);}else{if (str[i] != ')') // ')'不讓其入棧所以單獨列出來討論{operate (&soper, &sdata); // 進行一次運算并一處棧頂運算符Pop_ch(&soper); // 符號出棧PUSH_ch (&soper, str[i]); // 進行壓棧// 相當于用當前的運算符替換了剛才棧頂的運算符號}else // 遇到')'// 不斷取字符運算 知道遇到 ')'{do{operate (&soper, &sdata);Pop_ch (&soper);}while (soper.str[soper.top] != '(');Pop_ch (&soper);// 將‘(’彈出棧空間}}i++;}}while (soper.top != -1){operate (&soper, &sdata);Pop_ch (&soper);}printf ("%d\n", sdata.num[sdata.top]);return 0;
}