算符优先实验报告.docVIP

  • 148
  • 0
  • 约5.33千字
  • 约 9页
  • 2017-08-19 发布于北京
  • 举报
南华大学 计算机科学与技术学院 实验报告 ( 2011 ~2012 学年度 第二学期 ) 课程名称 程序设计与编译 实验名称 算符优先 姓名 王郑朋 学号 20104030337 专业 电气信息类 班级 1003 地点 8教 教师 曹军 实验目的 设计编写并调试一个算符优先程序,掌握算符优先分析法的原理E-E+T|E-T|T T-T*F|T/F|F F-(E)|i 实验代码 #includestring.h #includestdlib.h #include ctype.h #includeiostream using namespace std; const int maxsize=100; //为数组str[]、in[]分配的最大存储空间 const int length=100;//为数组array[]分配的最大存储空间 class stack { private: int size;//size为当前数组array[]的大小 char array[length];//用于存储读入的字符 public: stack() { size=0;//数组array[]的初始长度为0 } void push(char ch) { if(sizelength)//如果数组未满,则压入 { array[size]=ch; size++; } else//若数组已满,则给出出错信息 coutoverflow!endl; } int pop(char ch[],int len)//弹出字符准备规约 { if(size-len=0) { for(int i=0;ilen;i++) ch[i]=array[size-len+i]; size-=len; return len; } else { cout参数错误!endl; return 0; } } char saomiao(int pos)//判断大小是否合法 { if(pos=0possize) return array[pos]; return \0; } void saomiaosuoyou()//输出当前数组中的字符 { for(int i=0;igetsize();i++) coutsaomiao(i); coutendl; } int getsize()//返回当前数组大小 { return size; } }; char guiyue(char ch[])//规约为M { return M; } int isnumch(char ch)//判断ch是不是数字或小写字母 { return (ch=0ch=9||ch=ach=z); } int getrank(char ch1,char ch2)//根据算符优先分析矩阵设置读入优先次序 { //0表示等于,1表示大于,-1表示小于,2表示没有优先关系 if(isnumch(ch1)) ch1=i; if(isnumch(ch2)) ch2=i; if(ch1==+||ch1==-) { if(ch2==+||ch2==#||ch2==)||ch2==-) return 1; else return -1; } if(ch1==*||ch1==/) { if(ch2==(||ch2==i) return -1; else return 1; } if(ch1==() { if(ch2==)) return 0; else if(ch2==#) return 2; else return -1; } if(ch1==i||ch1==)) { if(ch2==i||ch2==() return 2; else return 1; } if(ch1==#) { if(ch2==#) return 0; else if(ch2==)) return 2; else return -1; } return 2; } int isvt(char ch)//区别参加规约的资格 { if(ch=ach=z) return 1; if(ch=(ch=+) return 1; if(ch==#)

文档评论(0)

1亿VIP精品文档

相关文档