Home > Back-end >  Pray god help modify it, and the generalized table sometimes not be output
Pray god help modify it, and the generalized table sometimes not be output

Time:12-24

#include
#include
#include
using namespace std;
Typedef struct BITNODE
{
Int flag;//0 used, digital nodes 1, 2 symbols node
Char sign;
Float number;
Struct lchild BITNODE * and * rchild;
} BitNode, * BiTree;
Typedef struct LINKLIST
{
Struct BITNODE * treeNode.
Struct LINKLIST * nextNode;
Struct LINKLIST * lastNode;
} linkNode, * LinkList;

Int stackTopBiTree=1;//the stack pointer
Int stackTopSign=1;
BiTree stackB [100].
Char stackC [100].

BiTree popB ()//in and out of the stack function
{
Return stackB [stackTopBiTree --];
}

Void pushB (BiTree BiTree)
{
StackB [+ + stackTopBiTree]=biTree;
}
///
Char popC ()
{
Return stackC [stackTopSign --];
}
Void pushC (char sign)
{
StackC [+ + stackTopSign]=sign;
}

BiTree outList (LinkList & amp; The list)
{
if(! The list - & gt; NextNode) return NULL;
if(! The list - & gt; NextNode - & gt; NextNode) list - & gt; LastNode=list;//if all out over the last
LinkList tempList. Nodes in the head//
BiTree tempTree;
TempList=list - & gt; NextNode;
TempTree=list - & gt; NextNode - & gt; TreeNode.
The list - & gt; NextNode=list - & gt; NextNode - & gt; NextNode;
Free (tempList);//release list contact
Return tempTree;
}
Void inList (LinkList & amp; The list of BiTree treeNode)
{
LinkList listNode;
ListNode=(LinkList) malloc (sizeof (linkNode));
ListNode - & gt; TreeNode=treeNode;//from the last into the stack
ListNode - & gt; NextNode=NULL;
ListNode - & gt; LastNode=NULL;
The list - & gt; LastNode - & gt; NextNode=listNode;//list the last point to the next
The list - & gt; LastNode=listNode;//update the position of the last
}
Void ccPrintTree (BiTree node)
{
if(! The node) {
Printf (" the empty tree!" );
return;
}
LinkList list;
List=(LinkList) malloc (sizeof (LinkList));
The list - & gt; NextNode=NULL;
The list - & gt; TreeNode=NULL;
The list - & gt; LastNode=list;

The inList (list, node);
BiTree temp.
While (the list - & gt; NextNode) {
Temp=outList (list);
If (temp - & gt; Lchild) inList (list, temp & gt; Lchild);//and node into the stack
If (temp - & gt; Rchild) inList (list, temp & gt; Rchild);
If (temp - & gt; Flag==1) printf (" \ t % f ", temp - & gt; Number);
The else printf (" c "\ t %, temp - & gt; Sign);
}
}
Void InitHead (BiTree & amp; L)//initializes the tree
{
L=(BiTree) malloc (sizeof (BitNode));
L-> Lchild=NULL;
L-> Rchild=NULL;
L-> Sign='\ 0';
L-> Number=0;
L-> Flag=0;
}
Float country (char * exp, int position1, int position2)//get the Numbers between the two operator
{
Int pot=position1 + 1, I=0;//pot is the position of the decimal point
Float numHigh=0.0, numLow=0.0;//store Numbers before and after the decimal point number
Char * p=exp + position1 + 1;
While ((* (p + I)!=') & amp; & (pot!={position2))
i++;
Pot++;
}
for(int j=0; JNumHigh +=(* (p + j) - '0');
NumHigh *=10.0;
}
NumHigh/=10.0;
For (int k=0; kNumLow +=(* (exp + position2-1 - k) - '0');
NumLow/=10.0;
}
Return numHigh + numLow;
}
BiTree CreatTreeNode (BiTree nodeA, char signs and BiTree nodeB)
{
BiTree signNode;
SignNode=(BiTree) malloc (sizeof (BiTree));//create a tree contact
SignNode - & gt; Lchild=nodeA;
SignNode - & gt; Rchild=nodeB;
SignNode - & gt; Sign the=sign;
SignNode - & gt; Flag=2;
Return signNode;
}

Char Precede (char top, char follow)//determine the priority function
Void InitExpTree (BiTree & amp; L)
{
InitHead (L);
Printf (" do input the express: ");
Char * exp=(char *) malloc (sizeof (char) * 255);
The scanf (" % s ", exp);//get expression
Int haveNum=0, I=0;
Char sign;
Int position1=1, position2=1;//record symbol position
StackTopBiTree=1;//initialize the stack
StackTopSign=1;
PushC (' \ 0 ');//the empty characters onto the stack
While (* (exp + I)!='\ 0' | | stackC [stackTopSign]!='\ 0') {//when to end the symbol
If ((* (exp + I) & lt; '0' | | * (exp + I) & gt; '9') & amp; & (* (exp + I)!='. ')) {
Position2=I;//the location of the new symbol
If (haveNum==1) {//digital switch, need to extract the digital
BiTree nodeNum=(BiTree) malloc (sizeof (BiTree));
NodeNum - & gt; Flag=1;
NodeNum - & gt; Lchild=NULL;
NodeNum - & gt; Rchild=NULL;
NodeNum - & gt; Number=country (exp, position1 position2);
PushB (nodeNum);
HaveNum=0;
}
The switch (Precede (stackC [stackTopSign], * (exp + I))) {//compare
Case '& lt; ':
PushC (* (exp + I));//in the top priority, pressure stack
Position1=position2;//update the symbol position
i++;//expression backward-shift
break;
Case: '='
PopC ();//to take off the brackets
Position1=position2;
i++;
break;
Case '& gt; ':
Sign=popC ();//symbol and a stack to calculate
BiTree t1, t2;
T2=popB ();
T1=popB ();
PushB (CreatTreeNode (t1, signs and t2));
break;
Default:
Printf (" errorout!" );//error protection

break;
}
} else {
HaveNum=1;
i++;
}
}
L=popB ();
}
BiTree CalculateTree (BiTree tree)
{
Float leftValue rightValue;
BiTree temp1, temp2;
If (tree - & gt; Lchild - & gt; Flag==2) {//the left node is a symbol, continue to be
Temp1=CalculateTree (tree - & gt; Lchild); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related