大牛在哪儿啊...
有一个奇怪的问题,我写了一把中缀表示法换为后缀表示法的程序,在Turbo C中能够通过,什么事都没有...
可是一旦拿到devc++上,能编译通过,但不能运行,调试时说是产生了段异常,怎么会这样啊,,,
难道不通的编译器之间还打架不成啊?
下面是代码:
#include "stdio.h "
#define n 14
int top=-1;
char stack[n];
void parsing(char *a);
void push(char ch);
int pop();
int empty();
int priority(char ch);
int isoperator(char ch);
main()
{
char *a= "A+B*(C+D)-E/F ";
printf( "the data before the conversing: ");
printf( "%s\n ",a);
parsing(a);
printf( "the data after the conversing: ");
printf( "%s\n ",a);
system( "pause ");
}
void parsing(char *a)
{
char ch,ch1;
char b[n];
int i,j=0;
for(i=0;i <n-1;i++)
{
ch=a[i];
if(isoperator(ch))
b[j++]=ch;
else
{
if(ch== '( ')
push(ch);
else if(ch== ') ')
{
while(!empty())
{
ch=pop();
if(ch== '( ')
break;
else
b[j++]=ch;
}
}
else
{
if(!empty())
{
do
{
ch1=pop();
if(priority(ch1)> =priority(ch))
{
b[j++]=ch1;
if(empty())
{
push(ch);
break;
}
}
else
{
push(ch1);
push(ch);
break;
}
}while(!empty());
}
else
{
push(ch);
}
}
}
}
while(!empty())
b[j++]=pop();
for(i=0;i <j;i++)
a[i]=b[i];
a[i]= '\0 ';
}
int isoperator(char ch)
{
int p;
switch(ch)
{
case '+ ':
case '- ':
case '( ':
case ') ':
case '* ':
case '/ ':
p=0;
break;
default:
p=1;
break;
}
return p;
}
int empty()
{
int t;
if(top==-1)
t=1;
else
t=0;
return t;
}
void push(char ch)
{
stack[++top]=ch;
}
int pop()
{
int t;
t=stack[top];
stack[top]=0;
top--;
return t;
}
int priority(char ch)
{
int t;
switch(ch)
{
case '+ ':
case '- ':
t=1;
break;
case '* ':
case '/ ':
t=2;
break;
default:
t=0;
break;
}
return t;
}
[解决办法]
第一个问题:
void parsing(char *a)
{
char ch,ch1;
char b[n]; //n 是常量吗?如果不是,就非法“数组的下表必须明确给定大小,不能用变量”
int i,j=0;
for(i=0;i <n-1;i++)