用文本读取的方式,没有输出结果,可能读取方式不对?哪位大佬帮忙看看啊

#define elementType char #define MaxLen 100 #include #include #include typedef struct blNode { elementType data; struct blNode *lchild,*rchild; }btNode,BiTree; void ReadFileToArray(char fileName[],char strLine[100][3],int &nArrLen) { FILE *pFile; char str[1000]; pFile=fopen("bt151.btr","r"); if(!pFile) { printf("二叉树数据文件打开失败"); } //文件读取第1行,判断二叉树标识BinaryTree是否正确 if(fgets(str,1000,pFile)!=NULL) { if(strcmp(str,"BinaryTree\n") != 0) { printf("打开的文件格式错误!\n"); fclose(pFile);//关闭文件 } } nArrLen=0; while(fscanf(pFile,"%c %c %c\n",&strLine[nArrLen][0],&strLine[nArrLen][1],&strLine[nArrLen][2])!=EOF) { nArrLen++; printf("%c %c %c\n",strLine[nArrLen][0],strLine[nArrLen][1],strLine[nArrLen][2]); } fclose(pFile);//关闭文件 } void CreateBiTreeFromFile(btNode *&pBT,char strLine[100][3],int nLen,int &nRow) { /×rLine[100][3]为保留结点数据的二维数组 //nLen为结点个数 //nRow为数组当前序号 int nRowNext; if((nRow>=nLen) || (nLen==0)) printf("ERROR"); pBT=(btNode*)malloc(sizeof(btNode)); pBT->data=strLine[nRow][0]; pBT->lchild=NULL; pBT->rchild=NULL; nRowNext=nRow; if(strLine[nRowNext][1]=='1') { //当前节点有左子树,读取下一行数据,递归调用创建左子树 nRow++; CreateBiTreeFromFile(pBT->lchild,strLine,nLen,nRow); } if(strLine[nRowNext][2]=='1') { nRow++; CreateBiTreeFromFile(pBT->rchild,strLine,nLen,nRow); } } void visit(btNode *T) { if(T==NULL) { printf("empty tree,ERROR"); } else printf("%c\t",T->data); } void PreOrder(btNode *T)//先序遍历 { if(T) { visit(T); PreOrder(T->lchild); PreOrder(T->rchild); } } void Inorder(btNode *T)//中序遍历 { if(T) { Inorder(T->lchild); visit(T); Inorder(T->rchild); } } void PostOrder(btNode *T)//后序遍历 { if(T) { PostOrder(T->lchild); PostOrder(T->rchild); visit(T); } }int main (void) { int ispal; int nRow=0; BiTree T; btNode *pT; char fileName[MaxLen],strLine[100][3]; int nArrLen; btNode pBT; ReadFileToArray(fileName,strLine,nArrLen); CreateBiTreeFromFile(pT,strLine,nArrLen,nRow); PreOrder(&T); Inorder(&T); PostOrder(&T); }

    while (fscanf(pFile, "%c    %c    %c\n", &strLine[nArrLen][0], &strLine[nArrLen][1], &strLine[nArrLen][2]) != EOF) {
        printf("%c    %c    %c\n", strLine[nArrLen][0], strLine[nArrLen][1], strLine[nArrLen][2]);
        nArrLen++;
    }

把 ++ 放在打印的后面

这代码,别人没办法给你看啊,你电脑端发代码段的方式发出来。

#define elementType char
#define MaxLen 100

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct blNode {
    elementType data;
    struct blNode *lchild, *rchild;
} btNode, BiTree;

void ReadFileToArray(char fileName[], char strLine[100][3], int *nArrLen) {
    FILE *pFile;
    char str[1000];
    pFile = fopen("book.dat", "r");
    if (!pFile) {
        printf("二叉树数据文件打开失败");
    }
    //文件读取第1行,判断二叉树标识BinaryTree是否正确
    if (fgets(str, 1000, pFile) != NULL) {
        if (strcmp(str, "BinaryTree\n") != 0) {
            printf("打开的文件格式错误!\n");
            fclose(pFile);//关闭文件
        }
    }

    *nArrLen = 0;
    while (fscanf(pFile, "%c    %c    %c\n", &strLine[*nArrLen][0], &strLine[*nArrLen][1], &strLine[*nArrLen][2]) != EOF) {
        printf("%c    %c    %c\n", strLine[*nArrLen][0], strLine[*nArrLen][1], strLine[*nArrLen][2]);
        (*nArrLen)++;
    }
    fclose(pFile);//关闭文件

}


void CreateBiTreeFromFile(btNode *pBT, char strLine[100][3], int nLen, int * nRow) {    //strLine[100][3]为保留结点数据的二维数组
    //nLen为结点个数
    //nRow为数组当前序号
    int nRowNext;
    if ((*nRow >= nLen) || (nLen == 0))
        printf("ERROR");
    pBT->data = strLine[*nRow][0];
    pBT->lchild = NULL;
    pBT->rchild = NULL;
    nRowNext = *nRow;
    if (strLine[nRowNext][1] == '1') {
        // 必须要提前分配好内存
        pBT->lchild = (btNode *) malloc(sizeof(btNode));
        //当前节点有左子树,读取下一行数据,递归调用创建左子树
        (*nRow)++;
        CreateBiTreeFromFile(pBT->lchild, strLine, nLen, nRow);
    }
    if (strLine[nRowNext][2] == '1') {
        // 必须要提前分配好内存
        pBT->rchild = (btNode *) malloc(sizeof(btNode));
        (*nRow)++;
        CreateBiTreeFromFile(pBT->rchild, strLine, nLen, nRow);
    }
}

