Home > Back-end >  Data structure infix expression evaluation and suffix OJ RE
Data structure infix expression evaluation and suffix OJ RE

Time:10-04

 # include & lt; stdio.h> 
# include & lt; Stdlib. H>
#include

//operator stack
Typedef struct SChar {
Char data;
Struct SChar * next;
} SChar, * LinkStack;

//the operand stack
Typedef struct SNum {
Float data;
Struct SNum * next;
} SNum, * LinkStack2;

//char initialization
LinkStack InitChar () {
Return NULL;
}

//char stack
LinkStack PushChar (LinkStack pTop, char e) {
SChar * S=(LinkStack) malloc (sizeof (SChar));
S - & gt; Data=https://bbs.csdn.net/topics/e;
S - & gt; Next=pTop;
PTop=S;
Return pTop;
}

//char out stack
LinkStack PopChar (LinkStack pTop, char * e) {
If (pTop==NULL)
Printf (" empty stack! \n");
* e=pTop - & gt; The data;
LinkStack p=pTop;
PTop=pTop - & gt; Next;
Free (p);
Return pTop;
}

//num initialization
LinkStack2 InitNum () {
Return NULL;
}

//num stack
LinkStack2 PushNum (LinkStack2 pTop, float e) {
SNum * S=(LinkStack2) malloc (sizeof (SNum));
S - & gt; Data=https://bbs.csdn.net/topics/e;
S - & gt; Next=pTop;
PTop=S;
Return pTop;
}

//num out stack
LinkStack2 PopNum (LinkStack2 pTop, float * e) {
If (pTop==NULL)
Printf (" empty stack! \n");
* e=pTop - & gt; The data;
LinkStack2 p=pTop;
PTop=pTop - & gt; Next;
Free (p);
Return pTop;
}

Float Cal (char a, float b, float c)//operation function, a is operator, b, c respectively two Numbers
{
Float r=0;
The switch (a)
{
Case: '+'
R=b + c;
break;
In case the '-' :
R=b - c;
break;
Case: '*'
R=b * c.
break;
Case '/' :
If (c!=0)
R=b/c;
break;
}
Return r;
}

//infix expression evaluation
Void the Count (char * STR) {
Char * p=STR.
Char e;
Float f, num=0, temp=0;
LinkStack s=NULL;
LinkStack2 q=NULL;//s check OPTR (operator), q hosting OPND (operand)
S=InitChar ();
Q=InitNum ();
S=PushChar (s, '#');
While ((* p)! )='\ n' {
//read the operand
//if the stack is empty, it directly into; If not null, stack top element judgment symbol: symbol stack as a '*', '/' hours calculation, or pressed into
If (isdigit (* p)) {
Temp=(float) * p - '0'.
If (isdigit (* (+ + p))) {
Temp=temp * 10 + (float) - '0' (* p).
} the else
P -;
if(! Q)
Q=PushNum (q, temp);
Else if (s - & gt; Data=https://bbs.csdn.net/topics/='*' | | s - & gt; The data '/') {
https://bbs.csdn.net/topics/==S=PopChar (s, & amp; E);
Num=Cal (e, q - & gt; The data, temp);
Q=PopNum (q, & amp; F);
Q=PushNum (q, num);
} the else
Q=PushNum (q, temp);
}
//read in operator,
The else {
If (s - & gt; Data='#')
https://bbs.csdn.net/topics/=S=PushChar (s, p);
Else if (* p=='+' | | * p=='-') {
If (s - & gt; The data!='(') {
S=PopChar (s, & amp; E);
Q=PopNum (q, & amp; F);
Num=Cal (e, q - & gt; The data, f);
Q=PopNum (q, & amp; F);
Q=PushNum (q, num);
}
S=PushChar (s, p);
}
Else if (* p==') ') {
S=PushChar (s, p);
While (s - & gt; The data!='(') {
If (s - & gt; Data='https://bbs.csdn.net/topics/=')
S=PopChar (s, & amp; E);
S=PopChar (s, & amp; E);
Q=PopNum (q, & amp; F);
Num=Cal (e, q - & gt; The data, f);
Q=PopNum (q, & amp; F);
Q=PushNum (q, num);
}
S=PopChar (s, & amp; E);
}
Else if (* p=='*' | | * p=='/') {
If (s - & gt; Data=https://bbs.csdn.net/topics/='*' | | s - & gt; The data '/') {
https://bbs.csdn.net/topics/==S=PopChar (s, & amp; E);
Q=PopNum (q, & amp; F);
Num=Cal (e, q - & gt; The data, f);
Q=PopNum (q, & amp; F);
Q=PushNum (q, num);
}
S=PushChar (s, p);
}
Else if (* p=='(')
S=PushChar (s, p);
}
P++;
}
While (s - & gt; The data!='#') {
Q=PopNum (q, & amp; F);
Num=Cal (s - & gt; The data, the q - & gt; The data, f);
If (q - & gt; Next)
Q=PopNum (q, & amp; F);
Q=PushNum (q, num);
S=s - & gt; Next;
}
Num=q - & gt; The data;
Printf (" % 2 f \ n ", num);
}

//infix expression suffix
Void Conversion (char * STR) {
Char * p=STR.
Char e;
LinkStack top=NULL;
Top=InitChar ();
While (* p! )='\ n' {
//digital print directly
If (isdigit (* p)) {
Char * t=p;
If (isdigit (* (+ + p)))
Printf (" % c "c %, * t, * p).
The else {
P -;
Printf (" % c ", * p).
}
}
//the stack is empty, the press directly into the
if(! Top & amp; & ! Isdigit (* p)) {
(top, top=PushChar * p);
}
//the stack is not empty
The else {
If (* p=='+' | | * p=='-') {
//the stack is not empty, because the '+' '-' as the lowest priority, the absence of parentheses, the existing elements in the stack pop-up and print the
While (top & amp; & Top - & gt; The data!='+' & amp; & Top - & gt; The data!='-' & amp; & Top - & gt; The data!='(') {
(top, top=PopChar & amp; E);
Printf (" % c ", e);
}
(top, top=PushChar * p);//into the symbol
}
If (* p=='*' | | * p=='/') {
While (top - & gt; The data!='+' & amp; & Top - & gt; The data!='-' & amp; & Top - & gt; The data!='(') {
(top, top=PopChar & amp; E);
Printf (" % c ", e);
if(! Top)
break;
}
(top, top=PushChar * p);
}
If (* p=='(')
(top, top=PushChar * p);
If (* p==') ') {
While (top - & gt; The data!='(') {
(top, top=PopChar & amp; E);
Printf (" % c ", e);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related