数据结构 表达式求值

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

表达式求值
#include<iostream>
using namespace std;
#define STACK_INIT_SIZE 50
#define SATCKINCREAMENT 10
typedef struct
{
char *base;
char *top;
int stacksize;
}sqstack;
sqstack InitStack()
{
sqstack s;
char *base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if(!base)
{
cout<<"the stack fails to be initalized here"<<endl;
exit(0);
}
s.base = base;
s.top = s.base;
s.stacksize = STACK_INIT_SIZE;
return s;
}
void push(sqstack *s,char c)
{
if(s->top - s->base >= s->stacksize)
{
char *newbase = (char *)realloc(s->base, (SATCKINCREAMENT + s->stacksize) * sizeof(char));
if(!newbase)
{
cout<<"the stack fails to be recreated here"<<endl;
exit(0);
}
s->base = newbase;
s->top = s->base + s->stacksize;
s->stacksize += SATCKINCREAMENT;
}
*(s->top ++) = c;
}
char gettop(sqstack s)
{
char c;
if(s.top == s.base)
return 0;
c = *(s.top - 1);
return c;
}
char pop(sqstack *s)
{
char c;
if(s->top == s->base)
{
return 0;
}
c = *(--s->top);
return c;
}
char compare(sqstack optr,char optr2)
{
char optr1 = gettop(optr); //取得栈顶的操作符与当前输入的操作符进行比较
char a[8][8] = {
{'!','+' , '-', '*', '/', '(', ')', '#'},
{'+','>' , '>', '<', '<', '<', '>', '>' },
{'-','>' , '>', '<', '<', '<', '>', '>' },
{'*','>' , '>', '>', '>', '<', '>', '>' },
{'/','>' , '>', '>', '>', '<', '>', '>' },
{'(','<' , '<', '<', '<', '<', '=', '!'},
{')','>' , '>', '>', '>', '!', '>', '>' },
{'#','<' , '<', '<', '<', '<', '!', '='} };
for(int i = 0; i<8; i ++)
for(int j = 0; j<8; j++)
{
if(a[i][0] == optr1 && a[0][j] == optr2)
{
return a[i][j];
}
}
return 0;
}
int operate(int a,char op, int b)
{
int sum = 0;
switch(op)
{
case '+': sum = a + b; break;
case '-': sum = a - b; break;
case '*': sum = a * b; break;
case '/': sum = a / b; break;
}
return sum;
}
void express_handle()
{
sqstack optr = InitStack(); //操作符栈
sqstack opnd = InitStack(); //操作数栈
push(&optr,'#');
cout<<"please cin the expression:"<<endl;
char c;
char sum = 0;
cin>>c;
while(c != '#' || gettop(optr) != '#')
{
if(c>='0' && c<='9')
{
push(&opnd,c - '0');
cin>>c;
}
else
{
switch(compare(optr,c))
{
case '<':
push(&optr,c);
cin>>c;
break;
case '=':
pop(&optr);
cin>>c;
break;
case '>':
char op = pop(&optr);
int b =pop(&opnd);
int a =pop(&opnd);
sum = operate(a,op,b);
push(&opnd,sum); //此处先要将sum转换为一个char类型的然后使其进栈
break;
}
}
}
cout<<"the result is: ";
cout<<(*(--opnd.top) - 0)<<endl;
}
void main()
{
/*
sqstack optr = InitStack(); //操作符栈
sqstack opnd = InitStack(); //操作数栈push(&optr,'#');
cout<<compare(optr,'*')<<endl;
*/
express_handle();
/* sqstack optr = InitStack(); //操作符栈sqstack opnd = InitStack(); //操作数栈for(int i = 0; i < 5; i++)
{
push(&opnd,i);
}
for(int j = 0; j < 5; j++)
{
cout<<(*(--opnd.top) - 0)<<endl;
}
*/
/*
int num = 0;
push(&opnd,4);
push(&opnd,4);
push(&opnd,4);
while(opnd.top >= opnd.base)
{
cout<<"element:"<<(int)(*--opnd.top)<<endl;
}
*/
}。

相关文档
最新文档