📄 scaner.cpp
字号:
//*******************************************************************************************
// scaner.cpp : Defines the entry point for the console application.
// 扫描C++源程序,给出其中的常量、变量和函数列表,包括出现位置(行号)、定义性/使用性出现、
//类型(函数给出行参类型和返回值类型),最终判断是否满足"先定义后使用"原则。
//written by 束文辉(031251096).
//*******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void printhead();
void openfile(ifstream&);
void findfunction(string);
string sample;
void finddata(string);
bool statement=false;
string filename;
//********************************************************************************************
int main(int argc, char* argv[])
{ printhead();
cout<<"Please input file name:"<<endl;
cin>>filename;
cout<<"const and variable:"<<endl;
finddata("int");
finddata("float");
finddata("long");
finddata("double");
finddata("short");
finddata("string");
finddata("long double");
finddata("char");
finddata("bool");
cout<<endl<<endl<<"function:"<<endl;
findfunction("void");
findfunction("int");
findfunction("char");
findfunction("float");
findfunction("long");
findfunction("double");
findfunction("short");
findfunction("long double");
findfunction("bool");
return 0;
}
//*********************************************************************************************
void printhead()
{ cout<<"********************programme for scanning the file***********************"<<endl;
cout<<"***********************welcome to scan programme**************************"<<endl;
}
//*********************************************************************************************
void openfile(/*inout*/ifstream& somefile)
{
somefile.open(filename.c_str());
if(!somefile)
cout<<"can not open "<<filename<<endl;
}
//*********************************************************************************************
void finddata(string type)
{ int len;
int len2;
int count(1);
ifstream file;
openfile(file);
string sub;
string sub2;
string sub3;
int ifcon(0);
bool ifconst=true;
bool ifannounce=false;
bool array=false;
int ads(0);
int count2=0;
int count3=1;
int ads2;
len2=type.length();
getline(file,sample);
while(file)
{
len=sample.length();
count++;
for(int i=0;i<len-len2;i++)
{
sub=sample.substr(i,len2);
sub3=sample.substr(i+len2,1);
if(sub==type&&sub3==" ")
{ads=i;
ads2=i;
if(i>5)
ifcon=i-6;
for(int j=0;j<ifcon;j++)//judge if is const
{ sub2=sample.substr(ifcon,5);
if(sub2=="const")
ifconst=true;
}
for(;ads<len;ads++)
{ if(sample[ads]==';')
{ if(sample[ads-1]==')')
ifannounce=true;
}
}
if(sample[ads-1]==';')
statement=true;
if(sample[ads-2]==']')
array=true;
if(ifannounce)
{for(int k=0;k<ads;k++)
{if(sample[k]==',')
count2++;//how many paramenter there is in a funtion
}
}
if(statement)
{if(!ifannounce)
{ for(int l=0;l<ads;l++)
{if(sample[l]==',')
count3++;//see a type has how many paramenter
}
if(ifconst)
{ if(array)
{ cout<<"const "<<type<<" array"<<sample.substr(i+len2,ads-1)<<endl;
if(count3==1)
cout<<" There "<<"is "<<count3<<" "<<type<< " array in line:"<<count<<endl;
else
cout<<" There "<<"are "<<count3<<" "<<type<<"s"<< " arrays in line:"<<count<<endl;
}
else
{
cout<<"const "<<type<<" "<<sample.substr(i+len2,ads-1)<<endl;
if(count3==1)
cout<<" There "<<"is "<<count3<<" const "<<type<< " in line:"<<count<<endl;
else
cout<<" There "<<"are "<<count3<<" const "<<type<<"s"<< " in line:"<<count<<endl;
}
}
else
{ if(array)
{
cout<<type<<" array"<<sample.substr(i+len2,ads-3)<<endl;
if(count3==1)
cout<<" There "<<"is "<<count3<<" "<<type<< " array in line:"<<count<<endl;
else
cout<<" There "<<"are "<<count3<<" "<<type<<"s"<< " arrays in line:"<<count<<endl;
}
else
{
cout<<type<<" "<<sample.substr(i+len2,ads-3)<<endl;
if(count3==1)
cout<<" There "<<"is "<<count3<<" "<<type<< " in line:"<<count<<endl;
else
cout<<" There "<<"are "<<count3<<" "<<type<<"s"<< " in line:"<<count<<endl;
}
}
}
}
}
ifannounce=false;
ifconst=false;
statement=false;
ifcon=0;
ads=0;
count2=0;
count3=1;
}
getline(file,sample);
}
file.close();
}
//*********************************************************************************************
void findfunction(string type)
{ int leng;
int leng2;
int leng3;
int countf(1);
bool ifannounce=false;
bool iffind=false;
bool def=false;
int ads=0;
int ads2=0;
string sub3;
ifstream file2;
string sub;
string sub2;
string sub4;
string sub5;
string sub6;
file2.open(filename.c_str());
if(!file2)
cout<<"can not open "<<filename<<endl;;
getline(file2,sample);
while(file2)
{ leng=sample.length();
countf++;
leng2=type.length();
for(int i=0;i<leng-leng2;i++)
{
sub=sample.substr(i,leng2);
sub6=sample.substr(i+leng2,1);
if(sub==type&&sub6==" ")
{ ads=i;
if(sample[leng-1]==')')
iffind=true;
if(iffind)//find the function
{ for(int j=0;j<leng;j++)//find the paramenter
{if(sample[j]=='(')
ads2=j;
}
sub2=sample.substr(ads2+1,leng-2-ads2);
for(int k=0;k<leng-ads2-2;k++)
{if(sub2[k]==';')
iffind=false;
}
}
if(iffind)
{ sub4=sample.substr(leng2+1,ads2-leng2-1);
for(int l=1;l<countf-1;l++)//to see if define later
{ ifstream file3;
file3.open(filename.c_str());
if(!file3)
cout<<"can not"<<endl;
getline(file3,sub3);
leng3=sub3.length();
for(int m=0;m<leng3-ads2+leng2+1;m++)
{ sub5=sub3.substr(m,ads2-leng2-1);
if(sub4==sub5)
def=true;
}
}
}
if(ads>=ads2-3)
iffind=false;
if(iffind)
{cout<<type<<sample.substr(leng2,ads2-leng2)<<" in line "<<countf<<endl;
cout<<" its return type: "<<type<<endl;
cout<<" its paramenters : ";
if(sub2=="")
cout<<"none"<<endl;
else
cout<<sub2<<endl;
if(!def)
cout<<" it is defined first and used later..."<<endl;
else
cout<<" it is not defined first and used later..."<<endl;
}
}
ifannounce=false;
iffind=false;
def=false;
ads=0;
ads2=0;
}
getline(file2,sample);
}
file2.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -