int k=0,n,m,p;
int syn;
char ch,token[8],prog[255];
int f(char op);
int g(char op);
int compare(char op);
void middle(char op);
void push_ostack(char op);
char pop_ostack();
char top_ostack();
void push_vstack(char *);
void pop_vstack(char *);
void top_vstack(char *);
struct stack1 //操作符栈
{
char stack[100];
int top;
}ostack;
struct stack2//操作数栈,可以是标识符或者常数,都用字符串形式保存
{
char stack[100][8];
int top;
}vstack;
char *newtemp()
{
char *p;
char m[8];
p=(char )malloc(8sizeof(char));
k++;
itoa(k,m,10);
strcpy(p+1,m);
p[0]='t';
return p;
}
int scanner()
{
for(n=0;n<8;n++) token[n]='\0';
m=0;
n=0;
ch=prog[p++];
while(ch' ') ch=prog[p++];
if(isalpha(ch)) //标识符
{
while(isalpha(ch)||isdigit(ch))
{
token[m++]=ch;ch=prog[p++];
}
token[m++]='\0';
syn=10;
p--;
}
else if(isdigit(ch))
{
while(isdigit(ch))
{
token[m++]=ch;
ch=prog[p++];
}
token[m++]='\0';
syn=11;
p--;
}
else switch(ch)
{
case '+': token[m++]=ch;syn=13;break;
case '-': token[m++]=ch;syn=14;break;
case '': token[m++]=ch;syn=15;break;
case '/': token[m++]=ch;syn=16;break;
case '$': token[m++]=ch;syn=-1;break;
case '#': syn=0;token[m++]='#';break;
default: printf("Unknown Character in Expression,Only Support ID/Number/+/-///\n");
exit(-1);
}
return 0;
}
int f(char op)
{
switch(op)
{
case '':return 5;
case '/':return 5;
case '+':return 3;
case '-':return 3;
case '$':return 0;
default:return -1;
}
}
int g(char op)
{
switch(op)
{
case '':return 4;
case '/':return 4;
case '+':return 2;
case '-':return 2;
case '$':return 0;
default:return -1;
}
}
int compare(char op)
{
if(f(top_ostack())<g(op)||f(top_ostack())g(op))
push_ostack(op);
else if(f(top_ostack())>g(op))
{
while(top_ostack()!='$'||ch!='$') //两者均为$时不用处理
{
if(f(top_ostack())<=g(op))
{
push_ostack(op);
return 0;
}
else if(f(top_ostack())>g(op))
middle(top_ostack());
}
}
return 0;
}
void middle(char op) //产生中间代码
{
char* result=newtemp();
char v2[8],v1[8];
pop_vstack(v2);
pop_vstack(v1);
op=pop_ostack();
if(op'+'||op'-'||op'*'||op'/')
{
printf("%s = %s %c %s\n",result,v1,op,v2);
push_vstack(result);
}
}
void push_ostack(char op)
{
ostack.top++;
ostack.stack[ostack.top]=op;
}
char pop_ostack()
{
char c;
if(ostack.top>=0)
{
c=ostack.stack[ostack.top];
ostack.top--;
return c;
}
else
{
printf("Operator Stack Empty!Error in Pop Operation!\n");
exit(-2);
}
}
char top_ostack()
{
if(ostack.top>=0)
return ostack.stack[ostack.top];
else
{
printf("Operator Stack Empty!Error in Top Operation!\n");
exit(-2);
}
}
void push_vstack(char *v)
{
vstack.top++;
strcpy(vstack.stack[vstack.top],v);
}
void pop_vstack(char *v)
{
if(vstack.top>=0)
{
strcpy(v,vstack.stack[vstack.top]);
vstack.top--;
}
else
{
printf("Value Stack Empty!Error in Pop Operation!\n");
exit(-4);
}
}
void top_vstack(char *v)
{
if(vstack.top>=0)
strcpy(v,vstack.stack[vstack.top]);
else
{
printf("Value Stack Empty!Error in Top Operation!\n");
exit(-5);
}
}
int main()
{
char end='$';
int p_input;
freopen("sf.in","r",stdin);
gets(prog);
p_input=0;
scanner();
vstack.top=-1;
ostack.top=-1;
push_ostack(end);
while(syn!=0)
{
if(syn==10||syn==11)
{
push_vstack(token);
scanner();
}
else if(syn==15||syn==16||syn==13||syn==14||syn==-1)
{
compare(token[0]);
scanner();
}
else
scanner();
}
printf("Success\n");
}
诶呀,你能不能全部代码都像最后那一段那样选择插入代码,粘贴复制过来格式全部没了,看不了
直接粘贴过去的😂
@留侯君: 你解决问题了?这么快结贴
找到了
– 留侯君 4年前