C語言利用棧實現(xiàn)對后綴表達式的求解
本文實例為大家分享了C語言實現(xiàn)對后綴表達式(逆波蘭表達式)的求解代碼,供大家參考,具體內容如下
逆波蘭表達式:
逆波蘭表達式又叫后綴表達式。它是由相應的語法樹的后序遍歷的結果得到的。
例:5 - 8*(6 + 7) + 9 / 4:
其中綴表達式為:5 - 8 * 6 + 7 + 9 / 4
其語法樹如下:
因此根據(jù)語法樹可以得出他后序遍歷(后綴表達式)為:
5 8 6 7 + * - 9 4 / +
這樣就實現(xiàn)了中綴表達式到后綴表達式的轉換。
同樣的也可以得出他的前序遍歷(前綴表達式也稱波蘭表達式):
+ - 5 * 8 + 6 7 / 9 4
逆波蘭表達式計算實現(xiàn)原理:
1.首先當遇到運算操作數(shù)時將其進行push操作;
2.當遇到操作符是將此時的棧pop兩次,先取出的棧頂為右操作數(shù);
3.執(zhí)行此方法到整個數(shù)組遍歷完。
實現(xiàn)算法如下:
void CalFunction(SqStack *S,char str[]) {/*實現(xiàn)浮點型數(shù)據(jù)后綴表達式的加減乘除*/ Elemtype number,e,d; char arr[MAXBUFFER]; int i=0,j=0; InitStack(S); while(str[i]!='\0') { while(isdigit(str[i])||str[i]=='.') //過濾數(shù)字 { arr[j++]=str[i++]; arr[j]='\0'; if( j >= MAXBUFFER ) { printf("輸入單個數(shù)據(jù)過大!\n"); return ; } if(str[i]==' ') { number=atof(arr); //利用atof函數(shù)將數(shù)字字符串轉化為double型數(shù)據(jù) PushStack(S,number); //將轉換的數(shù)進行壓棧 j=0; //這里不要忘記將j重新初始化進行下個數(shù)據(jù)的轉化 break; } } /*如果遇到操作運算符則,彈出兩個數(shù)據(jù)進行運算,然后將得出的結果重新入棧*/ switch(str[i]) { case '+': PopStack(S,&e); PopStack(S,&d); PushStack(S,d+e); break; case '-': PopStack(S,&e); PopStack(S,&d); PushStack(S,d-e); break; case '*': PopStack(S,&e); PopStack(S,&d); PushStack(S,d*e); break; case '/': PopStack(S,&e); PopStack(S,&d); if(e == 0) { printf("輸入出錯,分母為零!\n"); return ; } PushStack(S,d/e); break; } i++; //繼續(xù)遍歷直到遍歷字符串結束 } PopStack(S,&e); printf("計算結果為:%lf",e); }
完整代碼如下:
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include<ctype.h> #define INITSIZE 20 #define INCREMENT 10 #define MAXBUFFER 10 #define LEN sizeof(Elemtype) /*棧的動態(tài)分配順序存儲結構*/ typedef double Elemtype; typedef struct{ Elemtype *base; Elemtype *top; int StackSize; }SqStack; void InitStack(SqStack *S) { S->base=(Elemtype*)malloc(LEN*INITSIZE); assert(S->base != NULL); S->top=S->base; S->StackSize=INITSIZE; } void PushStack(SqStack *S,Elemtype e) { if(S->top - S->base >= S->StackSize) { S->base=(Elemtype*)realloc(S->base,(S->StackSize+INCREMENT)*LEN); assert(S->base !=NULL); S->top=S->base+S->StackSize; S->StackSize+=INCREMENT; } *S->top =e; S->top++; } void PopStack(SqStack *S,Elemtype *e) { *e=*--S->top; } void CalFunction(SqStack *S,char str[]) { Elemtype number,e,d; char arr[MAXBUFFER]; int i=0,j=0; InitStack(S); while(str[i]!='\0') { while(isdigit(str[i])||str[i]=='.') //過濾數(shù)字 { arr[j++]=str[i++]; arr[j]='\0'; if( j >= MAXBUFFER ) { printf("輸入單個數(shù)據(jù)過大!\n"); return ; } if(str[i]==' ') { number=atof(arr); //利用atof函數(shù)將數(shù)字字符轉化為double型數(shù)據(jù) PushStack(S,number); //將轉換的數(shù)進行壓棧 j=0; break; } } switch(str[i]) { case '+': PopStack(S,&e); PopStack(S,&d); PushStack(S,d+e); break; case '-': PopStack(S,&e); PopStack(S,&d); PushStack(S,d-e); break; case '*': PopStack(S,&e); PopStack(S,&d); PushStack(S,d*e); break; case '/': PopStack(S,&e); PopStack(S,&d); if(e == 0) { printf("輸入出錯,分母為零!\n"); return ; } PushStack(S,d/e); break; } i++; } PopStack(S,&e); printf("計算結果為:%lf",e); } int main() { char str[100]; SqStack S; printf("請按逆波蘭表達式輸入數(shù)據(jù),每個數(shù)據(jù)之間用空格隔開:"); gets(str); CalFunction(&S,str); return 0; } // 檢測用例 5 - (6 + 7) * 8 + 9 / 4 // 輸入:5 8 6 7 + * - 9 4 / + # // 輸出: - 96.750000
運行效果截圖如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++產(chǎn)生隨機數(shù)的實現(xiàn)代碼
本篇文章是對C++中產(chǎn)生隨機數(shù)的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下2013-05-05