x特遣队迅雷1080:编程高手进啊!!急!!我明天要用!!

来源:百度文库 编辑:高考问答 时间:2024/03/29 02:28:24
#include<stdio.h>
#include"conio.h"
#include"stdlib.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef char selemtype;
typedef int status;
#define ok 1
#define OVERFLOW 0
#define error 0
typedef struct{
selemtype *base;
selemtype *top;
int stacksize;
}sqstack;
status initstack(sqstack &s)
{
s.base=(selemtype*)malloc(STACK_INIT_SIZE*sizeof(selemtype));
if(!s.base)exit(OVERFLOW);
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
return ok;
}
selemtype getop(sqstack s)
{
selemtype e;
if(s.top==s.base) return error;
e=*(s.top-1);
return e;
}
status push(sqstack &s,selemtype e)
{
if (s.top-s.base>=s.stacksize)
{
s.base=(selemtype*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(selemtype));
if(!s.base)exit (OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=STACKINCREMENT;
}
*s.top++=e;
return ok;
}
status pop(sqstack &s,selemtype &e)
{
if(s.top==s.base) return error;
e=*--s.top;
return ok;
}
char precede(char e,char c)
{
if(e=='+')
{
if(c=='+'||c=='-'||c==')'||c=='#')
return '>';
return'<';
}
else if (e=='-')
{
if(c=='+'||c=='-'||c==')'||c=='#')
return '>';
return '<';
}
else if (e=='*')
{
if(c=='(')
return '<';
return '>';
}
else if(e=='/')
{
if(c=='(')
return '<';
return '>';
}
else if(e=='(')
{
if(c==')')
return'=';
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
}
else if(e==')')
{
if (c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
}
else if(e=='#')
{
if(c=='#')
return '=';
else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='(')
return '<';
}
return 0;
}
int operate(char b,char theta,char a)
{
int temp;
if(theta=='+')
temp=b+a;
else if(theta=='*')
temp=b*a;
else if(theta=='-')
temp=b-a;
else if(theta=='/')
temp=b/a;
return temp;
}
char op[]={'+','-','*','/','(',')','#'};
int in(char c,char op[])
{
int i;
for(i=0;i<=6;++i)
if(c==op[i])
return 1;
return 0;
}
char evluateexpression()
{
sqstack optr,opnd;
char a,b,c,x,theta;
initstack(optr);
push(optr,'#');
initstack(opnd);
c=getchar();
while(c!='#'||getop(optr)!='#')
{
if(!in(c,op))
{
push(opnd,c-48);
c=getchar();
}
else
switch(precede(getop(optr),c))
{
case'<':
push(optr,c);
c=getchar();
break;
case'=':
pop(optr,c);
c=getchar();
break;
case'>':
pop(optr,theta);
pop(opnd,b);
pop(opnd,a);
push(opnd,operate(a,theta,b));
break;
}
}
return getop(opnd);
}
void main()
{
char temp;
temp=evluateexpression();
printf("%d\n",temp);
return ;
}
这是一个表达式求值!!但是不能计算象34*5+(86-7)之类的运算 也就有两位的 大于10以上的数据运算
拜托拉!!!小弟我明天要用 .请帮我改一下 使他可以进行10以上的运算 谢谢!!

你把你程序的结构里好来
就很容易的找出问题