(2)程序
#include
char cost[8][8]={' ','+','*','!','i','(',')','#', '+','>','<','<','<','<','>','>', '*','>','>','<','<','<','>','>', '!','>','>','<','<','<','>','>', 'i','>','>','>',' ',' ','>','>', '(','<','<','<','<','<','=',' ', ')','>','>','>',' ',' ','>','>',
'#','<','<','<','<','<',' ','='};//the priority realiton graph class stack {
int top;
char array[MAX]; public:
stack(){top=0;} char GetTop() {
return array[top-1]; }
int Getsize() {
return top; }
void PushStack(char c) {
if(top array[top++]=c; else cout<<\ } char *PopStack(int length) { if(top>=length) { char *temp=new char[MAX]; for(int i=0;i temp[i]=array[top-length+i]; } 15 top=top-length; temp[i]='\\0'; return temp; } else { return NULL; } } char GetChar(int pos) { if((pos>=0)&&(pos return '\\0'; } void DispStack() { for(int i=0;i if (GetChar(i)=='!') { cout<<\ cout<<'\\b'; } else cout< if (this->top>=8)cout<<'\\t'; else cout<<'\\t'<<'\\t'; } }; void DispCost() { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(cost[i][j]=='!') cout<<\ else cout< 16 cout< bool IsVT(char c) { bool flag=false; for(int i=0;i<8;i++) { if(c==cost[0][i]) { flag=true; } } if (((c>='a')&&(c<='z')) || ((c>='0')&&(c<='9'))) flag=true; return flag; } bool Num_Char(char c) { if (((c>='a')&&(c<='z')) || ((c>='0')&&(c<='9'))) return true; else return false; } int SearchCost(char ch1,char ch2)//set the realiton according to the cost { //0==equal ,1==above,-1==low,2==NULL int i,j; if(Num_Char(ch1)) ch1='i'; if(Num_Char(ch2)) ch2='i'; switch(ch1) { case '+':i=1;break; case '*':i=2;break; case '!':i=3;break; case 'i':i=4;break; case '(':i=5;break; case ')':i=6;break; case '#':i=7;break; } switch(ch2) { case '+':j=1;break; 17 case '*':j=2;break; case '!':j=3;break; case 'i':j=4;break; case '(':j=5;break; case ')':j=6;break; case '#':j=7;break; } switch(cost[i][j]) { case '>':return 1;break; case '<':return -1;break; case '=':return 0;break; default:return 2;break; } } void main() { char string[MAX]; //for the sentence to be ananlyse char *ch=new char [MAX]; //the mergeing string stack s; //for the merge stack s.PushStack('#'); //init the stack char a; //store the letter need to be judge int strp; //point to the letter of the string int statop; //point to the top of the stack int statopvt; //point to the top VT cout<<\ DispCost(); cout<<\ cout<<\ cin.getline(string,MAX); cout<<\ a=string[0]; //init the pointers strp=0; statop=s.Getsize()-1; statopvt=statop; while(a!='#') //The analysing progress is as following: { //if the letter's priority is litter than the top start merge a=string[strp]; // get the letter if((a=='*')&&(string[strp+1]=='*')) { a='!'; 18 strp++; } if(IsVT(s.GetChar(statop))) statopvt=statop; else statopvt=statop-1; //if the letter's priority is greater than the top the push it while(IsVT(a)&&(SearchCost(s.GetChar(statopvt),a)==1))//the topvt is greater than the letter the can not push then merge { s.DispStack(); //show the stack before merging int highpos=statopvt,lowpos=highpos-1; //calculate the length of the mergeing string if(!IsVT(s.GetChar(lowpos))) lowpos--; while(SearchCost(s.GetChar(lowpos),s.GetChar(highpos))!=-1) { highpos=lowpos; lowpos--; if(!IsVT(s.GetChar(lowpos))) lowpos--; } int start=s.Getsize(); lowpos++; int length=start-lowpos; strcpy(ch,s.PopStack(length)); s.PushStack('N'); cout<<\ for(int i=0;i if(ch[i]=='!') cout<<\ else cout< cout<<\ statopvt=s.Getsize()-1; if(!IsVT(s.GetChar(statopvt))) statopvt--; } if (!((a>='A')&&(a<='Z'))&&(SearchCost(s.GetChar(statopvt),a)==2)) { cout<<\ 19 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库编译课程设计报告书(4)在线全文阅读。
相关推荐: