有关从中缀向后缀转换表达式的问题?无效内存引用?谁可以帮帮忙?
#define Size 500#include<stdio.h>
char str[500];
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=='+')
f='<';
else
f='>';
break;
case '*':
case '/':if(t2=='#'||t2=='('||t2=='+'||t2=='-')
f='>';
else
f='<';
break;
case'(': f='>'; break;
case')': e=GetTop(S);
c=str[i++];
while(e!='(') {
Pop(S,e);
printf(" %c",e);
e=GetTop(S);
f='a';
}
if(e=='(') Pop(S,e);
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) { //相当与switch,只是需要一个递归,所以写成了子函数
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,flag;
char e;
SqStack *S;
InitStack(S);
scanf("%d",&N);
getchar();
for(k=0;k<N;k++) {
Push(S,'#');
gets(str);
i=flag=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) {
if(flag==0)
{printf("%d",num); flag=1;}
else printf(" %d",num);
num=0;
}
SWITCH (Precede(c,GetTop(S),S),S);
}
}
printf("\n");
}
getchar();
}
输入:
3
(1)+2*3#
(1+2*3-4)#
1*2+(3)/4#
输出:
1 2 3 * +
1 2 3 * + 4 -
1 2 * 3 4 / +
编译的结果是出来下面一段:
main.c:11:Bounds error: NULL or ILLEGAL pointer used in component reference.
main.c:11: Pointer value: NULL
[[it] 本帖最后由 20072815 于 2008-10-19 16:16 编辑 [/it]]