Nanyang OJ depressed C small plus (iii) topic 409

Source: Internet
Author: User

/* Expression tree Idea (a+b) * (c* (d+e); the suffix expression ab+cde+** is obtained by post-sequential traversal of the tree; Here is how to construct its corresponding expression tree based on a suffix expression.algorithm idea: Actually very simple, mainly is the use of the stack. The algorithm time complexity is O (n), and n is the length of the suffix expression. The suffix expression is scanned backwards, and if it is an operand, a single-node tree is created and its pointer is pressed into the stack. If it is an operator, theCreate a tree that Fu Weigen the operation and then pop up two pointers from the stack (these 2 pointers point to 2 trees, respectively), as the tree'sleft and right sub-trees. Then push the pointer to the tree into the stack. The suffix expression is not completed until it is scanned. There will be only one pointer in the last stack. This pointer points to the root node of the constructed expression tree. * * (A+B) * (c* (d+e);
Post-sequential traversal of the tree to get a suffix expression
ab+cde+**;
The implementation of this is how to construct a corresponding expression tree based on a suffix expression.
Algorithm idea: Actually very simple, mainly is the use of the stack. The algorithm time complexity is O (n), and n is the length of the suffix expression.
The suffix expression is scanned backwards, and if it is an operand, a single-node tree is created and its pointer is pressed into the stack. If it is an operator, the
Create a tree that Fu Weigen the operation and then pop up two pointers from the stack (these 2 pointers point to 2 trees, respectively), as the tree's
Left and right sub-trees. Then push the pointer to the tree into the stack. The suffix expression is not completed until it is scanned.
There will be only one pointer in the last stack. This pointer points to the root node of the constructed expression tree. #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <stack>
#define N 1000
using namespace Std;
Char Str[n],s[2*n];
typedef struct NODE//Two fork tree, node type
{
Char x[10];
struct node *lchild;
struct node *rchild;
}*linklist;
stack<linklist>ch;//Storage Node
stack<char>ca;//Storage Operators
Stack<double> num;
int top;
int COM (char x)
{
Switch (x)
{
Case ' + ':
Case '-': return 1;
Case ' * ':
Case '/': return 2;
Case ' (': return 0;
default:return-1;
}
}
void Zhuan ()
{
while (!ca.empty ())
Ca.pop ();
scanf ("%s", str);
Top=-1;
int K=strlen (str);
Ca.push (' # ');
for (int i=0;i<k;i++)
{
if (str[i]>= ' 0 ' &&str[i]<= ' 9 ' | | str[i]== '. ')
{
top++;
S[top]=str[i];
}
else if (str[i]== ' + ' | | str[i]== '-' | | str[i]== ' * ' | | str[i]== '/')
{
top++;
S[top]= ', ';
if (COM (str[i]) >com (Ca.top ())) Ca.push (Str[i]);
Else
{
while (COM (ca.top ()) >=com (Str[i]))
{
top++;
S[top]=ca.top ();
Ca.pop ();
top++;
S[top]= ', ';
}
Ca.push (Str[i]);
}
}
else if (str[i]== ' (') Ca.push (Str[i]);
else if (str[i]== ') ')
{
while (Ca.top ()! = ' (')
{
top++;
S[top]= ', ';
top++;
S[top]=ca.top ();
Ca.pop ();
}
Ca.pop ();
}
}
while (Ca.top ()! = ' # ')
{
top++;
S[top]= ', ';
top++;
S[top]=ca.top ();
Ca.pop ();
}
}
Linklist CRE ()//Create two-fork tree based on suffix
{
while (!ch.empty ())
Ch.pop ();
for (int i=0;i<=top;i++)
{
if (s[i]== ' + ' | | s[i]== '-' | | s[i]== '/' | | s[i]== ' * ')
{
Linklist Tree;
Tree= (linklist) malloc (sizeof (struct node));
tree->x[0]=s[i];
tree->x[1]= ' + ';
Tree->rchild=ch.top ();
Ch.pop ();
Tree->lchild=ch.top ();
Ch.pop ();
Ch.push (tree);
i++;
}
else if (s[i]>= ' 0 ' &&s[i]<= ' 9 ')
{
Linklist Tree;
Tree= (linklist) malloc (sizeof (struct node));
tree->lchild=null;
tree->rchild=null;
int j=0;
Char b[10];
while (s[i]!= ', ')
{
B[j]=s[i];
j + +;
i++;
}
b[j]= ' + ';
strcpy (TREE-&GT;X,B);
Ch.push (tree);
}
}
return Ch.top ();
}
void XTree (linklist head)//prefix output
{
if (head)
{
printf ("%s", head->x);
XTree (Head->lchild);
XTree (Head->rchild);
}
}
void Xiao (linklist head)
{
if (head)
{
if (head->lchild) xiao (head->lchild);
if (head->rchild) xiao (head->rchild);
Free (head);
}
}
void Ji ()
{
while (!num.empty ())
Num.pop ();
Num.push (' # ');
Double a1,a2;
Char s3[15];
char x;
int k;
for (int i=0;i<=top;i++)
{
X=s[i];
if (x>= ' 0 ' &&x<= ' 9 ')
{
k=0;
while (s[i]!= ', ')
{
S3[k]=s[i];
k++;
i++;
}
s3[k]= ' + ';
Num.push (Atof (S3));
}
Else
{
Switch (x)
{
Case ' + ':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A1+A2);
Break
Case '-':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2-A1);
Break
Case ' * ':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2*A1);
Break
Case '/':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2/A1);
Break
}
}
}
printf ("%.2lf\n", Num.top ());
}
int main ()
{
int m;
scanf ("%d", &m);
while (m--)
{
Zhuan ();
Linklist head;
Head=cre ();
XTree (head);
printf ("=\n");
Xiao (head);
for (int i=0;i<=top;i++)
{
if (s[i]== ', ') printf ("");
else printf ("%c", S[i]);
}
printf ("=\n");
Ji ();

}
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nanyang OJ depressed C small plus (iii) topic 409

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.