主题:谁能帮我修改一下程序 中缀表达式求值
#include<stdio.h>
#include <ctype.h>
#include <string.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
#define elemtype char
#define selemtype int
#define volume 20
int operate(char a,char ch,char b);
typedef struct {
elemtype *base;
elemtype *top;
int stacksize;
}eqstack;
typedef struct {
selemtype *base;
selemtype *top;
int stacksize;
}sqstack;
eqstack optr;
sqstack opnd;
void initstack(sqstack &s)
{ s.base=(selemtype *)malloc(volume*sizeof(selemtype));
if(!s.base)exit(1);
s.top=s.base;
s.stacksize=volume;
}
void initstack(eqstack &s)
{ s.base=(elemtype *)malloc(volume*sizeof(elemtype));
if(!s.base)exit(1);
s.top=s.base;
s.stacksize=volume;
}
selemtype gettop(sqstack s)
{ if(s.top!=s.base)return *(s.top-1);
}
elemtype gettop(eqstack s)
{ if(s.top!=s.base)return *(s.top-1);
}
void push(sqstack &s,selemtype e)
{ if(s.top-s.base>=s.stacksize){
s.base=(selemtype *)realloc(s.base,(s.stacksize+volume)*sizeof(selemtype));
if(!s.base)exit(1);
s.top=s.base+s.stacksize;
s.stacksize+=volume;
}
*s.top=e;
s.top++;
}
void push(eqstack &s,elemtype e)
{ if(s.top-s.base>=s.stacksize){
s.base=(elemtype *)realloc(s.base,(s.stacksize+volume)*sizeof(elemtype));
if(!s.base)exit(1);
s.top=s.base+s.stacksize;
s.stacksize+=volume;
}
*s.top=e;
s.top++;
}
void pop(sqstack &s,selemtype &e){
if(s.top!=s.base){
s.top--;
e=*s.top;}
}
void pop(eqstack &s,elemtype &e){
if(s.top!=s.base){
s.top--;
e=*s.top;}
}
int precede(char a,char c)
{
if((a=='+'||a=='-'||a=='*')&&(c=='+'||c=='-'))return 1;//a的优先级大于c
if((a=='+'||a=='-')&&(c=='*'||c=='/'||c=='('))return -1;//a的优先级小于c
if((a=='+'||a=='-'||a=='*'||a=='/')&&(c==')'||c=='#'))return 1;//a的优先级大于c
if((a=='*')&&(c=='*'||c=='/'))return 1;//a的优先级大于c
if((a=='/'||a==')')&&(c=='+'||c=='-'||c=='*'||c=='/'))return 1;//a的优先级大于c
if((a=='('||a=='#')&&(c=='+'||c=='-'||c=='*'||c=='/'))return -1;//a的优先级小于c
if((a=='+'||a=='/'||a=='('||a=='#')&&(c=='('))return -1;//a的优先级小于c
if((a==')')&&(c==')'||c=='#'))return 1;//a的优先级大于c
if((a=='('&&c==')')||(a=='#'&&c=='#'))return 0;//a的优先级等于c
}
int operandtype()
{ char c,x,theta;
int a,b;
initstack(optr);
push(optr,'#');
initstack(opnd);
c=getchar();
while(c!='#' ){
if(c<='9'&&c>='0'){push(opnd,c-'0');c=getchar();}
else switch(gettop(optr),c){
case -1://栈顶元素优先级低
push(optr,c);c=getchar();break;
case 0://脱括号并接收下一个字符
pop(optr,x);c=getchar();break;
case 1://退栈并将运算结果入栈
pop(optr,theta);
pop(opnd,a);
pop(opnd,b);
push(opnd,operate(a,theta,b));break;
}
}
return gettop(opnd);
}
int operate(char a,char ch,char b)
{
int s;
switch(ch){
case '+': s=a+b;break;
case '-': s=a-b;break;
case '*': s=a*b;break;
case '/': s=a/b;break;
default:break;
}
return s;
}
void main()
{int s;
s=operandtype();
}
#include <ctype.h>
#include <string.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
#define elemtype char
#define selemtype int
#define volume 20
int operate(char a,char ch,char b);
typedef struct {
elemtype *base;
elemtype *top;
int stacksize;
}eqstack;
typedef struct {
selemtype *base;
selemtype *top;
int stacksize;
}sqstack;
eqstack optr;
sqstack opnd;
void initstack(sqstack &s)
{ s.base=(selemtype *)malloc(volume*sizeof(selemtype));
if(!s.base)exit(1);
s.top=s.base;
s.stacksize=volume;
}
void initstack(eqstack &s)
{ s.base=(elemtype *)malloc(volume*sizeof(elemtype));
if(!s.base)exit(1);
s.top=s.base;
s.stacksize=volume;
}
selemtype gettop(sqstack s)
{ if(s.top!=s.base)return *(s.top-1);
}
elemtype gettop(eqstack s)
{ if(s.top!=s.base)return *(s.top-1);
}
void push(sqstack &s,selemtype e)
{ if(s.top-s.base>=s.stacksize){
s.base=(selemtype *)realloc(s.base,(s.stacksize+volume)*sizeof(selemtype));
if(!s.base)exit(1);
s.top=s.base+s.stacksize;
s.stacksize+=volume;
}
*s.top=e;
s.top++;
}
void push(eqstack &s,elemtype e)
{ if(s.top-s.base>=s.stacksize){
s.base=(elemtype *)realloc(s.base,(s.stacksize+volume)*sizeof(elemtype));
if(!s.base)exit(1);
s.top=s.base+s.stacksize;
s.stacksize+=volume;
}
*s.top=e;
s.top++;
}
void pop(sqstack &s,selemtype &e){
if(s.top!=s.base){
s.top--;
e=*s.top;}
}
void pop(eqstack &s,elemtype &e){
if(s.top!=s.base){
s.top--;
e=*s.top;}
}
int precede(char a,char c)
{
if((a=='+'||a=='-'||a=='*')&&(c=='+'||c=='-'))return 1;//a的优先级大于c
if((a=='+'||a=='-')&&(c=='*'||c=='/'||c=='('))return -1;//a的优先级小于c
if((a=='+'||a=='-'||a=='*'||a=='/')&&(c==')'||c=='#'))return 1;//a的优先级大于c
if((a=='*')&&(c=='*'||c=='/'))return 1;//a的优先级大于c
if((a=='/'||a==')')&&(c=='+'||c=='-'||c=='*'||c=='/'))return 1;//a的优先级大于c
if((a=='('||a=='#')&&(c=='+'||c=='-'||c=='*'||c=='/'))return -1;//a的优先级小于c
if((a=='+'||a=='/'||a=='('||a=='#')&&(c=='('))return -1;//a的优先级小于c
if((a==')')&&(c==')'||c=='#'))return 1;//a的优先级大于c
if((a=='('&&c==')')||(a=='#'&&c=='#'))return 0;//a的优先级等于c
}
int operandtype()
{ char c,x,theta;
int a,b;
initstack(optr);
push(optr,'#');
initstack(opnd);
c=getchar();
while(c!='#' ){
if(c<='9'&&c>='0'){push(opnd,c-'0');c=getchar();}
else switch(gettop(optr),c){
case -1://栈顶元素优先级低
push(optr,c);c=getchar();break;
case 0://脱括号并接收下一个字符
pop(optr,x);c=getchar();break;
case 1://退栈并将运算结果入栈
pop(optr,theta);
pop(opnd,a);
pop(opnd,b);
push(opnd,operate(a,theta,b));break;
}
}
return gettop(opnd);
}
int operate(char a,char ch,char b)
{
int s;
switch(ch){
case '+': s=a+b;break;
case '-': s=a-b;break;
case '*': s=a*b;break;
case '/': s=a/b;break;
default:break;
}
return s;
}
void main()
{int s;
s=operandtype();
}