程序的源代码的相似性判别Word文档格式.docx

上传人:b****5 文档编号:19818425 上传时间:2023-01-10 格式:DOCX 页数:35 大小:106.78KB
下载 相关 举报
程序的源代码的相似性判别Word文档格式.docx_第1页
第1页 / 共35页
程序的源代码的相似性判别Word文档格式.docx_第2页
第2页 / 共35页
程序的源代码的相似性判别Word文档格式.docx_第3页
第3页 / 共35页
程序的源代码的相似性判别Word文档格式.docx_第4页
第4页 / 共35页
程序的源代码的相似性判别Word文档格式.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

程序的源代码的相似性判别Word文档格式.docx

《程序的源代码的相似性判别Word文档格式.docx》由会员分享,可在线阅读,更多相关《程序的源代码的相似性判别Word文档格式.docx(35页珍藏版)》请在冰豆网上搜索。

程序的源代码的相似性判别Word文档格式.docx

数据类型有哈希表,顺序表。

ADT类classHash如图2.2:

图2.2ADT类classHash

这个程序主要包括三个函数

(1)主函数intmain()负责调用其他的函数

(2)文件处理函数count(charsourceData[],count)对文件进行处理

(3)相似度计算函数doublepossibality(Hashhash1,Hashhash2)根据已有的公式计算相似度。

三、课题相关数据结构及算法设计

1主要数据结构

主程序为:

intmain()

{

charoperation;

cout<

<

"

其他键开始比较E程序结束:

endl<

"

;

cin>

>

operation;

while(operation!

='

E'

intcount1=1;

//1,2用于判断第一个文件或者第二个文件

intcount2=2;

//定义两个字符数组,用于存放经过处理后的文件的字符///////

charsourceData1[M];

///////第一个字符数组///////////////

charsourceData2[M];

///////第二个字符数组///////////////

count1=(sourceData1,count1);

//数组赋值并返回字符个数

count2=(sourceData2,count2);

Hashhash1(sourceData1,count1);

//定义第一个哈希表

Hashhash2(sourceData2,count2);

//定义第二个

hash1.calcuNum();

//计算1中每个关键字的频度

hash2.calcuNum();

breakcharclassdoelseforifintvoidwhile"

endl;

hash1.Display();

//输出第一个哈希表中关键字的个数

hash2.Display();

//输出第二个

相似度为:

possibality(hash1,hash2)<

}

return0;

2、文件处理函数int(charsourceData[],intcount)

类型:

全局函数,

返回值为文件中字符的个数,为查找做准备。

目的:

取消连续的空格或空行,以减少查找时间,并把处理后的字符存到字符数组中。

(1)从文件里读取字符,将字符进行简单处理:

把所有的括号包括”()”,”[]”,”{}”,”<

”替换成空格,

(2)将字符读到firstProcessed.txt文件中;

代码为:

//括号边空格部分代码

while(in(ch))

if(ch=='

('

||ch=='

)'

{'

}'

['

]'

ch='

'

'

'

out(ch);

}//字符读到数组里的代码

&

&

sourceData[i-1]=='

//取消连续的空格

elseif(ch=='

//添加空格

sourceData[i]='

i++;

sourceData[i]=ch;

else

sourceData[i]=ch;

3.相似度计算函数doublepossibality(Hashhash1,Hashhash2)

参数hash1,hash2里分别存放两个文件中关键字的次数;

原理为:

公式:

s=sqrt(∑(xi1-xi2)2);

返回值为double型值;

功能:

计算两个文件相似度;

doublepossibality(Hashhash1,Hashhash2)

inti;

doublesum=0;

doublepos=0;

for(i=0;

i<

10;

i++)

sum=sum+(hash1.hashTab[i]-hash2.hashTab[i])*(hash1.hashTab[i]-hash2.hashTab[i]);

pos=sqrt(sum);

returnpos;

复杂度分析:

本程序时间复杂度为O(n),空间复杂度为charsourceData[10^4]

四、源程序代码

Main.cpp程序源代码:

#include<

math.h>

//#include"

Hash.h"

#include"

Hash.cpp"

int(charsourceData[],intcount);

//将处理后的文件保存到字符数组里,该函数返回字符数组里字符的总个数

doublepossibality(Hashhash1,Hashhash2);

//根据两个哈希表和已有公式计算相似度

charoperation;

cout<

输入操作"

cin>

while(operation!

{

intcount1=1;

intcount2=2;

//定义两个字符数组,用于存放经过处理后的文件的字符///////

charsourceData1[M];

charsourceData2[M];

count1=(sourceData1,count1);

count2=(sourceData2,count2);

Hashhash1(sourceData1,count1);

Hashhash2(sourceData2,count2);

hash1.calcuNum();

hash2.calcuNum();

hash1.Display();

hash2.Display();

输入操作:

}

return0;

int(charsourceData[],intcount)

///////////////////////处理第一个文件////////////////////////////////////////////////////////////////

if(count==1)

inti=0;

charch;

charfirst[10];

cout<

第一个文件的文件名:

cin>

first;

ifstreamin);

ofstreamoutfile("

firstProcessed.txt"

ios:

:

out);

if(!

infile)

{

cerr<

openfirsterror"

}

outfile)

openfirstProcessederror"

//处理文件即将()[]{}替换为空格保存在firstProcessed中

while(in(ch))

if(ch=='

{

ch='

}

out(ch);

in();

out();

ifstreaminfile2("

in);

//打开firstProcessed需要比较的第一个文件

count=0;

infile2)

openfirstProcessederror!

//把文件存储在sourceData数组里并进行进一步处理把多个空格合并为一个空格以减少空间开销2//

//取消连续的空格

elseif(ch=='

\n'

//取消连续的空行

//添加空格

sourceData[i]='

i++;

sourceData[i]=ch;

else

sourceData[i]=ch;

count=i;

////////////////////////////////////////////////////////////////////////////2////

returncount;

else

charsecond[10];

第二个文件的文件名:

second;

secondProcessed.txt"

openseconderror"

opensecondProcessederror"

///////////////////////////处理文件即将()[]{}替换为空格/////////////////////////1//

||ch=='

ch='

//////需要比较的第二个文件

opensecondProcessederror!

//cout<

ch;

///////////////////////////////相似度比较/////////////////////////////////////

inti;

doublesum=0;

doublepos=0;

for(i=0;

sum=sum+(hash1.hashTab[i]-hash2.hashTab[i])*(hash1.hashTab[i]-hash2.hashTab[i]);

pos=sqrt(sum);

returnpos;

Hash.cpp源代码:

//Hash.cpp:

implementationoftheHashclass.

#include"

//Construction/Destruction

Hash:

Hash(charprocessed[],intn_count)

count=n_count;

count;

data[i]=processed[i];

/*哈希表长度为10;

存放10个整数来记录出现关键字的次数其中hashTab【0】-【9】分别为break,

char,class,do,else,//for,if,int,void,while*/

hashTab[i]=0;

~Hash()

voidHash:

Display()

hashTab[i]<

Cumulating(intlocation)

hashTab[location]++;

calcuNum()

//inta=0;

inti=0;

while(i!

=count)

if(data[i]=='

i'

if(data[i-1]=='

||data[i-1]=='

||data[i-1]==9||i==0)

i++;

if(data[i]=='

f'

{

i++;

if(data[i]=='

||data[i]=='

{

Cumulating(6);

}

else

while(data[i]!

data[i]!

{

i++;

}

}

elseif(data[i]=='

n'

t'

i++;

if(data[i]=='

Cumulating(7);

else

while(data[i]!

{

i++;

}

else

while(data[i]!

while(data[i]!

elseif(data[i]=='

o'

r'

Cumulating(5);

b'

e'

a'

if(data[i]=='

k'

if(data[i]=='

{

Cumulating(0);

}

else

while(data[i]!

{

i++;

}

else

while(data[i]!

da

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 自考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1