|
|
#2
xichong2010-06-27 18:24
错误太多了,好多变量的值都没有接收到和传回去,引用和指针的使用好多都错了。我有个C语言版的,算法一模一样,VC++6.0下运行通过!
程序代码:#define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #include <stdio.h> #include <stdlib.h> typedef struct { char *base; char *top; int stacksize; }OPTR,*StackOPTR;//运算符栈 typedef struct { char *base; char *top; int stacksize; }OPND,*StackOPND;//操作数栈 void initialOPTR(StackOPTR s) { s->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char)); if(!s->base) exit(0); s->top=s->base; s->stacksize=STACK_INIT_SIZE; } void initialOPND(StackOPND s) { s->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char)); if(!s->base) exit(0); s->top=s->base; s->stacksize=STACK_INIT_SIZE; } void PushOPTR(StackOPTR s,char e) { if(s->top-s->base>=s->stacksize) { s->base=(char *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(char)); if(!s->base) exit(0); s->top=s->base+s->stacksize; s->stacksize+=STACKINCREMENT; } *(s->top)++=e; } void PushOPND(StackOPND s,char e) { if(s->top-s->base>=s->stacksize) { s->base=(char *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(char)); if(!s->base) exit(0); s->top=s->base+s->stacksize; s->stacksize+=STACKINCREMENT; } *(s->top)++=e; } void PopOPTR(StackOPTR s,char *e) { if(s->top==s->base) exit(0); *e=*--(s->top); } void PopOPND(StackOPND s,char *e) { if(s->top==s->base) exit(0); *e=*--(s->top); } char GetTopOPTR(StackOPTR s) { char e; if(s->top==s->base) exit(0); e=*(s->top-1); return e; } char GetTopOPND(StackOPND s) { char e; if(s->top==s->base) exit(0); e=*(s->top-1); return e; } int In(char c)//判断输入的字符是否为运算符 { if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#') return(1); else return(0); } //---------------------------------------------------------- char Precede(char op,char c) { char ch; if(op=='+'||op=='-') switch(c) { case '+': case '-': case ')': case '#':ch ='>';break; case '*': case '/': case '(':ch ='<';break; } else if(op=='*'||op=='/') switch(c) { case '+': case '-': case '*': case '/': case ')': case '#':ch ='>';break; case '(':ch ='<';break; } else if(op=='(') switch(c) { case '+': case '-': case '*': case '/': case '(':ch='<';break; case ')':ch='=';break; case '#':printf("Error!\n");exit(0);//--------------------------------------------- } else if(op==')') switch(c) { case '+': case '-': case '*': case '/': case ')': case '#':ch='>';break; case '(':printf("Error!\n");exit(0); } else if(op=='#') switch(c) { case '+': case '-': case '*': case '/': case '(':ch ='<';break; case '#':ch='=';break; case ')':printf("Error!\n");exit(0); } return ch; } char operate(char c1,char theta,char c2)//仅操作0-9内的数之间的运算与ASCIII转换 { char answer; int m,n,result; m=c1-48; n=c2-48; switch(theta) { case '+':result=m+n;break; case '-':result=m-n;break; case '*':result=m*n;break; case '/':result=m/n;break; } answer=result+48; return answer; } int EvaluateExpression() { OPTR optr; OPND opnd; char c,theta,a,b; initialOPTR(&optr); initialOPND(&opnd); PushOPTR(&optr,'#'); printf("请输入你要计算的算术表达式,并以#表示结束:\n"); c=getchar(); while(c!='#'||GetTopOPTR(&optr)!='#') { if(!In(c)) { PushOPND(&opnd,c); c=getchar(); } else switch(Precede(GetTopOPTR(&optr),c)) { case '<': PushOPTR(&optr,c);c=getchar(); break; case '=': PopOPTR(&optr,&c);//弹出左括号 c=getchar(); break; case '>': PopOPTR(&optr,&theta);PopOPND(&opnd,&b);PopOPND(&opnd,&a);PushOPND(&opnd,operate(a,theta,b)); break; } } return(GetTopOPND(&opnd)); } void main() { char result; result=EvaluateExpression(); printf("该算术表达式的结果为%c\n",result); } |
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define stackinitsize 100
struct sqstack
{ char *base;
char *top;
char stacksize;};
initstack(struct sqstack &s)
{ s.base=(char*)malloc(stackinitsize*sizeof(char));
s.top=s.base;
s.stacksize=stackinitsize;return 1;
}
push1(struct sqstack &s,char c)
{*s.top=c;
s.top++;
return c;}
push2(struct sqstack &s,int p)
{*s.top=p;
s.top++;
return p;}
gettop1(sqstack s,char c)
{if(s.top==s.base)return 0;
c=*(s.top-1);
return c;
}
gettop2(sqstack s,int p)
{if(s.top==s.base)return 0;
p=*(s.top-1);
return p;
}
pop1(struct sqstack &s,char c)
{if(s.base==s.top)return 0;
else c=*--s.top;
return c;}
pop2(struct sqstack &s,int p)
{if(s.base==s.top)return 0;
else p=*--s.top;
return p;}
char precede(char t1,char t2)
{ char f;
switch(t2)
{case'+':
if(t1=='('||t1=='#')
f='<';
else f='>';
case'-':
if(t1=='('||t1=='#')
f='<';
else f='>';
break;
case'*':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
case'/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
case'(':
if(t1==')')
{printf("error\n");}
case')':
if(t1=='#')
{printf("error\n");};
if(t1=='(')f='=';
else f='>';
case'#':
if(t1=='(')printf("error\n");
else if(t1=='#')f='=';
else f='>';
}return f;
}
in(char c)
{
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':return 1;
}
}
int operate(int a,char theta,int b)
{int c;
switch(theta)
{case'+':c=a+b;break;
case'-':c=a-b;break;
case'*':c=a*b;break;
case'/':c=a/b;break;}
return c;}
main()
{char c;
sqstack optr,opnd;
int p;
int a;
int b;
char theta;
char t1;
initstack(optr);
initstack(opnd);
printf("input expressions end with '#'\n");
push1(optr,'#');
c=getchar();
while(c!='#'||gettop1(optr,c)!='#')
{if(!in(c)){
push2(opnd,p);
c=getchar();}
else
switch(precede(t1,c))
{case'<':push1(optr,c);
c=getchar();
break;
case'=':pop1(optr,c);
c=getchar();
break;
case'>':pop1(optr,theta);
pop2(opnd,b);
pop2(opnd,a);
push2(opnd,operate(a,theta,b));
break;}
}
gettop2(opnd,p);
printf("%d",p);
}
程序代码: