📄 findidentify.cpp
字号:
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
using namespace std;
void Analyse(ifstream&);
//main()函数
int main()
{
ifstream file; // input file
//open the file
string filename;
cout <<"Input the c++ file name: ";
cin>> filename;
file.open(filename.c_str());
//如果打开文件就分析文件
if(!file)
cout<<"Can't open "<<filename<<endl;
else Analyse(file);
return 0;
}
//分析读入的cpp文件。
void Analyse(ifstream& readFile)
{
const int v1=10;
const int v2=50;
const int f1=11;
const int f2=50;
int low;
int high;
int i,j,k,p,q;
k=-1;
q=-1;
int lineNum=0;//定义的行数.
int lastpos;
bool flag=true;
bool done=true;
string aLine;
string modifier;
string::size_type pos;
string::size_type temp;
string::size_type temp1;
string::size_type temp2;
string::size_type temp3;
string variableNames[v1][v2];
int variableNumber[v1];
int varlineNum[v1][v2][100];
int appearNumber[v2][v2];
string functionNames[f1][f2];
int functionNumber[f1];
int funlineNum[f1][f2];
char oper[]={'#','!','~','-','*','/','%','+','<','>','&','?','^','|','='};
const int operLength=15;
int oper_line[100];
string oper_name[100];
string::size_type oper_pos;
functionNames[0][0]=variableNames[0][0]="char ";
functionNames[1][0]=variableNames[1][0]="short ";
functionNames[2][0]=variableNames[2][0]="int ";
functionNames[3][0]=variableNames[3][0]="long ";
functionNames[4][0]=variableNames[4][0]="bool ";
functionNames[5][0]=variableNames[5][0]="float ";
functionNames[6][0]=variableNames[6][0]="double ";
functionNames[7][0]=variableNames[7][0]="string ";
functionNames[8][0]=variableNames[8][0]="ifstream ";
functionNames[9][0]=variableNames[9][0]="ofstream ";
functionNames[10][0]="void ";
//初始化;
for( i=0;i<v1;i++)
{
functionNumber[i]=variableNumber[i]=0;
}
for(i=0;i<v1;i++)
{
for(j=0;j<v2;j++)
{
for(p=0;p<100;p++)
varlineNum[i][j][p]=0;
}
}
for(i=0;i<v1;i++)
{
for(j=0;j<v2;j++)
appearNumber[i][j]=0;
}
functionNumber[10]=0;
//start to analyse;
getline(readFile,aLine);
while(readFile)
{
lineNum++;
flag=true;
temp=aLine.find("/");
temp1=aLine.find('\'');
temp2=aLine.find('\"');
//用来检测说明部分
if(temp!=string::npos)
{
if(temp1!=string::npos&&(temp1<temp))
{
for(i=temp1+1;i<aLine.length();i++)
{
if(aLine.at(i)=='\'')
{k=i;continue;}
}
if(k!=-1)
{
if(k<temp)
for(i=temp;i<aLine.length();i++)
aLine.at(i)=' ';
}
k=-1;
}
else if(temp2!=string::npos&&(temp1<temp))
{
for(i=temp1+1;i<aLine.length();i++)
{
if(aLine.at(i)=='\'')
{k=i;continue;}
}
if(k!=-1)
{
if(k<temp)
for(i=temp;i<aLine.length();i++)
aLine.at(i)=' ';
}
k=-1;
}
else if(aLine.at(temp+1)=='/'){
for(i=temp;i<aLine.length();i++)
aLine.at(i)=' ';
}
else if(aLine.at(temp+1=='*'))
{
temp3=aLine.find("*/");
for(i=temp;i<aLine.length();i++)
aLine.at(i)=' ';
if(temp3==string::npos)
{
getline(readFile,aLine);
lineNum++;
temp3=aLine.find("*/");
while(temp3==string::npos&&readFile)
{
getline(readFile,aLine);
lineNum++;
temp3=aLine.find("*/");
}
getline(readFile,aLine);
if(!readFile)
break;
lineNum++;
}
}
}
//**********************************************************************************************
for( i=0;i<v1;i++)
{
pos=aLine.find(variableNames[i][0]);
while ( (pos !=string::npos) && (pos==0 || (aLine.at(pos-1)==' ') ||(aLine.at(pos-1)=='(')
|| (aLine.at(pos-1)==';')||(aLine.at(pos-1)==',')||(aLine.at(pos-1)=='\t')))
{
low=pos+variableNames[i][0].length();
high=low;
if (flag==false)
{
low=high;
while( aLine.at(low)==' ')
low++;
high=low;
if (aLine.at(low)!=',' &&aLine.at(low)!=')')
{
high=low+1;
while ( (isalnum(aLine.at(high))!=0 || aLine.at(high)=='_') ||aLine.at(high)=='['||aLine.at(high)==']'&& high<(aLine.length()-1))
high++;
variableNames[i][ ++variableNumber[i] ]=aLine.substr(low,high-low);
varlineNum[i][variableNumber[i]][0]=lineNum;
}
if(aLine.at(high)==')') flag=true;
}
else
{
while(aLine.at(high)!=';' && aLine.at(high)!='='&&flag)
{
low=high;
while (aLine.at(low)==' ' || aLine.at(low)==',')
low++;
high=low+1;
while ( (isalnum(aLine.at(high))!=0 || aLine.at(high)=='_') ||aLine.at(high)=='['||aLine.at(high)==']'&& high<aLine.length()-1)
high++;
int label=high;
while(aLine.at(label)==' ')
label++;
if(aLine.at(label)=='(')
{
functionNames[i][++functionNumber[i]]=aLine.substr(low,high-low);
funlineNum[i][functionNumber[i]]=lineNum;
flag=false;
}
else
{
variableNames[i][ ++variableNumber[i] ]=aLine.substr(low,high-low);
varlineNum[i][variableNumber[i]][0]=lineNum; //begining line
}
}
}
if( aLine.length()-high >= variableNames[i][0].length())
pos=aLine.find(variableNames[i][0],high);
else pos=string::npos;
}
}
//*************************************************************************************************
pos=aLine.find(functionNames[10][0]);
if((pos !=string::npos) && (pos==0 || (aLine.at(pos-1)==' ')))
{
low=pos+functionNames[10][0].length();
while(aLine.at(low)==' ')
low++;
if ( (aLine.find("(",low)!=string::npos) &&(aLine.find(" ",low)!=string::npos)&&(aLine.find("(",low)>aLine.find(" ",low)))
{
functionNames[10][ ++functionNumber[10] ]=aLine.substr(low,aLine.find(" ",low)-low);
funlineNum[10][functionNumber[10]]=lineNum;
}
else if(aLine.find("(",low)!=string::npos)
{
functionNames[10][ ++functionNumber[10] ]=aLine.substr(low,aLine.find("(",low)-low);
funlineNum[10][functionNumber[10]]=lineNum;
}
}
//*****************************************************************************************************
//分析变量是不是改变;
for( i=0;i<v1;i++)
if( variableNumber[i]>0 )
{
for( j=1;j<=variableNumber[i];j++)
{
pos=aLine.find(variableNames[i][j]);
if (pos!=string::npos)
if( pos==0 || ( isalnum(aLine.at(pos-1))==0))
{
lastpos=pos+variableNames[i][j].length();
if(pos>=0&&pos<2&&((aLine.at(lastpos)=='+'&&aLine.at(lastpos+1)=='=')||(aLine.at(lastpos)=='+'&&aLine.at(lastpos+1)=='+')
||(aLine.at(lastpos)=='-'&&aLine.at(lastpos+1)=='=')||(aLine.at(lastpos)=='-'&&aLine.at(lastpos+1)=='-')||(aLine.at(lastpos)=='='&&aLine.at(lastpos+1)!='=')))
varlineNum[i][j][++appearNumber[i][j]]=lineNum;
else if(pos>=2&&((aLine.substr(pos-2,2)=="++")||(aLine.substr(pos-2,2)=="--")||((aLine.at(lastpos)=='+'&&aLine.at(lastpos+1)=='=')||(aLine.at(lastpos)=='+'&&aLine.at(lastpos+1)=='+')
||(aLine.at(lastpos)=='-'&&aLine.at(lastpos+1)=='=')||(aLine.at(lastpos)=='-'&&aLine.at(lastpos+1)=='-')||(aLine.at(lastpos)=='='&&aLine.at(lastpos+1)!='='))))
varlineNum[i][j][++appearNumber[i][j]]=lineNum;
}
}
}
//*****************************************************************************************************
//find operator;
for (int op_num=0;op_num<operLength&&done;op_num++)
{
oper_pos=aLine.find(oper[op_num]);
int oper_temp;
oper_temp=aLine.length();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -