栈和队列5 顺序栈的应用实例(表达式求值)

#include <stdio.h>

#include <stdlib.h>

#define INITSIZE 100

#define INCREAMENT 10

typedef struct{

char * data;

char * top;

int stacksize;

}OPTRStack;

typedef struct{

int * data;

int * top;

int stacksize;

}OPNDStack;

void InitOPTRStack(OPTRStack * S){

S -> data = (char *)malloc(INITSIZE * sizeof(char));

if(! S -> data) exit(0);

S -> top = S -> data;

S -> stacksize = INITSIZE;

}

void InitOPNDStack(OPNDStack * S){

S -> data = (int *)malloc(INITSIZE * sizeof(int));

if(! S -> data) exit(0);

S -> top = S -> data;

S -> stacksize = INITSIZE;

}

void PushOPTR(OPTRStack * S, char e){

if (S -> top - S -> data >= S -> stacksize){

S -> data = (char *)realloc(S -> data, (INITSIZE + INCREAMENT) * sizeof(char));

if(! S -> data) exit(0);

S -> top = S -> data;

S -> stacksize = INITSIZE + INCREAMENT;

}

* S -> top ++ = e;

}

void PushOPND(OPNDStack * S, int e){

if (S -> top - S -> data >= S -> stacksize){

S -> data = (int *)realloc(S -> data, (INITSIZE + INCREAMENT) * sizeof(int));

if(! S -> data) exit(0);

S -> top = S -> data;

S -> stacksize = INITSIZE + INCREAMENT;

}

* S -> top ++ = e;

}

void PopOPTR(OPTRStack * S, char * e){

if (S -> data == S -> top) exit(0);

* e = * -- S -> top;

}

void PopOPND(OPNDStack * S, int * e){

if (S -> data == S -> top) exit(0);

* e = * -- S -> top;

}

int GetTopOPND(OPNDStack S){

if (S.data == S.top) exit(0);

return * (S.top - 1);

}

char GetTopOPTR(OPTRStack S){

if (S.data == S.top) exit(0);

return * (S.top - 1);

}

bool In(char c, char OP\[\]){

for(int i = 0; i < 8; i++){

if(c == OPi)

return true;

}

return false;

}

char Precede(char a, char b){

int p, q;

if(a == '#')

p = 0;

else if(a == '(')

p = 1;

else if(a == '+' || a == '-')

p = 2;

else if(a == '*' || a == '/')

p = 3;

else if(a == ')')

p = 4;

if(b == '#')

q = 0;

else if(b == '(')

q = 4;

else if(b == '+' || b == '-')

q = 2;

else if(b == '*' || b == '/')

q = 3;

else if(b == ')')

q = 1;

if(a == '#' && b == '#' || a == '(' && b == ')')

return '=';

else if(p >= q)

return '>';

else if(p < q)

return '<';

}

int Operate(int a, char e, int b){

if(e == '+')

return a + b;

else if(e == '-')

return a - b;

else if(e == '*')

return a * b;

else if(e == '/')

return a / b;

}

int EvaluateExpression(){

OPTRStack OPTR;

OPNDStack OPND;

InitOPTRStack(&OPTR);

InitOPNDStack(&OPND);

PushOPTR(&OPTR, '#');

char c = getchar();

char e;

int a, b;

char OP8 = {'+', '-', '*', '/', '(', ')', '#'};

while(c != '#' || GetTopOPTR(OPTR) != '#'){

if(! In(c, OP)){

int temp = 0;

while(! In(c, OP)){

temp = temp * 10 + c - '0';

c = getchar();

}

PushOPND(&OPND, temp);

}

else{

switch(Precede(GetTopOPTR(OPTR), c)){

case '<':

PushOPTR(&OPTR, c);

c = getchar();

break;

case '=':

PopOPTR(&OPTR, &e);

c = getchar();

break;

case '>':

PopOPTR(&OPTR, &e);

PopOPND(&OPND, &b);

PopOPND(&OPND, &a);

PushOPND(&OPND, Operate(a, e, b));

break;

}

}

}

return GetTopOPND(OPND);

}

int main(){

int value = EvaluateExpression();

printf("%d", value);

return 0;

}

相关推荐
郝学胜_神的一滴30 分钟前
完全二叉树与堆底层原理深度剖析 | 手写C++大顶堆实现
数据结构·算法
asdzx6731 分钟前
Python 优雅解析 Excel:从原生行列到强类型对象的三层数据结构演进
数据结构·python·excel
是苏浙38 分钟前
Java实现链表2
java·开发语言·数据结构
KobeSacre1 小时前
ThreadLocal 源码
数据结构
少司府1 小时前
C++基础入门:_stack_queue 底层奥秘
开发语言·数据结构·c++·栈和队列·queue·stack
牛油果子哥q1 小时前
unordered_set / unordered_map 底层哈希表精讲,哈希原理、哈希冲突、链地址法、源码结构、有序与无序容器终极选型全解
数据结构·算法·哈希算法·散列表
进击的荆棘1 小时前
优选算法——优先级队列
数据结构·c++·算法·leetcode·优先级队列
牛油果子哥q1 小时前
哈希表经典刷题模型与布隆过滤器精讲,哈希查重、哈希计数、双哈希映射、误判原理与工业级落地应用
数据结构·算法·哈希算法·散列表
2601_961845152 小时前
花生十三图推思维导图|图形推理|技巧
数据结构·算法·链表·贪心算法·排序算法·线性回归·动态规划
前进吧-程序员2 小时前
反转链表完全指南:辅助容器、三指针、头插法
数据结构·c++·链表