| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
ADSL如何秒变专线,公网IP盒子了解一下千里之行 始于足下
共有 661 人关注过本帖
标题:[求助]帮我看下哪里错了
只看楼主 加入收藏
jojoba
Rank: 1
等 级:新手上路
帖 子:178
专家分:0
注 册:2005-11-29
  问题点数:0  回复次数:1   
[求助]帮我看下哪里错了

如题
程序如下
# include <stdio.h>
# include <malloc.h>
# include <iostream.h>
# include <conio.h>

# define STACK_INIT_SIZE 100
# define STACKINCREMENT 10

typedef struct
{ char *base;
char *top;
char stacksize;
}SqStack;

char InitStack(SqStack &S)
{ S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base)
{ printf("Allocate space failure !");
return 0;
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return 1;
}

char Push(SqStack &S,char e)
{ if(S.top-S.base>S.stacksize)
{ S.base=(char *)realloc(S.base,(S.stacksize+
STACKINCREMENT*sizeof(char)));
if(!S.base)
{ printf("Overflow!\n");
return 0;
};
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
};
*S.top++=e;
return 1;
};

char Pop(SqStack &S,char &e)
{ if(S.top==S.base)
{ printf("\nIt's a empty SqStack!");
};
e=*--S.top;
return e;
};
char GetTop(SqStack S,char &e)
{ if(S.top==S.base)
{ printf("It's a empty SqStack !");
};
e=*(S.top-1);
return e;
};

char OP[10]={'+','-','*','/','(',')','#'};
int precede[7][7]={
1,1,2,2,2,1,1,
1,1,2,2,2,1,1,
1,1,1,1,2,1,1,
1,1,1,1,2,1,1,
2,2,2,2,2,3,0,
1,1,1,1,0,1,1,
2,2,2,2,2,0,3};

int In(char c,char *op)
{
int i=0;
while(i<7)
if(c==op[i++])
return 1;
return 0;
}

char Precede(char op,char c)
{
int pos_op;
int pos_c;
int i;
for(i=0;i<7;i++)
{
if(op==OP[i]) pos_op=i;
if(c==OP[i]) pos_c=i;
};
switch(precede[pos_op][pos_c])
{
case 1: return '<';
case 2: return '>';
case 3: return '=';
} return 1;
}

char Operate(int a,char theta,int b)
{
switch(theta)
{
case '+':return a+b;break;
case '-':return a-b;break;
case '*':return a*b;break;
case '/':return a/b;break;
} return 1;
}

int EvaluateExpression()
{
char z[20];
char c,x,theta;
char a,b,e;
int k;
SqStack OPTR,OPND;
InitStack(OPTR); Push(OPTR,'#');
InitStack(OPND);
printf("please input your character:\n");
for(int i=0;z[i-1]!='#';i++)
scanf("%c",&z[i]);
printf("the char has been into the stack");

for( i=0;z[i-1]!='#';i++)
{
if(!In(z[i],OP))
Push(OPND,z[i]);
switch(Precede(GetTop(OPTR,e),z[i]))
{
case '<':
Push(OPTR,z[i]);
printf("The character %c has been in the stack.",z[i]);
break;
case'=':
Pop(OPTR,x);
break;
case'>':
Pop(OPTR,theta);
Pop(OPND,b); Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
printf("the character %c has been into the stack",&c);
break;
}
}k=GetTop(OPND,e);
return k;
}
void main()
{
int sum;
printf("Only within 0..9 evaluation,input a expression end with symbol #:\n");
sum=EvaluateExpression();
printf("This expression's result is: ");
printf("%d\n",sum);
}
问题是内存不能读取

搜索更多相关主题的帖子: char base include STACK INIT 
2005-11-29 20:22
jojoba
Rank: 1
等 级:新手上路
帖 子:178
专家分:0
注 册:2005-11-29
  得分:0 
请高手快救命啊
在线等啊
2005-11-29 20:27







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020059 second(s), 7 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved