目标代码生成-逆波兰式 求填空

thankfuls 2012-05-21 08:14:55

#include<iostream>
#include<string>
#include<stack>
using namespace std;
string temp1(8,0),temp2(8,0),value1;
/*判断表达式中括号是否配对,表达式中仅有小括号*/
bool Ispair(string expre)
{
bool flag=true;
stack<char> s;
for(int i=0;i<expre.length();i++)
{
if(expre[i]=='(')
s.push(expre[i]);
if(expre[i]==')')
{
if(s.empty())
{
flag=false;
return flag;
}
s.pop();
}
}
if(!s.empty())/*栈中不为空,则说明左右括号不匹配*/
{
flag=false;
}
return flag;
}

/*对输入的表达式作合法性检查*/
bool Isright(string expre)
{
bool flag=true;
if(expre.length()==0)/*输入表达式为空*/
{
cout<<"输入表达式不合法,表达式不能为空!"<<endl;
/*用getline输入string,必须按两个回车,string才出来,这属于vc6.0的bug*/
flag=false;
return flag;
}
for(int i=0;i<expre.length();i++)/*输入表达式含有空格*/
{
if(expre[i]==' ')
{
cout<<"输入表达式不合法,输入表达式不能含有空格!"<<endl;
flag=false;
return flag;
}
}
for(i=1;i<expre.length();i++)/*若(不为首字符,必须跟在运算符后面*/
{
if(expre[0]!='(')
{
if(expre[i-1]!='+'&&expre[i-1]!='-'&&expre[i-1]!='*'&&
expre[i-1]!='/'&&expre[i-1]!='%'&&expre[i]=='(')
{
cout<<"输入表达式不合法,运算变量和左括号不能直接相连!"<<endl;
flag=false;
return flag;
}
}
}
if(!Ispair(expre))
{
cout<<"输入表达式不合法,输入括号不匹配!"<<endl;
flag=false;
return flag;
}
if(!((expre[0]<='z'&&expre[0]>='a')||(expre[0]<='Z'&&expre[0]>='A')
||(expre[0]<='9'&&expre[0]>='0')||expre[0]=='-'||expre[0]=='('))
{
cout<<"输入表达式不合法,表达式首个字符必须为字母标识符、数字、负号或者左括号!"<<endl;
flag=false;
return flag;
}
if(!((expre[i-1]<='z'&&expre[i-1]>='a')||(expre[i-1]<='Z'&&expre[i-1]>=
'A')||(expre[i-1]<='9'&&expre[i-1]>='0')||expre[i-1]==')'))
{
cout<<"输入表达式不合法,表达式最后一个字符必须为字母标识符、数字或者右括号!"<<endl;
flag=false;
return flag;
}
if(!((expre[i-1]<='z'&&expre[i-1]>='a')||(expre[i-1]<='Z'&&expre[i-1]>='A')||(expre[i-1]<='9'&&expre[i-1]>='0')||expre[i-1]==')'))
{
cout<<"输入表达式不合法,表达式最后一个字符必须为字母标识符、数字或者右括号!"<<endl;
flag=false;
return flag;
}
for(i=0;i<expre.length();i++)/*输入表达式含有不合法字符*/
{
if((expre[i]<='z'&&expre[i]>='a')||(expre[i]<='Z'&&expre[i]>='A')
||(expre[i]<='9'&&expre[i]>='0'))
flag=true;
else
{
switch(expre[i])
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '(':
case ')':
case '_':
case '.':
flag=true;
break;
default:
flag=false;
cout<<"输入表达式不合法,表达式含有不合法字符!"<<endl;
return flag;
break;
}
}
}
for(i=0;i<expre.length();i++)/*表达式不能连续含有两个运算符,此程序不考虑++,--类运算*/
{
if(expre[i]=='+'||expre[i]=='-'||expre[i]=='*'||expre[i]=='/'
||expre[i]=='%'||expre[i]=='(')
if(expre[i+1]=='+'||expre[i+1]=='-'||expre[i+1]=='*'||expre[i+1]
=='/'||expre[i+1]=='%'||expre[i+1]==')')
{
flag=false;
cout<<"输入表达式不合法,不能连续含有两个运算符!"<<endl;
return flag;
}
}
return flag;
}


...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
nanjun520 2012-05-21
  • 打赏
  • 举报
回复
这代码 看得! 都不知道 LZ 想解决什么问题?
W170532934 2012-05-21
  • 打赏
  • 举报
