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

📄 match.c

📁 通配符匹配程序
💻 C
字号:
/*************************************************
本程序用于判断一个目标串是否与给定的含通配符串匹配
实例:
MATCH.IN
abcd?123*ab*qqw
4
abce123ab
abcda123dghfsdabgfaqqw
abcda123abqqw
1234
******************
MATCH.OUT
abcda123dghfsdabgfaqqw
abcda123abqqw
*************************************************/
#include <stdio.h>
#include <string.h>
#include <alloc.h>

int str_jud(char *source,char *object,int asterisk)
{
	char *sub_sour,*sub_obje;

	sub_sour=(char *)malloc(sizeof(char)*80);
	sub_obje=(char *)malloc(sizeof(char)*80);
	strcpy(sub_sour,source);
	strcpy(sub_obje,object);

	if(*sub_obje=='*')
	{
		asterisk=1;
	}
	if(((*sub_sour=='\0')&&(*sub_obje=='\0'))||((sub_obje[1]=='\0')&&(*sub_obje=='*')))
		return 1;
	else if((*sub_sour=='\0')&&(*sub_obje!='\0'))
		return 0;
	else if(asterisk==0)
	{
		if((*sub_obje==*sub_sour)||(*sub_obje=='?'))
		{
			sub_sour++;
			sub_obje++;
			return str_jud(sub_sour,sub_obje,asterisk);
		}
		else
			return 0;
	}
	else
	{

		if(*sub_obje=='*')	/* 类似于 IFNDEF*/
		{
			sub_obje++;
		}
		asterisk=0;

		if(str_jud(sub_sour,sub_obje,asterisk))
		{
			return 1;
		}
		else
		{
			asterisk=1;
			sub_sour++;
			return str_jud(sub_sour,sub_obje,asterisk);
		}
	}
}
main()
{
	int i,asterisk=0;	/*asterisk变量用于记录'*'是否出现*/
	FILE *fp, *pp;		/*fp为match.in的指针,pp为match.out*/
	short len;		/*len为待判断的字符串的个数*/
	char *str, obj[80];	/*str存储含通配符的串,obj存储待处理的串*/

	if((fp=fopen("match.in","r"))==NULL)	/*打开源文件*/
	{
		printf("can not open this file!");
		exit(0);
	}
	if((pp=fopen("match.out","w"))==NULL)	/*打开目标文件*/
	{
		printf("can not open this file!");
		exit(0);
	}

	fscanf(fp,"%s",obj);
	fscanf(fp,"%d",&len);
	for(i=0; i<len; i++)
	{
		str=(char *)malloc(sizeof(char)*80);
		fscanf(fp,"%s",str);
		if(str_jud(str,obj,asterisk))
		{
		  fputs(str,pp);
		  fputc('\n',pp);
		}
		free(str);
		asterisk=0;
	}
	fclose(fp);
	fclose(pp);
}

⌨️ 快捷键说明

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