




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、# include <stdio.h># include <malloc.h># include <conio.h># define maxsize 100typedef double datatype1;typedef char datatype2;typedef struct stack1 datatype1 data1maxsize; int top1;/*棧頂元素*/seqstack1,*pseqstack1; /*順序棧*/typedef struct stack2 datatype2 data2maxsize; int top2;/*棧頂元素*/
2、seqstack2,*pseqstack2; /*順序棧*/*棧的初始化*/pseqstack1 init_seqstack1(void) pseqstack1 S; S=(pseqstack1)malloc(sizeof(pseqstack1); if(S) S->top1=-1; return S;pseqstack2 init_seqstack2(void) pseqstack2 S; S=(pseqstack2)malloc(sizeof(pseqstack2); if(S) S->top2=-1; return S;/*判斷???/int empty_seqstack1(
3、pseqstack1 S) if(S->top1=-1) return 1; else return 0;int empty_seqstack2(pseqstack2 S) if(S->top2=-1) return 1; else return 0;/*X入棧*/int push_seqstack1(pseqstack1 S,datatype1 X) if(S->top1=maxsize-1) printf("棧滿,無法入棧!n"); return 0; else S->top1+; S->data1S->top1=X; return
4、1; int push_seqstack2(pseqstack2 S,datatype2 X) if(S->top2=maxsize-1) printf("棧滿,無法入棧!n"); return 0; else S->top2+; S->data2S->top2=X; return 1; /*X出棧*/int pop_seqstack1(pseqstack1 S,datatype1 *X) if(empty_seqstack1(S) return 0; else *X=S->data1S->top1; S->top1-; retur
5、n 1; int pop_seqstack2(pseqstack2 S,datatype2 *X) if(empty_seqstack2(S) return 0; else *X=S->data2S->top2; S->top2-; return 1; /*求棧頂元素*/int gettop_seqstack1(pseqstack1 S,datatype1 *X) if(empty_seqstack1(S) return 0; else *X=S->data1S->top1; return 1;int gettop_seqstack2(pseqstack2 S,d
6、atatype2 *X) if(empty_seqstack2(S) return 0; else *X=S->data2S->top2; return 1;/*判斷字符是否為操作數(shù)。若是返回1,否則返回0*/int isnum(char c) if(c>='0' && c<='9') return 1; else return 0;/*求后綴表達式的值*/double postfix_exp(char *A) pseqstack1 S;/*定義棧S*/double operand=0; double result;/*存
7、放棧頂元素*/double a;/*運算符ch前的操作數(shù)出棧存入a*/double b;/*運算符ch后的操作數(shù)出棧存入b*/double c;/*c=a ch b*/ char ch;/*存放讀取到的表達式(A)的字符*/ ch=*A+;/*讀表達式字符=>A*/ S=init_seqstack1();/*初始化棧*/ while(ch!='#')/*遇到元素!='#'時*/ if(isnum(ch)/*判斷ch是否為數(shù)字字符,計算出操作數(shù)*/ operand=operand*10+(ch-'0'); else/*否則*/ if(oper
8、and)push_seqstack1(S,operand);/*當前字符不是數(shù)字,操作數(shù)結(jié)束,要入棧*/operand=0;if(ch!='' && ch!=' ')pop_seqstack1(S,&b);/*運算符ch后的操作數(shù)出棧存入b*/pop_seqstack1(S,&a);/*運算符ch前的操作數(shù)出棧存入a*/switch(ch)/*求 a ch b=? ,將結(jié)果賦給 c */ case '+' : c=a+b;break;case '-' : c=a-b;break;case '
9、*' : c=a*b;break;case '/' :if(b!=0)c=a/b;elseprintf("分母為零!"); push_seqstack1(S,c);/*將c壓入棧中*/ ch=*A+;/*指針向下移動一位*/ /*遇到'#'循環(huán)結(jié)束*/ gettop_seqstack1(S,&result);/*此時棧頂元素即為計算結(jié)果result*/ return result;/*優(yōu)先級判斷函數(shù)*/int priority(char op)switch(op)case '#': return 1;case
10、')': return 2;case '+': case '-': return 3;case '*': case '/': return 4;case '(': return 5;default : return 0; /*將指針infixexp指向的中綴表達式轉(zhuǎn)換為指針postfixexp指向的后綴表達式*/int infix_exp_value(char *infixexp,char *postfixexp)pseqstack2 S;/*定義棧S*/int count=0;char w;/*存
11、放讀取到的表達式(infixexp)的字符*/char c;/*存放棧頂元素*/char topelement;/*存出棧元素*/S=init_seqstack2();/*初始化棧*/if(!S)/*棧的初始化判斷*/printf("棧初始化失敗!");return 0;push_seqstack2(S,'#');/*將結(jié)束符'# '加入運算符棧S中*/w=*infixexp;/*讀表達式字符=>w*/while(gettop_seqstack2(S,&c),c)!='#'|w!='#')/*&l
12、t;3>棧頂元素不等于'#'或w不等于'#'時循環(huán)*/if(isnum(w)/*判斷w是否為操作數(shù),若是直接輸出,讀下一個字符=>w,轉(zhuǎn)<3>*/if(count)*postfixexp=''postfixexp+;count=0;*postfixexp=w;postfixexp+;w=*(+infixexp);else/*w若是運算符分類如下*/count=1;if( (gettop_seqstack2(S,&c),c)='(' && w=')' )/*如果棧頂為&
13、#39;('并且w為')'則'('出棧不輸出,讀下一個字符=>w,轉(zhuǎn)<3>*/ pop_seqstack2(S,&topelement); /*將'('出棧存入topelement*/w=*(+infixexp);elseif(gettop_seqstack2(S,&c),c)='('|priority(gettop_seqstack2(S,&c),c) ) < priority(w) )/*如果棧頂為'('或者棧頂優(yōu)先級小于w優(yōu)先級,則w入棧,讀下一個字符=
14、>w,轉(zhuǎn)<3>*/ push_seqstack2(S,w);w=*(+infixexp);else/*否則*/*從運算符棧中出棧并輸出,轉(zhuǎn)<3>*/ pop_seqstack2(S,&topelement);*postfixexp=topelement;postfixexp+;*postfixexp='#'/*在指針postfixexp指向的后綴表達式結(jié)尾追加字符'#'*/*(+postfixexp)='0'/*在指針postfixexp指向的后綴表達式最后追加結(jié)束符'0'*/return 1;/*主函數(shù)*/int main() int i=0;char Amaxsize;char Bmaxsize;printf("請輸入表達式,如:20+13#,必須以#號結(jié)尾!n"); /* 1+2*(9+7)-4/2# 23+(12*3-2)/4+34*5/7)+108/9#
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 綠色建材智能制造項目可行性研究報告(模板)
- Java語言程序設(shè)計-v3-15
- 高端護膚品牌連鎖行業(yè)跨境出海項目商業(yè)計劃書
- 環(huán)保型塑料回收助劑企業(yè)制定與實施新質(zhì)生產(chǎn)力項目商業(yè)計劃書
- 耐油耐化學藥品人造革行業(yè)跨境出海項目商業(yè)計劃書
- 鄉(xiāng)村茶藝體驗月企業(yè)制定與實施新質(zhì)生產(chǎn)力項目商業(yè)計劃書
- 金屬氧化物共摻雜修飾家居用木材及催化降解甲醛性能研究
- 黃土高原人工林深層土壤水碳估算及轉(zhuǎn)化可持續(xù)性研究
- 紅細胞分布寬度-白蛋白比值與非酒精性脂肪性肝病的相關(guān)性分析
- 體育賽事夏季防暑降溫措施
- 2025江蘇中考:物理高頻考點
- 日料店空間設(shè)計
- 2024年高級審計師試題及答案解析
- 2025年江西省安福縣事業(yè)單位公開招聘輔警36名筆試題帶答案
- 2025年全國國家版圖知識競賽題庫及答案
- 2025年春人教版英語七年級下冊 Unit 7 A Day to Remember(教學設(shè)計)
- 《船舶管理》助理船副考試復習題庫(含答案)
- YAMAHA(雅馬哈)貼片機編程培訓教材
- 創(chuàng)業(yè)計劃書 創(chuàng)業(yè)計劃書word文檔(三篇)
- 廣東省中山市2021-2022學年八年級下學期期末水平測試道德與法治試卷
- 飼料學第五章粗飼料課件
評論
0/150
提交評論