前言周五加班的時候,在九度oj上練習了一道簡單運算式求值的題目,用到了“算符優先法”,這裡簡單的記錄一下題目
題目描述: 讀入一個只包含 +, -, *, / 的非負整數計算運算式,計算該運算式的值。輸入: 測試輸入包含若干測試案例,每個測試案例佔一行,每行不超過200個字元,整數和運算子之間用一個空格分隔。沒有非法運算式。當一行中只有0時輸入結束,相應的結果不要輸出。輸出: 對每個測試案例輸出1行,即該運算式的值,精確到小數點後2位。範例輸入:1 + 24 + 2 * 5 - 7 / 110範例輸出:3.0013.36
思路對錶達式 4 + 2 × 5 - 7 / 11進行計算,需要瞭解演算法四則運算的規則,即:
實現演算法
- 為了實現算符優先法,可以使用兩個工作棧。一個稱作OPTR,用以寄存運算子;另一個稱為OPRD,用以寄存運算元或者運算結果。
- 初始化兩個棧,並且置棧空。(假設運算式是沒有語法錯誤的)
- 依次讀入運算式中每個字元,若是運算元則進OPTD棧,若是運算子,則和OPTR棧的棧頂運算子比較優先權後作相應操作,直至整個運算式求值完畢(即運算式指標到了最後一個字元並且OPTR棧為空白)
代碼實現(c語言順序棧)
#include <stdio.h>#include <stdlib.h>#include <string.h>#define stacksize 201struct optr{char operator[stacksize];int top;};struct optd{double data[stacksize];int top;};void pushStackOptr(struct optr *, char );void pushStackOptd(struct optd *, double );char getStackOptrTop(struct optr *);int compareOperator(char, char);char popStackOptr(struct optr *);double popStackOptd(struct optd *);double calculateData(double, double, char);int main(){struct optr *optr;struct optd *optd;int i, len;double sum, x, data1, data2;char string[201], opt, opt1, opt2;while(gets(string) && string[0] != '0'){//初始化棧、運算式長度len = strlen(string);optr = (struct optr *)malloc(sizeof(struct optr));optd = (struct optd *)malloc(sizeof(struct optd));if(optr == NULL || optd == NULL){exit(EXIT_FAILURE);}optr->top = optd->top = 0;//計算運算式的值,使用“算符優先法”for(i = 0; i < len; i ++){if(string[i] != ' '){if(string[i] >= '0' && string[i] <= '9'){x = string[i] - '0';while(string[i + 1] >= '0' && string[i + 1] <= '9'){x = 10 * x + (string[i + 1] - '0');i ++;}//運算元進棧pushStackOptd(optd, x);}else{//擷取棧頂運算子opt = getStackOptrTop(optr);switch(compareOperator(opt, string[i])){case 0://棧頂優先順序低pushStackOptr(optr, string[i]);break;case 1://棧頂優先順序高opt = popStackOptr(optr);data1 = popStackOptd(optd);data2 = popStackOptd(optd);sum = calculateData(data1, data2, opt);pushStackOptd(optd, sum);i --;break;}}}}//判斷算符棧是否為空白while(optr -> top != 0){opt = popStackOptr(optr);data1 = popStackOptd(optd);data2 = popStackOptd(optd);sum = calculateData(data1, data2, opt);pushStackOptd(optd, sum);}//輸出計算結果printf("%.2lf\n",sum);}return 0;}void pushStackOptr(struct optr *s, char opt){//判斷棧滿if(s->top - 1 == stacksize){exit(EXIT_FAILURE);}s->operator[s->top ++] = opt;}void pushStackOptd(struct optd *s, double x){//判斷棧滿if(s->top - 1 == stacksize){exit(EXIT_FAILURE);}s->data[s->top ++] = x;}char getStackOptrTop(struct optr *s){//判斷棧空if(s->top == 0){return '#';}return s->operator[s->top - 1];}int compareOperator(char opt1, char opt2){//預設opt1優先順序高,opt2優先順序高返回0int flag = 1;if(((opt1 == '+' || opt1 == '-') && (opt2 == '*' || opt2 == '/')) || opt1 == '#'){flag = 0;}return flag;}char popStackOptr(struct optr *s){//判斷棧空if(s->top == 0){exit(EXIT_FAILURE);}return s->operator[-- s->top];}double popStackOptd(struct optd *s){//判斷棧空if(s->top == 0){exit(EXIT_FAILURE);}return s->data[-- s->top];}double calculateData(double data1, double data2, char operator){switch(operator){case '+':return data2 + data1;case '-':return data2 - data1;case '*':return data2 * data1;case '/':if(data1 == 0){exit(EXIT_FAILURE);}return data2 / data1;}}