數(shù)據(jù)結(jié)構(gòu) 魔王語言學(xué)習(xí)資料_第1頁
數(shù)據(jù)結(jié)構(gòu) 魔王語言學(xué)習(xí)資料_第2頁
數(shù)據(jù)結(jié)構(gòu) 魔王語言學(xué)習(xí)資料_第3頁
數(shù)據(jù)結(jié)構(gòu) 魔王語言學(xué)習(xí)資料_第4頁
數(shù)據(jù)結(jié)構(gòu) 魔王語言學(xué)習(xí)資料_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

#include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACK_INCREMENT10structStack{char*base;char*top;intstacksize;};voidInitStack(structStack&s){//構(gòu)造棧s.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));s.top=s.base;s.stacksize=STACK_INIT_SIZE;}voidPush(structStack&s,chare){//壓入元素if(s.top-s.base>=STACK_INIT_SIZE){s.base=(char*)realloc(s.base,(s.stacksize+STACK_INCREMENT)*sizeof(char));s.top=s.base+s.stacksize;s.stacksize+=STACK_INCREMENT;}*(s.top)=e;s.top++;}voidPop(structStack&s,char&e){//取出元素e=*--s.top;}intStackEmpty(structStacks){//棧是否為空if(s.top==s.base){return1;}else{return0;}}voidClearStack(structStack&s){s.top=s.base;}structQueue{chardata;structQueue*next;};structLinkQueue{structQueue*front;structQueue*rear;};voidInitQueue(structLinkQueue&q){//構(gòu)造隊(duì)q.front=q.rear=(structQueue*)malloc(sizeof(structQueue));q.front->next=NULL;}voidEnQueue(structLinkQueue&q,chare){//元素入隊(duì)structQueue*p;p=(structQueue*)malloc(sizeof(structQueue));p->data=e;p->next=NULL;q.rear->next=p;q.rear=p;}voidDeQueue(structLinkQueue&q,char&e){//元素出隊(duì)structQueue*p;p=q.front->next;e=p->data;q.front->next=p->next;if(q.rear==p){q.rear=q.front;}free(p);}intQueueEmpty(structLinkQueueq){//隊(duì)是否為空if(q.front==q.rear){return1;}else{return0;}}voidInStack(char*ch,structStack&s){//把字符數(shù)組從右至左壓入棧中inti,L=0;while(ch[L]!='\0'){L++;}for(i=L-1;i>=0;i--){Push(s,ch[i]);}}intmain(){inti=0;charA[]="sae";charB[]="tsaedsae";charflag='0';//flag用來標(biāo)記處理括號intmark=1;intf=0;structStackS;structStacktemp;//用來處理括號外的元素InitStack(S);InitStack(temp);structLinkQueueQ;InitQueue(Q);charMoWang[100]="\0";chare1,key,e2,e;printf("**************************************************************\n");printf("**歡迎光臨廣東工業(yè)大學(xué)**\n");printf("******************************\n");printf("**魔王語言解釋系統(tǒng)**\n");printf("******************************\n");printf("*班級:計(jì)算機(jī)學(xué)院網(wǎng)絡(luò)工程2007級4班*\n");printf("*姓名:黃文龍學(xué)號:3107007087*\n");printf("**************************************************************\n\n");printf("請輸入你想要解釋的魔王語言(最多含有一個(gè)括號):\n");gets(MoWang);InStack(MoWang,S);//把要解釋的魔王語言壓入棧中while(!StackEmpty(S)){Pop(S,e1);if(e1=='('){if(StackEmpty(S)){printf("魔王語言錯(cuò)誤!\n");mark=0;break;}while(!StackEmpty(S)){Pop(S,e1);if(e1==')'){f=1;break;}elseif(!(e1>='a'&&e1<='z')&&!(e1>='A'&&e1<='Z')){printf("魔王語言錯(cuò)誤!\n");mark=0;break;}}if(mark==0){break;}if(f!=1){printf("魔王語言錯(cuò)誤!\n");break;}}elseif(e1==')'){printf("魔王語言錯(cuò)誤!\n");mark=0;break;}elseif(!(e1>='a'&&e1<='z')&&!(e1>='A'&&e1<='Z')){printf("魔王語言錯(cuò)誤!\n");mark=0;break;}}if(mark==1&&f==1){ClearStack(S);InStack(MoWang,S);while(!StackEmpty(S)){//棧不空時(shí)Pop(S,e1);if(e1=='B'){Push(temp,e1);}elseif(e1=='A'){Push(temp,e1);}elseif(e1=='('){//用隊(duì)存儲括號中的元素Push(temp,flag);//有括號的話就用flag標(biāo)記Pop(S,e1);while(e1!=')'){EnQueue(Q,e1);Pop(S,e1);}if(!QueueEmpty(Q)){DeQueue(Q,key);}}else{Push(temp,e1);f=0;}}while(!StackEmpty(temp)){//邊處理邊進(jìn)棧Pop(temp,e1);if(e1!=flag){//把括號外的元素壓入中Push(S,e1);}else{while(!QueueEmpty(Q)){//處理括號中的元素進(jìn)棧DeQueue(Q,e2);Push(S,key);Push(S,e2);}if(f!=0){//最后還要壓一個(gè)keyPush(S,key);}}}printf("解釋后的語言為:\n");while(!StackEmpty(S)){//依次出棧輸出處理后的元素Pop(S,e);EnQueue(Q,e);//元素進(jìn)隊(duì)是為了輸出對應(yīng)漢字if(e=='B'){printf("%s",B);}elseif(e=='A'){printf("%s",A);}else{printf("%c",e);}}printf("\n");while(!QueueEmpty(Q)){//輸出對應(yīng)漢字DeQueue(Q,e);switch(e){case't':printf("天");break;case'd'

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論