| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 573 人关注过本帖
标题:各位大神过来看看这个程序怎么写。拜托了!
只看楼主 加入收藏
初学计算机
Rank: 1
等 级:新手上路
帖 子:16
专家分:6
注 册:2012-3-21
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:6 
各位大神过来看看这个程序怎么写。拜托了!
题目.查找算法实现与性能分析
功能要求:根据菜单选择相应算法,分别实现顺序查找、折半查找、二叉排序树(建立、删除、查找)算法。存储结构自己选择。
输入:data.txt(文件名,从文件读取待查找数据序列)
      待查找的键值
输出:各种算法的查找结果。
      (1)查找结果(查找到的位置或者找不到)
      (2)查找次数
搜索更多相关主题的帖子: 算法 
2012-05-31 14:25
初学计算机
Rank: 1
等 级:新手上路
帖 子:16
专家分:6
注 册:2012-3-21
收藏
得分:0 
大家都来帮一下忙呀!
2012-06-01 17:12
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:10 
你有哪些疑点不懂,列出来,偶一一给你回答。
是需求分析不明确?还是不会设计构架?
2012-06-01 18:11
初学计算机
Rank: 1
等 级:新手上路
帖 子:16
专家分:6
注 册:2012-3-21
收藏
得分:0 
回复 3楼 silent_world
谢谢了,   我现在读取文件可以了 ,但是我们刚刚学了数据结构,我学的特别烂,我不知道怎么将文件中的东西放在顺序表当中!还请赐教啊!
2012-06-01 20:53
会分手的空气
Rank: 2
来 自:湖北孝感
等 级:论坛游民
帖 子:26
专家分:32
注 册:2012-3-30
收藏
得分:10 
你要说清楚哪些具体的问题啊 ,,这样才好回答你啊啊
2012-06-03 08:44
初学计算机
Rank: 1
等 级:新手上路
帖 子:16
专家分:6
注 册:2012-3-21
收藏
得分:0 
回复 5楼 会分手的空气
我需要打开一个文件,然后将文件构造成以下的二叉树然后进行操作
这是要求:题目.查找算法实现与性能分析
功能要求:根据菜单选择相应算法,分别实现顺序查找、折半查找、二叉排序树(建立、删除、查找)算法。存储结构自己选择。
输入:data.txt(文件名,从文件读取待查找数据序列)
      待查找的键值
输出:各种算法的查找结果。
      (1)查找结果(查找到的位置或者找不到)
      (2)查找次数
我已经把前面的顺序和折半查找都做完了,但是二叉树那个不会啊!
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define  INFMT  "%d"
#define  OUTFMT "%d "
/* #define  NULL 0L */
#define  BOOL int
#define  TRUE 1
#define  FALSE 0
#define  LEN    10000

typedef int ElemType;
typedef struct BSTNode
{
 ElemType data;
 struct BSTNode *lchild, *rchild;
} BSTNode, *BSTree;

/* 插入新节点 */
void Insert(BSTree *tree, ElemType item)
{
 BSTree node = (BSTree)malloc(sizeof(BSTNode));
 node->data = item;
 node->lchild = node->rchild = NULL;

 if (!*tree)
  *tree = node;
 else
 {
  BSTree cursor = *tree;

  while (1)
  {
   if (item < cursor->data)
   {
    if (NULL == cursor->lchild)
    {
     cursor->lchild = node;
     break;
    }

    cursor = cursor->lchild;
   }
   else
   {
    if (NULL == cursor->rchild)
    {
     cursor->rchild = node;
     break;
    }

    cursor = cursor->rchild;
   }
  }
 }
 return;
}

/* 查找指定值 */
BSTree Search(BSTree tree, ElemType item)
{
 BSTree cursor = tree;

 while (cursor)
 {
  if (item == cursor->data)
   return cursor;
  else if ( item < cursor->data)
   cursor = cursor->lchild;
  else
   cursor = cursor->rchild;
 }

 return NULL;
}

/* 中缀遍历 */
void Inorder(BSTree tree)
{
 BSTree cursor = tree;

 if (cursor)
 {
  Inorder(cursor->lchild);
  printf(OUTFMT, cursor->data);
  Inorder(cursor->rchild);
 }
}

/* 回收资源 */
void Cleanup(BSTree tree)
{
 BSTree cursor = tree, temp = NULL;

 if (cursor)
 {
  Cleanup(cursor->lchild);
  Cleanup(cursor->rchild);
  free(cursor);
 }
}

/* 产生一组随机数 */
void randnum(int *a, int s)
{
 int i, j, mod = s * 10;
 srand(time(NULL));

 for (i = 0; i < s; ++i)
 {
  a[i] = rand() % mod + 1;

  for (j = 0; j < i; ++j)
  {
   if (a[i] == a[j])
   {
    a[i] = rand() % mod + 1;
    j = -1;
    continue;
   }
  }
 }
}

int main()
{
    int i;
    //char ch [100] = {0};
    char find;   
    long num = 0;

nihao:
    printf ( "请输入文件所在目录及文件名:\n");
    gets (ch); //scanf ("%s",ch);
    FILE *fp;
    fp = fopen ("a.txt","r");
    if (!fp)
        
    {
        printf   ( "打开文件%s失败!");
        printf   ( "打开文件%s失败!\n\n请重新输入:\n",ch);
        goto nihao ;
    }
    int x1 = 1;
    SeqList R;
    while ( !feof ( fp ) )
    {
        fscanf (fp,"%c", &R[x1].key);
        x1++;
    }
    num = x1 - 2;//num用来统计文件中的字符个数

//我的问题就是这一块怎么将他们串联起来!

 ElemType item;
 char choice;
 BSTree root = NULL, ret; /* 必须赋予NULL值,否则出错 */
 BOOL finish = FALSE;

 printf("***欢迎使用二叉排序树演示程序***\n\n");
 printf("请选择创建树的方式:\n");
 printf("1. 手动输入数据创建二叉排序树\n");
 printf("2. 自动产生数据创建二叉排序树\n");

 do
 {
  scanf("%c", &choice);
  getchar();

  if (choice == '1' || choice == '2')
   finish = TRUE;

 } while (FALSE == finish);


 switch (choice)
 {
 case '1':
  {
   printf("请输入数据(-10000结束):\n");

   while (1)
   {
    scanf(INFMT, &item);

    if (-10000 != item)
     Insert(&root, item);
    else
     break;
   }
   break;
  }
 case '2':
  {
   int ia[LEN], i = 0, loop = LEN;
   randnum(ia, LEN);

   while (loop--)
   {
    Insert(&root, ia[i++]);
   }

   break;
  }
 }

 printf("\n\n创建完成...\n");
 Inorder(root);
 printf("\n\n");

 /* 二叉排序树的查找测试 */
 do
 {
  printf("\n请输入查找数据:");
  scanf("%d", &item);
  getchar();
  printf("Searching...\n");
  ret = Search(root, item);

  if (NULL == ret)
   printf("查找失败!");
  else
   printf("查找成功!");

  printf("\n继续测试按y,退出按其它键。\n");
  choice = getchar();
 } while (choice=='y'||choice=='Y');

 Cleanup(root);
fclose(fp);
return 0 ;
}

2012-06-03 23:23
初学计算机
Rank: 1
等 级:新手上路
帖 子:16
专家分:6
注 册:2012-3-21
收藏
得分:0 
回复 3楼 silent_world
拜托你了,下面有我的问题具体所在,麻烦帮我看一下吧!
2012-06-03 23:25
快速回复:各位大神过来看看这个程序怎么写。拜托了!
数据加载中...
 
   



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

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