关于信号放大器的c++问题,求解答

从度娘上down了一个信号放大器的c++,编译可以通过,但是在运行时会出错,调试时
发现问题可能出在BiTree()函数上,但是还是弄不明白,希望有人能来指点我,下面附
上源代码 环境是vc6.0
#include

using namespace std;

struct Node //定义树的元素
{
struct Node *lchild;
struct Node *rchild;
char data ;
int weight;
int D; //当前衰减量最大值
};
typedef struct Node * BiTREE ;

typedef enum{L, R} tagtype;

typedef struct
{
BiTREE ptr;
tagtype tag;
}stackNode;

typedef struct
{
stackNode Elem[100];
int top;
} Stack;

stackNode Pop( Stack &S )
{
stackNode c;
c=S.Elem[ S.top ];
S.top = S.top - 1 ;
return c;
}

void Push ( stackNode x, Stack &S )
{
S.top = S.top + 1 ;
S.Elem[ S.top ] = x ;
}

typedef struct
{
char data, parent, tag ;
int weight;
} Bnode;

typedef struct //队列
{
BiTREE ele[100];
int front,rear;
}Squeue;

void Makenull ( Squeue &Q)
{
Q.front = 0;
Q.rear =99;
}

BiTREE CreatBT( ) //按逻辑结构建立二叉树,依次输入结点、父结点、标号、权值
{
cout<<"请按逻辑结构输入二叉树"< Bnode p;
Squeue q;
BiTREE root, s;
root=new Node;
root=NULL;
Makenull(q);
cin>>p.data>>p.parent>>p.tag>>p.weight;
while (p.data!='0')
{
s=new Node;
s->data=p.data;
s->weight=p.weight;
s->lchild=s->rchild=NULL;
q.rear=(q.rear+1)%100;
q.ele[q.rear]=s;
q.front=(q.front+1)%100;
return q.ele[q.front];
if (p.parent=='0')
root=s;
else
{
while (q.rear+1%100!=q.front&&q.ele[q.front]->data!=p.parent)
q.front=(q.front+1)%100;
if (q.ele [q.front]->data==p.parent)
{
if (p.tag=='L')
q.ele[q.front]->lchild=s;
else
q.ele[q.front]->rchild=s;
}
}
cin>>p.data>>p.parent>>p.tag>>p.weight;
}
return root;
}

void Amplifier(BiTREE sb,int tolerance) //设置信号放大器函数
{
BiTREE bt;
Stack s;
stackNode x;
s.top=0;
bt=sb;
do
{
while (bt!=NULL)
{ x.ptr = bt;
x.tag = L;
Push(x,s);
bt = bt->lchild;
}
while (s.top!=0&& s.Elem[s.top].tag==R)
{
x = Pop(s);
bt = x.ptr;
if (bt->lchild==NULL&&bt->rchild==NULL)
{
bt->D=0; //初始化
}
else if (bt->lchild==NULL&&bt->rchild!=NULL)
{
bt->D=bt->rchild->D+bt->rchild->weight; //只有右子树时当前最大衰减量
if (bt->D>tolerance) //大于容忍值则设置放大器
{
cout<<"应该放置放大器的结点为:";
cout<rchild->data< bt->D=bt->rchild->weight;
}

        }
        else if (bt->lchild!=NULL&&bt->rchild==NULL)      //只有左子树时当前最大衰减量
        {
            bt->D=bt->lchild->D+bt->lchild->weight;
            if (bt->D>tolerance)                  //大于容忍值则放置放大器
            {
                cout<<"应该放置放大器的结点为:";
                cout<<bt->lchild->data<<endl;
                bt->D=bt->lchild->weight;
            }
        }
        else if ((bt->lchild->D+bt->lchild->weight)>(bt->rchild->D+bt->rchild->weight))           //左子树衰减量大于右子树
        {
            bt->D=bt->lchild->D+bt->lchild->weight;             //更新最大衰减量
            if (bt->D>tolerance)
            {
                cout<<"应该放置放大器的结点为:";              //放置放大器
                cout<<bt->lchild->data<<endl;
                if ((bt->rchild->D+bt->rchild->weight)>(bt->lchild->weight))         //进一步比较
                {
                    bt->D=bt->rchild->D+bt->rchild->weight;        //更新
                    if (bt->D>tolerance)
                    {
                        cout<<"应该放置放大器的结点为:";
                        cout<<bt->rchild->data<<endl;
                        if ((bt->rchild->weight)>(bt->lchild->weight))                //进一步比较
                            bt->D=bt->rchild->weight;
                        else
                            bt->D=bt->lchild->weight;       //更新
                    }
                }
                else
                    bt->D=bt->lchild->weight;        //更新
            }
        }
        else if ((bt->rchild->D+bt->rchild->weight)>=(bt->lchild->D+bt->lchild->weight))       //右子树衰减量大于左子树
        {
            bt->D=bt->rchild->D+bt->rchild->weight;
            if (bt->D>tolerance)
            {
                cout<<"应该放置放大器的结点为:";         //大于容忍值放置放大器
                cout<<bt->rchild->data<<endl;
                if ((bt->lchild->D+bt->lchild->weight)>(bt->rchild->weight))         //进一步比较
                {
                    bt->D=bt->lchild->D+bt->lchild->weight;         //更新
                    if (bt->D>tolerance)
                    {
                        cout<<"应该放置放大器的结点为:";
                        cout<<bt->lchild->data<<endl;
                        if ((bt->rchild->weight)>(bt->lchild->weight))                //进一步比较
                            bt->D=bt->rchild->weight;
                        else
                            bt->D=bt->lchild->weight;
                    }
                }
                else
                    bt->D=bt->rchild->weight;
            }
        }
    }
    if (s.top!=0)
    {
        s.Elem[s.top].tag =R;
        bt=s.Elem[s.top].ptr->rchild;
    }
}
while (s.top!=0);

}

