主题:高手请进
小弟编了一个程序,但是运行不到正确的结果,不知道怎样改
#include<stdio.h>
#include<iostream.h>
#include<malloc.h>
#define STACK_INIT_SIZE1 40
#define STACK_INIT_SIZE2 20
#define STACKINITCRENT1 40
#define STACKINITCRENT2 20
#define OK 1
#define ERROR 0
typedef struct{
float *base;
float *top;
int stacksize;
}s_stack;
typedef struct{
char *base;
char *top;
int stacksize;
}f_stack;
float s_Gettop(s_stack OPND,float &e)
{if(OPND.top=OPND.base)return ERROR;
e=*(OPND.top-1);
return OK;};
char f_Gettop(f_stack OPTR,char &e)
{if(OPTR.top=OPTR.base)return ERROR;
e=*(OPTR.top-1);
return OK;};
float s_push(s_stack &OPND,float e){
if(OPND.top-OPND.base>=OPND.stacksize){
OPND.base=(float *)realloc(OPND.base,(OPND.stacksize+STACKINITCRENT1)*sizeof(float));
if(!OPND.base)return ERROR;
OPND.top=OPND.base+OPND.stacksize;};
*OPND.top++=e;
;return OK;};
char f_push(f_stack &OPTR,char e){
if(OPTR.top-OPTR.base>=OPTR.stacksize){
OPTR.base=(char *)realloc(OPTR.base,(OPTR.stacksize+STACKINITCRENT2)*sizeof(char));
if(!OPTR.base)return ERROR;
OPTR.top=OPTR.base+OPTR.stacksize;};
*OPTR.top++=e;
;return OK;};
float s_Pop(s_stack &OPND,float &e){
if(OPND.base==OPND.top)return ERROR;
e=*--OPND.top;return OK;};
char f_Pop(f_stack &OPTR,char &e){
if(OPTR.base==OPTR.top)return ERROR;
e=*--OPTR.top;return OK;};
float operate(float x,char y,float z)
{ switch(y)
{
case '+': return x+z;
case '-': return x-z;
case '*': return x*z;
case '/': return x/z;
};
};
int in(char c)
{
switch(c)
{
case '+': return 0;
case '-': return 1;
case '*': return 2;
case '/': return 3;
case '(': return 4;
case ')': return 5;
case '#':return 6;
default:return 9;
}
};
char Precede(char c1,char c2)
{int c_temp1,c_temp2;
switch(c1)
{case'*':
case'/':c_temp1=4;break;
case'+':
case'-':c_temp1=2;break;
case'(':c_temp1=0;break;
case')':c_temp1=5;break;
case'#':c_temp1=-1;break;
default: ERROR;}
switch(c2)
{case'*':
case'/':c_temp2=3;break;
case'+':
case'-':c_temp2=1;break;
case'(':c_temp2=5;break;
case')':c_temp2=0;break;
case'#':c_temp2=-1;break;
default: ERROR;
}
if(c_temp1<c_temp2)return('<');
if(c_temp1=c_temp2)return('=');
if(c_temp1>c_temp2)return('>');
};
float EvalluateExpression(){
s_stack OPND;
f_stack OPTR;
char c,e,theta;
float a,b,d;
OPTR.base=(char *)malloc(STACK_INIT_SIZE2*sizeof(char));
if(!OPTR.base)return ERROR;
OPTR.top=OPTR.base;
OPTR.stacksize=STACK_INIT_SIZE2;
return OK;f_push(OPTR,'#');
OPND.base=(float *)malloc(STACK_INIT_SIZE1*sizeof(float));
if(!OPND.base)return ERROR;
OPND.top=OPND.base;
OPND.stacksize=STACK_INIT_SIZE1;
return OK;c=getchar();
while(c!='#'||f_Gettop(OPTR,c)!='#'){
if(!in(c)){s_push(OPND,c);c=getchar();}
else
switch(Precede(f_Gettop(OPTR,c),c)){
case'<':f_push(OPTR,c);c=getchar();
break;
case'=':f_Pop(OPTR,e);c=getchar();
break;
case'>':f_Pop(OPTR,theta);
s_Pop(OPND,b);s_Pop(OPND,a);
s_push(OPND,operate(a,theta,b));
break;
}
}return s_Gettop(OPND,d);
};
void main()
{char c,y;
float e,x,z;
cout<<"请输入运算表达式:";
c=getchar(c);
float EvalluateExpression(c);
cout<<"最后结果是:";
e=operate(x,y,z);
cout<<e;
};
请高手指教
#include<stdio.h>
#include<iostream.h>
#include<malloc.h>
#define STACK_INIT_SIZE1 40
#define STACK_INIT_SIZE2 20
#define STACKINITCRENT1 40
#define STACKINITCRENT2 20
#define OK 1
#define ERROR 0
typedef struct{
float *base;
float *top;
int stacksize;
}s_stack;
typedef struct{
char *base;
char *top;
int stacksize;
}f_stack;
float s_Gettop(s_stack OPND,float &e)
{if(OPND.top=OPND.base)return ERROR;
e=*(OPND.top-1);
return OK;};
char f_Gettop(f_stack OPTR,char &e)
{if(OPTR.top=OPTR.base)return ERROR;
e=*(OPTR.top-1);
return OK;};
float s_push(s_stack &OPND,float e){
if(OPND.top-OPND.base>=OPND.stacksize){
OPND.base=(float *)realloc(OPND.base,(OPND.stacksize+STACKINITCRENT1)*sizeof(float));
if(!OPND.base)return ERROR;
OPND.top=OPND.base+OPND.stacksize;};
*OPND.top++=e;
;return OK;};
char f_push(f_stack &OPTR,char e){
if(OPTR.top-OPTR.base>=OPTR.stacksize){
OPTR.base=(char *)realloc(OPTR.base,(OPTR.stacksize+STACKINITCRENT2)*sizeof(char));
if(!OPTR.base)return ERROR;
OPTR.top=OPTR.base+OPTR.stacksize;};
*OPTR.top++=e;
;return OK;};
float s_Pop(s_stack &OPND,float &e){
if(OPND.base==OPND.top)return ERROR;
e=*--OPND.top;return OK;};
char f_Pop(f_stack &OPTR,char &e){
if(OPTR.base==OPTR.top)return ERROR;
e=*--OPTR.top;return OK;};
float operate(float x,char y,float z)
{ switch(y)
{
case '+': return x+z;
case '-': return x-z;
case '*': return x*z;
case '/': return x/z;
};
};
int in(char c)
{
switch(c)
{
case '+': return 0;
case '-': return 1;
case '*': return 2;
case '/': return 3;
case '(': return 4;
case ')': return 5;
case '#':return 6;
default:return 9;
}
};
char Precede(char c1,char c2)
{int c_temp1,c_temp2;
switch(c1)
{case'*':
case'/':c_temp1=4;break;
case'+':
case'-':c_temp1=2;break;
case'(':c_temp1=0;break;
case')':c_temp1=5;break;
case'#':c_temp1=-1;break;
default: ERROR;}
switch(c2)
{case'*':
case'/':c_temp2=3;break;
case'+':
case'-':c_temp2=1;break;
case'(':c_temp2=5;break;
case')':c_temp2=0;break;
case'#':c_temp2=-1;break;
default: ERROR;
}
if(c_temp1<c_temp2)return('<');
if(c_temp1=c_temp2)return('=');
if(c_temp1>c_temp2)return('>');
};
float EvalluateExpression(){
s_stack OPND;
f_stack OPTR;
char c,e,theta;
float a,b,d;
OPTR.base=(char *)malloc(STACK_INIT_SIZE2*sizeof(char));
if(!OPTR.base)return ERROR;
OPTR.top=OPTR.base;
OPTR.stacksize=STACK_INIT_SIZE2;
return OK;f_push(OPTR,'#');
OPND.base=(float *)malloc(STACK_INIT_SIZE1*sizeof(float));
if(!OPND.base)return ERROR;
OPND.top=OPND.base;
OPND.stacksize=STACK_INIT_SIZE1;
return OK;c=getchar();
while(c!='#'||f_Gettop(OPTR,c)!='#'){
if(!in(c)){s_push(OPND,c);c=getchar();}
else
switch(Precede(f_Gettop(OPTR,c),c)){
case'<':f_push(OPTR,c);c=getchar();
break;
case'=':f_Pop(OPTR,e);c=getchar();
break;
case'>':f_Pop(OPTR,theta);
s_Pop(OPND,b);s_Pop(OPND,a);
s_push(OPND,operate(a,theta,b));
break;
}
}return s_Gettop(OPND,d);
};
void main()
{char c,y;
float e,x,z;
cout<<"请输入运算表达式:";
c=getchar(c);
float EvalluateExpression(c);
cout<<"最后结果是:";
e=operate(x,y,z);
cout<<e;
};
请高手指教