| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 518 人关注过本帖
标题:哪里出错了呢?二叉树实现四则运算
只看楼主 加入收藏
kws9624
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-10-26
收藏
 问题点数:0 回复次数:0 
哪里出错了呢?二叉树实现四则运算
共有五个类。CExpression,CBst,Node,COp,CVar。
可以计算出答案来,但是CBst析构函数void CBst::clear(Node*& t)会出错。
哪里错了呢?

//COp.h
class COp  
{
public:
    CString strFunc;
    CString var;
    static int brackets;

    int pri;
    double cal(void);
    double r;
    double l;
    double v;
    char s;
    COp();
    COp(CString,char);
    COp(char);
    COp(double,char);
    COp(char, double);
    COp(double, char, double);
    virtual ~COp();

private:
    void setPri(void);
};

//COp.cpp
#include "Ex.h"
#include "Op.h"

int COp::brackets = 0;

COp::COp()
{
}
COp::COp(CString vvar,char vop)
{
    l = 0;
    s = vop;
    r = 0;
    v = 0;
    var = vvar;
    setPri();
}
COp::COp(char vop)
{
    l = 0;
    s = vop;
    r = 0;
    v = 0;
    var = "";
    setPri();
}
COp::COp(char vop,double vr)
{
    l = 0;
    s = vop;
    r = vr;
    v = 0;
    var = "";
    setPri();
}
COp::COp(double vl,char vop)
{
    l = vl;
    s = vop;
    r = 0;
    v = 0;
    var = "";
    setPri();
}
COp::COp(double vl,char vop,double vr)
{
    l = vl;
    s = vop;
    r = vr;
    v = 0;
    var = "";
    setPri();
}
COp::~COp()
{
}

void COp::setPri()
{
    switch (s)
    {
        case '=': pri = 10*COp::brackets + 0; break;
        case '+': pri = 10*COp::brackets + 1; break;
        case '-': pri = 10*COp::brackets + 1; break;
        case '*': pri = 10*COp::brackets + 2; break;
        case '/': pri = 10*COp::brackets + 2; break;
        case '^': pri = 10*COp::brackets + 3; break;
        case '(': pri = 10*(++COp::brackets) + 0; break;
        case ')': COp::brackets-- ; break;
    }
    return;
}

double COp::cal(void)
{
    switch (s)
    {
        case '=':    v = r ; l =  r ; break;
        case '(':    v = r ; l =  r ; break;
        case '+':     v = l + r ; break;
        case '-':     v = l - r ; break;
        case '*':    v = l * r ; break;
        case '/':    v = l / r ; break;
        case '^':    v = 1;
                    for (int i = 0 ; i<r ; i++)
                        v = v*l;
                    break;
    }
    return v ;
}

//Node.h

#include "op.h"

class Node
{
public:
    COp data;
     Node* L;
     Node* R;
    Node(COp d, Node*& lp,Node*& rp);
    Node(COp d);
    Node(void);
public:
    ~Node(void);
};

//Node.cpp
#include "Node.h"

Node::Node(COp d, Node*& lp, Node*& rp)
{
    data = d;
    L = lp;
    R = rp;
}
Node::Node(COp d)
{
    data = d;
    L = NULL;
    R = NULL;
}
Node::Node(void)
{
}

Node::~Node(void)
{

}

//CBst.h
#include "Op.h"
#include "node.h"

class CBst
{
public:
    Node* root;
    int num;
    CBst(void);
    void clear(Node*& t);
    void clear();
    int empty();
    double cal(Node*& tree);
    double cal();
    void insert(Node*& tree, Node*& ptree, const COp& d);
    void insert(Node*& tree, const COp& d);
    void CBst::insert(const COp& d);

public:
    ~CBst(void);
};

//CBst.cpp
#include "Bst.h"
#include <iostream>
using namespace std;

CBst::CBst(void)
{
    root=NULL;
    num=0;
}
void CBst::clear(Node*& t)
{
    if(!t) return;
    clear(t->L);
    clear(t->R);
    delete t;
}
void CBst::clear()
{
    clear(root);
    num = 0;
}
int CBst::empty()
{
    if (!root)    return 0; else return 1;
}

double CBst::cal(Node*& tree)
{        
    if(tree->L){
        tree->data.l = cal(tree->L);
    }
    if(tree->R){
        tree->data.r = cal(tree->R);
    }
    return tree->data.cal();
}

double CBst::cal()
{
    return cal(root);
}

void CBst::insert(Node*& tree, Node*& ptree, const COp& d)
{
    if(!tree)
        tree = new Node(d);
    else if(d.pri > tree->data.pri)
        insert(tree->R, tree, d);
    else if(d.pri <= tree->data.pri){
        Node* newnode = new Node(d);
        newnode->L = tree;
        ptree->R = newnode;
        }                 
}
void CBst::insert(Node*& tree, const COp& d)
{
    if(!tree)
        tree = new Node(d);
    else if(d.pri > tree->data.pri)
        insert(tree->R, tree,d);  
}
void CBst::insert(const COp& d)
{
    if (d.s != ')'){
        insert(root, d);
        num++;
    }
}

CBst::~CBst(void)
{
    clear(root);
    num=0;
}

//CMExpression.h
#include <list>
#include "Bst.h"
#include "Op.h"
#include "Var.h"

using namespace std ;
typedef list<COp> LSTCSTRING;
typedef list<CVar> LSTCVAR;

class CMExpression
{
public:
    static LSTCVAR lstvar;
    CString ex ;
    CString var ;
    double v ;
    CBst b ;

    int c ;
    LSTCSTRING lst;
private:
    void getVar();
    double getdata(const int iOpPos,const CString strExp);
    double getdata(const CString strExp);
    void Parse(CString);
    void trim(CString&);

public:
    CMExpression(CString strex);
    CMExpression(void);
public:
    void createTree(void);
    ~CMExpression(void);
};

//CMExpression.cpp
#include "MExpression.h"

LSTCVAR CMExpression::lstvar;

void CMExpression::Parse(CString strExp)
{
    double dl;
    COp* op;
    CString strl;

    int iOpPos = strExp.FindOneOf("=+-*/^()");
    if (iOpPos > -1){
        CString strL = strExp.Left(iOpPos);
        strL.TrimRight();
        char cop = strExp.GetAt(iOpPos);

        switch (cop)
        {
        case '=':
            op = new COp(strL,'=');
            lst.insert(lst.begin(),*op);
            break;
        case '+':
            dl = getdata(iOpPos, strExp);
            lst.back().r = dl;
            op = new COp(dl,'+');
            lst.insert(lst.end(),*op);
            break;
        case '-':
            dl = getdata(iOpPos, strExp);
            lst.back().r = dl;
            op = new COp(dl,'-');
            lst.insert(lst.end(),*op);
            break;
        case '*':
            dl = getdata(iOpPos, strExp);
            lst.back().r = dl;
            op = new COp(dl,'*');
            lst.insert(lst.end(),*op);
            break;
        case '/':
            dl = getdata(iOpPos, strExp);
            lst.back().r = dl;
            op = new COp(dl,'/');
            lst.insert(lst.end(),*op);
            break;
        case '^':
            dl = getdata(iOpPos, strExp);
            lst.back().r = dl;
            op = new COp(dl,'^');
            lst.insert(lst.end(),*op);
            break;
        case '(':
            op = new COp('(');
            lst.insert(lst.end(),*op);
            break;
        case ')':
            dl = getdata(iOpPos, strExp);
            lst.back().r = dl;
            op = new COp(')');
            lst.insert(lst.end(),*op);
            break;
        }

        CString strR = strExp.Right(strExp.GetLength()-iOpPos-1);
        trim(strR);
        Parse(strR);
    }
    else
    {
        trim(strExp);
        dl= getdata(strExp);
        lst.back().r = dl;
    }

}

CMExpression::CMExpression(CString strex)
{
    ex = strex;
    Parse(ex);
    createTree();
    v = b.cal();
    getVar();
}
void CMExpression::trim(CString& str)
{
    str.TrimLeft();
    str.TrimRight();
}
CMExpression::CMExpression(void)
{
}

CMExpression::~CMExpression(void)
{

}

void CMExpression::createTree()
{
    LSTCSTRING::iterator it;
    for (it = lst.begin(); it != lst.end(); it++){
        b.insert(*it);
    }

}

double CMExpression::getdata(const CString strExp)
{
    CString strl=strExp;
    double dl = 0;

    trim(strl);
    char c = strl.GetAt(0);
    if (0 !=isdigit(c)){
        dl= atof((LPCTSTR)strl);
    }
    else{
        LSTCVAR::iterator it;
        for (it = CMExpression::lstvar.begin(); it != CMExpression::lstvar.end(); it++)
        {
            if (strl == ((*it).var)){
                dl = (*it).v;
            }
        }
        return dl;
    }
    return dl;
}

double CMExpression::getdata(const int iOpPos, const CString strExp)
{
    CString strl;
    double dl = 0;
    if (iOpPos > 0){
        strl = strExp.Left(iOpPos);
        trim(strl);
        char c = strl.GetAt(0);
        if (0 !=isdigit(c)){
            dl= atof((LPCTSTR)strl);
        }
        else{
            LSTCVAR::iterator it;
            for (it = CMExpression::lstvar.begin(); it != CMExpression::lstvar.end(); it++)
            {
                if (strl == ((*it).var)){
                    dl = (*it).v;
                }
            }
            return dl;
        }
    }
    else{
        dl = 0;
    }
    return dl;
}

void CMExpression::getVar()
{
    var.Format("%s",lst.begin()->var);
    CVar* cvar = new CVar(var,v);
    CMExpression::lstvar.insert(CMExpression::lstvar.end(),*cvar);
}

//CVar.h
class CVar  
{
public:
    double v;
    CString var;
    CVar();
    CVar(CString vvar, double vv);
    virtual ~CVar();

};

//CVar.cpp
#include "stdafx.h"
#include "Ex.h"
#include "Var.h"

CVar::CVar(CString vvar, double vv)
{
    var = vvar;
    v = vv;
}

CVar::CVar()
{

}

CVar::~CVar()
{

}
搜索更多相关主题的帖子: 运算 二叉树 
2009-10-26 14:40
快速回复:哪里出错了呢?二叉树实现四则运算
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013058 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved