| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 481 人关注过本帖
标题:二叉树主函数和子函数输出问题求助!!
取消只看楼主 加入收藏
larkyzc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-3-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
二叉树主函数和子函数输出问题求助!!
各位大神看看为什么main函数里面out(T)和run函数的out(T)输出的结果会不一样
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#include"conio.h"
struct matrix{
    int l;                    //记录矩阵长度
    char title[22][22];        //记录每位的字母
    int f[22];                //记录每列字母个数
};                        //记录矩阵状态
struct tree{
    char *names;               
    float num;
    struct tree *left;
    struct tree *right;
};                            //记录二叉树
float usk[4][22][22]={
  //A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V
    5,0,3,0,0,0,0,5,0,0,0,3,1,0,0,0,5,0,0,0,0,0,//s_A
    3,5,0,0,5,0,1,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,//B
    0,0,5,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,//C
    0,5,0,5,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,//D
    0,0,0,0,5,0,0,0,0,0,0,3,0,0,0,0,0,0,3,0,0,0,//E
    1,0,0,3,0,5,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,//F
    0,0,0,0,0,0,5,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,//G
    0,0,3,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//H
    0,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,1,0,0,0,0,1,//I
    1,0,0,0,3,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,//J
    0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,//K
    0,0,5,0,0,0,3,0,0,0,0,5,0,0,0,0,0,3,0,0,0,0,//L
    0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,3,0,1,//M
    0,0,0,0,5,0,0,0,3,3,0,0,0,5,0,5,0,0,0,0,0,0,//N
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,//O
    0,3,0,0,0,0,0,0,0,0,0,5,0,0,0,5,0,0,0,0,0,0,//P
    0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,//Q
    0,0,0,1,0,3,0,0,5,0,0,3,0,0,0,0,0,5,0,0,0,0,//R
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,5,0,5,0,//S
    1,0,3,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,5,0,0,//T
    0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,5,5,//U
    0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,3,0,0,0,5 //V
};//初始矩阵
void out(tree ls);
matrix ma[2];
tree ercha[22];   
tree *t[22];        //用于指向所有字母
    //用于每次更新矩阵所排出的关联度

//初始化矩阵和树
void install(){
    int i,j;
    ma[0].l=ma[1].l=22;
    for(i=0;i<22;i++){
        ma[1].f[i]=ma[0].f[i]=1;
        ma[1].title[i][0]=ma[0].title[i][0]=65+i;
    }//初始化每列字母信息

    for(i=0;i<22;i++){
        ercha[i].names=&ma[1].title[i][0];
        ercha[i].num=100;
        ercha[i].left=ercha[i].right=NULL;
        t[i]=&ercha[i];
    }//为每个字母构造一棵树

    for(i=0;i<22;i++){
        for(j=0;j<22;j++)
            usk[3][i][j]=usk[0][i][j];
    }//保存初始矩阵
}
        
//计算初始关联度
void count(){
    int i,j,k;
    float m,n;    //分子,分母
    for(i=0;i<22;i++)
        for(j=i+1;j<22;j++){
            m=0;
            n=0;   
            for(k=0;k<22;k++){
                n+=usk[3][k][i]+usk[3][k][j];
                if(usk[3][k][i] && usk[3][k][j])
                    m+=usk[3][k][i]+usk[3][k][j];
            }
            usk[2][j][i]=usk[1][j][i]=m/n;    //将初始关联度保存到第三个矩阵
        }
}

//更新矩阵
void run(int s,tree *T){
    int i,j,a,b,c;
    int k=s%2;
    int m=1,n=0;
    float unite[22];    //保存联合关联度
    for(i=0;i<22;i++){
        for(j=0;j<22;j++)
            usk[(s+1)%2][i][j]=0;
    }    //清空另一个矩阵
    for(i=0;i<MA[K].L;I++){

        for(j=i+1;j<MA[K].L;J++){
            if(usk[k][j][i]>usk[k][m][n]){
                m=j;
                n=i;    //记住最大的关联度的行与列
            }
        }
    }
    T->num=usk[k][m][n];        //保留最大关联度
    if(m<N){
        i=m;
        m=n;
        n=i;
    }
    T->left=t[m];
    T->right=t[n];
    out(*T);
    system("pause");
    //排序关联度最大的两列
    printf("新的联合关联度为:");
    for(i=0,j=0;i<MA[K].L;I++){
            if(i!=m && i!=n){
                unite[j]=(usk[k][i][m]+usk[k][i][n]+usk[k][m][i]+usk[k][n][i])/2;
                printf("  %.3f",unite[j]);
                j++;
            }
    }
    printf("\n");
   
    //去掉排序关联度最大的两列,并把后面的往前推进
    for(i=0;i<MA[K].L;I++){
        if(i==m || i==n)
            continue;
        if(i>m)
            c=2;
        else if(i>n)
            c=1;
        else c=0;
        t[i-c]=t[i];
        for(j=0;j<MA[K].L;J++){
            if(j==m || j==n)
                continue;
            if(i>m)
                a=2;
            else if(i>n)
                a=1;
            else a=0;
            if(j>m)
                b=2;
            else if(j>n)
                b=1;
            else b=0;
            usk[(s+1)%2][i-a][j-b]=usk[k][i][j];//将剩下的关联度保存到另一个矩阵里(压缩矩阵)
        }
    }
    t[i-2]=T;//将取出的行和列的字母转到最后的行和列
    for(i=0;i<MA[K].L;I++)
        usk[(s+1)%2][ma[k].l-2][i]=unite[i];    //将联合关联度保存到矩阵中
    for(i=0;i<MA[K].L;I++){
        if(i==m || i==n)
            continue;
        if(i>m)
            a=2;
        else if(i>n)
            a=1;
        else a=0;
        for(j=0;j<MA[K].F[I];J++)
            ma[(s+1)%2].title[i-a][j]=ma[k].title[i][j];
        ma[(s+1)%2].f[i-a]=ma[k].f[i];
    }
    b=0;
    for(j=0;j<MA[K].F[M];J++){
        ma[(s+1)%2].title[i-2][b]=ma[k].title[m][j];
        b++;
    }
    for(j=0;j<MA[K].F[N];J++){
        ma[(s+1)%2].title[i-2][b]=ma[k].title[n][j];
        b++;
    }
    ma[(s+1)%2].f[i-2]=ma[k].f[m]+ma[k].f[n];
    ma[(s+1)%2].l=ma[k].l-1;
}
void display(int s){
    int i,j;
    printf("\nThe Maxtrix %d\n",ma[s%2].l);
    for(i=0;i<MA[S%2].L;I++){
        for(j=0;j<MA[S%2].F[I];J++)
            printf("%c",ma[s%2].title[i][j]);
        putchar('\n');
    }
    getchar();
}
void out(tree s){
    tree ls=s;
    if(ls.left && ls.right){
        out(*ls.left);
        out(*ls.right);}
    else if(ls.num==100)
    printf(" %s",ls.names);

}

void main(){
    int i;
    tree T;
    getchar();
    install();
    count();
    for(i=0;ma[i%2].l>2;i++){
        run(i+1,&T);
        out(T);
        system("pause");
        display(i);
    }

    out(T);
    getchar();
}
搜索更多相关主题的帖子: 记录 矩阵 include 二叉树 names 
2012-05-13 13:20
larkyzc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-3-8
收藏
得分:0 
无大神解答吗?这问题纠结了好久了,求救求救!!!
2012-05-13 15:49
larkyzc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-3-8
收藏
得分:0 
就是同一个树,在不同函数里面数出来不一样。。。。一直搞不出来啊。。。。。
2012-05-13 19:30
快速回复:二叉树主函数和子函数输出问题求助!!
数据加载中...
 
   



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

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