Red de conocimiento informático - Material del sitio web - Cómo implementar un programa simple de evaluación de expresiones aritméticas utilizando el formato de estructura de datos del lenguaje C

Cómo implementar un programa simple de evaluación de expresiones aritméticas utilizando el formato de estructura de datos del lenguaje C

Utilice la pila para convertir la expresión infija (la fórmula de entrada) en una expresión postfija según la prioridad (estilo polaco inverso, es decir, el operador es el primero y el operando es el último), y luego utilice la pila apilar para calcular Guarde el resultado para el siguiente paso del cálculo y finalmente calcule la respuesta a la ecuación

El siguiente código ingresa una ecuación (con = como marca final de entrada) y genera el resultado. como -3 están representados por 0-3. Admite operaciones de bits altos

#include lt;stdio.hgt;

#include lt;stdlib.hgt;

#include lt;math.hgt;

p>

#include lt; malloc.hgt;

#define OK 1

# definir ERROR -1

typedef char SElemType;

typedef char Estado;

#define STACK_INIT_SIZE 100000

#define STACKINCREMENT 2

struct SqStack

{

SElemType *base

SElemType *top

int tamaño de pila; >

};

struct SqStack1

{

int *base

int *top; p> int tamaño de pila;

};

SqStack OPTR;

SqStack1 OPND;

char Precede(char c1, char c2)

{

if(c1 ==' ' || c1=='-')

{

if(c2= =' ' || c2=='-' || c2==') ' || c2=='=') devuelve 'gt;';

de lo contrario devuelve 'lt;';

}

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

{

if(c2= ='(') return 'lt;';

else return 'gt;';

}

else if(c1=='(')

{

if(c2 ==')') return '=';

else return 'lt;';

}

else if(c1==')') return 'gt;';

else if(c1=='=')

{

if(c2=='=') return '= ';

else return 'lt;';

}

else return '\0';

}

int In(char c)

{

if(c==' ' | || c=='-' || c=='*' || c =='/' || c=='(' || c==')' ||

c=='=')

devuelve 1;

de lo contrario devuelve 0;

}

int Operarate(int m, char). b, int n)

{

switch(b)

{

case ' ': return m n;

caso '-': devolver m-n;

caso '*': devolver m*n

caso '/': devolver m/n; p> }

return 0;

}

//operandos

int InitStack1(SqStack1 amp;S)

{

S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

return OK

}

int Push1(SqStack1 amp; S, int e)

{

if(S.top-S.basegt;=S.stacksize)

{

S.base=(int *)realloc(S . base, (S.stacksize STACKINCREMENT)*sizeof(int));

S.top=S.base S.stacksize;

S.stacksize=S.stacksize STACKINCREMENT; /p>

}

*S.top =e;

return OK

}

int Pop1( SqStack1 amp;S, int amp;e)

{

if(S.top==S.base) return ERROR

e=* - - S.top;

return OK

}

int GetTop1(SqStack1 S)

{

if(S.top==S.base) return ERROR;

return *(S.top-1);

}

//calcular símbolo

int InitStack(SqStack & S)

{

S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

devuelve OK; int Push(SqStack&S,SElemType e)

{

if(S.top-S.basegt;=S.stacksize)

{

S.base=(SElemType *)realloc(S.base, (S.stacksize STACKINCR

EMENT)*sizeof(SElemType));

S.top=S.base S.stacksize;

S.stacksize=S.stacksize STACKINCREMENT;

}

*S.top =e;

return OK

}

int Pop(SqStack y S, SElemType y ;e)

{

if(S.top==S.base) devuelve ERROR

e=* --S.top; p>

return OK;

}

Estado GetTop(SqStack S)

{

if(S. top= =S.base) devuelve ERROR;

devuelve *(S.top-1);

}

int Calculate()

{

char c, theta, p;

int a, b, i=0, ans, x

InitStack(OPTR);

p>

Push(OPTR,'=');

InitStack1(OPND);

c=getchar();

while(c! ='=' || GetTop(OPTR)!='=')

{

if(!In(c) amp;amp; cgt ;='0' amp; clt;='9')

{

Push1(OPND, c-'0');

c =getchar();

p>

while(cgt;='0' amp;amp; clt;='9')

{

Pop1( OPND, x);

Push1(OPND, x*10 c-'0');

c=getchar()

}

}

else if(In(c))

{

cambiar(Precede(GetTop(OPTR), c))

{

caso 'lt;':

Push(OPTR, c);

c=getchar(); p> romper;

case '=":

Pop(OPTR, p

c=getchar(); romper;

caso 'gt;':

Pop(OPTR, theta

Pop1(OPND, b); Pop1(OPND, a);

p>

ans=Operar(a, theta, b

Push1(OPND, ans); > romper;

}

}

else

{

c=getchar();

}

}

return GetTop1(OPND

}

int main()

{

int ans;

ans=Calcular();

printf("d\n",

devuelve 0;

}