⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scan.cpp

📁 可以自动识别一个程序中调用的所有函数
💻 CPP
字号:
/* ***********函数分析识别程序*******************/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include <stdlib.h>

#define MAX 40
#define LEN sizeof(struct function)
void main();
bool isdivided(char);
bool isOperation(char );
struct function{
			char *hanshu;
			struct function *next;
			};
//	struct function *head;
//	struct function *p;
	//	head=NULL;
	//	p=head;

char *key[]=
{
		"main()","auto()","break()","case()","char()","const()",
		"continue()","default()","do()","double()",	"else()","enum()",
		"extern()","float()","for()","goto()","if()","int()",
		"long()","register()","return()","short()","signed()","static()",
		"struct()","switch()","typedef()","union()","unsigned()","void()",
		"volatile()","while()","False()"
};    

const int limitLen=19;
const int dividLen=15;
int count=0;

char divid[dividLen]={
					'!','+','&','*','%','<','>',
					'=','|',',',';','/','}',' '
					};

char limit[limitLen]={
						'[',']','!','+','-','&','~',
						'*','%','<','>','=','|',
							',',';','{','}','#',')'
					};

void main()
{
	struct function *head;
	head=NULL;
	struct function *p;
	p=NULL;
	struct function *temp;
//	p=head;
	FILE *input;
	FILE *output;
	char ch;
	bool isreserv=0,isfuction=0;
	bool isoperation=0;
	char *str;
	str=(char *)malloc(40*sizeof(char));
	if((input=fopen("libin.c","r"))==NULL)//打开input文件失败返回
	{ 
		printf("file open in readonly mode,but an error generate!\n");
		exit(0);
	}
	
	if((output=fopen("output.txt","w"))==NULL)//打开output文件失败返回
	{
		printf("file open in new_create mode,but an error generate!\n");
		exit(0);
	}
	ch=fgetc(input);
	while(ch!=EOF)
	{
		while((ch==' '||ch=='\n'||ch=='\t'))
			ch=fgetc(input);
			//消除空格、回车、制表符
		if(ch=='/')
		{
			ch=fgetc(input);
			if(ch=='/')//消除//后一行的注释内容
			{
				ch=fgetc(input);
				while(ch!='\n')
					ch=fgetc(input);
				ch=fgetc(input);
			}
			
			else if(ch=='*')//消除/* */ 中的所有字符
			{
				count=0;
				ch=fgetc(input);
				while(count!=2)
				{
					count=0;
					while(ch!='*')
						ch=fgetc(input);
					count++;
					ch=fgetc(input);
					if(ch=='/')
						count++;
					else ch=fgetc(input);
				}
			}
			else //是除号
			{
				int m=0;
				str[m]=ch;
				ch=fgetc(input);
				if(ch=='(')
				{
					str[++m]=ch;
					str[++m]=')';
					/*while(ch!=')')
					{
						ch=fgetc(input);
					}*/
				}
				else
				while(ch!='\n'&&ch!=')'&&!isdivided(ch))
				{
					str[++m]=ch;
					ch=fgetc(input);
					if(ch=='(')
					{
						str[++m]=ch;
						str[++m]=')';
					/*	while(ch!=')')
						{
							ch=fgetc(input);
							
						}
						*/
						ch=fgetc(input);
						break;
					}
				}
				str[++m]='\0';
				int k=0;
				while(k<33)
				{
					if(!strcmp(str,key[k++]))
						break;
				}
				if(k<33)
				{
					for(int i=0;i<m;i++)
						str[i]='\0';
				}
				else
				{
					int i=0;
					bool ishanshu=false;
					if(isalpha(str[i]))
					{
						for(int i=0;i<m;i++)
						{
							if(str[i]=='(')
								ishanshu=true;
						}
					}
					if(ishanshu)
					{
						//	head=NULL;
						p=(struct function *)malloc(LEN);
						int slen=strlen(str);
						p->hanshu=(char *)malloc(slen*sizeof(char)+1);
						for(int j=0;j<slen;j++)
							*((p->hanshu)+j)=str[j];
						*((p->hanshu)+j)='\0';
						p->next=NULL;
						temp=head;
						bool repeat=false;
						if(head==NULL)
						{
							head=p;
							fprintf(output,"%s\n",str);
							printf("%s\n",str);
						}
						else 
						{							
							while (temp!=NULL) 
							{
								if(!strcmp(temp->hanshu,str))
									break;
								temp=temp->next;
							}
							if(temp==NULL)
							{	
								p->next=head;
								head=p;
								fprintf(output,"%s\n",str);
								printf("%s\n",str);
							}
						}
						for(i=0;i<m;i++)
							str[i]='\0';
					}
				}

			}
			ch=fgetc(input);
		}
		else if(ch=='\"')
		{
			ch=fgetc(input);
			while(ch!='\"')
				ch=fgetc(input);
			ch=fgetc(input);
		}
		else if(ch=='\'')
		{
			ch=fgetc(input);
			while(ch!='\'')
				ch=fgetc(input);
			ch=fgetc(input);
		}
		else if(isOperation(ch)==true)	//是运算符
		{
			while(isOperation(ch)==true)
				ch=fgetc(input);
		}
		else
		{
			int m=0;
//			char *str;
//			str=(char *)malloc(40*sizeof(char));
			str[m]=ch;
			ch=fgetc(input);
			while((ch!='\n')&&(ch!=')')&&!isdivided(ch))
			{
				str[++m]=ch;
				ch=fgetc(input);
				if(ch=='(')
				{
					str[++m]=ch;
					str[++m]=')';
					/*while(ch!=')')
					{
						ch=fgetc(input);
					}*/
					ch=fgetc(input);
					break;
				
				}
			}
			str[++m]='\0';
			int k=0;
			while(k<33)
			{
				if(!strcmp(str,key[k++]))
					break;
			}
			if(k<33)
			{
				for(int i=0;i<m;i++)
					str[i]='\0';
			}
			else
			{
				int i=0;
				bool ishanshu=false;
				if(isalpha(str[i]))
				{
					for(int i=0;i<m;i++)
					{
						if(str[i]=='(')
							ishanshu=true;
					}
				}
			
				if(ishanshu)
				{
					//	head=NULL;
					p=(struct function *)malloc(LEN);
					int slen=strlen(str);
					p->hanshu=(char *)malloc(slen*sizeof(char)+1);
					for(int j=0;j<slen;j++)
						*((p->hanshu)+j)=str[j];
					*((p->hanshu)+j)='\0';
					p->next=NULL;
					temp=head;
					bool repeat=false;
					if(head==NULL)
					{
						head=p;
						fprintf(output,"%s\n",str);
						printf("%s\n",str);
					}
					else 
					{							
						while (temp!=NULL) 
						{
							if(!strcmp(temp->hanshu,str))
								break;
							temp=temp->next;
						}
						if(temp==NULL)
						{	
							p->next=head;
							head=p;
							fprintf(output,"%s\n",str);
							printf("%s\n",str);
						}
					}
					for(i=0;i<m;i++)
						str[i]='\0';
				}
				for(i=0;i<m;i++)
					str[i]='\0';
			}
			
		}
	}

}


bool isOperation(char ch)
{
	
	int i=0;
	while((i<19)&&(ch!=limit[i]))
		i++;
	
	if(i==19) return false;
	else return true;
	
	
}
bool isdivided(char ch)
{
	
	int i=0;
	while((i<15&&(ch!=divid[i])))
		i++;
	
	if(i==15) return false;
	else return true;
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -