Home > Back-end >  How to add output function of expression in the following code
How to add output function of expression in the following code

Time:09-21

 # include 
#include
#include
# define TRUE 1
# define FALSE 0
# define MAXNUM 1000
Typedef int DataType.
Struct BinTreeNode;
Typedef struct BinTreeNode * PBinTreeNode;
Struct BinTreeNode
{
DataType info;
PBinTreeNode llink;
PBinTreeNode rlink;
};
Typedef struct BinTreeNode * BinTree;
Typedef BinTree * PBinTree;
Int extoBinTree (PBinTree pbtree, const char * ex, int n)
/* from infix expression ex (of length n) create a binary tree, if a legal expression, it returns TRUE, and the algorithm at the end of the * pbtree storing the addresses of root node of the binary tree; Otherwise it returns FALSE */
{
char c;
Int index, I, bracket;
Int have_bracket=FALSE;/* record whether the expression contains parentheses */
Int num, state_int nint;
Int tag1 and tag2.
If (ex [0]=='| | the ex [0]==' \ t '| | the ex [0]==' \ n ')
Return extoBinTree (pbtree, ex + 1, n - 1);/* ignore to the left of the number of null character */
If (ex [n - 1]=='| | the ex [0]==' \ t '| | the ex [0]==' \ n ')
Return extoBinTree (pbtree, ex, n - 1);/* */ignore the right number of null character
If (ex [0]=='(' & amp; & The ex [n - 1]==') ')
Return extoBinTree (pbtree, ex + 1, n - 2);/* ignore paired parentheses around */
Bracket=0;
The index=n;
For (I=n - 1; i>=0; I -)/* from the forward search, to find the first is not the lowest priority in parentheses operator */
{
C=the ex [I];
If (c==') ')/* into a layer of */brackets
{
Have_bracket=TRUE;
Bracket++;
}
If (c=='(') bracket -; Layer/* a */brackets
If (bracket<0)/* brackets do not match, expression of illegal */
{
* pbtree=NULL;
return FALSE;
}
If (bracket> 0) continue;/* if the current position in a layer of parentheses, direct search next position */
If (c=='+' | | c=='-')
If (index==n | | the ex [index]=='*' | | the ex [index]=='/')
index=i;
If (c=='*' | | c=='/')
If (index==n)
index=i;
}
If (bracket!=0) return FALSE./* brackets do not match, expression of illegal */
If (index==n)/* suggests that this is a only a several empty expression of the characters, Numbers and accordingly to create a tree contains only a root node of the binary tree */
{
If (have_bracket==TRUE)
{
* pbtree=NULL;
return FALSE;
}/* should not contain */brackets
Nint=0;/* nint record expression contains the integer number of */
State_int=FALSE;/* state_int record whether the current read character is numeric characters */
num=0;
for(i=0; i{
C=the ex [I];
The switch (c)
{
Case '0' case '1' : case '2' : case: '3' case '4' :
Case '5' : case '6' : case '7' : case '8' : case '9' :
If (state_int==FALSE)
{
Num=c - '0';
State_int=TRUE;
Nint++;
}
The else
{
Num=num * 10 + c - '0';
}
break;
Case 'case' \ t ': case' \ n ':
State_int=FALSE;
break;
Default:/* the illegal characters */
* pbtree=NULL;
return FALSE;
}
}
If (nint!
=1){
* pbtree=NULL;
return FALSE;
}
* pbtree=(BinTree) malloc (sizeof (struct BinTreeNode));
(* pbtree) - & gt; Info=num;
(* pbtree) - & gt; Llink=NULL;
(* pbtree) - & gt; Rlink=NULL;
Return TRUE;/* successfully created a tree contains only a root node of the binary tree */
}
* pbtree=(BinTree) malloc (sizeof (struct BinTreeNode));
(* pbtree) - & gt; Info=ex [index];
Tag1=extoBinTree (& amp; (* pbtree) - & gt; Llink, ex, index);/* number of recursive calls the algorithm to create the left child */
Tag2=extoBinTree (& amp; (* pbtree) - & gt; Rlink, ex + index + 1, n - index - 1);/* number of recursive calls the algorithm to create the right child */
If (tag1==TRUE& & Tag2==TRUE) return TRUE;
return FALSE;
}
Int CAL (BinTree btree, int * presult)/* calculated binary tree btree represents the value of the expression, if a legal expression, it returns TRUE, and the algorithm at the end of the calculation results in the * presult; Otherwise, returns FALSE. */
{
Int result1, result2;
If (btree==NULL) return FALSE./* empty tree, cannot calculate */
If (btree - & gt; Llink==NULL& & Btree - & gt; Rlink==NULL)/* only one node, direct calculation */
{
* presult=btree - & gt; The info.
Return TRUE;
}
If (btree - & gt; Llink==NULL | | btree - & gt; Rlink==NULL)/* only left subtree or right subtree only, cannot calculate */
return FALSE;
The switch (btree - & gt; Info)
{
Case: '+'
If (CAL (btree - & gt; Llink, & amp; Result1)==FALSE) return FALSE;
If (CAL (btree - & gt; Rlink, & amp; Result2)==FALSE) return FALSE;
* presult=result1 + result2;
Return TRUE;
In case the '-' :
If (CAL (btree - & gt; Llink, & amp; Result1)==FALSE) return FALSE;
If (CAL (btree - & gt; Rlink, & amp; Result2)==FALSE) return FALSE;
* presult=result1 - result2;
Return TRUE;
Case: '*'
If (CAL (btree - & gt; Llink, & amp; Result1)==FALSE) return FALSE;
If (CAL (btree - & gt; Rlink, & amp; Result2)==FALSE) return FALSE;
* presult result2=result1 *;
Return TRUE;
Case '/' :
If (CAL (btree - & gt; Llink, & amp; Result1)==FALSE) return FALSE;
If (CAL (btree - & gt; Rlink, & amp; Result2)==FALSE) return FALSE;
* presult=result1/result2;
Return TRUE;
Default:
return FALSE;
}
}
Void delete_BTree (PBinTree ptree)
{
BinTree temp=(* ptree);
If (temp==NULL) return;
Delete_BTree (& amp; (temp & gt; Llink));
Delete_BTree (& amp; (temp & gt; Rlink));
Free (temp);
}
Void getline (char * line, int limit)/* read a line from standard input, as a string line */
{
char c;
int i=0;
While (i='\ n'Line [i++]=c;
Line [I]='\ 0';
}
Int main ()
{
Char c, ex [MAXNUM];
Int result, flag=TRUE;
BinTree btree;
While (flag==TRUE)
{
Printf (" please enter the expression: ");
Getline (ex, MAXNUM);/* */infix expression
If (extoBinTree (& amp; Btree, ex, strlen (ex))==FALSE)
{
Printf (" input is wrong! \n");
Delete_BTree (& amp; Btree);
Printf (" \ n to continue? (y/n (n) (is) : ");
The scanf (" % c ", & amp; C);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related