void crossOverThree() { //Go through all the parents for(int y = 0; y < pathToGen - 1; y++) { //Set the field to all full setChosenToFalse(); //The first tile is always the starting tile tmpChild = insertNum(tmpChild, 0, 25); //Go through the length of the path for(int x = 1; x < pathDepth; x++) { tmpChild = insertNum(tmpChild, x, findUnusedRandomNumber(y, x); } //Find the distance of the new path int tmpDist = 0; for(int x = 0; x < pathDepth-1; x++) tmpDist += findDistance(extractNum(tmpChild, x), extractNum(tmpChild, x+1)); //Go through the current paths and see if this one is better for(int x = 0; x < pathToGen; x++) { //Did we find a match? if(parents[x].fitness > tmpDist) { //If this is the best match, update the best map if(x==0) { bestChosen = chosen; } //Replace the current data with the new path data parents[x].fitness = tmpDist; parents[x].path = tmpChild; } break; } } }
int main(int argc, char* argv[]) { //read in file char str[100]; FILE *fp; fp=fopen(argv[1],"r"); if(fp==NULL) { fprintf(stderr,"Can't open input file!"); exit(1); } //LEXICAL ANALYSIS int numLines = 0; while(fgets(str,MAX_LINE_LEN,fp)!=NULL) { char* curPos = str; while(*curPos != '\n') { //CASE 1: NUM if(isdigit(*curPos)) { tokens[numTokens].type = 0; tokens[numTokens].lineNum = numLines; tokens[numTokens].lexeme = (char*)malloc(100); extractNum(curPos, tokens[numTokens].lexeme); numTokens++; continue; } //CASE 2: KEYWORD / IDENTIFIER / operator AND-OR if(isalpha(*curPos)) { //get alphanum token char* token = (char*)malloc(100); memset(token,'\0',100); getToken(curPos,token); //keyword case if(searchKeyword(token) != -1) tokens[numTokens].type = 1; else if(searchOperators(token) != -1) tokens[numTokens].type = 3; //identifier case else tokens[numTokens].type = 2; //add line num tokens[numTokens].lineNum = numLines; //add lexeme tokens[numTokens].lexeme = (char*)malloc(100); memcpy(tokens[numTokens].lexeme,token,strlen(token)); numTokens++; continue; } //CASE : WHITESPACE if(*curPos == ' ' || *curPos == '\t') curPos++; } // char* combined = (char*)malloc(100); // memcpy(combined,punctuation,strlen(punctuation)); // strcat(combined,symbols); // char* test = strtok(str,combined); // while(test != NULL) // { // std::cout<<test<<std::endl; // test = strtok(NULL,combined); // } // continue; // std::cout<<"here\n\n\n"<<std::endl; //Examine line char by char /* char* curPos = str; char* token = (char*)malloc(100);//NULL; memset(token,'\0',100); getToken(curPos,token); while(token != NULL) { //CASE 1: NUM CASE if(isdigit(*token)) { tokens[numTokens].type = 1; tokens[numTokens].lineNum = numLines; tokens[numTokens].lexeme = (char*)malloc(100); extractNum(token, tokens[numTokens].lexeme); numTokens++; } //CASE 2: KEYWORD CASE getToken(curPos,token); } */ /* // int str_i = 0; while(str_i < strlen(str)) { char c = str[str_i]; //CASE 1: NUMBER if(isdigit(c)) { //populate next element of tokens array tokens[numTokens].type = 1; tokens[numTokens].lineNum = numLines; //iterate through str until non-digit char encountered. //also iterate through lexeme to populate int lex_i = 0; while(isdigit(c) && str_i < strlen(str)) { tokens[numTokens].lexeme[lex_i] = c; lex_i++; str_i++; c = str[str_i]; } } //CASE 2: CHAR - always encapsulated by ' ' if(c == '\'') { str_i++; c = str[str_i]; } //CASE 3: } */ numLines++; } fclose(fp); printTokens(); fflush(stdout); //END FILE READ }
void next() { //LEXICAL ANALYSIS // std::cout << "in next" << std::endl; type = -1; val = (char*)malloc(MAX_LINE_LEN); // char c = fgetc(fp); // std::cout << c << std::endl; while(c!=EOF) { if(c == '\n') { numLines++; c = fgetc(fp); continue; } //CASE : WHITESPACE if(c == ' ' || c == '\t') { c = fgetc(fp); continue; } //CASE 1: NUM if(isdigit(c)) { extractNum(c,val); type = 0; } //CASE 2: KEYWORD / IDENTIFIER / operator AND-OR else if(isalpha(c)) { //get alphanum token getToken(c,val); if(searchArray(keywords,val,NUM_KEYWORDS) != -1) type = 1; else if(searchArray(boolSymbols,val,NUM_BOOLSYMBOLS) != -1) type = 12; else //identifier type = 2; } else if(searchArray(mathSymbols,c,NUM_MATHSYMBOLS) != -1) { type = 6; val[0] = c; c = fgetc(fp); } else if(searchArray(relationalSymbols,c,NUM_RELATIONALSYMBOLS) != -1) { type = 7; val[0] = c; // std::cout << val << std::endl; c = fgetc(fp); } else if(searchArray(equalSymbols,c,NUM_EQUALSYMBOLS) != -1) { type = 8; val[0] = c; c = fgetc(fp); } else if(searchArray(arraySymbols,c,NUM_ARRAYSYMBOLS) != -1) { type = 9; val[0] = c; c = fgetc(fp); } else if(searchArray(blockSymbols,c,NUM_BLOCKSYMBOLS) != -1) { type = 10; val[0] = c; c = fgetc(fp); } else if(searchArray(parenSymbols,c,NUM_PARENSYMBOLS) != -1) { type = 11; val[0] = c; c = fgetc(fp); } else if(searchArray(boolSymbols,c,NUM_BOOLSYMBOLS) != -1) { type = 12; val[0] = c; c = fgetc(fp); } //CASE 4: PUNCTUATION else if(searchArray(punctuation,c,NUM_PUNCTUATION) != -1) { //std::cout << "In PUNCT " << c << std::endl; type = 4; val[0] = c; c = fgetc(fp); } //CASE 5: CHAR else if(c == '\'') { c = fgetc(fp); if(c == '\\') c = fgetc(fp); type = 5; val[0] = c; // c = fgetc(fp); c = fgetc(fp); } //CASE : UNKNOWN else { type = 13; val[0] = c; c = fgetc(fp); } return; // addToken(type,numLines,val); // type = -1; // memset(val,'\0',strlen(val)); } }
void AutonomousSeedControl(Path finalPath)//Chance to "Paths finalPath".. and make sure the finalPath is a different array from the main "parents" arrays becuase it's passed by reference { //Get current X and Y int curX = nodes(extractNum(finalPath.path, 0), 0); int curY = nodes(extractNum(finalPath.path, 0), 1); int tarX = 0; int tarY = 0; int curNode = 0; startDegree(0);//Which degree should we start on? (Default = 0 degrees) //Go through each target for(int k = 1; k < 6; k++)//CHANGE 6 TO PATHDEPTH WITH CORTEX { curNode = extractNum(finalPath.path,k); tarX = nodes(curNode, 0); tarY = nodes(curNode, 1); //One of the 4 stacks of 5 if(curNode>=16&&curNode<=19) { if(k+1<6)//CHANGE 6 TO PATHDEPTH WITH CORTEX { if(extractNum(finalPath.path, k+1) == 20 || extractNum(finalPath.path, k+1) == 21) { //Do Y-Fix first, and then turn to prepare for the upcoming bonus sack and X-fix yFix(curY, tarY); xFix(curX, tarX); } else if(extractNum(finalPath.path, k+1) == 14 || extractNum(finalPath.path, k+1) == 15) { //Do X-Fix first, and then turn to prepare for the upcoming grouping of scakcs and Y-Fix xFix(curX, tarX); yFix(curY, tarY); } } else { //By default to a Y-Fix first then an X-fix yFix(curY, tarY); xFix(curX, tarX); } } //Left/Right wall Sacks else if(curNode >= 0 && curNode <= 7) { yFix(curY, tarY); xFix(curX, tarX); } //Up/Down wall sacks else if(curNode >= 8 && curNode <= 11) { xFix(curX, tarX); yFix(curY, tarY); } else if(curNode == 21 || curNode == 20) { yFix(curY, tarY); xFix(curX, tarX); } //Reset curX and curY curX = tarX; curY = tarY; } }
int main(int argc, char* argv[]) { //read in file char str[MAX_LINE_LEN]; FILE *fp; fp=fopen(argv[1],"r"); if(fp==NULL) { fprintf(stderr,"Can't open input file!"); exit(1); } //LEXICAL ANALYSIS int numLines = 0; int type = -1; char* val = (char*)malloc(MAX_LINE_LEN); while(fgets(str,MAX_LINE_LEN,fp)!=NULL) { char* curPos = str; while(*curPos != '\n' && strcmp(curPos,"") != 0) { //CASE : WHITESPACE if(*curPos == ' ' || *curPos == '\t') { curPos++; continue; } //CASE 1: NUM else if(isdigit(*curPos)) { extractNum(curPos,val); type = 0; } //CASE 2: KEYWORD / IDENTIFIER / operator AND-OR else if(isalpha(*curPos)) { //get alphanum token getToken(curPos,val); if(searchArray(keywords,val,NUM_KEYWORDS) != -1) type = 1; else if(searchArray(boolSymbols,val,NUM_BOOLSYMBOLS) != -1) type = 12; else //identifier type = 2; } else if(searchArray(mathSymbols,*curPos,NUM_MATHSYMBOLS) != -1) { type = 6; val[0] = *curPos; curPos++; } else if(searchArray(relationalSymbols,*curPos,NUM_RELATIONALSYMBOLS) != -1) { type = 7; val[0] = *curPos; curPos++; } else if(searchArray(equalSymbols,*curPos,NUM_EQUALSYMBOLS) != -1) { type = 8; val[0] = *curPos; curPos++; } else if(searchArray(arraySymbols,*curPos,NUM_ARRAYSYMBOLS) != -1) { type = 9; val[0] = *curPos; curPos++; } else if(searchArray(blockSymbols,*curPos,NUM_BLOCKSYMBOLS) != -1) { type = 10; val[0] = *curPos; curPos++; } else if(searchArray(parenSymbols,*curPos,NUM_PARENSYMBOLS) != -1) { type = 11; val[0] = *curPos; curPos++; } else if(searchArray(boolSymbols,*curPos,NUM_BOOLSYMBOLS) != -1) { type = 12; val[0] = *curPos; curPos++; } //CASE 4: PUNCTUATION else if(searchArray(punctuation,*curPos,NUM_PUNCTUATION) != -1) { type = 4; val[0] = *curPos; curPos++; } //CASE 5: CHAR else if(*curPos == '\'') { curPos++; if(*curPos == '\\') curPos++; type = 5; val[0] = *curPos; curPos = curPos+2; } //CASE : UNKNOWN else { type = 13; val[0] = *curPos; curPos++; //addToken(13,numLines,val); } addToken(type,numLines,val); type = -1; memset(val,'\0',strlen(val)); } numLines++; } fclose(fp); printTokens(); fflush(stdout); //END FILE READ }