生活随笔
收集整理的這篇文章主要介紹了
中缀转后缀表达式并求值
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通過棧將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式并根據(jù)后綴表達(dá)式求解,包含的Header.h為之前發(fā)過的棧操作相關(guān)函數(shù)實(shí)例程序,改成頭文件就行。練習(xí)+記錄,高手無視。
OutPut:
The init formula:3+4*5+(6*7+8)*9
The stack is empty.
Convert result:345*+67*8+9*+
Calculate result:473
? ? #include?<stdio.h> ? #include?<Windows.h> ? #include?"Header.h" ? ? #define?MAXLENGTH?50 ? ? VOID?ConvertAndCalc(LPSTR?lpContainer,Stack?S); ? int?Caculate(LPSTR?lpContainer,Stack?S); ? int?getOptPriv(CHAR?cOpt); ? ? int?main(int?argc,char?**argv) ? { ? ????LPSTR?lpContainer?=?"3+4*5+(6*7+8)*9"; ? ????Stack?myStack?=?CreateStack(); ? ????printf("\nThe?init?formula:%s",lpContainer); ? ????ConvertAndCalc(lpContainer,myStack); ? ????system("pause"); ? ????return?0; ? } ?? int?getOptPriv(CHAR?cOpt) ? { ? ????switch(cOpt) ? ????{ ? ????case?'(': ? ????????return?7; ? ????case?')': ? ????????return?7; ? ????case?'*': ? ????????return?5; ? ????case?'/': ? ????????return?5; ? ????case?'+': ? ????????return?3; ? ????case?'-': ? ????????return?3; ? ????default: ? ????????return?0; ? ????} ?} ?? VOID?ConvertAndCalc(LPSTR?lpContainer,Stack?S) ? { ? ????LPSTR?lpResult?=?(LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH); ? ????LPSTR?lpTemp?=?(LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH); ? ????while(*lpContainer?!=?'\0') ? ????{ ? ????????if(isalnum(*lpContainer)) ? ????????{ ? ????????????sprintf(lpTemp,"%c",*lpContainer); ? ????????????strcat(lpResult,lpTemp); ?????????????ZeroMemory(lpTemp,MAXLENGTH); ? ????????}else? ????????{ ? ????????????if(isEmpty(S)) ? ????????????{ ?????????????????Push(*lpContainer,S); ? ????????????}else?if?(*lpContainer?==?')') ? ????????????{ ? ????????????????while(!isEmpty(S)?&&?getTop(S)?!=?'(') ? ????????????????{ ? ????????????????????sprintf(lpTemp,"%c",Pop(S)); ? ????????????????????strcat(lpResult,lpTemp); ?????????????????????ZeroMemory(lpTemp,MAXLENGTH); ?????????????????} ?????????????????Pop(S); ? ????????????}else? ????????????{ ? ????????????????if(getOptPriv(getTop(S))?<?getOptPriv(*lpContainer)) ? ????????????????{ ?????????????????????Push(*lpContainer,S); ? ????????????????}else? ????????????????{ ? ????????????????????while((!isEmpty(S))?&&?(getOptPriv(getTop(S))?>=?getOptPriv(*lpContainer))?&&?getTop(S)?!=?'(') ? ????????????????????{ ? ????????????????????????sprintf(lpTemp,"%c",Pop(S)); ? ????????????????????????strcat(lpResult,lpTemp); ?????????????????????????ZeroMemory(lpTemp,MAXLENGTH); ?????????????????????} ?????????????????????Push(*lpContainer,S); ?????????????????} ?????????????} ?????????} ?????????lpContainer++; ?????} ? ????while(!isEmpty(S)) ? ????{ ? ????????sprintf(lpTemp,"%c",Pop(S)); ? ????????strcat(lpResult,lpTemp); ?????????ZeroMemory(lpTemp,MAXLENGTH); ?????} ?????MakeEmpty(S); ? ????printf("\nConvert?result:%s",lpResult); ? ????int?iResult?=?Caculate(lpResult,S); ? ????printf("\nCalculate?result:%d",iResult); ? ????return; ? } ?? int?Caculate(LPSTR?lpContainer,Stack?S) ? { ? ????int?iTemp; ? ????int?iResult; ? ????while(*lpContainer?!=?'\0') ? ????{ ? ????????if(isdigit(*lpContainer)) ? ????????{ ? ????????????LPSTR?lpTemo?=?(LPSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,MAXLENGTH); ? ????????????sprintf(lpTemo,"%c",*lpContainer); ? ????????????Push(atoi((const?char?*)lpTemo),S); ? ????????}else? ????????{ ? ????????????switch(*lpContainer) ? ????????????{ ? ????????????case?'+': ? ????????????????Push(Pop(S)+Pop(S),S); ? ????????????????break; ? ????????????case?'-': ? ????????????????Push(Pop(S)-Pop(S),S); ? ????????????????break; ? ????????????case?'*': ? ????????????????Push(Pop(S)*Pop(S),S); ? ????????????????break; ? ????????????case?'/': ? ????????????????Push(Pop(S)/Pop(S),S); ? ????????????????break; ? ????????????default: ? ????????????????break; ? ????????????} ?????????} ?????????lpContainer++; ?????} ? ????return?Pop(S); ? }??
本文轉(zhuǎn)hackfreer51CTO博客,原文鏈接:http://blog.51cto.com/pnig0s1992/812563,如需轉(zhuǎn)載請自行聯(lián)系原作者
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的中缀转后缀表达式并求值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。