User forums > Help

Is the result of "printf" and "debug watch" different?

(1/4) > >>

hnust_zhangyehua:
I have to ask for a help, for I meet this problem twice. The problem is that the result , which is printed by the "printf" and which is showed by the "debug watches" is different. For example, the value of integer a printed by the "printf" is 0, while the other is  1.
Can anyone help me?

BlueHazzard:
not without example code...

hnust_zhangyehua:
#include <iostream>
#include<cstdio>
using namespace std;
int m=0,k=0,n,c[100]={0},f=0,g;
typedef struct LNode
{
    int data;
    struct LNode *lchild,*rchild;
}LNode,*List;
void bl(List &T)//遍历后应该输出的数按顺序存入数组c中。
{
    if(T)
    {
        bl(T->rchild);
        c[f]=T->data;
        printf("c[%d]=%d\n",f,c[f]);/* ********这条语句在VC和CB中跑的结果不一样******** */
        f++;
        bl(T->lchild);
    }
}
void creat(List &T,int e)//创建树
{
    if(!T)
    {
        LNode *S;
        S=new LNode;
        S->data=e;
        S->lchild=S->rchild=NULL;
        T=S;
    }
    else if(e>T->data)creat(T->rchild,e);
    else if(e<T->data)creat(T->lchild,e);
}
List search1(List &T,int e)//查找树的节点,用m来记载节点的层数;即深度。
{
    if((!T)||e==T->data)return T;
    else if(T)
    {
    m++;
    if(e>T->data)return search1(T->rchild,e);
    if(e<T->data)return search1(T->lchild,e);
    }
    //return m;
}
int main()
{
    int a[100],b[100],z[100],i,s,F,w,x;
    LNode *Q;
    while((scanf("%d",&n)!=EOF))
    {
        m=w=x=k=f=g=0,F=1;
        Q=NULL;
        Q=new LNode;
        Q->lchild=Q->rchild=NULL;
        //cin>>n;//cout<<'123'<<'\n';
        if(n<0||n>100)break;
        for(i=0;i<n;i++)
        {
            cin>>a;
        }


        for(w=1; w<n; w++)
        {
            for(x=0; x<w; x++)
            {
                if(a[x]==a[w])
                    break;
                //z[w]=a[w];
            }
            if(x==w)
            {
                z[F]=a[w];
                F++;
            }
        }
        z[0]=a[0];


        for(i=0;i<F;i++)
        {
            creat(Q,z);
        }
        bl(Q);
        for(i=0;i<F;i++)
        {
            search1(Q,c);//用数组b存储树的层数;即深度
            b[k]=m;k++;
            m=0;
        }
        for(i=0;i<F;i++)
        {
            for(g=0;g<4*(b-1);g++)
            {
                 cout<<" ";
            }
            cout<<c<<endl;
            g=0;
        }
    }
    return 0;
}
/*
1725: 二叉查找树(Ⅱ)-文本显示
时间限制: 1 Sec  内存限制: 128 MB
提交: 425  解决: 204
[提交][状态][讨论版]
题目描述
本任务是在空二叉查找树的基础上,依次插入一些关键字,然后文本方式显示该树。要求树中不能有重复关键字。


输入
由多组数据 组成。

每组数据由两行组成。第一行是待插入关键字的数目n(1<=n<=100)。第2行是n个空格分开的正整数,值不超过100。


输出
对于每一组数据,显示对应的二叉查找树,相当于常规树形左旋90度。见样例。 注意每一层缩进为4,每一行行尾没有空格符号。


input data
6
4 2 1 5 3 6

output data
        6
    5
4
        3
    2
        1


}*/

hnust_zhangyehua:
The input data is at the bottom

BlueHazzard:
pls use code tags (the # symbol in the forum editor) to post code, it is more easy to read the code...


i can't find any discrepancy between debugger and printf... But i had to correct your code in a lot places, because the formatting is wrong
, better you repaste the code in code tags...

Navigation

[0] Message Index

[#] Next page

Go to full version