int main() //主函数
{
printf("说明:1、输入二叉树信息时请按 当前结点、该结点父结点、左右标号、与父结点权值的顺序输入,均使用大写。\n");
printf(" 2、对于根结点请输入 结点、0、0、0\n");
printf(" 3、输入结束时请输入0、0、0、0\n\n");
BiTREE bt;
int tolerance;
bt=CreatBT();
cout<<"请输入容忍值:"< cin>>tolerance;
Amplifier(bt,tolerance); //设置放大器函数
return 0;
}

4个回答

两个注释,从代码来看这应该是不可用的代码。想要用,要自己把逻辑搞清楚后再修改。

 BiTREE CreatBT( ) //按逻辑结构建立二叉树,依次输入结点、父结点、标号、权值
{
    cout<<"请按逻辑结构输入二叉树"< Bnode p;
    Squeue q;
    BiTREE root, s;
    root=new Node;
    root=NULL;      **// 刚给 root new 了一个空间,接着又赋值为 NULL。这里看不懂,应该是错误的代码。**
    Makenull(q);
    cin>>p.data>>p.parent>>p.tag>>p.weight;
    while (p.data!='0')
    {
        s=new Node;
        s->data=p.data;
        s->weight=p.weight;
        s->lchild=s->rchild=NULL;
        q.rear=(q.rear+1)%100;
        q.ele[q.rear]=s;
        q.front=(q.front+1)%100;
        return q.ele[q.front];      **// 这里就 return 了,后面的代码不执行!!!**
        if (p.parent=='0')
            root=s;
        else
        {
            while (q.rear+1%100!=q.front&&q.ele[q.front]->data!=p.parent)
            q.front=(q.front+1)%100;
            if (q.ele [q.front]->data==p.parent)
            {
                if (p.tag=='L')
                    q.ele[q.front]->lchild=s;
                else
                    q.ele[q.front]->rchild=s;
            }
        }
        cin>>p.data>>p.parent>>p.tag>>p.weight;
    }
    return root;
}
xin008qiang
xin008qiang 非常感谢你,这是同学从网上down的,我又是半桶水,不过知道了方向就好办多了
接近 6 年之前 回复

BiTree 只是一个结构体,不是函数。你所说的 BiTree() 函数在哪里?

xin008qiang
xin008qiang 是我说错了,应该是CreatBT()函数。。
接近 6 年之前 回复

我也下载了一个,想问一下程序运行后该怎么输入,左右标号是什么?

单步调试的时候,查看各个变量的值,callstack

立即提问