c++程序实现c++代码相似度计算


用比较简单的方法实现代码相似度的计算!
资源截图
代码片段和文件信息
/*
对于两个C++语言的源程序代码,用哈希表的方法分别统计两个程序中使用C++语言关键字的情况,
并最终按定量的计算结果,得出两份程序的相似性。
基本要求:建立C++语言关键字的哈希表,统计在每个源程序中C++关键字出现的频度 得到两个
向量X1和X2,通过计算向量X1和X2的相对距离来判断两个源程序的相似性。
例如:  
关键字            void   int   for   char  if  else  while  do   break  class
程序1关键字频度    4     3      0     4     3    0     7     0     0      2
程序2关键字频度    4     2      0     5     4    0     5     2     0      1
X1=[4304307002]
X2=[4205405201]
设s是向量X1和X2的相对距离,s=sqrt(∑(xi1-xi2)^2),当X1=X2时,s=0 反映出可能是同一个程序;
s值越大,则两个程序的差别可能也越大。
测试数据: 选择若干组编译和运行都无误的C++程序,程序之间有相近的和差别大的,用上述方法求s 
对比两个程序的相似性。*/
#include
#include
#include
#include
#include
#define M 100000
using namespace std;
int hash1[20]hash2[20];
int fileProcessed(char sourceData[]int count);
void calcunum(char data[]int nint num);
double possibality(int hash1[]int hash2[]);
int main()
{
int i;
    int count1=1;//12用于判断第一个文件或者第二个文件
int count2=2;
//定义两个字符数组,用于存放经过处理后的文件的字符
char sourceData1[M];//第一个字符数组
    char sourceData2[M];//第二个字符数组
count1 = fileProcessed(sourceData1count1);  //数组赋值并返回字符个数
count2 = fileProcessed(sourceData2count2);
memset(hash10sizeof(hash1));
memset(hash20sizeof(hash2));
calcunum(sourceData1count11);
calcunum(sourceData2count22);
cout<<“break  char   class  do     else   for    if     int    void   while“< for(i=0;i<10;i++)
printf(“%-7d“hash1[i]);
cout< for(i=0;i<10;i++)
printf(“%-7d“hash2[i]);
cout< cout<<“相似度为: “< return 0;
}
int fileProcessed(char sourceData[]int count)
{
if(count == 1)
{//处理第一个文件
int i = 0;
char chfirst[10];
cout<<“第一个文件的文件名:“< cin>>first;
ifstream infile(firstios::in);
ofstream outfile(“firstProcessed.txt“ios::out);
if(!infile)
{
cerr<<“open first error“< }
if(!outfile)
{
cerr<<“open firstProcessed error“< }
//处理文件即将()[]{}替换为空格保存在firstProcessed中
while(infile.get(ch))
{
if(ch == ‘(‘ || ch == ‘)‘ || ch == ‘{‘ || ch == ‘}‘ || ch == ‘[‘ || ch == ‘]‘)
{
ch = ‘ ‘;
}
if(ch == ‘‘ || ch == ‘<‘ || ch == ‘>‘||ch==‘;‘)
{
ch = ‘ ‘;
}
outfile.put(ch);
}
infile.close(); outfile.close();
ifstream infile2(“firstProcessed.txt“ios::in);//打开firstProcessed需要比较的第一个文件
if(!infile2)
{
cerr<<“open firstProcessed error!“< }
       //把文件存储在sourceData数组里并进行进一步处理把多个空格合并为一个空格以减少空间开销
while(infile2.get(ch))
{
if(ch == ‘ ‘ && sourceData[i - 1] == ‘ ‘)
{
continue;//取消连续的空格
}
else if(ch == ‘
‘ && sourceData[i - 1] == ‘
‘)
{
continue;//取消连续的空行
}
else
{
sourceData[i++]= ch;
}
}
count = i; infile2.close();
return count;
}
else
{
int i = 0;
char chsecond[10];
cout<<“第二个文件的文件名:“< cin>>second;
ifstream infile(secondios::in);
ofstream outfile(“secondProcessed.txt“ios::out)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-07-02 14:10  c++程序实现c++代码相似度计算
     文件        1621  2016-07-02 10:59  c++程序实现c++代码相似度计算1.txt
     文件        1639  2016-07-02 10:59  c++程序实现c++代码相似度计算2.txt
     文件        1848  2009-12-30 15:29  c++程序实现c++代码相似度计算3.txt
     目录           0  2016-07-02 12:07  c++程序实现c++代码相似度计算Debug
     文件      581715  2016-07-02 12:07  c++程序实现c++代码相似度计算Debughua.exe
     文件      829972  2016-07-02 12:07  c++程序实现c++代码相似度计算Debughua.ilk
     文件      297946  2016-07-02 12:07  c++程序实现c++代码相似度计算Debughua.obj
     文件     2148984  2016-07-02 10:59  c++程序实现c++代码相似度计算Debughua.pch
     文件     1139712  2016-07-02 12:07  c++程序实现c++代码相似度计算Debughua.pdb
     文件       74752  2016-07-02 12:10  c++程序实现c++代码相似度计算Debugvc60.idb
     文件      110592  2016-07-02 12:07  c++程序实现c++代码相似度计算Debugvc60.pdb
     文件        1621  2016-07-02 12:10  c++程序实现c++代码相似度计算firstProcessed.txt
     文件        5491  2016-07-02 12:07  c++程序实现c++代码相似度计算hua.cpp
     文件        3365  2016-07-02 12:10  c++程序实现c++代码相似度计算hua.dsp
     文件         531  2016-07-02 14:10  c++程序实现c++代码相似度计算hua.dsw
     文件       41984  2016-07-02 14:10  c++程序实现c++代码相似度计算hua.ncb
     文件       48640  2016-07-02 14:10  c++程序实现c++代码相似度计算hua.opt
     文件         240  2016-07-02 12:10  c++程序实现c++代码相似度计算hua.plg
     文件        1848  2016-07-02 12:10  c++程序实现c++代码相似度计算secondProcessed.txt

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)