回复
楼主这作业贴也太长了吧
thankfuls 2012-05-21
  • 打赏
  • 举报
回复

/*目标代码生成*/
void Listcode(string expre)
{
int k=0;
int sr,sr1;/*保存栈顶寄存器的值*/
int n=0,p=0,q=0; /*用来保存寄存器的下标*/
string result;
stack<string> s;
result=Apoland(expre);/*result为生成的逆波兰式*/
cout<<"表达式生成的逆波兰式如下:"<<endl;
while(result[k]!='\0')
{
cout<<result[k];
k++;
}
cout<<endl;
cout<<"表达式生成的目标代码如下:"<<endl;
for(int i=0;i<result.length();i++)
{
if(result[i]==',')
{
i++;
}
temp1=" ",temp2=" ";
p=0,q=0;
if(n>=7)/*当寄存器的号超过7时,依次释放R0-R7寄存器*/
n=0;
if(!(result[i]=='+'||result[i]=='-'||result[i]=='*'||result[i]=='/'
||result[i]=='%'))
{
while(!(result[i]=='+'||result[i]=='-'||result[i]=='*'||result[i]
=='/'||result[i]=='%'
||result[i]==',')) /*运算分量不为单字符,如12*/
{

temp1[p++]=result[i];/*将运算分量的值保存在temp1数组中*/
i++;
}
//补充语句3/*运算分量进栈*/
}
if(result[i]=='+'||result[i]=='-'||result[i]=='*'||result[i]=='/'
||result[i]=='%')
{
if(result[i]=='+')
{
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"
||s.top()=="["||
s.top()=="]"||s.top()=="{"||s.top()=="}")/*栈顶为寄存器变量*/
{
temp2=s.top();
s.pop();
sr=top(temp2);/*保存栈顶寄存器的号*/
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"
||s.top()=="["||
s.top()=="]"||s.top()=="{"||s.top()=="}")/*次栈顶也为寄存器变量*/
{
temp2=s.top();
s.pop();
sr1=top(temp2);
cout<<"ADD"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<sr1<<endl;
stackin(sr1);
s.push(value1);
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"ADD"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
//补充语句4(+,-,*,/,%处补充语句均相同) /*运算结果入栈*/
n++;
}
}
else
{
temp2=s.top();
s.pop();
//补充语句5((+,-,*,/,%处补充语句均相同))/*栈顶为寄存器变量*/
{
cout<<"ADD"<<"\t"<<temp2<<"\t"<<"R"<<top(s.top())<<endl;
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"ADD"<<"\t"<<temp2<<"\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
s.push(value1);
n++;
}
}
}
if(result[i]=='-')
{
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr=top(temp2);
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr1=top(temp2);
cout<<"SUB"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<sr1<<endl;
stackin(sr1);
s.push(value1);
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"SUB"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
//补充语句4(+,-,*,/,%处补充语句均相同)/*运算结果入栈*/
n++;
}
}
else
{
temp2=s.top();
s.pop();
//补充语句5((+,-,*,/,%处补充语句均相同))/*栈顶为寄存器变量*/
{
cout<<"SUB"<<"\t"<<temp2<<"\t"<<"R"<<top(s.top())<<endl;
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"SUB"<<"\t"<<temp2<<"\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
s.push(value1);
n++;
}
}
}
if(result[i]=='*')
{
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr=top(temp2);
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr1=top(temp2);
cout<<"MUL"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<sr1<<endl;
stackin(sr1);
s.push(value1);
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"MUL"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
//补充语句4(+,-,*,/,%处补充语句均相同) /*运算结果入栈*/
n++;
}
}
else
{
temp2=s.top();
s.pop();
//补充语句5((+,-,*,/,%处补充语句均相同))/*栈顶为寄存器变量*/
{
cout<<"MUL"<<"\t"<<temp2<<"\t"<<"R"<<top(s.top())<<endl;
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"MUL"<<"\t"<<temp2<<"\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
s.push(value1);
n++;
}
}
}
if(result[i]=='/')
{
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr=top(temp2);
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr1=top(temp2);
cout<<"DIV"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<sr1<<endl;
stackin(sr1);
s.push(value1);
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"DIV"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
//补充语句4(+,-,*,/,%处补充语句均相同) /*运算结果入栈*/
n++;
}
}
else
{
temp2=s.top();
s.pop();
//补充语句5((+,-,*,/,%处补充语句均相同))/*栈顶为寄存器变量*/
{
cout<<"DIV"<<"\t"<<temp2<<"\t"<<"R"<<top(s.top())<<endl;
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"DIV"<<"\t"<<temp2<<"\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
s.push(value1);
n++;
}
}
}
if(result[i]=='%')
{
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"||
s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr=top(temp2);
if(s.top()=="@"||s.top()=="#"||s.top()=="<"||s.top()==">"
||s.top()=="["||s.top()=="]"||s.top()=="{"||s.top()=="}")
{
temp2=s.top();
s.pop();
sr1=top(temp2);
cout<<"MOD"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<sr1<<endl;
stackin(sr1);
s.push(value1);
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"MOD"<<"\t"<<"R"<<sr<<"\t\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
//补充语句4(+,-,*,/,%处补充语句均相同) /*运算结果入栈*/
n++;
}
}
else
{
temp2=s.top();
s.pop();
//补充语句5(+,-,*,/,%处补充语句均相同))栈顶为寄存器变量*/
{
cout<<"MOD"<<"\t"<<temp2<<"\t"<<"R"<<top(s.top())<<endl;
}
else
{
cout<<"MOV"<<"\t"<<s.top()<<"\t"<<"R"<<n<<endl;
cout<<"MOD"<<"\t"<<temp2<<"\t"<<"R"<<n<<endl;
s.pop();
stackin(n);
s.push(value1);
n++;
}
}
}
}
}
}

