标题:求帮助,二叉排序树
只看楼主
shinee绿豆
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-17
结帖率:50%
 问题点数:0 回复次数:2 
求帮助,二叉排序树
编译是零错误零警告,也能运行,但是选择用二叉排序树进行查找,就不能运行。求大家帮忙找找错(用的是VC 6.0)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <memory.h>
#define MAXSIZE 50
#define FILENAME "F:\\学生信息.txt"

struct massage
{
    char number[MAXSIZE];//学号
    char name[MAXSIZE];//姓名
    double score;//成绩
};
/***************读取文本文件***************/
int readfile (struct massage A[ ])
{
    int i=0;
    FILE *fp;
    if ((fp=fopen(FILENAME,"r"))==NULL)                     //提示文件打开情况
    {
        printf("\n");
        puts ("文件打开失败!");
        return 0;
    }
    while (1)
    {
        fscanf (fp,"%s%s%lf",&A[i].number,&A[i].name,&A[i].score);     //信息从文件中读出
        if (feof(fp))
            break;
        i++;
    }
    fclose(fp);
    printf("\n");
    puts ("从文件中读取数据成功!");                         //提示数据读出情况
    printf("\n");
    return i;                                               //返回i值,即学生人数
}
/***************写入文本文件***************/
void writefile (struct massage A[ ],int n)
{
    int i;
    FILE *fp;
    if ((fp=fopen(FILENAME,"w"))==NULL)                     //提示文件打开情况
    {
        printf("\n");
        puts ("文件打开失败!");
        exit (0);
    }
    for (i=0;i<n;i++)
        fprintf (fp,"%s\t%s\t%lf\n",A[i].number,A[i].name,A[i].score);  //信息写入文件中
    fclose(fp);
    printf("\n");
    puts ("数据写入成功!");
    printf("\n");
}
/***************信息的录入***************/
void input (struct massage A[ ],int n)
{
    int i;
    for (i=0;i<n;i++)
    {
        printf("\n");
        printf("**********请输入第%d个学生的相关信息**********\n",i+1);   //提示输入学生个数
        printf("学号:");                           //输入学号
        scanf("%s",&A[i].number);
        printf("姓名:");                            //输入姓名
        scanf("%s",&A[i].name);
        printf("成绩:");                            //输入成绩
        scanf("%lf",&A[i].score);                        
        printf("\n");
    }
    puts("录入成功!");
}
/***************信息的输出***************/
void output (struct massage A[ ],int n)
{
    int i;
    puts ("           ==================信息列表==================");  //信息输出的形式
    printf ("              学号\t         姓名\t            成绩\n");
    for (i=0;i<n;i++)
    {
        printf ("              %s\t         %s\t           %.2lf\n",&A[i].number,&A[i].name,A[i].score);
    }
}
/***************顺序查找*****************/
void SSearch(struct massage A[],int n)
{
    int i;
    char numberSS[50];
    printf ("请输入要查找的学生的学号:");
    scanf ("%s",&numberSS);
    for (i=0;i<n;i++)
    {
        if (strcmp(A[i].number,numberSS) == 0)
            printf("该同学的成绩为:%.2lf",A[i].score);
    }
}
/***************折半查找*****************/
double BSearch(struct massage A[],char key[],int low,int high)
{
    int mid;
    if(low > high)
        return -1;
    mid = (low + high)/2;
    if (strcmp(A[mid].number,key) == 0)
        return A[mid].score;
    else if(strcmp(A[mid].number,key) > 0)
            return BSearch(A,key,low,mid-1);
         else
             return BSearch(A,key,mid+1,high);
}
/**************二叉排序树查找****************/
# define ENDKEY 0
typedef  struct node {
    struct massage  data[MAXSIZE];
    struct  node  *lchild,*rchild;
}BSTNode,*BSTree;
BSTNode * InsertBST(BSTree root,int n,struct massage A[])
//在二叉排序树bst中不存在关键字等于key的元素,插入该元素
{
    int i;
    BSTree r,s;
    BSTNode *p,*q;
    for (i=0;i<n;i++)
    {
        if  (root==NULL)
        {
            s=(BSTree)malloc(sizeof(BSTNode));
            strcmp(s->data[i].number, A[i].number);
            strcmp(s->data[i].name , A[i].name);
            s->data[i].score=A[i].score;
            s->lchild=NULL;
            s->rchild=NULL;
            root=s;
        }
        else
        {           //建立新结点
             r=(BSTree)malloc(sizeof(BSTNode));
             strcmp(r->data[i].number, A[i].number);
             strcmp(r->data[i].name , A[i].name);
             r->data[i].score=A[i].score;
             r->lchild=NULL;      
             r->rchild=NULL;
   
             p=root;  
             for (i=0;i<n;i++)         //寻找新结点的插入位置
             {
                 q=p;
                 if(strcmp(r->data[i].number, A[i].number) > 0)  
                     p=p->lchild;
                 else
                     p=p->rchild;
             }
             if (strcmp(q->data[i].number, A[i].number) == 0)   
                 return root;
             if (strcmp(q->data[i].number, A[i].number) > 0)   
                 q->lchild=r;    //插入r做q的左孩子   
             else
                 q->rchild=r;     //插入r做q的右孩子
        }
    }
            return root;
}
BSTree   SearchBST (BSTree  bst,char key[],int n)
/* 在根结点bst所指的二叉排序树中,递归查找关键字等于key的元素,
若查找成功,返回指向该元素结点的指针,否则返回空指针*/
{
    int i;
    BSTree  q;      
    q=bst;
    for (i=0;i<n;i++)
    {printf("ddddddddddd");
        if(strcmp(q->data[i].number,key) == 0)//查找成功
        {
            printf("该同学的成绩为:%.2lf",q->data[i].score);
            return  q;
        }  
        if(strcmp(q->data[i].number,key) > 0)
            q=q->lchild;  //在左子树中查找
        else
            q=q->rchild;             //在右子树中查找
    }
    return  NULL;          //查找失败
}
/*******************查询*********************/
void menu_s(int n,struct massage A[])
{
    int m;
    system("cls");
    printf ("\n");
    puts ("         ============按学号查找对应的成绩============");
    puts ("         ==      1.顺序查找                        ==");
    puts ("         ==      2.折半查找(递归)                ==");
    puts ("         ==      3.二叉排序树查找                  ==");
    puts ("         ============================================");
    printf ("请选择:");
    scanf ("%d",&m);     //接受选择,显示二级菜单,进行选择
    printf("\n");
    if(m == 1)
    {
        SSearch(A,n);
        printf("\n");
    }
    if(m == 2)
    {
        int low = 0,high = n-1;
        char key[50];
        printf ("请输入要查找的学生的学号:");
        scanf ("%s",&key);
        double scoreBS;
        scoreBS = BSearch(A,key,low,high);
        printf ("该同学的成绩为:%.2lf",scoreBS);
        printf("\n");
    }
    if(m == 3)
    {
        char key[50];
        BSTree bst=NULL;
        printf ("请输入要查找的学生的学号:");
        scanf("%s,",&key);
        if(SearchBST( bst,key,n))
            printf("search  is  success");
        else
            printf("this key is not exist!!");
        
        printf("\n");
    }
}
/*******************主函数*********************/
void main ( )
{
    struct massage A[MAXSIZE];
    int n,choice;
    memset(A, 0, sizeof(A));    //将数组进行初始化
    while (1)
    {
        system ("cls");
        puts ("          ================主菜单================");
        puts ("          ==    1.学生信息的录入              ==");
        puts ("          ==    2.学生信息的输出              ==");
        puts ("          ==    3.学生信息的查询              ==");
        puts ("          ==    0.退出                        ==");
        puts ("          ======================================");
        printf("\n");
        printf ("请选择:");
        scanf ("%d",&choice);
        printf("\n");
        if (choice==0)
        {
            writefile (A,n);                                  //首次结束操作后,将信息存入文件中
            printf("\n");
            puts ("谢谢使用!");
            return;
        }
        switch (choice)
        {
            case 1:n=readfile(A);
                   if (n==0)
                   {
                       printf ("请输入此次所需要记录的学生人数(n<50):");
                       scanf ("%d",&n);
                       input (A,n);
                   }
                   break;
            case 2:output (A,n);break;
            case 3:menu_s (n,A);break;
        }
        printf("\n");
        puts ("按任意键返回主菜单......");
        getch ( );
    }
}
搜索更多相关主题的帖子: 文本文件 include double number 信息 
2016-12-24 20:11
marlow
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:125
专家分:419
注 册:2016-7-18
得分:0 
粗略看一下,没见你在main中调用insertBst(),建议写一个printbst(),看二叉树是否成功建立

一切都在学习、尝试、摸索中
2016-12-24 23:47
shinee绿豆
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-11-17
得分:0 
回复 2楼 marlow
哈哈谢谢啊
2016-12-25 11:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-472888-1-1.html




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

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