void visit(btNode *T) {
    if (T == NULL) {
        printf("empty tree,ERROR");
    } else
        printf("%c\t", T->data);
}

void PreOrder(btNode *T)//先序遍历
{
    if (T) {
        visit(T);
        PreOrder(T->lchild);
        PreOrder(T->rchild);
    }
}

void Inorder(btNode *T)//中序遍历
{
    if (T) {
        Inorder(T->lchild);
        visit(T);
        Inorder(T->rchild);
    }
}

void PostOrder(btNode *T)//后序遍历
{
    if (T) {
        PostOrder(T->lchild);
        PostOrder(T->rchild);
        visit(T);
    }
}
void test(btNode *pBT, int num)
{
    if (num >= 3)
        return;
    pBT->data = 'A' + num;
    pBT->lchild = (btNode *) malloc(sizeof(btNode));
    pBT->rchild = (btNode *) malloc(sizeof(btNode));
    test(pBT->lchild, num + 1);
    test(pBT->rchild, num + 1);
}
int main(void) {
    int nRow = 0;
    // 必须要提前分配好内存
    btNode *pT = (btNode *) malloc(sizeof(btNode));
    char fileName[MaxLen], strLine[100][3];
    int nArrLen;

    ReadFileToArray(fileName, strLine, &nArrLen);

    CreateBiTreeFromFile(pT, strLine, nArrLen, &nRow);
    PreOrder(pT);
    putchar('\n');
    Inorder(pT);
    putchar('\n');
    PostOrder(pT);

}

 

你用电脑端登录,然后用代码段控件来发,直接发文字,有些符号会被过滤掉。

#define elementType char
#define MaxLen 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
typedef struct blNode
{
    elementType data;
    struct blNode *lchild,*rchild;
}btNode,BiTree;
 
void ReadFileToArray(char fileName[],char strLine[100][3],int &nArrLen)
{
    FILE *pFile;
    char str[1000];
    pFile=fopen("bt151.btr","r");
    if(!pFile) 
    {
        printf("二叉树数据文件打开失败");
    } 
 //文件读取第1行,判断二叉树标识BinaryTree是否正确
 if(fgets(str,1000,pFile)!=NULL)
 {
     if(strcmp(str,"BinaryTree\n") != 0)
     {
         printf("打开的文件格式错误!\n");
         fclose(pFile);//关闭文件 
     }
  } 

  nArrLen=0;
    while(fscanf(pFile,"%c    %c    %c\n",&strLine[nArrLen][0],&strLine[nArrLen][1],&strLine[nArrLen][2])!=EOF)
    {
        nArrLen++; 
        printf("%c    %c    %c\n",strLine[nArrLen][0],strLine[nArrLen][1],strLine[nArrLen][2]);
     } 
     fclose(pFile);//关闭文件
      
}


void CreateBiTreeFromFile(btNode *&pBT,char strLine[100][3],int nLen,int &nRow)
{    //strLine[100][3]为保留结点数据的二维数组
    //nLen为结点个数
    //nRow为数组当前序号
    int nRowNext;
    if((nRow>=nLen) || (nLen==0))
    printf("ERROR");
    pBT=(btNode*)malloc(sizeof(btNode));
    pBT->data=strLine[nRow][0];
    pBT->lchild=NULL;
    pBT->rchild=NULL;
    nRowNext=nRow;
    if(strLine[nRowNext][1]=='1') 
    {
        //当前节点有左子树,读取下一行数据,递归调用创建左子树
        nRow++;
        CreateBiTreeFromFile(pBT->lchild,strLine,nLen,nRow);
    }
    if(strLine[nRowNext][2]=='1')
    {
        nRow++;
        CreateBiTreeFromFile(pBT->rchild,strLine,nLen,nRow);
        
    
    }
}

void visit(btNode *T)
{
    if(T==NULL)
    {
        printf("empty tree,ERROR");
    }
    else
    printf("%c\t",T->data);
}
 
void PreOrder(btNode *T)//先序遍历 
{
    if(T)
    {
        visit(T);
        PreOrder(T->lchild);
        PreOrder(T->rchild);
    }    
}

void  Inorder(btNode *T)//中序遍历 
{
    if(T)
    {
        Inorder(T->lchild);
        visit(T);
        Inorder(T->rchild);
    }
}

void PostOrder(btNode *T)//后序遍历 
{
    if(T)
    {
        PostOrder(T->lchild);
        PostOrder(T->rchild);
        visit(T);
    }
}

int main (void)
{
    int ispal;
    int nRow=0;
    BiTree T;
    btNode *pT;
    char fileName[MaxLen],strLine[100][3];
    int nArrLen;
    btNode pBT;
    
 
    ReadFileToArray(fileName,strLine,nArrLen);

        CreateBiTreeFromFile(pT,strLine,nArrLen,nRow);
            PreOrder(&T);
            Inorder(&T);
            PostOrder(&T);

}
      

 

 

 

 

 

 

 

 

 

 

BinaryTree
A    1    1
B    1    1
C    1    1
D    0    0
E    0    0
F    1    1
G    0    0
H    0    0
I    1    1
J    1    1
K    0    0
L    0    0
M    1    1
N    0    0
O    0    0

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632