颜颜颜M 2017-04-15 15:28 采纳率: 100%
浏览 1507
已采纳

C语言中random和time函数的问题

color=#FFFF00][color=#FFCC00]C语言中的一个数据查找的实验
程序可以编译通过,但是连接的时候就会出bug,小白表示不知道怎么改了,请大神赐教
附连接时代码:[/color]
--------------------Configuration: schtb - Win32 Debug--------------------
Linking...
schtb.obj : error LNK2001: unresolved external symbol "int __cdecl random(int)" (?random@@YAHH@Z)
schtb.obj : error LNK2001: unresolved external symbol "int __cdecl random(int)" (?random@@YAHH@Z)
schtb.obj : error LNK2001: unresolved external symbol "int __cdecl random(int)" (?random@@YAHH@Z)
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/schtb.exe : fatal error LNK1120: 3 unresolved externals
执行 link.exe 时出错.

schtb.cpp中的代码

#include
#include
#include

#include "schtb.h"
//创建查找表操作
void CreatSchTb(SeqList R)
{
int i ;
int random(int sum);
//randomize() ;
srand(n) ; //以相同的种子值,初始化随机数发生器
for(i=1; i<=n; i++)
R[i].key = random(n*10) ;//产生随机数
}
//排序查找表,采用简单选择排序
void SortSchTb(SeqList R)
{
int i, j, k ;
for(i=1; i k = i ;
for(j=i+1; j if (R[k].key>R[j].key) k = j ;
if (k!=i) {
R[0] = R[i] ;
R[i] = R[k] ;
R[k] = R[0] ;
}
}
}
//顺序查找操作
int SeqSearch(SeqList R, KeyType K)
{
int i ;
R[0].key = K ;
for(i=n; R[i].key!=K; i--) ;
return i ;
}
//折半查找操作
int BinSearch(SeqList R, KeyType K)
{
int low = 1, high = n, mid ;
while(low <= high) {
mid= (low + high) / 2 ;
if (R[mid].key < K) low = mid + 1 ;
else if (R[mid].key == K) return mid ;
else high = mid - 1 ;
}
return 0 ;
}
//输出查找表数据
void PrtSchTb(SeqList R)
{
int i ;
for (i=1; i<=n; i++)
printf("%4d", R[i].key) ;
printf("\n") ;
}

//二叉排序树的插入操作
void InsertBST(BSTree *BT, KeyType key)
{
BSTNode *f, *p = *BT ;//f指向要插入的双亲结点
while(p) {
if (p->key == key) return ; //有相同关键字,不插入
f = p ;
p = (keykey) ? p->lchild : p->rchild ;
}
p = (BSTNode *)malloc(sizeof(BSTNode)) ;
p->key = key ;
p->lchild = p->rchild = NULL ;
if (*BT == NULL) *BT = p ; //如果二叉排序树为空,则为根结点
else
if (keykey) f->lchild = p ;
else f->rchild = p ;
}
//创建二叉排序树操作
void CreatBST(BSTree *TP)
{
int random(int sum);
int i ;
*TP = NULL ;
srand(n) ;
for (i=1; i<=n; i++)
InsertBST(TP, random(n*10)) ;
}
//二叉排序树的查找操作
BSTNode *SearchBST(BSTree BT, KeyType key )
{
if (BT==NULL||BT->key==key) return BT ;
if (BT->key>key) return SearchBST(BT->lchild, key) ;
else return SearchBST(BT->rchild, key) ;
}
//输出二叉排序树结点的值操作,以中序遍历输出
void PrtBST(BSTree BT)
{
if (BT) {
PrtBST(BT->lchild) ;
printf("%4d", BT->key) ;
PrtBST(BT->rchild) ;
}
}
//取得当前系统时间操作
double GetTime(void)
{
double T;
CTime t = CTime::GetCurrentTime();
int h=t.GetHour(); //获取当前为几时
int mm=t.GetMinute(); //获取当前分钟
int s=t.GetSecond(); //获取当前秒
int w=t.GetDayOfWeek(); //获取星期几,注意1为星期天,7为星期六
double hund=s/100;
//struct time;
//struct t;
//gettime(&t);
T = ((h*60+mm)*60+s)*100+hund ;
return 0 ;
}

//主程序
void main(void)
{
SeqList SR, BR ;
BSTree BT ;
KeyType k[m] ;
int i ;
double t1, t2 ;
int random(int sum);

//randomize() ; 该用法在VC环境下不能使用,需要使用下面的用法
srand(time(0));
//产生待查找的数据
for(i=0; i<m; i++)
k[i] = random(n*10) ;
//顺序查找算法
printf("顺序查找算法的时间为:\n") ;
CreatSchTb(SR) ;
t1 = GetTime() ;
for(i=0; i<m; i++)
SeqSearch(SR, k[i]) ;
t2 = GetTime() ;
printf("%d毫秒\n", (int)(t2-t1)) ;
//折半查找算法
printf("顺序查找算法的时间为:\n") ;
CreatSchTb(BR) ;
SortSchTb(BR) ;
t1 = GetTime() ;
for(i=0; i<m; i++)
BinSearch(BR, k[i]) ;
t2 = GetTime() ;
printf("%d毫秒\n", (int)(t2-t1)) ;
//二叉排序树查找算法
printf("二叉排序树查找算法的时间为:\n") ;
CreatBST(&BT) ;
t1 = GetTime() ;
for(i=0; i<m; i++)
SearchBST(BT, k[i]) ;
t2 = GetTime() ;
printf("%d毫秒\n", (int)(t2-t1)) ;
//PrtBST(BT) ;
}

schtb.h中的代码
//以下是schtb.h的头文件内容。
//以下用于顺序查找和折半查找
#define n 1000 //定义查找表长度
#define m 500 //定义待查找数据个数
typedef int KeyType ; //定义关键字类型
typedef struct node { //定义查找结点结构
KeyType key ;
//如果查找表的结点包含其它信息,在此加入
//InfoType otherinfo ;
} NodeType ;
typedef NodeType SeqList[n+1] ;//定义查找表,含有监视哨

//以下用于二叉排序树的查找
typedef struct treenode { //定义二叉排序树结点
KeyType key ;
//InfoType otherinfo ; //结点的其它信息
//二叉链表结点的左右孩子指针
struct treenode *lchild, *rchild ;
} BSTNode ;
typedef BSTNode *BSTree ; //定义二叉排序树

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-04-15 17:18
    关注

    在VC++中,没有random()和randomize()函数,只有rand()和srand()函数。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条