程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 樹-關於信號放大器的c++問題,求解答

樹-關於信號放大器的c++問題,求解答

編輯:編程綜合問答
關於信號放大器的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;
}

最佳回答:


兩個注釋,從代碼來看這應該是不可用的代碼。想要用,要自己把邏輯搞清楚後再修改。

 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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved