Hello to everyone. I am working on pattern matching.

Problem is that I have match source codes weather they are copies or not (c++ source code only).

take two .cpp files and match them. and result that if they are match or not..


Please help me. Thanks in Advance.

Recommended Answers

All 5 Replies

code?

like i just code a small part of it.

int main ()
{
	string array1[300]; 
	short loop1=0; 
	string line1; 
	ifstream myfile1 ("sample2.cpp"); 
	if (myfile1.is_open()) 
	{
	while (! myfile1.eof() ) 
	{
	getline (myfile1,line1); 
	array1[loop1] = line1;
	cout << array1[loop1] << endl; 
	loop1++;
	}
	myfile1.close(); 
	}
	else cout << "Unable to open file"; 
	system("PAUSE");

	// file 2 open 


	string array2[300]; 
	short loop2=0; 
	string line2; 
	ifstream myfile2 ("sample3.cpp"); 
	if (myfile2.is_open()) 
	{
	while (! myfile2.eof() ) 
	{
	getline (myfile2,line2); 
	array2[loop2] = line2;
	cout << array2[loop2] << endl; 
	loop2++;
	}
	myfile2.close(); 
	}
	else cout << "Unable to open file"; 
	system("PAUSE");

this code open two .cpp files files are

// Sample2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "conio.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a[4],max=0;
for(int i=0;i<4;i++)
{
cout<<"Enter integars value"<<endl;
cin>>a[i];
if(max<a[i])
max=a[i];
}
cout<<"The MAX is "<<max<<endl;
getch();
return 0;
}

and

// sample3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include "conio.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int b[4],maximum=0;
for(int i=0;i<4;i++)
{
cout<<"Enter integars value"<<endl;
cin>>b[i];
if(maximum<b[i])
maximum=b[i];
}
cout<<"The MAXIMUN is "<<maximum<<endl;
getch();
return 0;
}

both codes are same just variables are different. i want to have a code who matches the pattern of above coding.

this is a basic problem. If it is solved then i move to larger and complex codes.
please help me that how i compare both codes. what kind of algorithm is needed.

Maybe you could create a program that will compile the cpp files and save the assembly code into a file and then see if the assembly code is the same. Otherwise I guess you would have to create a parser and store the code as tokens and then match the tokes.

Open file 1
Open file 2
Read a string of n chars from file1 into array1
Read a string of n chars from file 2 into aray2

Compare array1 and array2. You will have to ignore spaces and new lines when you are trying to comapre

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.