编程论坛
注册
登录
编程论坛
→
数据结构与算法
编译没问题,但无法运行!
烧包谷
发布于 2010-12-05 23:00, 494 次点击
只有本站会员才能查看附件,请
登录
我写了代码后,编译后运行时出现这样的情况,求高手指点!
[
本帖最后由 烧包谷 于 2010-12-5 23:04 编辑
]
4 回复
#2
寒风中的细雨
2010-12-05 23:02
把代码贴出来
#3
烧包谷
2010-12-05 23:07
回复 2楼 寒风中的细雨
程序代码:
#include
<stdio.h>
#include
<malloc.h>
#include
<stdlib.h>
#define
STACK_INIT_SIZE 100
#define
STACKINCREMENT 10
int
y,z;
char
theta;
typedef
struct
{
int
*base;
int
*top;
int
stacksize;
}Sqstack1;
typedef
struct
S2{
char
*base;
char
*top;
int
stacksize;
}Sqstack2;
void
Initstack1(Sqstack1 *S)
{
S->base=(
int
*)malloc(STACK_INIT_SIZE*
sizeof
(
int
));
if
(!S->base)printf(
"
内存分配错误
"
);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void
Initstack2(Sqstack2 *Q)
{
Q->base=(
char
*)malloc(STACK_INIT_SIZE*
sizeof
(
char
));
if
(!Q->base)printf(
"
内存分配错误
"
);
Q->top=Q->base;
Q->stacksize=STACK_INIT_SIZE;
}
void
Push1(Sqstack1 *S,
int
e)
{
if
(S->top-S->base>=S->stacksize)
{
S->base=(
int
*)realloc(S->base,(S->stacksize+STACKINCREMENT)*
sizeof
(
int
));
if
(!S->base)printf(
"
从新分配内存错误
"
);
S->top=S->base+S->stacksize;
}
*S->top++=e;
}
void
Push2(Sqstack2 *Q,
char
e)
{
if
(Q->top-Q->base>=Q->stacksize)
{
Q->base=(
char
*)realloc(Q->base,(Q->stacksize+STACKINCREMENT)*
sizeof
(
char
));
if
(!Q->base)printf(
"
从新分配内存错误
"
);
Q->top=Q->base+Q->stacksize;
}
*Q->top++=e;
}
int
Gettop1(Sqstack1 *S)
{
int
e;
if
(S->top=S->base)printf(
"
栈已经为空,无法执行!
"
);
e=*(S->top-
1
);
return
e;
}
char
Gettop2(Sqstack2 *Q)
{
char
c;
if
(Q->top=Q->base)printf(
"
栈已经为空,无法执行!
"
);
c=*(Q->top-
1
);
return
c;
}
int
Pop1(Sqstack1 *S)
{
int
e;
if
(S->top==S->base)printf(
"
ERROR\n
"
);
e=*--S->top;
return
e;
}
char
Pop2(Sqstack2 *Q)
{
char
c;
if
(Q->top==Q->base)printf(
"
ERROR\n
"
);
c=*--Q->top;
return
c;
}
char
Precede(
char
a1,
char
a2)
{
if
((a1==
'
+
'
||a1==
'
-
'
)&&(a2==
'
+
'
||a2==
'
-
'
||a2==
'
)
'
||a2==
'
#
'
))
return
'
>
'
;
else
if
((a1==
'
+
'
||a1==
'
-
'
)&&(a2==
'
*
'
||a2==
'
/
'
||a2==
'
(
'
||a2==
'
#
'
))
return
'
<
'
;
else
if
((a1==
'
*
'
||a1==
'
/
'
)&&(a2==
'
+
'
||a2==
'
-
'
||a2==
'
*
'
||a2==
'
/
'
||a2==
'
)
'
||a2==
'
#
'
))
return
'
>
'
;
else
if
((a1==
'
*
'
||a1==
'
/
'
)&&a2==
'
(
'
)
return
'
<
'
;
else
if
(a1==
'
(
'
&&(a2==
'
+
'
||a2==
'
-
'
||a2==
'
*
'
||a2==
'
/
'
||a2==
'
(
'
))
return
'
>
'
;
else
if
(a1==
'
(
'
&&a2==
'
)
'
)
return
'
=
'
;
else
if
(a1==
'
)
'
&&(a2==
'
+
'
||a2==
'
-
'
||a2==
'
*
'
||a2==
'
/
'
||a2==
'
)
'
||a2==
'
#
'
))
return
'
>
'
;
else
if
(a1==
'
#
'
&&(a2==
'
+
'
||a2==
'
-
'
||a2==
'
*
'
||a2==
'
/
'
||a2==
'
(
'
))
return
'
<
'
;
else
if
(a1==
'
#
'
&&a2==
'
#
'
)
return
'
=
'
;
else
return
(
0
);
}
Operate(
int
n,
char
c,
int
m)
{
if
(c==
'
+
'
)
return
(n+m);
else
if
(c==
'
-
'
)
return
(n-m);
else
if
(c==
'
*
'
)
return
(n*m);
else
if
(c==
'
/
'
)
return
(n/m);
else
return
(
0
);
}
void
main()
{
Sqstack1 OPND;
Sqstack2 OPTR;
int
a,t=
1
,add=
0
,k=
0
;
Initstack1(&OPND);
Push2(&OPTR,
'
#
'
);
Initstack2(&OPTR);
char
c;
c=getchar();
while
(c!=
'
#
'
||Gettop2(&OPTR)!=
'
#
'
)
{
while
(c>=
'
0
'
&&c<=
'
9
'
)
{ a=c-
48
;
add=add+a*t;
t=t*
10
;
k=
1
;
c=getchar();
};
if
(k==
1
) Push1(&OPND,add);
else
switch
(Precede(Gettop2(&OPTR),c)){
case
'
<
'
:
//
栈顶元素优先权低
Push2(&OPTR,c);
c=getchar();
break
;
case
'
=
'
:
//
脱括号并接受下一字符
Pop2(&OPTR);
c=getchar();
break
;
case
'
>
'
:
//
退栈并将运算结果入栈
theta=Pop2(&OPTR);
y=Pop1(&OPND);z=Pop1(&OPND);
Push1(&OPND,Operate(y,theta,z));
break
;
default
:
break
;
}
printf(
"
***
"
);
}
printf(
"
\n结果是:%d
"
,Gettop1(&OPND));
}
[
本帖最后由 烧包谷 于 2010-12-5 23:14 编辑
]
#4
寒风中的细雨
2010-12-06 12:14
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
int y,z;
char theta;
typedef struct
{
int *base;
int *top;
int stacksize;
}Sqstack;
void Initstack(Sqstack *S)
{
S->base = (int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S->base)
printf("内存分配错误");
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
}
void Push(Sqstack *S,int e)
{
if(S->top-S->base >= S->stacksize)
{
S->base = (int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int));
if(!S->base)
printf("从新分配内存错误");
S->top = S->base+S->stacksize;
}
*S->top++ = e;
}
int Gettop(Sqstack *S)
{
if(S->top == S->base)
printf("栈已经为空,无法执行!");
return *(S->top-1);
}
int Pop(Sqstack *S)
{
if(S->top==S->base)
printf("ERROR\n");
return *--S->top;
}
/*
*若为空则返回1 否则返回0
*/
int Isempty(Sqstack s)
{
if( s.top == s.base )
return 1;
return 0;
}
char Precede(char a1,char a2)//a1 in, a2 out
{
if( (a1=='+'||a1=='-') && (a2=='+'||a2=='-'||a2==')' || a2=='#' ))
return '>';//pop
else if( (a1=='+'||a1=='-') && (a2=='*'||a2=='/'||a2=='('))
return '<';//push
else if((a1=='*'||a1=='/')&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2==')'||a2=='#'))
return '>';//pop
else if((a1=='*'||a1=='/') && a2=='(')
return '<';
else if(a1=='('&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'))
return '
<
';
else if(a1=='('&&a2==')')
return '=';
// else if(a1==')'&&(a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2==')'||a2=='#'))
// return '>';
else if(a1=='#'&& (a2=='+'||a2=='-'||a2=='*'||a2=='/'||a2=='(') )
return '<';
else if(a1=='#' && a2=='#')
return '=';
else
return(0);
}
int Operate(int n,char c,int m)
{
if(c=='+') return(n+m);
else if(c=='-') return(n-m);
else if(c=='*') return(n*m);
else if(c=='/') return(n/m);
else return(0);
}
void main()
{
Sqstack OPND, OPTR;
int a = 0;
Initstack(&OPND);//operand stack
Initstack(&OPTR);//operator stack
Push(&OPTR,'#');
char c;
c = getchar();
// while(c!='#'||Gettop(&OPTR)!='#')
while ( !Isempty(OPTR) )
{
a = 0;//initialize variable a
while(c>='0' && c<='9')
{
a = a*10 + c - '0';
c = getchar();
};
if( 0 != a )
Push(&OPND,a);
else
switch(Precede(Gettop(&OPTR),c))
{
case '<'://栈顶元素优先权低
Push(&OPTR,c);
c=getchar();break;
case '='://脱括号并接受下一字符
Pop(&OPTR);
c=getchar();
break;
case '>'://退栈并将运算结果入栈
theta = Pop(&OPTR);
//y=Pop(&OPND);z=Pop(&OPND);
z = Pop(&OPND); y = Pop(&OPND);
Push(&OPND,Operate(y,theta,z));
break;
default:break;
}
}
printf("\n结果是:%d",Gettop(&OPND));
}
#5
烧包谷
2010-12-06 14:01
回复 4楼 寒风中的细雨
谢谢啦!
1