void main()
{
string expression;
cout<<"请输入表达式:";
getline(cin,expression);
while(!Isright(expression))/*输入表达式合法性检查*/
{
cout<<"请重新输入:";
getchar();
getline(cin,expression);
}
Listcode(expression); /*调用目标代码生成函数*/
}

thankfuls 2012-05-21
  • 打赏
  • 举报
回复

/*判断运算符的优先级*/
int power(char ch)
{
switch(ch)
{
case '+':
case '-':
return 1;
break;
case '*':
case '/':
return 2;
break;
case '%':
break;
default:
return 0;
}
}

/*判断字符是不是运算符*/
bool op(char ch)
{
bool flag=false;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='('||ch==')')
{
flag=true;
}
return flag;
}

/*将表达式转化为逆波兰式输出*/
string Apoland(string expre)
{
stack<char> s;/*运算符栈*/
char value[30];
int j=0;
bool flag;
int m,n;
for(int i=0;i<expre.length();i++)
{
flag=true;
if(op(expre[i]))/*属于运算符和'('和')'*/
{
if(expre[i]!='('&&expre[i]!=')')
value[j++]=','; /*逗号作为分隔符*/
while(flag)
{
if((s.empty()&&expre[i]!=')')||expre[i]=='(')
{
s.push(expre[i]);
flag=false;
}
else if(expre[i]==')'&&s.top()=='(')
{
s.pop();
flag=false;
}
else
{
m=power(expre[i]);/*扫描运算符的优先级别*/
n=power(s.top());/*栈顶运算符级别*/
if(!s.empty())
{
if(m>n)/*当前大于栈顶优先级*/
{
//补充语句1
flag=false;
}
if(m<=n)/*当前优先级小于等于栈顶优先级*/
{
if(s.top()!='(')
{
if(expre[i]==')')
{
value[j++]=',';
value[j++]=s.top();
}
else
{
value[j++]=s.top();
value[j++]=',';
}
}
s.pop();
flag=true;
}
}
}
}
}
else/*若当前字符是运算量*/
{
//补充语句2
}
}
while(!s.empty())
{
if(s.top()!='(')
{
value[j++]=',';
value[j++]=s.top();
}
s.pop();
}
value[j]='\0';
return value;
}

/*返回栈顶所使用寄存器的号*/
int top(string ch)
{
if(ch=="@")
return 0;
if(ch=="#")
return 1;
if(ch=="<")
return 2;
if(ch==">")
return 3;
if(ch=="[")
return 4;
if(ch=="]")
return 5;
if(ch=="{")
return 6;
if(ch=="}")
return 7;
}

/*对于一个寄存器的号,将代表该寄存器的字符保存在全局变量value1中*/
void stackin(int a)
{

switch(a)
{
case 0:
value1="@";
break;
case 1:
value1="#";
break;
case 2:
value1="<";
break;
case 3:
value1=">";
break;
case 4:
value1="[";
break;
case 5:
value1="]";
break;
case 6:
value1="{";
break;
case 7:
value1="}";
break;
}
}


64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