Four operations implementation

zhaozj2021-02-16  54

#include "conoe.h"

#include "stdio.h"

#include "stdlib.h"

#define stack_init_size 100

#define stackincrement 10

Typedef char selemtype;

Typedef int stat;

#define ok 1

#define overflow 0

#define error 0

Typedef struct {

SELEMTYPE * BASE;

SELEMTYPE * TOP;

Int stacksize;

} SQSTACK;

STATUS INITSTACK (SQSTACK & S) {// Constructs a empty stack

S.BASE = (SELEMTYPE *) Malloc (stack_init_size * sizeof (selemtype));

IF (! s.base) exit (overflow);

S.TOP = S.BASE;

s.stacksize = stack_init_size;

Return OK;

} // initstack

SELEMTYPE GETOP (SQSTACK S) {// If the stack is not available, then return to the S stack element with e returns

SELEMTYPE E;

IF (S.TOP == S.Base) Return Error;

e = * (S.TOP-1);

Return E;

} // getop

Status Push (Sqstack & S, SELEMTYPE E) {// Insert Element E is the top elements of the new stack

IF (S.Top-S.Base> = S.Stacksize) {// Stack full, add storage space

S.BASE = (SELMTYPE *) Realloc (S.Base, (S.Stacksize StackIncrement) * Sizeof (SELE

mType));

IF (! s.base) exit (overflow); // Storage allocation failed

S.TOP = S.BASE S.STACKSIZE;

S.stacksize = stackincrement;

}

* S.TOP = E;

Return OK;

} // push

STATUS POP (Sqstack & S, SELEMTYPE & E) {// If the stack is not empty, then the stack top element of S is deleted, and returns its value with E.

Return OK, otherwise returns Error

IF (S.TOP == S.Base) Return Error;

e = * - s.top;

Return OK;

} // POP

Char Precede (Char E, CHAR C) {// Comparative Operation Any Priority

IF (e == ' ')

{IF (c == ' ' || c == '| || c ==') '|| c ==' # ')

Return '>';

Return '<';

}

Else IF (e == '-')

{

IF (c == ' ' || c == '-' || c == ')' || c == '#')

Return '>';

Return '<';

}

ELSE IF (e == '*')

{

IF (c == '(')

Return '<';

Return '>';

Else IF (e == '/')

{

IF (c == '(')

Return '<';

Return '>';

}

Else IF (e == '(')

{

IF (c == ')')

Return '=';

ELSE IF (c == ' ' || c == '-' || c == '*' || c == '/' || c == '(')

Return '<';

}

ELSE IF (E == ')')

{

IF (c == ' ' || c == '-' || c == '*' || c == '/' || c == ')' || c == '#')

Return '>';

}

Else IF (e == '#')

{IF (c == '#')

Return '=';

ELSE IF (c == ' ' || c == '-' || c == '*' || c == '/' || c == '(')

Return '<';

}

Return 0;

}

Int Operate (Char B, Char Theta, Char A) {// Computing

Int temp;

// b- = 48; A- = 48;

IF (Theta == ' ')

TEMP = B A;

Else IF (Theta == '*')

TEMP = B * a;

Else IF (Theta == '-')

TEMP = B-A;

Else IF (Theta == '/')

TEMP = B / A;

Return Temp;

}

Char op [] = {' ', '-', '*', '/', '(', ')', '#'};

INT in (Char C, char OP [] // is more operator

{INT I;

For (i = 0; i <= 6; i )

IF (c == op [i])

Return 1;

Return 0;

}

Char evluetexpression () {

// Arithmetic expression evaluation operator priority algorithm. Set OPTR and OPND to operator stacks and arithmetic count stacks,

// OP is a collection of operations

SQSTACK OPTR, OPND; CHAR A, B, C, X, THETA Download

INITSTACK (OPTR); Push (Optr, '#');

INITSTACK (OPND); c = getchar ();

While (c! = '#' || getop (optr)! = '#') {

IF (! in (c, op)) {push (OPND, C-48); c = getchar ();} / / is not an operator

Else

Switch (Precede (getop (optr), c)) {case '<': // Stack top element priority

Push (OPTR, C); C = getchar (); Break;

CASE '=': // Remoded borders and receive the next character

POP (OPTR, X); C = getchar (); Break;

Case '>': // Repends and puts the result of the operation into the stack

POP (OPTR, THETA);

POP (OPND, B); POP (OPND, A);

Push (Opnd, Operate (A, Theta, B));

Break;

} // switch

} // while

Return Getop (OPND);

} // evluateExpression

void main ()

{Char TEMP;

Temp = evluateExpression ();

Printf ("% D / N", TEMP);

Return;

}

/ * This example can be implemented as: 9 6-5 * 2/1 #

The result is 5

Implement four operations. For everyone to learn.

转载请注明原文地址:https://www.9cbs.com/read-23858.html

New Post(0)