[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 <stdio.h>
#include <stdlib.h>
#include <afxinet.h>
#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<n; i++) {
k = i ;
for(j=i+1; j<=n; 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 = (key<p->key) ? p->lchild : p->rchild ;
}
p = (BSTNode *)malloc(sizeof(BSTNode)) ;
p->key = key ;
p->lchild = p->rchild = NULL ;
if (*BT == NULL) *BT = p ; //如果二叉排序樹為空,則為根結點
else
if (key<f->key) 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 ; //定義二叉排序樹
uj5u.com熱心網友回復:
#include <stdio.h>
#include <stdlib.h>
#include <afxinet.h>
#include "schtb.h"
int random(int sum) {
return rand()%sum;
}
//創建查找表操作
void CreatSchTb(SeqList R) {
int i ;
//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<n; i++) {
k = i ;
for(j=i+1; j<=n; 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 = (key<p->key) ? p->lchild : p->rchild ;
}
p = (BSTNode *)malloc(sizeof(BSTNode)) ;
p->key = key ;
p->lchild = p->rchild = NULL ;
if (*BT == NULL) *BT = p ; //如果二叉排序樹為空,則為根結點
else
if (key<f->key) f->lchild = p ;
else f->rchild = p ;
}
//創建二叉排序樹操作
void CreatBST(BSTree *TP) {
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 ;
//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) ;
}
uj5u.com熱心網友回復:
請問為什么運行結果還是0?uj5u.com熱心網友回復:
#include<windows.h>#include<math.h>
uj5u.com熱心網友回復:
代碼功能歸根結底不是別人幫自己看或講解或注釋出來的;而是被自己靜下心來花足夠長的時間和精力親自動手單步或設斷點或對執行到某步獲得的中間結果顯示或寫到日志檔案中一步一步分析出來的。
提醒:再牛×的老師也無法代替學生自己領悟和上廁所!
單步除錯和設斷點除錯(VS IDE中編譯連接通過以后,按F10或F11鍵單步執行,按Shift+F11退出當前函式;在某行按F9設斷點后按F5執行停在該斷點處。)是程式員必須掌握的技能之一。
uj5u.com熱心網友回復:
random 改為 rand編譯環境屬性中 改為 MT 或 MTD
uj5u.com熱心網友回復:
謝謝趙4老師的鼓勵,多謝指教轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/102536.html
標籤:基礎類
上一篇:opencv擬合平面
