无效内存引用,谁有功能强点的软件帮忙指点一下,实在没辙了........(.从中缀向后缀转换表达式)
#include<stdio.h>#define Size 100
char str[1000];
char c;
int i;
typedef struct node{
int top;
char no[Size];
} SqStack;
InitStack (SqStack *S) {
S->top = -1;
}
char GetTop (SqStack *S) {
char c;
if(S->top != -1)
c=S->no[S->top];
return (c);
}
Push (SqStack *S, char c) {
if(S->top==Size-1)
printf("error\n");
else {
S->top++;
S->no[S->top]=c;
}
}
Pop (SqStack *S, char *c) {
if(S->top == -1) printf("error\n");
else {
*c=S->no[S->top];S->top--;
}
}
In(char c) {
switch(c) {
case'(':
case')':
case'*':
case'+':
case'-':
case'/':
case'#': return 0;break;
default: return 1;break;
}
}
char Precede(char t1,char t2,SqStack *S)
{char e,f;
switch(t1)
{
case'+':
case '-':if(t2=='*'||t2=='/'||t2=='('||t2=='-'||t2=='+')
f='<';
else
f='>';
break;
case '*':
case '/':if(t2=='(')
f='<';
else
f='>';
break;
case'(': f='<'; break;
case')': e=GetTop(S);
while(e!='(') {
Pop(S,e);
printf("%c",e);
e=GetTop(S);
f='a';
}
break;
case'#': e=GetTop(S);
while(e!='#') {
Pop(S,e);
printf("%c",e);
e=GetTop(S);
f='a';
}
break;
}
return f;
}
void SWITCH (char f,SqStack *S)
{ char e;
if(f=='<') { printf("%c",Pop(S,e));
SWITCH (Precede(c,GetTop(S),S),S);
f='a';
}
if(f=='=') { Pop(S,e); c=str[i++]; f='a';}
if(f=='>') { Push(S,c);
c=str[i++]; f='a';}
}
main()
{ int k,N,num=0;
char e;
SqStack *S;
InitStack(S);
scanf("%d",&N);
getchar();
for(k=0;k<N;k++) {
Push(S,'#');
gets(str);
i=0;
c=str[i++];
while(c!='#'||GetTop(S)!='#') {
if(In(c)) {
num=c-'0';
c=str[i++];
while(In(c)) {
num=num*10+c-'0';
c=str[i++];
}
}
else {
if(num!=0)
printf("%d ",num);
SWITCH (Precede(c,GetTop(S),S),S);
}
}
printf("\n");
}
}
[[it] 本帖最后由 20072815 于 2008-10-31 19:33 编辑 [/it]]