二叉树和线性的,这2种方法分开都没问题,在windows和unix下都不报错!~
但是整合到一起就不行了,windows下正常,unix下报错!~
unix下执行到二叉树就报错……
unix下报“ Memory fault(coredump) ”
#include <stdio.h>
#include <string.h>
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
typedef BSTNode *BSTree; /* 二叉排序树 */
int judge_argc(int argc);
void check(char *key); /* 处理数据(去掉读取数据最后的控制字符) */
void InsertBST(BSTree *ptr,char *key); /* 插入结点 */
void Show(BSTNode *pHead); /* 显示树 */
BSTNode *SearchBST(BSTree tree,char *key); /* 递归查找 */
BSTree CreateBST(char *filename,char *data); /* 生成树 */
long matchstring(char *str,char *substr);
long get_interspace(char *argv1,long *iInterSpace);
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY);
static long counttree = 0;
static long countline = 0;
/* 主函数:通过命令行输入参数,输入格式为:程序可执行文件名 文件名 */
int main(int argc,char *argv[])
{
char key1[SIZE];
char key2[SIZE];
long iInterSpace = 0; /* 文件大小(字节数) */
long iMatchX = 0; /* 行数 */
long iMatchY = 0; /* 列数 */
judge_argc(argc); /* 判断参数个数,如果个数不足,退出程序 */
/* 输入数据 */
memset(key1,'\0',sizeof(key1));
memset(key2,'\0',sizeof(key2));
printf("\nPlease input the searched data:");
scanf("%s",key1);
strcpy(key2,key1);
/* 线性形式 */
printf("\n***********Linear Form***********\n");
get_interspace(argv[1],&iInterSpace); /* 获取文件大小 */
matchposition(argv[1],key1,&iInterSpace,&iMatchX,&iMatchY); /*获取字符串在文件中出现的次数和所在的行、列数 */
/* tree形式 */
printf("***********Tree Form***********\n");
CreateBST(argv[1],key2);
return 0;
}
/* 判断命令行参数个数,如果不足,退出程序 */
int judge_argc(int argc)
{
if(argc < 2)
{
printf("Input Error!\nUsage:programmename testfilename\n");
printf("输入错误!\n使用方法:程序可执行文件名 文件名\n");
exit(2);
}
return 0;
}
/* 获取文件大小,以便之后程序中申请合适的空间 */
long get_interspace(char *argv1,long *iInterSpace)
{
FILE *fp;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
while(! feof(fp))
{
fgetc(fp);
*iInterSpace = *iInterSpace + 1;
}
fclose(fp);
return 0;
}
/* 判断匹配字符串的个数 */
long matchstring(char *str,char *substr)
{
char *p,*sp;
long iNum = 0;
p = str;
sp = substr;
while(*str != '\0')
{
str = p;
countline = countline + 1;
while(*substr != '\0')
{
countline = countline + 1;
if(*str == *substr)
{
substr++;
str++;
}
else
{
substr = sp;
str = p;
break;
}
}
if(*substr == '\0')
{
iNum = iNum + 1;
substr = sp;
p = str;
continue;
}
p++;
}
return iNum;
}
/* 判断输出具体的匹配行、列数,最后统计匹配总个数 */
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY)
{
FILE *fp;
char cString[*iInterSpace + 2]; /* 所有字符 */
char cPerLine[*iInterSpace + 2]; /* 单行字符 */
int ii,jj,kk; /* 临时变量,用来控制循环 */
long iReadNum = 0; /* 实际读取字符个数 */
long iLine = 0; /* 行数 */
long iMatchNum = 0;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* 初始化缓冲空间,并一次性将数据都读出来 */
memset(cString,'\0',sizeof(cString));
memset(cPerLine,'\0',sizeof(cPerLine));
iReadNum = fread(cString,1,sizeof(cString),fp);
fclose(fp);
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* unix、linux下不需要,所以注释掉了.*/
/* (经调试,发现windows下为了获得正确的行数,需要在最后多加一个\n,于是,定义空间的时候多加了2个字符) */
/*
cString[iReadNum] = '\n';
*/
iLine = matchstring(cString,"\n"); /* 获取正确的行数,以便控制之后的读取次数 */
/* 重新打开文件,用fgets读取数据,通过行数控制读取次数 */
for(ii = 0;ii < iLine;ii++)
{
fgets(cPerLine,sizeof(cPerLine),fp);
*iMatchX = ii + 1; /* 获得正确的行数 */
/* 用strstr判断该行中是否有匹配的字符串,有的话,输出行、列数 */
if(strstr(cPerLine,argv2) != NULL)
{
for(jj = 0;jj < strlen(cPerLine);)
{
for(kk = 0;kk < strlen(argv2);kk++)
{
/* 有1个字符不匹配,退出本次循环 */
if(cPerLine[jj + kk] != argv2[kk])
{
jj++;
break;
}
}
/* 都匹配时,输出行、列数 */
if(kk == strlen(argv2))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
printf("(Line:%ld,Row:%ld)\n",*iMatchX,*iMatchY);
jj++;
}
}
}
}
fclose(fp);
if(0 == iMatchNum)
{
printf("0 matched.\n");
}
else
{
printf("The matched number of the string '%s' is %ld.\n",argv2,iMatchNum);
printf("Times:%ld\n",countline);
}
return 0;
}
/* 处理数据(去掉读取数据最后的控制字符) */
void check(char *key)
{
int i;
for(i = 0;i < strlen(key);i++)
{
if(('\n' == key[i]) || ('\r' == key[i]))
{
key[i] = '\0';
break;
}
}
}
/* 插入结点 */
void InsertBST(BSTree *ptr,char *key)
{
BSTNode *curr,*p;
p = *ptr;
/* 查找插入位置 */
while(p)
{
/* 若树中已有key则无需插入,不存在时插入 */
if(0 != strcmp((p->data),key))
{
curr = p;
p = (key < (p->data))?p->lchild:p->rchild;
}
}
/* 生成新结点 */
p = (BSTNode *)malloc(sizeof(BSTNode));
strcpy(p->data,key);
p->lchild = p->rchild = NULL;
/* 如果原树为空,则新插入的结点为根结点 */
if(*ptr == NULL)
{
*ptr = p;
}
/* 原树非空时,将新结点作为其左、右孩子插入 */
else
{
if(key < (curr->data))
{
curr->lchild = p;
}
else
{
curr->rchild = p;
}
}
}
/* 递归查找 */
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
counttree = counttree + 1;
return tree;
}
if(key < (tree->data))
{
counttree = counttree + 1;
return SearchBST(tree->lchild,key);
}
else
{
counttree = counttree + 1;
return SearchBST(tree->rchild,key);
}
}
/* 显示树 */
void Show(BSTNode *pHead)
{
if (pHead != NULL)
{
Show(pHead->lchild);
printf("%s ",pHead->data);
Show(pHead->rchild);
}
}
/* 生成树 */
BSTree CreateBST(char *filename,char *data)
{
FILE *fp;
char key[SIZE];
BSTree tree = NULL;
BSTree result;
if((fp = fopen(filename,"r")) == NULL)
{
printf("Open file error!\n");
fclose(fp);
exit(-1);
}
/* 循环取数据每行新建一个结点 */
while(!feof(fp))
{
memset(key,'\0',sizeof(key));
/*
fgets(key,SIZE,fp);
check(key);
*/
fscanf(fp,"%s",key); /* 以单词为单位建立结点(无论数据文件里是如何存放数据的都可实现) */
InsertBST(&tree,key);
}
fclose(fp);
#if 0
Show(tree); /* 遍历显示树 */
#endif
/* 输入数据 */
memset(key,'\0',sizeof(key));
strcpy(key,data);
/* 查找结果 */
result = SearchBST(tree,key);
if(result)
{
printf("Found!\nTimes:%ld\n",counttree);
}
else
{
printf("Not Found!\n");
}
return tree;
}
26 个解决方案
#1
对不起了 只能友情帮顶 树忘光了 还得复习啊
#2
up
#3
vs2005编译不过.
楼主用vc9 ??
看来我也得更新编译器了.
楼主用vc9 ??
看来我也得更新编译器了.
#4
data.txt
数据有点多,内存不够用,我怀疑……
数据有点多,内存不够用,我怀疑……
cct
kkq
twv
hrd
mye
cgd
vhy
ytm
sab
lnw
vvk
ajt
nfh
qsc
rov
cms
onv
uqd
gzy
nle
rrf
smy
nta
vay
nqj
qid
cus
zng
era
uzp
pmf
juy
zii
ryv
une
cho
jqj
vge
vjy
nxu
ywt
dri
vlx
fjy
oig
zec
gsh
vgf
nqs
adp
vsb
fua
qfc
gxc
yxq
kbq
rpz
mfj
zxd
hyo
sse
tpj
isr
zux
vkr
sxo
xze
gyy
ssz
dsf
eih
gxi
ije
sao
uru
hbm
hwk
jjh
xcp
jbr
sqe
ulo
ozy
sbm
mjl
hye
iqk
rst
ihf
ciy
hxl
svy
jat
qsk
sri
hvf
ngm
anp
akk
ykz
jhm
ahl
spc
jtw
wrx
grb
cfr
bhk
xyn
ajg
dxc
nyh
rys
zjm
nbh
jzf
wzd
jpd
xwi
fch
cdb
het
kgs
vih
hzk
sgy
eoe
ovd
rpu
hiw
enf
mjs
pqn
oep
fyg
jon
tty
gey
myy
gfa
brg
ycn
tbv
qdm
ntb
zzo
otq
znc
sfk
udz
dyt
zen
cse
vot
hqu
yjm
nrc
rfi
iog
hag
tre
uhg
ect
wxh
hzp
uux
lhp
beg
tis
uxo
soo
adz
xhh
plt
qyv
wtl
ece
ljs
iee
ytl
ifb
bry
guj
emu
yxe
siw
lqd
pde
onw
vuu
kug
blg
qym
kkx
njb
oot
sxc
djr
piq
foz
pei
ase
lha
ule
uul
zbv
ury
yxv
sng
mmk
dra
mfm
etv
gqy
bzg
hsd
cbt
gyt
npx
hzh
jji
kft
jyb
lue
sor
mjy
nio
zcs
dzv
yct
tbr
voj
qrv
tmm
icg
sfc
brc
xnp
fxk
lqu
pjk
cyx
kdl
tlr
pfb
aky
sie
zzl
owr
vnl
amt
mlf
hwi
lln
cit
wwd
get
yya
hit
shs
tmw
dyf
qot
rbo
cqo
zdg
xxy
jxx
xqu
knl
afv
aqq
sls
qfm
ptz
ycr
owx
cfu
pmp
zjl
fod
ogm
xwj
xit
ytc
wjg
vxs
jeg
njc
ogh
vmd
qbh
yyz
nhv
hrs
mig
cqr
kbi
hou
jgr
doe
jqo
evf
ypt
keg
cdk
bjj
wav
gvx
hco
lbw
kik
xrt
nwx
etj
tmq
rye
lki
hng
vje
fhz
lnm
cds
hhz
aal
bxy
eqh
vez
tqk
voi
hzz
ehc
fzi
xca
syo
yin
tkm
mba
njh
cxi
ssu
pjb
hkg
ips
xkp
fyv
vgl
aum
ayx
xrj
rgx
tph
rzj
ber
ovb
uku
ysh
omd
gvk
amr
hpg
adh
nyn
rcs
afe
uaf
hyp
boh
sfv
tfk
zzp
ngp
kay
zhw
sdu
dno
osg
vut
fgn
kns
ato
uuw
cdu
ohs
dqq
vix
uxx
pux
yrs
mer
kvc
dsu
yra
kre
mvx
pwt
xyx
sqn
vnf
zsx
eti
bqb
fjq
gps
saa
uga
ojk
pvg
hnv
dby
qcd
oom
emr
pwm
ssv
kxd
wof
jfg
qus
lyp
vcs
lff
dxi
pqb
csw
ecp
rph
qlf
cef
sjh
ona
gus
gbk
emt
yny
nba
wxj
pxl
itq
pfl
puf
uob
hcq
stx
dsq
eon
whw
dzt
pbx
leg
yfv
rfm
yws
cgl
bqt
eeq
dai
njy
bcd
vst
sau
mvz
atq
mjo
cdx
bsn
nml
ujx
ohd
awt
nxb
wej
obr
gbs
wzo
zlz
luf
pgi
yil
hqe
tld
fau
fop
vrl
nji
lyg
dfg
odz
fih
udl
wdw
mmi
kqp
awc
gxr
qxx
ukg
puv
uxq
enp
rnm
dck
wpo
tus
psj
occ
lqi
jdu
pwn
hgv
fif
txe
rit
oha
acv
ikf
goc
tzt
bxw
jyq
lfp
syg
axw
zkb
tvy
wba
byx
kpl
lui
iao
ogr
god
iyo
xub
lnf
wcj
arz
scv
bwl
zqd
udw
avx
twz
vdk
jzi
ker
mal
ntc
nwj
tcu
mtk
ozj
lhc
fes
ctg
tpo
abm
fiw
moa
gnz
tmi
isn
uqf
tja
das
srj
izj
uqm
urf
crd
vvi
azr
lmf
gua
qdv
sel
byc
ulw
nyq
ejo
xfe
apf
hbb
vqf
fhf
ydm
ugs
grx
hkm
ecc
wfu
cov
mzx
bpd
xcf
fhm
gkr
dqk
cme
ejn
rar
jdn
rki
myn
dke
vow
ops
dlv
mwf
vmn
cce
ria
wjc
ycb
vqq
vcl
cxo
wnd
zio
qld
kxp
igg
oze
ors
ini
lqa
prk
ryu
koc
pab
hvl
eod
uuk
jeq
bdt
lcf
siv
esp
zvd
gwc
snw
zcd
anw
jim
nyl
pdq
kwb
zwa
rnt
lxo
bem
num
rhl
amm
tpz
qfl
ukb
dja
qob
cfj
env
pdn
rho
pla
blk
iah
wxp
riv
fvq
tsa
beq
wal
ckg
lbe
qfn
jwg
gmx
miw
bea
wyf
sda
zza
tdm
uat
pnl
fjg
plu
jvw
uig
acy
bid
tst
npo
ssy
ozw
ewp
nuo
mci
sfd
smm
nlu
yir
lep
tzo
lkt
aqa
dku
ayo
fzo
mee
qxe
mpd
cno
pbd
rut
ymn
muj
psw
sbq
awh
prg
fpu
gzm
hdx
sxi
lys
jhi
hzl
gix
qxi
wri
fcp
ddr
iep
jle
api
cwf
qec
syw
veb
gpz
fml
bif
sso
umy
kgd
gnu
nlc
eoq
kcj
fmo
gcq
xzy
kjv
uuq
kue
yqj
yja
ito
jnc
llp
ygm
nhk
rhq
eze
pej
mxs
iwr
wsw
ykg
jqg
qwm
zcv
xth
dmj
svw
mdq
dne
vqc
dxl
ryf
wam
pgw
vfs
jje
hff
bss
eyp
rsz
mmp
dcl
muf
ukz
xrr
ncw
lbr
rnz
mbp
ygy
ski
lfx
lit
efz
qns
pkx
sbc
nrl
tdy
ymf
pru
gcf
wpj
gmf
rff
wat
wep
ubw
jxw
xey
bab
uqe
dwm
yig
afr
xct
lyo
zpy
zjp
vzq
ohe
bvk
ahx
swa
kor
bha
wxv
rgb
omg
dcv
msu
pdu
lfv
vlm
ppn
bmd
maj
eej
bsx
hyn
wjl
xlt
fsx
oto
oqr
ipy
nor
ocy
gel
pwc
hyw
azc
xuj
rjh
awr
lxr
iht
qve
eqb
qgv
gbz
yhk
awg
ymm
ywi
nbd
qqi
mso
whs
hhm
nrr
ctw
qmk
ocx
crl
yva
hgf
hvx
xxx
kiz
pzt
oui
ivk
kxt
emi
icb
ltd
ddo
tmy
dnf
zgg
ode
vxw
feo
bfz
cai
zlp
lcl
how
llm
xsp
voo
fsb
rmk
hoz
qyd
lml
ncd
pam
xwc
pns
mkp
aov
rum
yug
ebr
djt
uwt
dhv
bmx
lhd
lty
beb
qgz
xeo
wgn
jqh
wvq
amf
pbs
ozo
hzd
wxl
ukl
ttw
sji
ehk
hqb
npq
trt
qgd
fpt
xmu
tgn
heq
mpx
rtv
xzj
maz
dil
gis
vyz
vhm
nls
joh
ffz
syh
duw
oib
wrv
zus
dud
ocq
osd
xgj
dkv
dig
rer
ecw
cjd
nue
hce
cmk
rmm
zvt
eif
vqg
qnf
yel
cyo
cfd
bxt
dqr
uwj
ary
lga
jns
yvi
iyd
zth
onz
pob
jjl
eqp
qok
vjr
lqz
wrd
ndk
ojj
knz
mls
vfb
rtu
sjp
dzq
exn
bde
efs
fbz
ney
slc
ccr
jpn
amp
hyz
ryl
erd
#5
俺用dev,呵呵!~dev可以编译通过,linux、unix都可以编译通过……
#6
昨天给别人写了2个程序,我就觉得这个有问题,在unix下报错……
结果,他在linux下说没问题,晚上打电话说数据少没问题,多的话就有问题……
有没有什么好一些的方法?二叉树是必须的……
结果,他在linux下说没问题,晚上打电话说数据少没问题,多的话就有问题……
有没有什么好一些的方法?二叉树是必须的……
#7
楼主不好意思
偶也只有友情帮顶了
偶也只有友情帮顶了
#8
./1 ./dat.txt
Please input the searched data:ojj
***********Linear Form***********
(Line:1010,Row:1)
The matched number of the string 'ojj' is 1.
Times:12332
***********Tree Form***********
Found!
Times:1009
偶这里没问题的样子。。
Please input the searched data:ojj
***********Linear Form***********
(Line:1010,Row:1)
The matched number of the string 'ojj' is 1.
Times:12332
***********Tree Form***********
Found!
Times:1009
偶这里没问题的样子。。
#9
$ valgrind -v ./1 ./dat.txt
==27450== Memcheck, a memory error detector.
==27450== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==27450== Using LibVEX rev 1732, a library for dynamic binary translation.
==27450== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==27450== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==27450== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==27450==
--27450-- Command line
--27450-- ./1
--27450-- ./dat.txt
--27450-- Startup, with flags:
--27450-- -v
--27450-- Contents of /proc/version:
--27450-- Linux version 2.6.22-15-generic (buildd@king) (gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)) #1 SMP Fri Jul 11 18:56:36 UTC 2008
--27450-- Arch and hwcaps: AMD64, amd64-sse2
--27450-- Page sizes: currently 4096, max supported 4096
--27450-- Valgrind library directory: /usr/lib/valgrind
--27450-- Reading syms from /tmp/1 (0x400000)
--27450-- Reading syms from /lib/ld-2.6.1.so (0x4000000)
--27450-- Reading debug info from /lib/ld-2.6.1.so...
--27450-- ... CRC mismatch (computed F18656F0 wanted 247B0180)
--27450-- object doesn't have a symbol table
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/memcheck (0x38000000)
--27450-- object doesn't have a dynamic symbol table
--27450-- Reading suppressions file: /usr/lib/valgrind/default.supp
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_core.so (0x4A1E000)
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_memcheck.so (0x4C1F000)
--27450-- Reading syms from /lib/libc-2.6.1.so (0x4E26000)
--27450-- Reading debug info from /lib/libc-2.6.1.so...
--27450-- ... CRC mismatch (computed C5B5C357 wanted 602D23C5)
--27450-- object doesn't have a symbol table
--27450-- REDIR: 0x4E9EF30 (rindex) redirected to 0x4C22840 (rindex)
--27450-- REDIR: 0x4E9FD90 (memset) redirected to 0x4C22CA0 (memset)
Please input the searched data:abcd
--27450-- REDIR: 0x4E9E5F0 (strcpy) redirected to 0x4C239D0 (strcpy)
--27450-- REDIR: 0x4E9EB20 (strlen) redirected to 0x4C229F0 (strlen)
***********Linear Form***********
--27450-- REDIR: 0x4E99D90 (malloc) redirected to 0x4C21B94 (malloc)
--27450-- REDIR: 0x4E9B670 (free) redirected to 0x4C217A4 (free)
--27450-- REDIR: 0x4E9FEA0 (mempcpy) redirected to 0x4C23250 (mempcpy)
--27450-- REDIR: 0x4E9F640 (memchr) redirected to 0x4C22B80 (memchr)
--27450-- REDIR: 0x4EA08A0 (memcpy) redirected to 0x4C23770 (memcpy)
0 matched.
***********Tree Form***********
--27450-- REDIR: 0x4E9E5B0 (strcmp) redirected to 0x4C22AC0 (strcmp)
Not Found!
==27450==
==27450== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
--27450--
--27450-- supp: 8 dl-hack3
==27450== malloc/free: in use at exit: 279,616 bytes in 1,028 blocks.
==27450== malloc/free: 1,032 allocs, 4 frees, 281,888 bytes allocated.
==27450==
==27450== searching for pointers to 1,028 not-freed blocks.
==27450== checked 79,896 bytes.
==27450==
==27450== LEAK SUMMARY:
==27450== definitely lost: 279,616 bytes in 1,028 blocks.
==27450== possibly lost: 0 bytes in 0 blocks.
==27450== still reachable: 0 bytes in 0 blocks.
==27450== suppressed: 0 bytes in 0 blocks.
==27450== Rerun with --leak-check=full to see details of leaked memory.
--27450-- memcheck: sanity checks: 41 cheap, 2 expensive
--27450-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--27450-- memcheck: auxmaps: 0 searches, 0 comparisons
--27450-- memcheck: SMs: n_issued = 18 (288k, 0M)
--27450-- memcheck: SMs: n_deissued = 0 (0k, 0M)
--27450-- memcheck: SMs: max_noaccess = 524287 (8388592k, 8191M)
--27450-- memcheck: SMs: max_undefined = 0 (0k, 0M)
--27450-- memcheck: SMs: max_defined = 117 (1872k, 1M)
--27450-- memcheck: SMs: max_non_DSM = 18 (288k, 0M)
--27450-- memcheck: max sec V bit nodes: 0 (0k, 0M)
--27450-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--27450-- memcheck: max shadow mem size: 4432k, 4M
--27450-- translate: fast SP updates identified: 1,373 ( 86.1%)
--27450-- translate: generic_known SP updates identified: 139 ( 8.7%)
--27450-- translate: generic_unknown SP updates identified: 81 ( 5.0%)
--27450-- tt/tc: 4,229 tt lookups requiring 4,292 probes
--27450-- tt/tc: 4,229 fast-cache updates, 2 flushes
--27450-- transtab: new 2,107 (46,629 -> 834,409; ratio 178:10) [0 scs]
--27450-- transtab: dumped 0 (0 -> ??)
--27450-- transtab: discarded 0 (0 -> ??)
--27450-- scheduler: 4,183,562 jumps (bb entries).
--27450-- scheduler: 41/3,292 major/minor sched events.
--27450-- sanity: 42 cheap, 2 expensive checks.
--27450-- exectx: 30,011 lists, 17 contexts (avg 0 per list)
--27450-- exectx: 1,044 searches, 1,027 full compares (983 per 1000)
--27450-- exectx: 0 cmp2, 28 cmp4, 0 cmpAll
==27450== Memcheck, a memory error detector.
==27450== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==27450== Using LibVEX rev 1732, a library for dynamic binary translation.
==27450== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==27450== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==27450== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==27450==
--27450-- Command line
--27450-- ./1
--27450-- ./dat.txt
--27450-- Startup, with flags:
--27450-- -v
--27450-- Contents of /proc/version:
--27450-- Linux version 2.6.22-15-generic (buildd@king) (gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)) #1 SMP Fri Jul 11 18:56:36 UTC 2008
--27450-- Arch and hwcaps: AMD64, amd64-sse2
--27450-- Page sizes: currently 4096, max supported 4096
--27450-- Valgrind library directory: /usr/lib/valgrind
--27450-- Reading syms from /tmp/1 (0x400000)
--27450-- Reading syms from /lib/ld-2.6.1.so (0x4000000)
--27450-- Reading debug info from /lib/ld-2.6.1.so...
--27450-- ... CRC mismatch (computed F18656F0 wanted 247B0180)
--27450-- object doesn't have a symbol table
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/memcheck (0x38000000)
--27450-- object doesn't have a dynamic symbol table
--27450-- Reading suppressions file: /usr/lib/valgrind/default.supp
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_core.so (0x4A1E000)
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_memcheck.so (0x4C1F000)
--27450-- Reading syms from /lib/libc-2.6.1.so (0x4E26000)
--27450-- Reading debug info from /lib/libc-2.6.1.so...
--27450-- ... CRC mismatch (computed C5B5C357 wanted 602D23C5)
--27450-- object doesn't have a symbol table
--27450-- REDIR: 0x4E9EF30 (rindex) redirected to 0x4C22840 (rindex)
--27450-- REDIR: 0x4E9FD90 (memset) redirected to 0x4C22CA0 (memset)
Please input the searched data:abcd
--27450-- REDIR: 0x4E9E5F0 (strcpy) redirected to 0x4C239D0 (strcpy)
--27450-- REDIR: 0x4E9EB20 (strlen) redirected to 0x4C229F0 (strlen)
***********Linear Form***********
--27450-- REDIR: 0x4E99D90 (malloc) redirected to 0x4C21B94 (malloc)
--27450-- REDIR: 0x4E9B670 (free) redirected to 0x4C217A4 (free)
--27450-- REDIR: 0x4E9FEA0 (mempcpy) redirected to 0x4C23250 (mempcpy)
--27450-- REDIR: 0x4E9F640 (memchr) redirected to 0x4C22B80 (memchr)
--27450-- REDIR: 0x4EA08A0 (memcpy) redirected to 0x4C23770 (memcpy)
0 matched.
***********Tree Form***********
--27450-- REDIR: 0x4E9E5B0 (strcmp) redirected to 0x4C22AC0 (strcmp)
Not Found!
==27450==
==27450== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
--27450--
--27450-- supp: 8 dl-hack3
==27450== malloc/free: in use at exit: 279,616 bytes in 1,028 blocks.
==27450== malloc/free: 1,032 allocs, 4 frees, 281,888 bytes allocated.
==27450==
==27450== searching for pointers to 1,028 not-freed blocks.
==27450== checked 79,896 bytes.
==27450==
==27450== LEAK SUMMARY:
==27450== definitely lost: 279,616 bytes in 1,028 blocks.
==27450== possibly lost: 0 bytes in 0 blocks.
==27450== still reachable: 0 bytes in 0 blocks.
==27450== suppressed: 0 bytes in 0 blocks.
==27450== Rerun with --leak-check=full to see details of leaked memory.
--27450-- memcheck: sanity checks: 41 cheap, 2 expensive
--27450-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--27450-- memcheck: auxmaps: 0 searches, 0 comparisons
--27450-- memcheck: SMs: n_issued = 18 (288k, 0M)
--27450-- memcheck: SMs: n_deissued = 0 (0k, 0M)
--27450-- memcheck: SMs: max_noaccess = 524287 (8388592k, 8191M)
--27450-- memcheck: SMs: max_undefined = 0 (0k, 0M)
--27450-- memcheck: SMs: max_defined = 117 (1872k, 1M)
--27450-- memcheck: SMs: max_non_DSM = 18 (288k, 0M)
--27450-- memcheck: max sec V bit nodes: 0 (0k, 0M)
--27450-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--27450-- memcheck: max shadow mem size: 4432k, 4M
--27450-- translate: fast SP updates identified: 1,373 ( 86.1%)
--27450-- translate: generic_known SP updates identified: 139 ( 8.7%)
--27450-- translate: generic_unknown SP updates identified: 81 ( 5.0%)
--27450-- tt/tc: 4,229 tt lookups requiring 4,292 probes
--27450-- tt/tc: 4,229 fast-cache updates, 2 flushes
--27450-- transtab: new 2,107 (46,629 -> 834,409; ratio 178:10) [0 scs]
--27450-- transtab: dumped 0 (0 -> ??)
--27450-- transtab: discarded 0 (0 -> ??)
--27450-- scheduler: 4,183,562 jumps (bb entries).
--27450-- scheduler: 41/3,292 major/minor sched events.
--27450-- sanity: 42 cheap, 2 expensive checks.
--27450-- exectx: 30,011 lists, 17 contexts (avg 0 per list)
--27450-- exectx: 1,044 searches, 1,027 full compares (983 per 1000)
--27450-- exectx: 0 cmp2, 28 cmp4, 0 cmpAll
#10
你的是什么系统?狒狒……
linux?
那为什么我unix报错??
他那边数据多了也报错……
omg!~嘿嘿!~
不管了,一会儿我问问,看看要是问题解决了的话最好,
没解决的话,我昨天告诉他改#define SIZE 256
改小一些,不知道这个方法可行否?
linux?
那为什么我unix报错??
他那边数据多了也报错……
omg!~嘿嘿!~
不管了,一会儿我问问,看看要是问题解决了的话最好,
没解决的话,我昨天告诉他改#define SIZE 256
改小一些,不知道这个方法可行否?
#11
你让他放在 gdb 里跑,出错之后直接 bt 看是哪里的问题。
数据量大到什么程度出错?
我这里是
$ uname -a
Linux ubuntu-server 2.6.22-15-generic #1 SMP Fri Jul 11 18:56:36 UTC 2008 x86_64 GNU/Linux
数据量大到什么程度出错?
我这里是
$ uname -a
Linux ubuntu-server 2.6.22-15-generic #1 SMP Fri Jul 11 18:56:36 UTC 2008 x86_64 GNU/Linux
#12
呵呵unix系统没装,友情up~~
#13
unix下没有这个valgrind
#14
呵呵!~
谢谢狒狒同学!~
看来俺昨天晚上没打骚扰是个明智的选择……
o(∩_∩)o...哈哈!~
把这个帖子地址发过去,让他慢慢研究去吧!~
谢谢狒狒同学!~
看来俺昨天晚上没打骚扰是个明智的选择……
o(∩_∩)o...哈哈!~
把这个帖子地址发过去,让他慢慢研究去吧!~
#15
valgrind is only available to various Linux platforms.
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
这个函数参数是不是有问题?为什么是 BSTree,不是 BSTree *?
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
这个函数参数是不是有问题?为什么是 BSTree,不是 BSTree *?
#16
我错了。。i hate typedefs..
#17
顶
#18
o(∩_∩)o...
这个函数没有问题
/*
jfsnew.c(new)
更正查找次数
*/
#include <stdio.h>
#include <string.h>
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
typedef BSTNode *BSTree; /* 二叉排序树 */
int judge_argc(int argc);
void check(char *key); /* 处理数据(去掉读取数据最后的控制字符) */
void InsertBST(BSTree *ptr,char *key); /* 插入结点 */
void Show(BSTNode *pHead); /* 显示树 */
BSTNode *SearchBST(BSTree tree,char *key); /* 递归查找 */
BSTree CreateBST(char *filename,char *data); /* 生成树 */
long matchstring(char *str,char *substr);
long get_interspace(char *argv1,long *iInterSpace);
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY);
static long counttree = 0;
static long countline = 0;
/* 主函数:通过命令行输入参数,输入格式为:程序可执行文件名 文件名 */
int main(int argc,char *argv[])
{
char key1[SIZE];
char key2[SIZE];
long iInterSpace = 0; /* 文件大小(字节数) */
long iMatchX = 0; /* 行数 */
long iMatchY = 0; /* 列数 */
judge_argc(argc); /* 判断参数个数,如果个数不足,退出程序 */
/* 输入数据 */
memset(key1,'\0',sizeof(key1));
memset(key2,'\0',sizeof(key2));
printf("\nPlease input the searched data:");
scanf("%s",key1);
strcpy(key2,key1);
/* 线性形式 */
printf("\n***********Linear Form***********\n");
get_interspace(argv[1],&iInterSpace); /* 获取文件大小 */
matchposition(argv[1],key1,&iInterSpace,&iMatchX,&iMatchY); /*获取字符串在文件中出现的次数和所在的行、列数 */
/* tree形式 */
printf("***********Tree Form***********\n");
CreateBST(argv[1],key2);
return 0;
}
/* 判断命令行参数个数,如果不足,退出程序 */
int judge_argc(int argc)
{
if(argc < 2)
{
printf("Input Error!\nUsage:programmename testfilename\n");
printf("输入错误!\n使用方法:程序可执行文件名 文件名\n");
exit(2);
}
return 0;
}
/* 获取文件大小,以便之后程序中申请合适的空间 */
long get_interspace(char *argv1,long *iInterSpace)
{
FILE *fp;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
while(! feof(fp))
{
fgetc(fp);
*iInterSpace = *iInterSpace + 1;
}
fclose(fp);
return 0;
}
/* 判断匹配字符串的个数 */
long matchstring(char *str,char *substr)
{
char *p,*sp;
long iNum = 0;
p = str;
sp = substr;
while(*str != '\0')
{
str = p;
while(*substr != '\0')
{
if(*str == *substr)
{
substr++;
str++;
}
else
{
substr = sp;
str = p;
break;
}
}
if(*substr == '\0')
{
iNum = iNum + 1;
substr = sp;
p = str;
continue;
}
p++;
}
return iNum;
}
/* 判断输出具体的匹配行、列数,最后统计匹配总个数 */
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY)
{
FILE *fp;
char cString[*iInterSpace + 2]; /* 所有字符 */
char cPerLine[*iInterSpace + 2]; /* 单行字符 */
int ii,jj,kk; /* 临时变量,用来控制循环 */
long iReadNum = 0; /* 实际读取字符个数 */
long iLine = 0; /* 行数 */
long iMatchNum = 0;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* 初始化缓冲空间,并一次性将数据都读出来 */
memset(cString,'\0',sizeof(cString));
memset(cPerLine,'\0',sizeof(cPerLine));
iReadNum = fread(cString,1,sizeof(cString),fp);
fclose(fp);
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* unix、linux下不需要,所以注释掉了.*/
/* (经调试,发现windows下为了获得正确的行数,需要在最后多加一个\n,于是,定义空间的时候多加了2个字符) */
/*
cString[iReadNum] = '\n';
*/
iLine = matchstring(cString,"\n"); /* 获取正确的行数,以便控制之后的读取次数 */
/* 重新打开文件,用fgets读取数据,通过行数控制读取次数 */
for(ii = 0;ii < iLine;ii++)
{
fgets(cPerLine,sizeof(cPerLine),fp);
*iMatchX = ii + 1; /* 获得正确的行数 */
/* 用strstr判断该行中是否有匹配的字符串,有的话,输出行、列数 */
if(strstr(cPerLine,argv2) != NULL)
{
for(jj = 0;jj < strlen(cPerLine);)
{
/* 用strncmp比较 */
countline = countline + ii + 1;
check(cPerLine);
if(0 == strncmp(cPerLine + jj,argv2,strlen(argv2)))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
break;
}
else
{
jj = jj + strlen(argv2);
}
#if 0 /* 逐个字符比较 */
for(kk = 0;kk < strlen(argv2);kk++)
{
countline = countline + 1;
/* 有1个字符不匹配,退出本次循环 */
if(cPerLine[jj + kk] != argv2[kk])
{
jj++;
break;
}
}
/* 都匹配时,输出行、列数 */
if(kk == strlen(argv2))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
printf("(Line:%ld,Row:%ld)\n",*iMatchX,*iMatchY);
jj++;
}
#endif
}
}
}
fclose(fp);
if(0 == iMatchNum)
{
printf("0 matched.\n");
}
else
{
printf("The matched number of the string '%s' is %ld.\n",argv2,iMatchNum);
printf("Times:%ld\n",countline);
}
return 0;
}
/* 处理数据(去掉读取数据最后的控制字符) */
void check(char *key)
{
int i;
for(i = 0;i < strlen(key);i++)
{
if(('\n' == key[i]) || ('\r' == key[i]))
{
key[i] = '\0';
break;
}
}
}
/* 插入结点 */
void InsertBST(BSTree *ptr,char *key)
{
BSTNode *curr,*p;
p = *ptr;
/* 查找插入位置 */
while(p)
{
/* 若树中已有key则无需插入,不存在时插入 */
if(0 != strcmp((p->data),key))
{
curr = p;
p = (key < (p->data))?p->lchild:p->rchild;
}
}
/* 生成新结点 */
p = (BSTNode *)malloc(sizeof(BSTNode));
strcpy(p->data,key);
p->lchild = p->rchild = NULL;
/* 如果原树为空,则新插入的结点为根结点 */
if(*ptr == NULL)
{
*ptr = p;
}
/* 原树非空时,将新结点作为其左、右孩子插入 */
else
{
if(key < (curr->data))
{
curr->lchild = p;
}
else
{
curr->rchild = p;
}
}
}
/* 递归查找 */
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
counttree = counttree + 1;
return tree;
}
if(key < (tree->data))
{
counttree = counttree + 1;
return SearchBST(tree->lchild,key);
}
else
{
counttree = counttree + 1;
return SearchBST(tree->rchild,key);
}
}
/* 显示树 */
void Show(BSTNode *pHead)
{
if (pHead != NULL)
{
Show(pHead->lchild);
printf("%s ",pHead->data);
Show(pHead->rchild);
}
}
/* 生成树 */
BSTree CreateBST(char *filename,char *data)
{
FILE *fp;
char key[SIZE];
BSTree tree = NULL;
BSTree result;
if((fp = fopen(filename,"r")) == NULL)
{
printf("Open file error!\n");
fclose(fp);
exit(-1);
}
/* 循环取数据每行新建一个结点 */
while(!feof(fp))
{
memset(key,'\0',sizeof(key));
/*
fgets(key,SIZE,fp);
check(key);
*/
fscanf(fp,"%s",key); /* 以单词为单位建立结点(无论数据文件里是如何存放数据的都可实现) */
InsertBST(&tree,key);
}
fclose(fp);
#if 0
Show(tree); /* 遍历显示树 */
#endif
/* 输入数据 */
memset(key,'\0',sizeof(key));
strcpy(key,data);
/* 查找结果 */
result = SearchBST(tree,key);
if(result)
{
printf("Found!\nTimes:%ld\n",counttree);
}
else
{
printf("Not Found!\n");
}
return tree;
}
#19
void InsertBST(BSTree *ptr,char *key)
函数里有严重的问题,你的
curr变量只在while的if语句里才有被赋值的机会,
可是下面就直接访问curr-data,不出问题才怪呢。
函数里有严重的问题,你的
curr变量只在while的if语句里才有被赋值的机会,
可是下面就直接访问curr-data,不出问题才怪呢。
#20
帮顶~
#21
根据村长大人的指点,已经修改了!~
但是unix下还是有问题……omg
但是unix下还是有问题……omg
/*
从数据文件data.txt中查找数据,分别用线性的和二叉树形式查找,
只要找到就不再继续找。
显示结果:找到found + 比较次数
未找到not found
*/
#include <stdio.h>
#include <string.h>
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
typedef BSTNode *BSTree; /* 二叉排序树 */
int judge_argc(int argc);
void check(char *key); /* 处理数据(去掉读取数据最后的控制字符) */
void InsertBST(BSTree *ptr,char *key); /* 插入结点 */
void Show(BSTNode *pHead); /* 显示树 */
BSTNode *SearchBST(BSTree tree,char *key); /* 递归查找 */
BSTree CreateBST(char *filename,char *data); /* 生成树 */
long matchstring(char *str,char *substr);
long get_interspace(char *argv1,long *iInterSpace);
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY);
static long counttree = 0;
static long countline = 0;
/* 主函数:通过命令行输入参数,输入格式为:程序可执行文件名 文件名 */
int main(int argc,char *argv[])
{
char key1[SIZE];
char key2[SIZE];
long iInterSpace = 0; /* 文件大小(字节数) */
long iMatchX = 0; /* 行数 */
long iMatchY = 0; /* 列数 */
judge_argc(argc); /* 判断参数个数,如果个数不足,退出程序 */
/* 输入数据 */
memset(key1,'\0',sizeof(key1));
memset(key2,'\0',sizeof(key2));
printf("\nPlease input the searched data:");
scanf("%s",key1);
strcpy(key2,key1);
/* 线性形式 */
printf("\n***********Linear Form***********\n");
get_interspace(argv[1],&iInterSpace); /* 获取文件大小 */
matchposition(argv[1],key1,&iInterSpace,&iMatchX,&iMatchY); /*获取字符串在文件中出现的次数和所在的行、列数 */
/* tree形式 */
printf("***********Tree Form***********\n");
CreateBST(argv[1],key2);
return 0;
}
/* 判断命令行参数个数,如果不足,退出程序 */
int judge_argc(int argc)
{
if(argc < 2)
{
printf("Input Error!\nUsage:programmename testfilename\n");
printf("输入错误!\n使用方法:程序可执行文件名 文件名\n");
exit(2);
}
return 0;
}
/* 获取文件大小,以便之后程序中申请合适的空间 */
long get_interspace(char *argv1,long *iInterSpace)
{
FILE *fp;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
fseek(fp,0,SEEK_END);
*iInterSpace = ftell(fp);
#if 0
while(! feof(fp))
{
fgetc(fp);
*iInterSpace = *iInterSpace + 1;
}
#endif
fclose(fp);
return 0;
}
/* 判断匹配字符串的个数 */
long matchstring(char *str,char *substr)
{
char *p,*sp;
long iNum = 0;
p = str;
sp = substr;
while(*str != '\0')
{
str = p;
while(*substr != '\0')
{
if(*str == *substr)
{
substr++;
str++;
}
else
{
substr = sp;
str = p;
break;
}
}
if(*substr == '\0')
{
iNum = iNum + 1;
substr = sp;
p = str;
continue;
}
p++;
}
return iNum;
}
/* 判断输出具体的匹配行、列数,最后统计匹配总个数 */
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY)
{
FILE *fp;
char cString[*iInterSpace + 2]; /* 所有字符 */
char cPerLine[*iInterSpace + 2]; /* 单行字符 */
int ii,jj,kk; /* 临时变量,用来控制循环 */
long iReadNum = 0; /* 实际读取字符个数 */
long iLine = 0; /* 行数 */
long iMatchNum = 0;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* 初始化缓冲空间,并一次性将数据都读出来 */
memset(cString,'\0',sizeof(cString));
memset(cPerLine,'\0',sizeof(cPerLine));
iReadNum = fread(cString,1,sizeof(cString),fp);
fclose(fp);
for(ii = 0;ii < strlen(cString);)
{
/* 用strncmp比较 */
countline = countline + 1;
if(0 == strncmp(cString + ii,argv2,strlen(argv2)))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
break;
}
else
{
ii = ii + 1;
}
}
#if 0
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* unix、linux下不需要,所以注释掉了.*/
/* (经调试,发现windows下为了获得正确的行数,需要在最后多加一个\n,于是,定义空间的时候多加了2个字符) */
/*
cString[iReadNum] = '\n';
*/
iLine = matchstring(cString,"\n"); /* 获取正确的行数,以便控制之后的读取次数 */
/* 重新打开文件,用fgets读取数据,通过行数控制读取次数 */
for(ii = 0;ii < iLine;ii++)
{
fgets(cPerLine,sizeof(cPerLine),fp);
*iMatchX = ii + 1; /* 获得正确的行数 */
/* 用strstr判断该行中是否有匹配的字符串,有的话,输出行、列数 */
if(strstr(cPerLine,argv2) != NULL)
{
for(jj = 0;jj < strlen(cPerLine);)
{
/* 逐个字符比较 */
for(kk = 0;kk < strlen(argv2);kk++)
{
countline = countline + 1;
/* 有1个字符不匹配,退出本次循环 */
if(cPerLine[jj + kk] != argv2[kk])
{
jj++;
break;
}
}
/* 都匹配时,输出行、列数 */
if(kk == strlen(argv2))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
printf("(Line:%ld,Row:%ld)\n",*iMatchX,*iMatchY);
jj++;
}
}
}
}
fclose(fp);
#endif
if(0 == iMatchNum)
{
printf("0 matched.\n");
}
else
{
printf("The matched number of the string '%s' is %ld.\n",argv2,iMatchNum);
printf("Times:%ld\n",countline);
}
return 0;
}
/* 处理数据(去掉读取数据最后的控制字符) */
void check(char *key)
{
int i;
for(i = 0;i < strlen(key);i++)
{
if(('\n' == key[i]) || ('\r' == key[i]))
{
key[i] = '\0';
break;
}
}
}
/* 插入结点 */
void InsertBST(BSTree *ptr,char *key)
{
BSTNode *curr,*p;
p=NULL;
curr = NULL;
p = *ptr;
/* 查找插入位置 */
while(p)
{
/* 若树中已有key则无需插入,不存在时插入 */
if(0 != strcmp((p->data),key))
{
curr = p;
p = (key < (p->data))?p->lchild:p->rchild;
}
}
/* 生成新结点 */
p = (BSTNode *)malloc(sizeof(BSTNode));
strcpy(p->data,key);
p->lchild = p->rchild = NULL;
/* 如果原树为空,则新插入的结点为根结点 */
if(*ptr == NULL)
{
*ptr = p;
}
/* 原树非空时,将新结点作为其左、右孩子插入 */
else
{
if(curr == NULL)
{
printf("NULL pointer!");
exit (-1);
}
if(key < (curr->data))
{
curr->lchild = p;
}
else
{
curr->rchild = p;
}
}
}
/* 递归查找 */
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
counttree = counttree + 1;
return tree;
}
if(key < (tree->data))
{
counttree = counttree + 1;
return SearchBST(tree->lchild,key);
}
else
{
counttree = counttree + 1;
return SearchBST(tree->rchild,key);
}
}
/* 显示树 */
void Show(BSTNode *pHead)
{
if (pHead != NULL)
{
Show(pHead->lchild);
printf("%s ",pHead->data);
Show(pHead->rchild);
}
}
/* 生成树 */
BSTree CreateBST(char *filename,char *data)
{
FILE *fp;
char key[SIZE];
BSTree tree = NULL;
BSTree result;
if((fp = fopen(filename,"r")) == NULL)
{
printf("Open file error!\n");
fclose(fp);
exit(-1);
}
/* 循环取数据每行新建一个结点 */
while(!feof(fp))
{
memset(key,'\0',sizeof(key));
/*
fgets(key,SIZE,fp);
check(key);
*/
fscanf(fp,"%s",key); /* 以单词为单位建立结点(无论数据文件里是如何存放数据的都可实现) */
InsertBST(&tree,key);
}
fclose(fp);
#if 0
Show(tree); /* 遍历显示树 */
#endif
/* 输入数据 */
memset(key,'\0',sizeof(key));
strcpy(key,data);
/* 查找结果 */
result = SearchBST(tree,key);
if(result)
{
printf("Found!\nTimes:%ld\n",counttree);
}
else
{
printf("Not Found!\n");
}
return tree;
}
#22
jf
#23
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
变长的指针?
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
变长的指针?
#24
帮顶
#25
我顶你的肺
#26
这个帖子。。。有点很。。。
#1
对不起了 只能友情帮顶 树忘光了 还得复习啊
#2
up
#3
vs2005编译不过.
楼主用vc9 ??
看来我也得更新编译器了.
楼主用vc9 ??
看来我也得更新编译器了.
#4
data.txt
数据有点多,内存不够用,我怀疑……
数据有点多,内存不够用,我怀疑……
cct
kkq
twv
hrd
mye
cgd
vhy
ytm
sab
lnw
vvk
ajt
nfh
qsc
rov
cms
onv
uqd
gzy
nle
rrf
smy
nta
vay
nqj
qid
cus
zng
era
uzp
pmf
juy
zii
ryv
une
cho
jqj
vge
vjy
nxu
ywt
dri
vlx
fjy
oig
zec
gsh
vgf
nqs
adp
vsb
fua
qfc
gxc
yxq
kbq
rpz
mfj
zxd
hyo
sse
tpj
isr
zux
vkr
sxo
xze
gyy
ssz
dsf
eih
gxi
ije
sao
uru
hbm
hwk
jjh
xcp
jbr
sqe
ulo
ozy
sbm
mjl
hye
iqk
rst
ihf
ciy
hxl
svy
jat
qsk
sri
hvf
ngm
anp
akk
ykz
jhm
ahl
spc
jtw
wrx
grb
cfr
bhk
xyn
ajg
dxc
nyh
rys
zjm
nbh
jzf
wzd
jpd
xwi
fch
cdb
het
kgs
vih
hzk
sgy
eoe
ovd
rpu
hiw
enf
mjs
pqn
oep
fyg
jon
tty
gey
myy
gfa
brg
ycn
tbv
qdm
ntb
zzo
otq
znc
sfk
udz
dyt
zen
cse
vot
hqu
yjm
nrc
rfi
iog
hag
tre
uhg
ect
wxh
hzp
uux
lhp
beg
tis
uxo
soo
adz
xhh
plt
qyv
wtl
ece
ljs
iee
ytl
ifb
bry
guj
emu
yxe
siw
lqd
pde
onw
vuu
kug
blg
qym
kkx
njb
oot
sxc
djr
piq
foz
pei
ase
lha
ule
uul
zbv
ury
yxv
sng
mmk
dra
mfm
etv
gqy
bzg
hsd
cbt
gyt
npx
hzh
jji
kft
jyb
lue
sor
mjy
nio
zcs
dzv
yct
tbr
voj
qrv
tmm
icg
sfc
brc
xnp
fxk
lqu
pjk
cyx
kdl
tlr
pfb
aky
sie
zzl
owr
vnl
amt
mlf
hwi
lln
cit
wwd
get
yya
hit
shs
tmw
dyf
qot
rbo
cqo
zdg
xxy
jxx
xqu
knl
afv
aqq
sls
qfm
ptz
ycr
owx
cfu
pmp
zjl
fod
ogm
xwj
xit
ytc
wjg
vxs
jeg
njc
ogh
vmd
qbh
yyz
nhv
hrs
mig
cqr
kbi
hou
jgr
doe
jqo
evf
ypt
keg
cdk
bjj
wav
gvx
hco
lbw
kik
xrt
nwx
etj
tmq
rye
lki
hng
vje
fhz
lnm
cds
hhz
aal
bxy
eqh
vez
tqk
voi
hzz
ehc
fzi
xca
syo
yin
tkm
mba
njh
cxi
ssu
pjb
hkg
ips
xkp
fyv
vgl
aum
ayx
xrj
rgx
tph
rzj
ber
ovb
uku
ysh
omd
gvk
amr
hpg
adh
nyn
rcs
afe
uaf
hyp
boh
sfv
tfk
zzp
ngp
kay
zhw
sdu
dno
osg
vut
fgn
kns
ato
uuw
cdu
ohs
dqq
vix
uxx
pux
yrs
mer
kvc
dsu
yra
kre
mvx
pwt
xyx
sqn
vnf
zsx
eti
bqb
fjq
gps
saa
uga
ojk
pvg
hnv
dby
qcd
oom
emr
pwm
ssv
kxd
wof
jfg
qus
lyp
vcs
lff
dxi
pqb
csw
ecp
rph
qlf
cef
sjh
ona
gus
gbk
emt
yny
nba
wxj
pxl
itq
pfl
puf
uob
hcq
stx
dsq
eon
whw
dzt
pbx
leg
yfv
rfm
yws
cgl
bqt
eeq
dai
njy
bcd
vst
sau
mvz
atq
mjo
cdx
bsn
nml
ujx
ohd
awt
nxb
wej
obr
gbs
wzo
zlz
luf
pgi
yil
hqe
tld
fau
fop
vrl
nji
lyg
dfg
odz
fih
udl
wdw
mmi
kqp
awc
gxr
qxx
ukg
puv
uxq
enp
rnm
dck
wpo
tus
psj
occ
lqi
jdu
pwn
hgv
fif
txe
rit
oha
acv
ikf
goc
tzt
bxw
jyq
lfp
syg
axw
zkb
tvy
wba
byx
kpl
lui
iao
ogr
god
iyo
xub
lnf
wcj
arz
scv
bwl
zqd
udw
avx
twz
vdk
jzi
ker
mal
ntc
nwj
tcu
mtk
ozj
lhc
fes
ctg
tpo
abm
fiw
moa
gnz
tmi
isn
uqf
tja
das
srj
izj
uqm
urf
crd
vvi
azr
lmf
gua
qdv
sel
byc
ulw
nyq
ejo
xfe
apf
hbb
vqf
fhf
ydm
ugs
grx
hkm
ecc
wfu
cov
mzx
bpd
xcf
fhm
gkr
dqk
cme
ejn
rar
jdn
rki
myn
dke
vow
ops
dlv
mwf
vmn
cce
ria
wjc
ycb
vqq
vcl
cxo
wnd
zio
qld
kxp
igg
oze
ors
ini
lqa
prk
ryu
koc
pab
hvl
eod
uuk
jeq
bdt
lcf
siv
esp
zvd
gwc
snw
zcd
anw
jim
nyl
pdq
kwb
zwa
rnt
lxo
bem
num
rhl
amm
tpz
qfl
ukb
dja
qob
cfj
env
pdn
rho
pla
blk
iah
wxp
riv
fvq
tsa
beq
wal
ckg
lbe
qfn
jwg
gmx
miw
bea
wyf
sda
zza
tdm
uat
pnl
fjg
plu
jvw
uig
acy
bid
tst
npo
ssy
ozw
ewp
nuo
mci
sfd
smm
nlu
yir
lep
tzo
lkt
aqa
dku
ayo
fzo
mee
qxe
mpd
cno
pbd
rut
ymn
muj
psw
sbq
awh
prg
fpu
gzm
hdx
sxi
lys
jhi
hzl
gix
qxi
wri
fcp
ddr
iep
jle
api
cwf
qec
syw
veb
gpz
fml
bif
sso
umy
kgd
gnu
nlc
eoq
kcj
fmo
gcq
xzy
kjv
uuq
kue
yqj
yja
ito
jnc
llp
ygm
nhk
rhq
eze
pej
mxs
iwr
wsw
ykg
jqg
qwm
zcv
xth
dmj
svw
mdq
dne
vqc
dxl
ryf
wam
pgw
vfs
jje
hff
bss
eyp
rsz
mmp
dcl
muf
ukz
xrr
ncw
lbr
rnz
mbp
ygy
ski
lfx
lit
efz
qns
pkx
sbc
nrl
tdy
ymf
pru
gcf
wpj
gmf
rff
wat
wep
ubw
jxw
xey
bab
uqe
dwm
yig
afr
xct
lyo
zpy
zjp
vzq
ohe
bvk
ahx
swa
kor
bha
wxv
rgb
omg
dcv
msu
pdu
lfv
vlm
ppn
bmd
maj
eej
bsx
hyn
wjl
xlt
fsx
oto
oqr
ipy
nor
ocy
gel
pwc
hyw
azc
xuj
rjh
awr
lxr
iht
qve
eqb
qgv
gbz
yhk
awg
ymm
ywi
nbd
qqi
mso
whs
hhm
nrr
ctw
qmk
ocx
crl
yva
hgf
hvx
xxx
kiz
pzt
oui
ivk
kxt
emi
icb
ltd
ddo
tmy
dnf
zgg
ode
vxw
feo
bfz
cai
zlp
lcl
how
llm
xsp
voo
fsb
rmk
hoz
qyd
lml
ncd
pam
xwc
pns
mkp
aov
rum
yug
ebr
djt
uwt
dhv
bmx
lhd
lty
beb
qgz
xeo
wgn
jqh
wvq
amf
pbs
ozo
hzd
wxl
ukl
ttw
sji
ehk
hqb
npq
trt
qgd
fpt
xmu
tgn
heq
mpx
rtv
xzj
maz
dil
gis
vyz
vhm
nls
joh
ffz
syh
duw
oib
wrv
zus
dud
ocq
osd
xgj
dkv
dig
rer
ecw
cjd
nue
hce
cmk
rmm
zvt
eif
vqg
qnf
yel
cyo
cfd
bxt
dqr
uwj
ary
lga
jns
yvi
iyd
zth
onz
pob
jjl
eqp
qok
vjr
lqz
wrd
ndk
ojj
knz
mls
vfb
rtu
sjp
dzq
exn
bde
efs
fbz
ney
slc
ccr
jpn
amp
hyz
ryl
erd
#5
俺用dev,呵呵!~dev可以编译通过,linux、unix都可以编译通过……
#6
昨天给别人写了2个程序,我就觉得这个有问题,在unix下报错……
结果,他在linux下说没问题,晚上打电话说数据少没问题,多的话就有问题……
有没有什么好一些的方法?二叉树是必须的……
结果,他在linux下说没问题,晚上打电话说数据少没问题,多的话就有问题……
有没有什么好一些的方法?二叉树是必须的……
#7
楼主不好意思
偶也只有友情帮顶了
偶也只有友情帮顶了
#8
./1 ./dat.txt
Please input the searched data:ojj
***********Linear Form***********
(Line:1010,Row:1)
The matched number of the string 'ojj' is 1.
Times:12332
***********Tree Form***********
Found!
Times:1009
偶这里没问题的样子。。
Please input the searched data:ojj
***********Linear Form***********
(Line:1010,Row:1)
The matched number of the string 'ojj' is 1.
Times:12332
***********Tree Form***********
Found!
Times:1009
偶这里没问题的样子。。
#9
$ valgrind -v ./1 ./dat.txt
==27450== Memcheck, a memory error detector.
==27450== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==27450== Using LibVEX rev 1732, a library for dynamic binary translation.
==27450== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==27450== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==27450== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==27450==
--27450-- Command line
--27450-- ./1
--27450-- ./dat.txt
--27450-- Startup, with flags:
--27450-- -v
--27450-- Contents of /proc/version:
--27450-- Linux version 2.6.22-15-generic (buildd@king) (gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)) #1 SMP Fri Jul 11 18:56:36 UTC 2008
--27450-- Arch and hwcaps: AMD64, amd64-sse2
--27450-- Page sizes: currently 4096, max supported 4096
--27450-- Valgrind library directory: /usr/lib/valgrind
--27450-- Reading syms from /tmp/1 (0x400000)
--27450-- Reading syms from /lib/ld-2.6.1.so (0x4000000)
--27450-- Reading debug info from /lib/ld-2.6.1.so...
--27450-- ... CRC mismatch (computed F18656F0 wanted 247B0180)
--27450-- object doesn't have a symbol table
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/memcheck (0x38000000)
--27450-- object doesn't have a dynamic symbol table
--27450-- Reading suppressions file: /usr/lib/valgrind/default.supp
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_core.so (0x4A1E000)
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_memcheck.so (0x4C1F000)
--27450-- Reading syms from /lib/libc-2.6.1.so (0x4E26000)
--27450-- Reading debug info from /lib/libc-2.6.1.so...
--27450-- ... CRC mismatch (computed C5B5C357 wanted 602D23C5)
--27450-- object doesn't have a symbol table
--27450-- REDIR: 0x4E9EF30 (rindex) redirected to 0x4C22840 (rindex)
--27450-- REDIR: 0x4E9FD90 (memset) redirected to 0x4C22CA0 (memset)
Please input the searched data:abcd
--27450-- REDIR: 0x4E9E5F0 (strcpy) redirected to 0x4C239D0 (strcpy)
--27450-- REDIR: 0x4E9EB20 (strlen) redirected to 0x4C229F0 (strlen)
***********Linear Form***********
--27450-- REDIR: 0x4E99D90 (malloc) redirected to 0x4C21B94 (malloc)
--27450-- REDIR: 0x4E9B670 (free) redirected to 0x4C217A4 (free)
--27450-- REDIR: 0x4E9FEA0 (mempcpy) redirected to 0x4C23250 (mempcpy)
--27450-- REDIR: 0x4E9F640 (memchr) redirected to 0x4C22B80 (memchr)
--27450-- REDIR: 0x4EA08A0 (memcpy) redirected to 0x4C23770 (memcpy)
0 matched.
***********Tree Form***********
--27450-- REDIR: 0x4E9E5B0 (strcmp) redirected to 0x4C22AC0 (strcmp)
Not Found!
==27450==
==27450== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
--27450--
--27450-- supp: 8 dl-hack3
==27450== malloc/free: in use at exit: 279,616 bytes in 1,028 blocks.
==27450== malloc/free: 1,032 allocs, 4 frees, 281,888 bytes allocated.
==27450==
==27450== searching for pointers to 1,028 not-freed blocks.
==27450== checked 79,896 bytes.
==27450==
==27450== LEAK SUMMARY:
==27450== definitely lost: 279,616 bytes in 1,028 blocks.
==27450== possibly lost: 0 bytes in 0 blocks.
==27450== still reachable: 0 bytes in 0 blocks.
==27450== suppressed: 0 bytes in 0 blocks.
==27450== Rerun with --leak-check=full to see details of leaked memory.
--27450-- memcheck: sanity checks: 41 cheap, 2 expensive
--27450-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--27450-- memcheck: auxmaps: 0 searches, 0 comparisons
--27450-- memcheck: SMs: n_issued = 18 (288k, 0M)
--27450-- memcheck: SMs: n_deissued = 0 (0k, 0M)
--27450-- memcheck: SMs: max_noaccess = 524287 (8388592k, 8191M)
--27450-- memcheck: SMs: max_undefined = 0 (0k, 0M)
--27450-- memcheck: SMs: max_defined = 117 (1872k, 1M)
--27450-- memcheck: SMs: max_non_DSM = 18 (288k, 0M)
--27450-- memcheck: max sec V bit nodes: 0 (0k, 0M)
--27450-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--27450-- memcheck: max shadow mem size: 4432k, 4M
--27450-- translate: fast SP updates identified: 1,373 ( 86.1%)
--27450-- translate: generic_known SP updates identified: 139 ( 8.7%)
--27450-- translate: generic_unknown SP updates identified: 81 ( 5.0%)
--27450-- tt/tc: 4,229 tt lookups requiring 4,292 probes
--27450-- tt/tc: 4,229 fast-cache updates, 2 flushes
--27450-- transtab: new 2,107 (46,629 -> 834,409; ratio 178:10) [0 scs]
--27450-- transtab: dumped 0 (0 -> ??)
--27450-- transtab: discarded 0 (0 -> ??)
--27450-- scheduler: 4,183,562 jumps (bb entries).
--27450-- scheduler: 41/3,292 major/minor sched events.
--27450-- sanity: 42 cheap, 2 expensive checks.
--27450-- exectx: 30,011 lists, 17 contexts (avg 0 per list)
--27450-- exectx: 1,044 searches, 1,027 full compares (983 per 1000)
--27450-- exectx: 0 cmp2, 28 cmp4, 0 cmpAll
==27450== Memcheck, a memory error detector.
==27450== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==27450== Using LibVEX rev 1732, a library for dynamic binary translation.
==27450== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==27450== Using valgrind-3.2.3-Debian, a dynamic binary instrumentation framework.
==27450== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==27450==
--27450-- Command line
--27450-- ./1
--27450-- ./dat.txt
--27450-- Startup, with flags:
--27450-- -v
--27450-- Contents of /proc/version:
--27450-- Linux version 2.6.22-15-generic (buildd@king) (gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)) #1 SMP Fri Jul 11 18:56:36 UTC 2008
--27450-- Arch and hwcaps: AMD64, amd64-sse2
--27450-- Page sizes: currently 4096, max supported 4096
--27450-- Valgrind library directory: /usr/lib/valgrind
--27450-- Reading syms from /tmp/1 (0x400000)
--27450-- Reading syms from /lib/ld-2.6.1.so (0x4000000)
--27450-- Reading debug info from /lib/ld-2.6.1.so...
--27450-- ... CRC mismatch (computed F18656F0 wanted 247B0180)
--27450-- object doesn't have a symbol table
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/memcheck (0x38000000)
--27450-- object doesn't have a dynamic symbol table
--27450-- Reading suppressions file: /usr/lib/valgrind/default.supp
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_core.so (0x4A1E000)
--27450-- Reading syms from /usr/lib/valgrind/amd64-linux/vgpreload_memcheck.so (0x4C1F000)
--27450-- Reading syms from /lib/libc-2.6.1.so (0x4E26000)
--27450-- Reading debug info from /lib/libc-2.6.1.so...
--27450-- ... CRC mismatch (computed C5B5C357 wanted 602D23C5)
--27450-- object doesn't have a symbol table
--27450-- REDIR: 0x4E9EF30 (rindex) redirected to 0x4C22840 (rindex)
--27450-- REDIR: 0x4E9FD90 (memset) redirected to 0x4C22CA0 (memset)
Please input the searched data:abcd
--27450-- REDIR: 0x4E9E5F0 (strcpy) redirected to 0x4C239D0 (strcpy)
--27450-- REDIR: 0x4E9EB20 (strlen) redirected to 0x4C229F0 (strlen)
***********Linear Form***********
--27450-- REDIR: 0x4E99D90 (malloc) redirected to 0x4C21B94 (malloc)
--27450-- REDIR: 0x4E9B670 (free) redirected to 0x4C217A4 (free)
--27450-- REDIR: 0x4E9FEA0 (mempcpy) redirected to 0x4C23250 (mempcpy)
--27450-- REDIR: 0x4E9F640 (memchr) redirected to 0x4C22B80 (memchr)
--27450-- REDIR: 0x4EA08A0 (memcpy) redirected to 0x4C23770 (memcpy)
0 matched.
***********Tree Form***********
--27450-- REDIR: 0x4E9E5B0 (strcmp) redirected to 0x4C22AC0 (strcmp)
Not Found!
==27450==
==27450== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 8 from 1)
--27450--
--27450-- supp: 8 dl-hack3
==27450== malloc/free: in use at exit: 279,616 bytes in 1,028 blocks.
==27450== malloc/free: 1,032 allocs, 4 frees, 281,888 bytes allocated.
==27450==
==27450== searching for pointers to 1,028 not-freed blocks.
==27450== checked 79,896 bytes.
==27450==
==27450== LEAK SUMMARY:
==27450== definitely lost: 279,616 bytes in 1,028 blocks.
==27450== possibly lost: 0 bytes in 0 blocks.
==27450== still reachable: 0 bytes in 0 blocks.
==27450== suppressed: 0 bytes in 0 blocks.
==27450== Rerun with --leak-check=full to see details of leaked memory.
--27450-- memcheck: sanity checks: 41 cheap, 2 expensive
--27450-- memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
--27450-- memcheck: auxmaps: 0 searches, 0 comparisons
--27450-- memcheck: SMs: n_issued = 18 (288k, 0M)
--27450-- memcheck: SMs: n_deissued = 0 (0k, 0M)
--27450-- memcheck: SMs: max_noaccess = 524287 (8388592k, 8191M)
--27450-- memcheck: SMs: max_undefined = 0 (0k, 0M)
--27450-- memcheck: SMs: max_defined = 117 (1872k, 1M)
--27450-- memcheck: SMs: max_non_DSM = 18 (288k, 0M)
--27450-- memcheck: max sec V bit nodes: 0 (0k, 0M)
--27450-- memcheck: set_sec_vbits8 calls: 0 (new: 0, updates: 0)
--27450-- memcheck: max shadow mem size: 4432k, 4M
--27450-- translate: fast SP updates identified: 1,373 ( 86.1%)
--27450-- translate: generic_known SP updates identified: 139 ( 8.7%)
--27450-- translate: generic_unknown SP updates identified: 81 ( 5.0%)
--27450-- tt/tc: 4,229 tt lookups requiring 4,292 probes
--27450-- tt/tc: 4,229 fast-cache updates, 2 flushes
--27450-- transtab: new 2,107 (46,629 -> 834,409; ratio 178:10) [0 scs]
--27450-- transtab: dumped 0 (0 -> ??)
--27450-- transtab: discarded 0 (0 -> ??)
--27450-- scheduler: 4,183,562 jumps (bb entries).
--27450-- scheduler: 41/3,292 major/minor sched events.
--27450-- sanity: 42 cheap, 2 expensive checks.
--27450-- exectx: 30,011 lists, 17 contexts (avg 0 per list)
--27450-- exectx: 1,044 searches, 1,027 full compares (983 per 1000)
--27450-- exectx: 0 cmp2, 28 cmp4, 0 cmpAll
#10
你的是什么系统?狒狒……
linux?
那为什么我unix报错??
他那边数据多了也报错……
omg!~嘿嘿!~
不管了,一会儿我问问,看看要是问题解决了的话最好,
没解决的话,我昨天告诉他改#define SIZE 256
改小一些,不知道这个方法可行否?
linux?
那为什么我unix报错??
他那边数据多了也报错……
omg!~嘿嘿!~
不管了,一会儿我问问,看看要是问题解决了的话最好,
没解决的话,我昨天告诉他改#define SIZE 256
改小一些,不知道这个方法可行否?
#11
你让他放在 gdb 里跑,出错之后直接 bt 看是哪里的问题。
数据量大到什么程度出错?
我这里是
$ uname -a
Linux ubuntu-server 2.6.22-15-generic #1 SMP Fri Jul 11 18:56:36 UTC 2008 x86_64 GNU/Linux
数据量大到什么程度出错?
我这里是
$ uname -a
Linux ubuntu-server 2.6.22-15-generic #1 SMP Fri Jul 11 18:56:36 UTC 2008 x86_64 GNU/Linux
#12
呵呵unix系统没装,友情up~~
#13
unix下没有这个valgrind
#14
呵呵!~
谢谢狒狒同学!~
看来俺昨天晚上没打骚扰是个明智的选择……
o(∩_∩)o...哈哈!~
把这个帖子地址发过去,让他慢慢研究去吧!~
谢谢狒狒同学!~
看来俺昨天晚上没打骚扰是个明智的选择……
o(∩_∩)o...哈哈!~
把这个帖子地址发过去,让他慢慢研究去吧!~
#15
valgrind is only available to various Linux platforms.
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
这个函数参数是不是有问题?为什么是 BSTree,不是 BSTree *?
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
这个函数参数是不是有问题?为什么是 BSTree,不是 BSTree *?
#16
我错了。。i hate typedefs..
#17
顶
#18
o(∩_∩)o...
这个函数没有问题
/*
jfsnew.c(new)
更正查找次数
*/
#include <stdio.h>
#include <string.h>
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
typedef BSTNode *BSTree; /* 二叉排序树 */
int judge_argc(int argc);
void check(char *key); /* 处理数据(去掉读取数据最后的控制字符) */
void InsertBST(BSTree *ptr,char *key); /* 插入结点 */
void Show(BSTNode *pHead); /* 显示树 */
BSTNode *SearchBST(BSTree tree,char *key); /* 递归查找 */
BSTree CreateBST(char *filename,char *data); /* 生成树 */
long matchstring(char *str,char *substr);
long get_interspace(char *argv1,long *iInterSpace);
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY);
static long counttree = 0;
static long countline = 0;
/* 主函数:通过命令行输入参数,输入格式为:程序可执行文件名 文件名 */
int main(int argc,char *argv[])
{
char key1[SIZE];
char key2[SIZE];
long iInterSpace = 0; /* 文件大小(字节数) */
long iMatchX = 0; /* 行数 */
long iMatchY = 0; /* 列数 */
judge_argc(argc); /* 判断参数个数,如果个数不足,退出程序 */
/* 输入数据 */
memset(key1,'\0',sizeof(key1));
memset(key2,'\0',sizeof(key2));
printf("\nPlease input the searched data:");
scanf("%s",key1);
strcpy(key2,key1);
/* 线性形式 */
printf("\n***********Linear Form***********\n");
get_interspace(argv[1],&iInterSpace); /* 获取文件大小 */
matchposition(argv[1],key1,&iInterSpace,&iMatchX,&iMatchY); /*获取字符串在文件中出现的次数和所在的行、列数 */
/* tree形式 */
printf("***********Tree Form***********\n");
CreateBST(argv[1],key2);
return 0;
}
/* 判断命令行参数个数,如果不足,退出程序 */
int judge_argc(int argc)
{
if(argc < 2)
{
printf("Input Error!\nUsage:programmename testfilename\n");
printf("输入错误!\n使用方法:程序可执行文件名 文件名\n");
exit(2);
}
return 0;
}
/* 获取文件大小,以便之后程序中申请合适的空间 */
long get_interspace(char *argv1,long *iInterSpace)
{
FILE *fp;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
while(! feof(fp))
{
fgetc(fp);
*iInterSpace = *iInterSpace + 1;
}
fclose(fp);
return 0;
}
/* 判断匹配字符串的个数 */
long matchstring(char *str,char *substr)
{
char *p,*sp;
long iNum = 0;
p = str;
sp = substr;
while(*str != '\0')
{
str = p;
while(*substr != '\0')
{
if(*str == *substr)
{
substr++;
str++;
}
else
{
substr = sp;
str = p;
break;
}
}
if(*substr == '\0')
{
iNum = iNum + 1;
substr = sp;
p = str;
continue;
}
p++;
}
return iNum;
}
/* 判断输出具体的匹配行、列数,最后统计匹配总个数 */
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY)
{
FILE *fp;
char cString[*iInterSpace + 2]; /* 所有字符 */
char cPerLine[*iInterSpace + 2]; /* 单行字符 */
int ii,jj,kk; /* 临时变量,用来控制循环 */
long iReadNum = 0; /* 实际读取字符个数 */
long iLine = 0; /* 行数 */
long iMatchNum = 0;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* 初始化缓冲空间,并一次性将数据都读出来 */
memset(cString,'\0',sizeof(cString));
memset(cPerLine,'\0',sizeof(cPerLine));
iReadNum = fread(cString,1,sizeof(cString),fp);
fclose(fp);
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* unix、linux下不需要,所以注释掉了.*/
/* (经调试,发现windows下为了获得正确的行数,需要在最后多加一个\n,于是,定义空间的时候多加了2个字符) */
/*
cString[iReadNum] = '\n';
*/
iLine = matchstring(cString,"\n"); /* 获取正确的行数,以便控制之后的读取次数 */
/* 重新打开文件,用fgets读取数据,通过行数控制读取次数 */
for(ii = 0;ii < iLine;ii++)
{
fgets(cPerLine,sizeof(cPerLine),fp);
*iMatchX = ii + 1; /* 获得正确的行数 */
/* 用strstr判断该行中是否有匹配的字符串,有的话,输出行、列数 */
if(strstr(cPerLine,argv2) != NULL)
{
for(jj = 0;jj < strlen(cPerLine);)
{
/* 用strncmp比较 */
countline = countline + ii + 1;
check(cPerLine);
if(0 == strncmp(cPerLine + jj,argv2,strlen(argv2)))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
break;
}
else
{
jj = jj + strlen(argv2);
}
#if 0 /* 逐个字符比较 */
for(kk = 0;kk < strlen(argv2);kk++)
{
countline = countline + 1;
/* 有1个字符不匹配,退出本次循环 */
if(cPerLine[jj + kk] != argv2[kk])
{
jj++;
break;
}
}
/* 都匹配时,输出行、列数 */
if(kk == strlen(argv2))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
printf("(Line:%ld,Row:%ld)\n",*iMatchX,*iMatchY);
jj++;
}
#endif
}
}
}
fclose(fp);
if(0 == iMatchNum)
{
printf("0 matched.\n");
}
else
{
printf("The matched number of the string '%s' is %ld.\n",argv2,iMatchNum);
printf("Times:%ld\n",countline);
}
return 0;
}
/* 处理数据(去掉读取数据最后的控制字符) */
void check(char *key)
{
int i;
for(i = 0;i < strlen(key);i++)
{
if(('\n' == key[i]) || ('\r' == key[i]))
{
key[i] = '\0';
break;
}
}
}
/* 插入结点 */
void InsertBST(BSTree *ptr,char *key)
{
BSTNode *curr,*p;
p = *ptr;
/* 查找插入位置 */
while(p)
{
/* 若树中已有key则无需插入,不存在时插入 */
if(0 != strcmp((p->data),key))
{
curr = p;
p = (key < (p->data))?p->lchild:p->rchild;
}
}
/* 生成新结点 */
p = (BSTNode *)malloc(sizeof(BSTNode));
strcpy(p->data,key);
p->lchild = p->rchild = NULL;
/* 如果原树为空,则新插入的结点为根结点 */
if(*ptr == NULL)
{
*ptr = p;
}
/* 原树非空时,将新结点作为其左、右孩子插入 */
else
{
if(key < (curr->data))
{
curr->lchild = p;
}
else
{
curr->rchild = p;
}
}
}
/* 递归查找 */
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
counttree = counttree + 1;
return tree;
}
if(key < (tree->data))
{
counttree = counttree + 1;
return SearchBST(tree->lchild,key);
}
else
{
counttree = counttree + 1;
return SearchBST(tree->rchild,key);
}
}
/* 显示树 */
void Show(BSTNode *pHead)
{
if (pHead != NULL)
{
Show(pHead->lchild);
printf("%s ",pHead->data);
Show(pHead->rchild);
}
}
/* 生成树 */
BSTree CreateBST(char *filename,char *data)
{
FILE *fp;
char key[SIZE];
BSTree tree = NULL;
BSTree result;
if((fp = fopen(filename,"r")) == NULL)
{
printf("Open file error!\n");
fclose(fp);
exit(-1);
}
/* 循环取数据每行新建一个结点 */
while(!feof(fp))
{
memset(key,'\0',sizeof(key));
/*
fgets(key,SIZE,fp);
check(key);
*/
fscanf(fp,"%s",key); /* 以单词为单位建立结点(无论数据文件里是如何存放数据的都可实现) */
InsertBST(&tree,key);
}
fclose(fp);
#if 0
Show(tree); /* 遍历显示树 */
#endif
/* 输入数据 */
memset(key,'\0',sizeof(key));
strcpy(key,data);
/* 查找结果 */
result = SearchBST(tree,key);
if(result)
{
printf("Found!\nTimes:%ld\n",counttree);
}
else
{
printf("Not Found!\n");
}
return tree;
}
#19
void InsertBST(BSTree *ptr,char *key)
函数里有严重的问题,你的
curr变量只在while的if语句里才有被赋值的机会,
可是下面就直接访问curr-data,不出问题才怪呢。
函数里有严重的问题,你的
curr变量只在while的if语句里才有被赋值的机会,
可是下面就直接访问curr-data,不出问题才怪呢。
#20
帮顶~
#21
根据村长大人的指点,已经修改了!~
但是unix下还是有问题……omg
但是unix下还是有问题……omg
/*
从数据文件data.txt中查找数据,分别用线性的和二叉树形式查找,
只要找到就不再继续找。
显示结果:找到found + 比较次数
未找到not found
*/
#include <stdio.h>
#include <string.h>
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
typedef BSTNode *BSTree; /* 二叉排序树 */
int judge_argc(int argc);
void check(char *key); /* 处理数据(去掉读取数据最后的控制字符) */
void InsertBST(BSTree *ptr,char *key); /* 插入结点 */
void Show(BSTNode *pHead); /* 显示树 */
BSTNode *SearchBST(BSTree tree,char *key); /* 递归查找 */
BSTree CreateBST(char *filename,char *data); /* 生成树 */
long matchstring(char *str,char *substr);
long get_interspace(char *argv1,long *iInterSpace);
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY);
static long counttree = 0;
static long countline = 0;
/* 主函数:通过命令行输入参数,输入格式为:程序可执行文件名 文件名 */
int main(int argc,char *argv[])
{
char key1[SIZE];
char key2[SIZE];
long iInterSpace = 0; /* 文件大小(字节数) */
long iMatchX = 0; /* 行数 */
long iMatchY = 0; /* 列数 */
judge_argc(argc); /* 判断参数个数,如果个数不足,退出程序 */
/* 输入数据 */
memset(key1,'\0',sizeof(key1));
memset(key2,'\0',sizeof(key2));
printf("\nPlease input the searched data:");
scanf("%s",key1);
strcpy(key2,key1);
/* 线性形式 */
printf("\n***********Linear Form***********\n");
get_interspace(argv[1],&iInterSpace); /* 获取文件大小 */
matchposition(argv[1],key1,&iInterSpace,&iMatchX,&iMatchY); /*获取字符串在文件中出现的次数和所在的行、列数 */
/* tree形式 */
printf("***********Tree Form***********\n");
CreateBST(argv[1],key2);
return 0;
}
/* 判断命令行参数个数,如果不足,退出程序 */
int judge_argc(int argc)
{
if(argc < 2)
{
printf("Input Error!\nUsage:programmename testfilename\n");
printf("输入错误!\n使用方法:程序可执行文件名 文件名\n");
exit(2);
}
return 0;
}
/* 获取文件大小,以便之后程序中申请合适的空间 */
long get_interspace(char *argv1,long *iInterSpace)
{
FILE *fp;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
fseek(fp,0,SEEK_END);
*iInterSpace = ftell(fp);
#if 0
while(! feof(fp))
{
fgetc(fp);
*iInterSpace = *iInterSpace + 1;
}
#endif
fclose(fp);
return 0;
}
/* 判断匹配字符串的个数 */
long matchstring(char *str,char *substr)
{
char *p,*sp;
long iNum = 0;
p = str;
sp = substr;
while(*str != '\0')
{
str = p;
while(*substr != '\0')
{
if(*str == *substr)
{
substr++;
str++;
}
else
{
substr = sp;
str = p;
break;
}
}
if(*substr == '\0')
{
iNum = iNum + 1;
substr = sp;
p = str;
continue;
}
p++;
}
return iNum;
}
/* 判断输出具体的匹配行、列数,最后统计匹配总个数 */
long matchposition(char *argv1,char *argv2,long *iInterSpace,long *iMatchX,long *iMatchY)
{
FILE *fp;
char cString[*iInterSpace + 2]; /* 所有字符 */
char cPerLine[*iInterSpace + 2]; /* 单行字符 */
int ii,jj,kk; /* 临时变量,用来控制循环 */
long iReadNum = 0; /* 实际读取字符个数 */
long iLine = 0; /* 行数 */
long iMatchNum = 0;
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* 初始化缓冲空间,并一次性将数据都读出来 */
memset(cString,'\0',sizeof(cString));
memset(cPerLine,'\0',sizeof(cPerLine));
iReadNum = fread(cString,1,sizeof(cString),fp);
fclose(fp);
for(ii = 0;ii < strlen(cString);)
{
/* 用strncmp比较 */
countline = countline + 1;
if(0 == strncmp(cString + ii,argv2,strlen(argv2)))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
break;
}
else
{
ii = ii + 1;
}
}
#if 0
if((fp = fopen(argv1,"r")) == NULL)
{
printf("Open the file named '%s' error!\n",argv1);
fclose(fp);
exit(2);
}
/* unix、linux下不需要,所以注释掉了.*/
/* (经调试,发现windows下为了获得正确的行数,需要在最后多加一个\n,于是,定义空间的时候多加了2个字符) */
/*
cString[iReadNum] = '\n';
*/
iLine = matchstring(cString,"\n"); /* 获取正确的行数,以便控制之后的读取次数 */
/* 重新打开文件,用fgets读取数据,通过行数控制读取次数 */
for(ii = 0;ii < iLine;ii++)
{
fgets(cPerLine,sizeof(cPerLine),fp);
*iMatchX = ii + 1; /* 获得正确的行数 */
/* 用strstr判断该行中是否有匹配的字符串,有的话,输出行、列数 */
if(strstr(cPerLine,argv2) != NULL)
{
for(jj = 0;jj < strlen(cPerLine);)
{
/* 逐个字符比较 */
for(kk = 0;kk < strlen(argv2);kk++)
{
countline = countline + 1;
/* 有1个字符不匹配,退出本次循环 */
if(cPerLine[jj + kk] != argv2[kk])
{
jj++;
break;
}
}
/* 都匹配时,输出行、列数 */
if(kk == strlen(argv2))
{
iMatchNum = iMatchNum + 1; /* 匹配个数 */
*iMatchY = jj + 1; /* 获得正确的列数 */
printf("(Line:%ld,Row:%ld)\n",*iMatchX,*iMatchY);
jj++;
}
}
}
}
fclose(fp);
#endif
if(0 == iMatchNum)
{
printf("0 matched.\n");
}
else
{
printf("The matched number of the string '%s' is %ld.\n",argv2,iMatchNum);
printf("Times:%ld\n",countline);
}
return 0;
}
/* 处理数据(去掉读取数据最后的控制字符) */
void check(char *key)
{
int i;
for(i = 0;i < strlen(key);i++)
{
if(('\n' == key[i]) || ('\r' == key[i]))
{
key[i] = '\0';
break;
}
}
}
/* 插入结点 */
void InsertBST(BSTree *ptr,char *key)
{
BSTNode *curr,*p;
p=NULL;
curr = NULL;
p = *ptr;
/* 查找插入位置 */
while(p)
{
/* 若树中已有key则无需插入,不存在时插入 */
if(0 != strcmp((p->data),key))
{
curr = p;
p = (key < (p->data))?p->lchild:p->rchild;
}
}
/* 生成新结点 */
p = (BSTNode *)malloc(sizeof(BSTNode));
strcpy(p->data,key);
p->lchild = p->rchild = NULL;
/* 如果原树为空,则新插入的结点为根结点 */
if(*ptr == NULL)
{
*ptr = p;
}
/* 原树非空时,将新结点作为其左、右孩子插入 */
else
{
if(curr == NULL)
{
printf("NULL pointer!");
exit (-1);
}
if(key < (curr->data))
{
curr->lchild = p;
}
else
{
curr->rchild = p;
}
}
}
/* 递归查找 */
BSTNode *SearchBST(BSTree tree,char *key)
{
if((tree == NULL) || (0 == (strcmp((tree->data),key))))
{
counttree = counttree + 1;
return tree;
}
if(key < (tree->data))
{
counttree = counttree + 1;
return SearchBST(tree->lchild,key);
}
else
{
counttree = counttree + 1;
return SearchBST(tree->rchild,key);
}
}
/* 显示树 */
void Show(BSTNode *pHead)
{
if (pHead != NULL)
{
Show(pHead->lchild);
printf("%s ",pHead->data);
Show(pHead->rchild);
}
}
/* 生成树 */
BSTree CreateBST(char *filename,char *data)
{
FILE *fp;
char key[SIZE];
BSTree tree = NULL;
BSTree result;
if((fp = fopen(filename,"r")) == NULL)
{
printf("Open file error!\n");
fclose(fp);
exit(-1);
}
/* 循环取数据每行新建一个结点 */
while(!feof(fp))
{
memset(key,'\0',sizeof(key));
/*
fgets(key,SIZE,fp);
check(key);
*/
fscanf(fp,"%s",key); /* 以单词为单位建立结点(无论数据文件里是如何存放数据的都可实现) */
InsertBST(&tree,key);
}
fclose(fp);
#if 0
Show(tree); /* 遍历显示树 */
#endif
/* 输入数据 */
memset(key,'\0',sizeof(key));
strcpy(key,data);
/* 查找结果 */
result = SearchBST(tree,key);
if(result)
{
printf("Found!\nTimes:%ld\n",counttree);
}
else
{
printf("Not Found!\n");
}
return tree;
}
#22
jf
#23
#define SIZE 256
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
变长的指针?
typedef struct node
{
char data[SIZE]; /* 数据项 */
struct node *lchild,*rchild; /* 左右孩子结点 */
}BSTNode;
变长的指针?
#24
帮顶
#25
我顶你的肺
#26
这个帖子。。。有点很。。。