77范文网 - 专业文章范例文档资料分享平台

编译课程设计报告书(4)

来源:网络收集 时间:2018-12-05 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

(2)程序

#include #include using namespace std; const int MAX=100;

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)在线全文阅读。

编译课程设计报告书(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/342260.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: