回 帖 发 新 帖 刷新版面

主题:个人写的表达式转二叉树

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>                                           
#include<string.h>
typedef struct node{
    char data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
BTNode *CRTree(char s[],int i,int j){
    int k,posi,plus=0;
    BTNode *p;
    if(i==j){
        p=(BTNode *)malloc(sizeof(BTNode));
        p->data=s[i];
        p->lchild=NULL;
        p->rchild=NULL;
        return p;
    }
    for(k=i;k<=j;k++){
        if(s[k]=='+'||s[k]=='-'){
            plus++;
            posi=k;
        }
    }
    if(plus==0)
        for(k=i;k<=j;k++)
                if(s[k]=='*'||s[k]=='/'){
                    plus++;
                    posi=k;
                }
    
                if(plus!=0){
                    p=(BTNode *)malloc(sizeof(BTNode));
                    p->data=s[posi];
                    p->lchild=CRTree(s,i,posi-1);
                    p->rchild=CRTree(s,posi+1,j);
                    return p;
                }
}
void Disp(BTNode *T){
    if(T!=NULL){
        printf("%c",T->data);
        if(T->lchild||T->rchild){
            printf("(");
            Disp(T->lchild);
            if(T->rchild) printf(",");
            Disp(T->rchild);
            if(T->lchild) printf(")");
        }
    }
}
void main(){
    char s[20];
    BTNode *T;
    gets(s);
    T=CRTree(s,0,strlen(s)-1);
    Disp(T);
}[em1][em2]

回复列表 (共15个回复)

沙发

测试结果
a+b*c/d+e*f
+(+(a,/(*(b,c),d)),*(e,f))Press any key to continue

哪位能写一个一个含括号的

板凳

自己顶一一

3 楼


我写了一个:

4 楼


BinaryTreeNode.h
×××××××××××
template<class T> class BinaryTree;
template<class T>
class BinaryTreeNode
{
public:
    friend class BinaryTree<T>;
    BinaryTreeNode(){LeftChild=RightChild=0;}
    BinaryTreeNode(const T &e){data=e;LeftChild=RightChild=0;}
    BinaryTreeNode(const signed &s,bool){sign=s;LeftChild=RightChild=0;}
    BinaryTreeNode(const signed&s,BinaryTreeNode*l,BinaryTreeNode*r)
    {
        sign=s;
        LeftChild=l;
        RightChild=r;
    }
protected:
private:
    T data;
    char sign;
    BinaryTreeNode<T>*LeftChild,*RightChild;
};

5 楼


debug.h
××××××××××××
class NoMen//异常类
{
public:
    NoMen(){}
protected:
private:
};
class OutOfBounds
{
public:
    OutOfBounds(){}
protected:
private:
};
class BadInput
{
public:
    BadInput(){}
};

6 楼


stack.h
××××××××××××××××××
#include "debug.h"       
template <class T>
class Node
{  
public:
    T data;
    Node<T> *link;
protected:
private:

};
template<class T>
class Stack             
{
public:
    Stack(){top=0;}
    ~Stack();
    bool IsEmpty()const{return top==0;}
    bool IsFull()const;
    T Top()const;
    Stack<T>& Add(const T&x);
    Stack<T>&Delete(T&x);
protected:
private:
    Node<T>*top;
};
template<class T>
Stack<T>::~Stack()
{
    Node<T>*next;
    while (top)
    {
        next=top->link;
        delete top;
        top=next;
    }
}
template<class T>
bool Stack<T>::IsFull()const
{
    try
    {
        Node<T> *p=new Node<T>;
        delete p;
        return false;
    }
    catch (NoMen)
    {
        return true;
    }
}
template<class T>
T Stack<T>::Top()const
{
    if(IsEmpty())throw OutOfBounds();
    return top->data;
}
template<class T>
Stack<T>& Stack<T>::Add(const T&x)
{
    Node<T>*p=new Node<T>;
    p->data=x;
    p->link=top;
    top=p;
    return *this;
}
template<class T>
Stack<T>&Stack<T>::Delete(T&x)
{
    if (IsEmpty())throw OutOfBounds();
    x=top->data;
    Node<T>*p=top;
    top=top->link;
    delete p;
    return *this;
}

7 楼


Node.h
×××××××××××××
template<class T> class LinkedQueue;
template <class T>
class LNode
{  
    friend LinkedQueue<T>;
public:
protected:
private:
    T data;
    LNode<T> *link;
};

8 楼


LinkedQueue.h
×××××××××
#include "Node.h"
template<class T>
class LinkedQueue
{
public:
    LinkedQueue(){front=rear=0;}
    ~LinkedQueue();
    bool IsEmpty()const{return ((front)?false:true);}
    bool IsFull()const;
    T First()const;
    T Last()const;
    LinkedQueue<T>&Add(const T&x);
    LinkedQueue<T>&Delete(T&x);
protected:
private:
    LNode<T>*front;
    LNode<T>*rear;
};
template<class T>
LinkedQueue<T>::~LinkedQueue()
{
    LNode<T> *next;
    while (front)
    {
        next=front->link;
        delete front;
        front=next;
    }
}
template<class T>
bool LinkedQueue<T>::IsFull()const
{
    LNode<T>*p;
    try
    {
        p=new LNode<T>;
        delete p;
        return false;
    }
    catch (NoMem)
    {
        return false;
    }
}
template<class T>
T LinkedQueue<T>::First()const
{
    if (IsEmpty())throw OutOfBounds();
    return front->data;
}
template<class T>
T LinkedQueue<T>::Last()const
{
    if (IsEmpty())throw OutOfBounds();
    return rear->data;
}
template<class T>
LinkedQueue<T>&LinkedQueue<T>::Add(const T&x)
{
    LNode<T>*p=new LNode<T>;
    p->data=x;
    p->link=0;
    if (front)rear->link=p;
    else front=p;
    rear=p;
    return *this;
}
template<class T>
LinkedQueue<T>&LinkedQueue<T>::Delete(T&x)
{
    if (IsEmpty())throw OutOfBounds();
    x=front->data;
    LNode<T> *p=front;
    front=front->link;
    delete p;
    return *this;
}

9 楼


tree.h
×××××××××××
#include <iostream>
#include <vector>
#include <string>
#include "stack.h"
#include "LinkedQueue.h"
#include "BinaryTreeNode.h"
using namespace std;
int PRI(char sign)//优先级的计算
{
    if (sign=='*'||sign=='/'||sign=='%')return 2;
    if(sign=='+'||sign=='-')return 1;
    return 0;
}
template<class T>
class BinaryTree
{
public:
    BinaryTree(){root=0;}
    void InputBinaryTree();
    void Show_BinaryTree();
    void MakeTree(BinaryTreeNode<T>*left,BinaryTreeNode<T>*mid,BinaryTreeNode<T>*right);
    void TreeCount();
    void ShowFloor();
    void DeleteTree();
protected:
private:
    void DeleteNode(BinaryTreeNode<T>*p);
    void ShowNode(BinaryTreeNode<T>*p,LinkedQueue<BinaryTreeNode<T>*>&myqueue);
    T Count(BinaryTreeNode<T>*p);
    void Print_BNode(BinaryTreeNode<T> *p, int c, vector<bool>& isend,bool RorL);
    BinaryTreeNode<T> *root;
};
template<class T>
void BinaryTree<T>::DeleteTree()
{
    cout<<"销毁所建的树:"<<endl<<endl;
    Stack<BinaryTreeNode<T>*> node_stack;
    DeleteNode(root);
}
template<class T>
void BinaryTree<T>::DeleteNode(BinaryTreeNode<T>*p)
{
    if (!p)
    {
        return;
    }
    if (!p->LeftChild&&!p->RightChild)
    {
        cout<<"删除节点";
        if (p->sign>=37&&p->sign<=47)
        {
            cout<<p->sign;
        }
        else cout<<p->data;
        cout<<"!"<<endl;
        delete p;
        return;
    }
    DeleteNode(p->LeftChild);
    p->LeftChild=0;
    DeleteNode(p->RightChild);
    p->RightChild=0;
    DeleteNode(p);
    p=0;
}
//***********************************
template<class T>
void BinaryTree<T>::MakeTree(BinaryTreeNode<T>*left,BinaryTreeNode<T>*mid,BinaryTreeNode<T>*right)
{
    mid->LeftChild=left;
    mid->RightChild=right;
    left=right=0;
}
(待续)

10 楼


(接上面)
template<class T>
void BinaryTree<T>::InputBinaryTree()
{
    cout<<"请输入您要计算的表达式:"<<endl;
    string str;
    cin>>str;
    Stack<BinaryTreeNode<T>*>num_stack;
    Stack<BinaryTreeNode<T>*>sign_stack;
    int len=str.length();
    for (int i=0;i<len;)
    {
        if (str[i]=='+'&&(i==0||str[i-1]=='('))
        {
            i++;
        }
        else if (str[i]>47&&str[i]<58||str[i]=='-'&&(i==0||str[i-1]=='('))
        {
            T num;
            string one_num;
            while (str[i]>47&&str[i]<58||str[i]=='-'&&(i==0||str[i-1]=='(')||str[i]=='.')
            {
                one_num.append(&str[i],1);
                i++;
            }
            if(one_num=="-")
            {
                str.insert(i-1,"0",1);
                i--;
                len=str.length();
                continue;
            }
            num=atof(&one_num[0]);
            BinaryTreeNode<T>*r=new BinaryTreeNode<T>(num);
            num_stack.Add(r);
        }
        else if (sign_stack.IsEmpty()||str[i]=='(')
        {
            BinaryTreeNode<T>*r=new BinaryTreeNode<T>(str[i],true);
            sign_stack.Add(r);
            i++;
        }
        else
        {
            if (str[i]==')')
            {
                BinaryTreeNode<T>* l,*r,*m,*mid;
                while (sign_stack.Top()->sign!='(')
                {
                    num_stack.Delete(r);
                    num_stack.Delete(l);
                    sign_stack.Delete(m);
                    MakeTree(l,m,r);
                    num_stack.Add(m);
                }
                sign_stack.Delete(mid);
                i++;
            }
            else
            {
                int now=PRI(str[i]),s_top=PRI(sign_stack.Top()->sign);
                if (now==s_top||now<s_top)
                {
                    BinaryTreeNode<T>* l,*r,*m,*mid;
                    while((now==s_top||now<s_top))
                    {
                        
                        num_stack.Delete(r);
                        num_stack.Delete(l);
                        sign_stack.Delete(m);
                        MakeTree(l,m,r);
                        num_stack.Add(m);
                        if(sign_stack.IsEmpty())break;
                        now=PRI(str[i]),s_top=PRI(sign_stack.Top()->sign);
                    }
                    mid=new BinaryTreeNode<T>(str[i],true);
                    sign_stack.Add(mid);
                    i++;
                }
                else if (now>s_top)
                {
                    BinaryTreeNode<T>*r=new BinaryTreeNode<T>(str[i],true);
                    sign_stack.Add(r);
                    i++;
                }
            }    
        }
    }
    (待续)

我来回复

您尚未登录,请登录后再回复。点此登录或注册