📄 pentstub.c
字号:
fprintf(f," "); }}int disspace(char ch)/*;*/ /*funcdef - dgpc - disspace*/{if ((ch == '\t') || (ch == '\n') || (ch == ' ')) return(1);else return(0);}int disnonlabel(char ch)/*;*/ /*funcdef - dgpc - disspace*/{if ((ch == '\t') || (ch == '\n') || (ch == ' ') || (ch == ')') ) return(1);else return(0);}/*-----------------------------------------------------------------------------*/void GetNextToken(char *buf, FILE *f)/*;*/ /*funcdef - dgpc - GetNextToken*/{ int ibuf; int i = 0; while ((ibuf = getc(f)) != EOF) {/* printf("ibuf is %c\n",(char) ibuf);*/ if (disspace((char) ibuf)) { buf[i] = (char) 0; ungetc(' ',f); break; } else if (((char) ibuf) == ')') {/* printf("putting the ) back!\n");*/ ungetc(')', f); buf[i] = (char) 0; break; } else { buf[i++] = (char) ibuf; } } if (ibuf == (int)EOF) { printf("End of file too early\n"); exit(1); } buf[i]=(char)0;/* printf("%d items read in\n",i); printf("buf[%d] = %d\n",i,(int) (buf[i]));*/}void GotoEndBracket(FILE *f){int ibuf;char sbuf[5];int done=0;#if (DEBUG_READ_DUDE)printf("In GotoEndBracket!\n");#endif while (1) /*(ibuf = getc(f)) != EOF) */ { /*fscanf(f,"%1s",sbuf);*/ ibuf = getc(f);#if (DEBUG_READ_DUDE) printf("in goto end:ibuf is %c, %d\n",(char) ibuf,ibuf); printf("EOF is %d, and matches? %d\n",(int)EOF,EOF == ibuf);#endif /*fflush(stdout);*/ if ((char) ibuf == ')') { done=1; break; } }if (!done){ fprintf(stderr,"Error, no ) found\n"); exit(1);}}char GetToNonSpace(FILE * f){char tc; while ((tc =(char) getc(f))!= EOF) { if (!disspace(tc)) break; } return(tc);}/*-----------------------------------------------------------------------------*/int ReadBranch(Branch *br, FILE *f, int index)/*;*/ /*funcdef - dgpc - ReadBranch*//* a 0 return is bogus, otherwise, returns the index after the last thing read in. Right now, this function does only limited error checking -- it assumes the dudes are fed to it correctly.*/{ int i; int ibuf,endp,done; char buf[300]; int passindex; GTYPE temp; int constant_num;#if (DEBUG_READ_DUDE) printf("In ReadBranch\n");#endif/* if (fscanf(f,"%1s",buf) == EOF) return (1); */ buf[0] = GetToNonSpace(f);#if (DEBUG_READ_DUDE) printf("buf is %c\n",buf[0]);#endif if (buf[0] == '(') {/* fscanf(f,"%1s", buf); ungetc(buf[0],f);*/ GetNextToken(buf, f);#if (DEBUG_READ_DUDE) printf("token was %s \n",buf); #endif br->tree[index].opcode = LookupFunctionName(buf); passindex = index+1; #if (DEBUG_READ_DUDE) printf(" a!%d ",_function_arity(br->tree[index].opcode)); #endif for (i=0; i < _function_arity(br->tree[index].opcode); i++) { passindex = ReadBranch(br,f, passindex); if (passindex ==0) return(0); } endp=0;done=0; GotoEndBracket(f); } else { ungetc(buf[0], f); GetNextToken(buf, f);#if (DEBUG_READ_DUDE) printf("token was %s \n",buf); #endif sscanf(buf, GFORMAT, &temp); br->tree[index].opcode = FindConstantInTables(temp, &constant_num); if (br->tree[index].opcode == -1) { fprintf(stderr,"Error, constant %s not found in tables\n",buf); fprintf(gpop.out_file,"Error, constant %s not found in tables\n",buf); exit(1); }/* br->tree[index+1].opcode = constant_num; */ passindex = index+1; }return(passindex);}int FindConstantInTables(GTYPE temp, int *constant_num)/*;*/ /*funcdef - dgpc - FindConstantInTable*/{/*Returns the number of the constant array that the constant was found in,and places the index in constant_num. It returns -1 on failure*/ int i; int j; temp=temp; for (j=TOTAL_NUMBER_OF_FUNCTIONS-1;j<gpop.num_constants;j++) { if (gpop.pop_startup_info.random_constant_table[j] == temp) { *constant_num = j; return(j); } } *constant_num = gpop.primed_constants;#if (DEBUG_READ_DUDE) printf("temp in fcit %lf \n",temp);#endif gpop.pop_startup_info.random_constant_table[gpop.primed_constants++] =temp; if (gpop.primed_constants > gpop.num_constants) printf("TERRIBLE Error!!! -- too many constants in read individual\n");#if (DEBUG_READ_DUDE) printf("NumConstants Read in %d\n",gpop.primed_constants);#endif gpop.pop_startup_info.random_constant_table[*constant_num] = temp; return(*constant_num);}/*-----------------------------------------------------------------------------*/int PrintTree(Branch * br, int index, int depth, FILE *f, int *counter)/*;*/ /*funcdef - dgpc - PrintTree*/{ int passkey; int i; #if (SQUISH_PRINT) if ((*counter) % 10 == 0) fprintf(f,"\n"); #else blanks(f,depth); #endif if ((_function_is_constant(br->tree[index].opcode))) { fprintf(f," %f " ,(float) gpop.pop_startup_info.random_constant_table[br->tree[index].opcode]); passkey = index+1; } else { fprintf(f,"(%s ",_function_printname(br->tree[index].opcode) ); if (_function_arity(br->tree[index].opcode) == 0) { fprintf(f,")"); passkey = index+1; } else { #if (!(SQUISH_PRINT)) fprintf(f,"\n"); #endif passkey = index+1; for (i=0; i<_function_arity(br->tree[index].opcode); i++) { (*counter)++; passkey = PrintTree(br, passkey, depth+1, f, counter); #if (SQUISH_PRINT)/* if ((*counter) % 10 == 0) fprintf(f,"\n");*/ #endif if (i == (_function_arity(br->tree[index].opcode))-1) fprintf(f,")"); #if (!(SQUISH_PRINT)) else fprintf(f,"\n"); #endif } } }#if (SQUISH_PRINT) if (!depth) fprintf(f,"\n");#endif fflush(f); return(passkey);}void WriteBranch(Branch * br, FILE *f)/*;*/ /*funcdef - dgpc - WriteBranch*/{int i=0;PrintTree(br, 0, 0, f,&i);fprintf(f,"\n");fflush(f);}void PrintFunctionTable(FILE * ofp,Population * pop)/*;*/ /*funcdef - dgpc -Pr intFunctionTable*/{int i,j; fprintf(ofp,"\n**************Function Table Information****************\n"); fprintf(ofp,"\n There are %d functions in the function table\n", (*pop).num_general_functions); fprintf(ofp," Of these, %d are ADFs, %d are dummy-variables, and %d are general\n", MAX_NUM_ADFS, MAX_NUM_ADF_ARGS, (((*pop).num_general_functions)- (MAX_NUM_ADFS+MAX_NUM_ADF_ARGS))); for (i=0;i<MAX_NUM_ADFS;i++) { fprintf(ofp,"The number %d function is an ADF named %s\n",i,(*pop).func_table[i].print_name); } for (i=MAX_NUM_ADFS;i< MAX_NUM_ADFS+MAX_NUM_ADF_ARGS;i++) { fprintf(ofp,"The number %d function is a dummy var named %s\n",i,(*pop).func_table[i].print_name); } for (i=MAX_NUM_ADFS+MAX_NUM_ADF_ARGS;i< (*pop).num_general_functions;i++) { fprintf(ofp,"The number %d function is %s", i,(*pop).func_table[i].print_name); fprintf(ofp,", has arity %d",(*pop).func_table[i].arity); if ((*pop).func_table[i].macro) fprintf(ofp," and is a macro,"); if ((*pop).func_table[i].constant) fprintf(ofp," and is a constant,"); fprintf(ofp,"\n"); } if (!((*pop).func_table[(*pop).num_general_functions-1].constant)) { fprintf(ofp, "Error, The last function in the func table should be a random constant!\n"); fprintf(stderr, "Error, The last function in the func table should be a random constant!\n"); } fprintf(ofp,"\nThe initial Population starts with %d ADFs\n",NUM_INITIAL_ADFS); fprintf(ofp,"\n****************Result Producing Branches*************\n"); for (i=0;i<NUM_RPBS;i++) { fprintf(ofp,"RPB Number %d uses the following functions: \n",i); for (j=0;j<(*pop).num_general_functions;j++) { if (((*pop).function_assignment)[i][j] != -1) { fprintf(ofp,"%s with arity %d\n",(*pop).func_table[j].print_name, (*pop).function_assignment[i][j]); } } fprintf(ofp,"\n\n"); } fprintf(ofp,"****************Function Defining Branches (ADFs)*************\n"); for (i=0;i<MAX_NUM_ADFS;i++) { fprintf(ofp,"ADF Number %d uses the following functions: \n",i); for (j=0;j<(*pop).num_general_functions;j++) { if (((*pop).function_assignment)[i+NUM_RPBS][j] != -1) { fprintf(ofp,"%s with arity %d\n",(*pop).func_table[j].print_name, (*pop).function_assignment[i+NUM_RPBS][j]); } } fprintf(ofp,"\n\n"); } fprintf(ofp,"**************End of Function Table Report*****************\n");}/*----------------------------------------------------------------*/int LookupFunctionName(char *buf)/*;*/ /*funcdef - dgpc - CopySubtree*/{ int i; for (i=0; i<gpop.num_general_functions-1; i++) { if (!strcmp(buf, gpop.func_table[i].print_name)) { return(i); } } fprintf(stderr,"%s not found in function_table\n",buf); fprintf(gpop.out_file,"%s not found in function_table\n",buf); exit(1); return(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -