Пример #1
0
int main()
{
        srand(time(NULL));
        int M = myrand(100);
        int N = myrand();
        printf("(M,N) = (%d,%d)\n", M,N);
        StructArr arrM = oct2bin(M);
        StructArr arrN = oct2bin(N);
        insertNum(arrN, arrM);
        insertNumBit(N, M);
        return 0;
}
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;
    }
  }
}
Пример #3
0
//aqui recebe uma string e são tratados todos os chars para o proposito do programa
int calcular(char g_char[], pilha *p){

	if( isEnter(g_char[0]) || !isEmpty(p)){
		int i = 0;
		
		while(g_char[i] != '\0'){
			
			//insere o estado dos calculos
			if(!isClear(g_char[i]) && !isConsulta(g_char[i])){
				if(isEmpty(p))
					printf("- ");
				consulta_inversa(p);
				printf("\n");
			}
			
			//se for numero insere na pilha
			if(isNum(g_char[i])){
				insertNum(p, g_char[i] - 48);
				
				//se o char anterior tbm for numero faz calculo para fazer dos 2 um numero
				if(isNum(g_char[i-1] )|| isEnter(g_char[i-1]))
					faz_calculo(p);
				else{
					clear_calc(p);
					if(!RUN_CODES){
						exibeHeader();
						printf("\nErro! Voce deve usar necessariamente \'E\' para informar a entrada de um numero\n");
					}
				}
				i++;
				continue;
			}
			
			//se for um E insere 0 na pilha
			if(isEnter(g_char[i])){
        		insertNum(p, 0);
        		i++;
        		continue;
			}
			
			//se for '-' faz uma subtração
			if(g_char[i] == '-'){
				faz_sub(p);
				i++;
				continue;
			}
			
			//se for '+' faz uma adição
			if(g_char[i] == '+'){
				faz_soma(p);
				i++;
				continue;
			}
			
			//se for '/' faz uma divisão
            if(g_char[i] == '/'){
				faz_div(p);
				i++;
				continue;
			}
			
			//se for '*' faz uma multiplicação
            if(g_char[i] == '*'){
				faz_mult(p);
				i++;
				continue;
			}
			
			//se for '^' faz uma potencia
            if(g_char[i] == '^'){
				faz_pow(p);
				i++;
				continue;
			}
			
			//se for '!' faz um fatorial
            if(g_char[i] == '!'){
				faz_fatorial(p);
				i++;
				continue;
			}
			
			//se for C limpa a pilha
			if(isClear(g_char[i])){
				clear_calc(p);
				if(!RUN_CODES){
					exibeHeader();
					printf("\nPilha Vazia\n");
				}
				i++;
				continue;
			}
			
			//se for V faz uma consulta na pilha
			if(isConsulta(g_char[i]) && strlen(g_char) == 1){
				if(!RUN_CODES)
					consulta(p);
				i++;
			}
				
		}
		if(!isConsulta(g_char[i-1]))
			//consulta que exibe a pilha ao contrario
			consulta_inversa(p);
		if(!RUN_CODES)
			printf("\n");
		return 0;
	}else{
		//se o char indice 0 for V exibe a consulta
		if(isConsulta(g_char[0]))
			if(!RUN_CODES)
				consulta(p);
				
		else{
			//se o char indice 0 for C limpa a pilha
			if(isClear(g_char[0]))
				clear_calc(p);
				
			else
				return 1;
		}
	}
		
	
}