E->E+E
E->E-E
E->E*E
E->E/E
E->(E)
E->digit //也可以换成id 适当的改动
#include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int flag=0;
stack<int> S; //状态栈
float T[100]; //属性栈
int top=0; //属性栈栈顶
float sum=0; //digit的真实值
double SUM=0;
double temp=0.1;
char s[100]; //输入
int sptr=0;
int p1;
char ch;
int action1[14][8]={{1,0,0,0,0,1,0,0}, //action表的移进规约错误部分信息,1表示移进,2表示规约,0表示错误,3表示接受
{0,1,1,1,1,0,0,3},
{1,0,0,0,0,1,0,0},
{0,2,2,2,2,0,2,2},
{1,0,0,0,0,1,0,0},
{1,0,0,0,0,1,0,0},
{1,0,0,0,0,1,0,0},
{1,0,0,0,0,1,0,0},
{0,1,1,1,1,0,1,0},
{0,2,2,1,1,0,2,2},
{0,2,2,1,1,0,2,2},
{0,2,2,2,2,0,2,2},
{0,2,2,2,2,0,2,2},
{0,2,2,2,2,0,2,2},};
int action2[14][8]={{3,0,0,0,0,2,0,0}, //action表的数字部分信息,数字表示状态,或者产生式号
{0,4,5,6,7,0,0,-1},
{3,0,0,0,0,2,0,0},
{0,6,6,6,6,0,6,6},
{3,0,0,0,0,2,0,0},
{3,0,0,0,0,2,0,0},
{3,0,0,0,0,2,0,0},
{3,0,0,0,0,2,0,0},
{0,4,5,6,7,0,13,0},
{0,1,1,6,7,0,1,1},
{0,2,2,6,7,0,2,2},
{0,3,3,3,3,0,3,3},
{0,4,4,4,4,0,4,4},
{0,5,5,5,5,0,5,5},};
int Goto[14]={1,-1,8,-1,9,10,11,12,-1,-1,-1,-1,-1,-1}; //goto表
int chan[6]={3,3,3,3,3,1};
int zhong(char t)
{
if(t=='d')return 0;
else if(t=='+')return 1;
else if(t=='-')return 2;
else if(t=='*')return 3;
else if(t=='/')return 4;
else if(t=='(')return 5;
else if(t==')')return 6;
else if(t=='$')return 7;
}
void fenxi(int m,char n)
{
if(action1[m][zhong(n)]==1) //移进
{
//printf("%d\t移进!",S.top());
if(ch=='d')
{/*cout<<T[top]<<endl;*/}
else
{/*cout<<ch<<endl;*/top++;}
S.push(action2[m][zhong(n)]);
sptr++;
ch=s[sptr];
}
else if(action1[m][zhong(n)]==2) //规约
{
//printf("%d\t规约!",S.top());
if(action2[m][zhong(n)]==1)
{T[top-2]=T[top-2]+T[top];/*cout<<"E->E+E"<<endl*/;top=top-2;}
else if(action2[m][zhong(n)]==2)
{T[top-2]=T[top-2]-T[top];/*cout<<"E->E-E"<<endl*/;top=top-2;}
else if(action2[m][zhong(n)]==3)
{T[top-2]=T[top-2]*T[top];/*cout<<"E->E*E"<<endl*/;top=top-2;}
else if(action2[m][zhong(n)]==4)
{
if(T[top]!=0)
{
T[top-2]=T[top-2]/T[top];
//cout<<"E->E/E"<<endl;
top=top-2;
}
else
{
flag=2;
printf("\nerror!除号不能为零!\n");
}
}
else if(action2[m][zhong(n)]==5)
{T[top-2]=T[top-1];/*cout<<"E->(E)"<<endl;*/top=top-2;}
else if(action2[m][zhong(n)]==6)
{/*cout<<"E->digit"<<endl;*/}
for(p1=0;p1<chan[action2[m][zhong(n)]-1];p1++)
S.pop();
S.push(Goto[S.top()]);
}
else if(action1[m][zhong(n)]==0) //错误
{
flag=2;
cout<<"error!"<<endl;
}
else if(action1[m][zhong(n)]==3) //接受
flag=1;
}
void FENXI()
{
int length;
ch=s[sptr];
//printf("栈顶\t动作\n");
while(1)
{
if(s[sptr]>='0'&&s[sptr]<='9')
{
while(s[sptr]>='0'&&s[sptr]<='9')
{
sum=sum*10+s[sptr]-48;
sptr++;
}
if(s[sptr]=='.')
{
sptr++;
while(s[sptr]>='0'&&s[sptr]<='9')
{
SUM=SUM+(s[sptr]-48)*temp;
sptr++;
temp=temp*0.1;
}
}
top++;
sptr--;
T[top]=sum+SUM;
sum=0;
SUM=0;
ch='d';
}
fenxi(S.top(),ch);
if(flag==1)
{
cout<<"表达式正确!"<<endl;
length=strlen(s);
s[length-1]=s[length];
cout<<s<<"="<<T[top]<<endl;
break;
}
if(flag==2)
{
cout<<"重新输入!"<<endl;
break;
}
}
}
int main()
{
while(flag==0||flag==2)
{
for(p1=0;p1<S.size();p1++)
S.pop();
for(p1=0;p1<100;p1++)
{s[p1]=0;T[p1]=0;}
top=sptr=sum=flag=0;
temp=0.1;
S.push(0);
cout<<"please input!(end of '$')"<<endl;
cin>>s;
FENXI();
if(flag==1)
{
cout<<"继续?(继续->输入0;否则输入1)"<<endl;
cin>>flag;
}
}
system("pause");
return 0;
}