⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 suanfufenxi.cpp

📁 给定一个文法
💻 CPP
字号:

/*文法为:
 E->E+E|E-E|E*E|E/E|(E)|i */
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<fstream>

using namespace std;
#define maxsize 50
class stack
{
public:
 int top;
 char stacklist[maxsize];
 stack()
 {
  top=-1;
 }
 char pop();
 void push(const char &alp);
 char peek();
};
char stack::pop()
{
 char temp=stacklist[top];
 top--;
 return temp;
}
void stack::push(const char &alp)
{
 top++;
 stacklist[top]=alp;
}
char stack::peek()
{
 return stacklist[top];
}
int current_pos;
int table[8][8]={{1,1,-1,-1,-1,1,-1,1}, 
    {1,1,-1,-1,-1,1,-1,1}, 
    {1,1,1,1,-1,1,-1,1}, 
    {1,1,1,1,-1,1,-1,1}, 
    {-1,-1,-1,-1,-1,0,-1,0}, 
    {1,1,1,1,0,1,0,1}, 
    {1,1,1,1,0,1,0,1}, 
    {-1,-1,-1,-1,-1,0,-1,0}}; 
char token[50];
int sign;
stack opnd;//操作对象堆栈
stack optr;//操作符堆栈


int chartoint(char c)
{
 int temp;
 switch(c)
 {
 case '+':temp=0;
  break;
 case '-':temp=1;
  break;
 case '*':temp=2;
  break;
 case '/':temp=3;
  break;
 case '(':temp=4;
  break;
 case ')':temp=5;
  break;
 case 'i':temp=6;
  break;
 case '#':temp=7;
  break;
 default:temp=8;
  break;
 }
 return temp;
}
void docal()
{
 int lineno=1;
 int b=chartoint(token[current_pos]);
 if(b!=8)
 {
  cout<<"步骤\t"<<"对象栈\t"<<"符号栈\t"<<"剩余字符串"<<endl;
  optr.push('#');
  opnd.push('#');
  while(token[current_pos]!='#')
  {
   if(opnd.peek()=='i')
   {
    opnd.pop();
    opnd.push('E');
    cout<<lineno;
    cout<<setw(8);
    for(int i=0;i<=opnd.top;i++)
     cout<<opnd.stacklist[i];
    cout<<setw(8);
    for(i=0;i<=optr.top;i++)
     cout<<optr.stacklist[i];
    cout<<setw(8);
    for(i=current_pos;token[i]!='#';i++)
     cout<<token[i];
    cout<<"#"<<endl;
     lineno++;
   }
   else
   {
    int a=chartoint(optr.peek());
    int b=chartoint(token[current_pos]);
    if(b==8)
    {
     cout<<"分析失败"<<endl;
     exit(0);
    }
    else if(table[a][b]==1)
    {
     opnd.pop();
     opnd.pop();
     optr.pop();
     opnd.push('E');
    }
    else if(table[a][b]==-1)
    {
     if(token[current_pos]=='i')
      opnd.push('i');
     else
      optr.push(token[current_pos]);
     current_pos++;
    }
    else if(table[a][b]==0)
    {
     opnd.pop();
     optr.pop();
     opnd.push('E');
     current_pos++;
    }
    cout<<lineno<<setw(8);
    for(int i=0;i<=opnd.top;i++)
     cout<<opnd.stacklist[i];
    cout<<setw(8);
    for(i=0;i<=optr.top;i++)
     cout<<optr.stacklist[i];
    cout<<setw(8);
    for(i=current_pos;token[i]!='#';i++)
     cout<<token[i];
    cout<<"#"<<endl;
    lineno++;
   }
  }
  while(optr.peek()!='#')
  {
   if(opnd.peek()=='i')
   {
    opnd.pop();
    opnd.push('E');
    cout<<lineno;
    cout<<setw(8);
    for(int i=0;i<=opnd.top;i++)
     cout<<opnd.stacklist[i];
    cout<<setw(8);
    for(i=0;i<=optr.top;i++)
     cout<<optr.stacklist[i];
    cout<<setw(8);
    for(i=current_pos;token[i]!='#';i++)
     cout<<token[i];
    cout<<"#"<<endl;
     lineno++;
   }
   else
   {
    optr.pop();
    opnd.pop();
    opnd.pop();
    opnd.push('E');
    cout<<lineno<<setw(8);
    for(int i=0;i<=opnd.top;i++)
     cout<<opnd.stacklist[i];
    cout<<setw(8);
    for(i=0;i<=optr.top;i++)
     cout<<optr.stacklist[i];
    cout<<setw(8);
    for(i=current_pos;token[i]!='#';i++)
     cout<<token[i];
    cout<<"#"<<endl;
    lineno++;
   }
  }
 }
 else
  sign=0;
}

void main()
{
 sign=1;
    current_pos=0;
 cout<<"请输入您要分析的字符串以#结束"<<endl;
 cin>>token; 
 while(token[strlen(token)-1]!='#')
 {
  cout<<"输入的字符串应以#号结束"<<endl;
        cin>>token;
 }
 docal();
 if(sign==1)
  cout<<"分析成功"<<endl;
 else 
  cout<<"分析失败"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -