栈和队列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 == OP[i])

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 OP[8] = {'+', '-', '*', '/', '(', ')', '#'};

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;

}

相关推荐
浅念-2 小时前
LeetCode 回溯算法题——综合练习
数据结构·c++·算法·leetcode·职场和发展·深度优先·dfs
生成论实验室9 小时前
用事件关系网络重新理解AI(二):损失函数、优化器与深度学习的动力学
数据结构·人工智能·深度学习·算法·语言模型
阿文的代码库10 小时前
线段树入门:算法分析
数据结构·算法
悠仁さん10 小时前
数据结构 树 二叉树 堆 (堆的模拟实现篇)
数据结构
此生决int11 小时前
算法从入门到精通——位运算
数据结构·c++·算法·蓝桥杯
计算机安禾11 小时前
【算法分析与设计】第4篇:分治策略的理论框架与经典案例
数据结构·算法·排序算法
Kiling_070411 小时前
面向对象和集合编程题 ( 二 )
java·开发语言·数据结构·算法
过期动态11 小时前
【LeetCode 热题 100】两数之和— 暴力法与哈希表法详解
java·数据结构·算法·leetcode·散列表
Pointer Pursuit11 小时前
哈希表的实现
数据结构·哈希算法·散列表
故事和你9111 小时前
洛谷-【动态规划1】动态规划的引入4
开发语言·数据结构·c++·算法·动态规划·图论