| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 720 人关注过本帖
标题:strcmp函数的用法问题
取消只看楼主 加入收藏
lxh30907
Rank: 1
等 级:新手上路
帖 子:22
专家分:9
注 册:2010-12-15
结帖率:60%
收藏
已结贴  问题点数:15 回复次数:2 
strcmp函数的用法问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
//二叉树
typedef struct BiTreeNode
{
    char book_name[20];
    char writer[10];
    int year;
    struct BiTreeNode * lchild;
    struct BiTreeNode * rchild;
}Book;

Book * InitBiTree()
{
     Book * BookList;
     BookList = (Book *)malloc(sizeof(Book));
     strcpy(BookList->book_name,"");
     strcpy(BookList->writer,"");
     BookList->year=-1;
     BookList->lchild=0;
     BookList->rchild=0;
     return BookList;
}


void Insert(Book * BookList, Book * newBook)
{
    if (strcmp(BookList->book_name,newBook->book_name)>0)  //这里输入<就无显示了
    {
        if (BookList->lchild==0)
            BookList->lchild=newBook;
        else
            Insert(BookList->lchild, newBook);//递归调用
    }

    else
    {
        if (BookList->rchild==0)
           BookList->rchild=newBook;
        else
           Insert(BookList->rchild, newBook);//递归调用
    }
}


void InOrder(Book * BookList)
{
    if (BookList==0) return;
    if (BookList->year!=-1)
    {
        InOrder(BookList->lchild);

        printf("%s\t%s\t%d\n",BookList->book_name,
                BookList->writer, BookList->year);
    }
    InOrder(BookList->rchild);
}


int main()
{
    Book * BOOK=0;
    Book * newBook=0;

    BOOK=InitBiTree();

    newBook = (Book *)malloc(sizeof(Book));
    strcpy(newBook->book_name,"c#程序设计教程");
    strcpy(newBook->writer,"李春葆");
    newBook->year=2010;
    newBook->lchild=0;
    newBook->rchild=0;
    Insert(BOOK, newBook);
    printf("加入一本书以后:\n");
    InOrder(BOOK);
    printf("\n");

    newBook = (Book *)malloc(sizeof(Book));
    strcpy(newBook->book_name,"实用数据结构");
    strcpy(newBook->writer,"陈明");
    newBook->year=2009;
    newBook->lchild=0;
    newBook->rchild=0;
    Insert(BOOK, newBook);
    printf("加入一本书以后:\n");
    InOrder(BOOK);

    printf("\n");
    newBook = (Book *)malloc(sizeof(Book));
    strcpy(newBook->book_name,"应用数学");
    strcpy(newBook->writer,"吴静");
    newBook->year=2011;
    newBook->lchild=0;
    newBook->rchild=0;
    Insert(BOOK, newBook);
    printf("加入一本书以后:\n");
    InOrder(BOOK);
    printf("\n");

    newBook = (Book *)malloc(sizeof(Book));
    strcpy(newBook->book_name,"职业生涯规划");
    strcpy(newBook->writer,"谢彩英");
    newBook->year=2011;
    newBook->lchild=0;
    newBook->rchild=0;
    Insert(BOOK, newBook);
    printf("加入一本书以后:\n");
    printf("书名\t\t作者\t出版年份\n");
    InOrder(BOOK);
    getch();
}
求解

[ 本帖最后由 lxh30907 于 2011-5-15 10:51 编辑 ]
搜索更多相关主题的帖子: return 二叉树 
2011-05-15 10:13
lxh30907
Rank: 1
等 级:新手上路
帖 子:22
专家分:9
注 册:2010-12-15
收藏
得分:0 
没人知道?
2011-05-15 21:02
lxh30907
Rank: 1
等 级:新手上路
帖 子:22
专家分:9
注 册:2010-12-15
收藏
得分:0 
回复 3楼 baobaoisme
if (strcmp(BookList->book_name,newBook->book_name)>0)  
在这里把 > 改成 < 就无显示了
   
2011-05-15 21:58
快速回复:strcmp函数的用法问题
数据加载中...
 
   



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

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