Esempio n. 1
0
int main( void )
{
	char s[ maxstr ]; // String
	char ss[ maxstr ]; // String

	strassign( "Hello\0", s );
	strassign( "Goodbye\0", ss );

	printf( "Original:\n\ts = [%s]\n\tss = [%s]\n", s, ss );
	reverse( s ); reverse( ss );
	printf( "Mutation:\n\ts = [%s]\n\tss = [%s]\n", s, ss );

	return 0;
}
Esempio n. 2
0
int main()
{
	string s, t;
	char str1[INITSIZE], str2[INITSIZE];
	int pos, i;

	initstring(&s);
	initstring(&t);

	printf("请输入要查找的目标串 S:");
	gets(str1);									//gets()用于获取一行字符串,中间可以有空格,以回车结束
	strassign(&s, str1);

	printf("请输入要查找的模式串 T:");
	gets(str2);
	strassign(&t, str2);

	printf("请输入模式匹配的起始位序:");
	scanf("%d", &pos);

	i = BF_index(&s, &t, pos);
	if (i != 0)
		printf("1.Brute-Force算法匹配结果:\n\t模式串T在目标串S中的位序为:%d\n\n", i);
	else
		printf("1.Brute-Force算法匹配结果:\n\t模式匹配失败!\n\n");

	i = KMP_index(&s, &t, pos);
	if (i != 0)
		printf("2.KMP算法匹配结果:\n\t模式串T在目标串S中的位序为:%d\n\n", i);
	else
		printf("2.KMP算法匹配结果:\n\t模式匹配失败!\n\n");

	i = KMP_index2(&s, &t, pos);
	if (i != 0)
		printf("3.KMP算法匹配结果(使用next函数的修正值):\n\t模式串T在目标串S中的位序为:%d\n", i);
	else
		printf("3.KMP算法匹配结果(使用next函数的修正值):\n\t模式匹配失败!\n");
	return 0;
}
Esempio n. 3
0
/*
 * Initialise awk ARGC, ARGV variables.
 */
static void
awkarginit(int ac, char **av)
{
	int i;
	wchar_t *cp;

	ARGVsubi = node(INDEX, vlook(s_ARGV), constant);
	running = 1;
	constant->n_int = ac;
	(void) assign(varARGC, constant);
	for (i = 0; i < ac; ++i) {
		cp = mbstowcsdup(av[i]);
		constant->n_int = i;
		strassign(exprreduce(ARGVsubi), cp,
		    FSTATIC|FSENSE, wcslen(cp));
	}
	running = 0;
}
Esempio n. 4
0
/*
 * mainline for awk
 */
int
main(int argc, char *argv[])
{
	wchar_t *ap;
	char *cmd;

	cmd = argv[0];
	_cmdname = cmd;

	linebuf = emalloc(NLINE * sizeof (wchar_t));

	/*
	 * At this point only messaging should be internationalized.
	 * numbers are still scanned as in the Posix locale.
	 */
	(void) setlocale(LC_ALL, "");
	(void) setlocale(LC_NUMERIC, "C");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	awkvarinit();
	/* running = 1; */
	while (argc > 1 && *argv[1] == '-') {
		void *save_ptr = NULL;
		ap = mbstowcsdup(&argv[1][1]);
		if (ap == NULL)
			break;
		if (*ap == '\0') {
			free(ap);
			break;
		}
		save_ptr = (void *) ap;
		++argv;
		--argc;
		if (*ap == '-' && ap[1] == '\0')
			break;
		for (; *ap != '\0'; ++ap) {
			switch (*ap) {
#ifdef DEBUG
			case 'd':
				dflag = 1;
				continue;

#endif
			case 'f':
				if (argc < 2) {
					(void) fprintf(stderr,
				gettext("Missing script file\n"));
					return (1);
				}
				*progfilep++ = argv[1];
				--argc;
				++argv;
				continue;

			case 'F':
				if (ap[1] == '\0') {
					if (argc < 2) {
						(void) fprintf(stderr,
				gettext("Missing field separator\n"));
						return (1);
					}
					ap = mbstowcsdup(argv[1]);
					--argc;
					++argv;
				} else
					++ap;
				strescape(ap);
				strassign(varFS, linebuf, FALLOC,
				    wcslen(linebuf));
				break;

			case 'v': {
				wchar_t *vp;
				wchar_t *arg;

				if (argc < 2) {
					(void) fprintf(stderr,
		gettext("Missing variable assignment\n"));
					return (1);
				}
				arg = mbconvert(argv[1]);
				/*
				 * Ensure the variable expression
				 * is valid (correct form).
				 */
				if (((vp = wcschr(arg, '=')) != NULL) &&
				    isclvar(arg)) {
					*vp = '\0';
					strescape(vp+1);
					strassign(vlook(arg), linebuf,
					    FALLOC|FSENSE,
					    wcslen(linebuf));
					*vp = '=';
				} else {
					(void) fprintf(stderr, gettext(
					    "Invalid form for variable "
					    "assignment: %S\n"), arg);
					return (1);
				}
				--argc;
				++argv;
				continue;
			}

			default:
				(void) fprintf(stderr,
				gettext("Unknown option \"-%S\"\n"), ap);
				return (usage());
			}
			break;
		}
		if (save_ptr)
			free(save_ptr);
	}
	if (progfilep == &progfiles[0]) {
		if (argc < 2)
			return (usage());
		filename = "[command line]";	/* BUG: NEEDS TRANSLATION */
		progptr = mbstowcsdup(argv[1]);
		proglen = wcslen(progptr);
		--argc;
		++argv;
	}

	argv[0] = cmd;

	awkarginit(argc, argv);

	/* running = 0; */
	(void) yyparse();

	lineno = 0;
	/*
	 * Ok, done parsing, so now activate the rest of the nls stuff, set
	 * the radix character.
	 */
	(void) setlocale(LC_ALL, "");
	radixpoint = *localeconv()->decimal_point;
	awk();
	/* NOTREACHED */
	return (0);
}
Esempio n. 5
0
/*
 * Do initial setup of buffers, etc.
 * This must be called before most processing
 * and especially before lexical analysis.
 * Variables initialised here will be overruled by command
 * line parameter initialisation.
 */
static void
awkvarinit()
{
	NODE *np;

	(void) setvbuf(stderr, NULL, _IONBF, 0);

	if ((NIOSTREAM = sysconf(_SC_OPEN_MAX) - 4) <= 0) {
		(void) fprintf(stderr,
	gettext("not enough available file descriptors"));
		exit(1);
	}
	ofiles = (OFILE *)emalloc(sizeof (OFILE)*NIOSTREAM);
#ifdef A_ZERO_POINTERS
	(void) memset((wchar_t *)ofiles, 0, sizeof (OFILE) * NIOSTREAM);
#else
	{
		/* initialize file descriptor table */
		OFILE *fp;
		for (fp = ofiles; fp < &ofiles[NIOSTREAM]; fp += 1) {
			fp->f_fp = FNULL;
					fp->f_mode = 0;
					fp->f_name = (char *)0;
		}
	}
#endif
	constant = intnode((INT)0);

	const0 = intnode((INT)0);
	const1 = intnode((INT)1);
	constundef = emptynode(CONSTANT, 0);
	constundef->n_flags = FSTRING|FVINT;
	constundef->n_string = _null;
	constundef->n_strlen = 0;
	inc_oper = emptynode(ADD, 0);
	inc_oper->n_right = const1;
	asn_oper = emptynode(ADD, 0);
	field0 = node(FIELD, const0, NNULL);

	{
		RESFUNC near*rp;

		for (rp = &resfuncs[0]; rp->rf_name != (LOCCHARP)NULL; ++rp) {
			np = finstall(rp->rf_name, rp->rf_func, rp->rf_type);
		}
	}
	{
		RESERVED near*rp;

		for (rp = &reserved[0]; rp->r_name != (LOCCHARP)NULL; ++rp) {
			switch (rp->r_type) {
			case SVAR:
			case VAR:
				running = 1;
				np = vlook(rp->r_name);
				if (rp->r_type == SVAR)
					np->n_flags |= FSPECIAL;
				if (rp->r_svalue != NULL)
					strassign(np, rp->r_svalue, FSTATIC,
					    (size_t)rp->r_ivalue);
				else {
					constant->n_int = rp->r_ivalue;
					(void) assign(np, constant);
				}
				running = 0;
				break;

			case KEYWORD:
				kinstall(rp->r_name, (int)rp->r_ivalue);
				break;
			}
		}
	}

	varNR = vlook(s_NR);
	varFNR = vlook(s_FNR);
	varNF = vlook(s_NF);
	varOFMT = vlook(s_OFMT);
	varCONVFMT = vlook(s_CONVFMT);
	varOFS = vlook(s_OFS);
	varORS = vlook(s_ORS);
	varRS = vlook(s_RS);
	varFS = vlook(s_FS);
	varARGC = vlook(s_ARGC);
	varSUBSEP = vlook(s_SUBSEP);
	varENVIRON = vlook(s_ENVIRON);
	varFILENAME = vlook(s_FILENAME);
	varSYMTAB = vlook(s_SYMTAB);
	incNR = node(ASG, varNR, node(ADD, varNR, const1));
	incFNR = node(ASG, varFNR, node(ADD, varFNR, const1));
	clrFNR = node(ASG, varFNR, const0);
}
Esempio n. 6
0
  main( )
  {
    char inp;
    char *T[255];
    char *S[255];
    struct sstring *s1,*s2,*res;
    int pos,len;
    printf("1-------strassing\n");
    printf("2-------strlength\n");
    printf("3-------strcompare\n");
    printf("4-------clearstring\n");
    printf("5-------concat\n");
    printf("6-------substring\n");
    printf("*-------exit!");
    printf("please in put1--6 or *\n\n")
 while(1){
      scanf("%c",&inp);
      switch(inp){
	      case 1:{
		    scanf("%s",&S);
		    res->ch=strassign(s1,S);
		    if(res->ch==0)
			printf("the string is:%s",s1->ch);
		    else
		       printf("error"); }
	      case 2: {
		    scanf("%s",&S);
		    s1->length=strlen(S);
		    strcpy(s1->ch,S);
		    res->ch=strlength(S);
		    printf("the string is:%d\n",s1->length);}
	      case 3:{
		    scanf("%s",&S);
		    scanf("%s",&T);
		    s1->length=strlen(S);
		    strcpy(s1->ch,S);
		    s2->length=strlen(T);
		    strcpy(s2->ch,T);
		    res->ch=strcompare(S,T);
		    switch(res){
		       case 0:
			    printf("two strings are equle");
		       case 1:
			    printf("the first string > the second string");
		       case -1:
			    printf("the first string < the second string");}
                       }
	     case 4:{
		    res->ch=clearstring(s1);
		    res->ch=clearstring(s2);
		    printf("the string is NULL");}
	     case 5:{
		    scanf("%s",&S);
		    scanf("%s",&T);
		    strcat(&s1,S,T);
		    if(res==0)
		      printf("the string is:%s",s1->ch);
		    }
	     case 6: {
		    scanf("%s_%d_%d",S,&pos,&len);
		    s2->length=strlen(S);
		    strcpy(s2->ch,S);
		    res->ch=substring(s1,s2,pos,len);
		    if(res==0)
			printf("the string is:%s",s1->ch);
		    else printf("error");}
	     case *:
		    exit;
       }
      }
    }