예제 #1
0
void	morc_tri_r(t_all *res, int debut, int fin, int base)
{
	int	i;
	int	ind;

	i = debut;
	ind = 0;
	while (i++ <= fin)
		if (res->a->pile[res->a->len - 1] < base)
			push_b(res);
		else
		{
			if (res->a->pile[res->a->len - 1] == base)
			{
				push_b(res);
				rotate_b(res);
			}
			else
			{
				rotate_a(res);
				ind++;
			}
		}
	if ((fin - debut) != (res->a->len + res->b->len - 1))
		while (ind-- > 0)
			reverse_rotate_a(res);
	reverse_rotate_b(res);
}
int dfs(long a,long b,char ch)
{
	t1=t2=0;
	long c,d,k;
	push_a(0);
	push_b(0);
	push_a(a+1);
	push_b(b+1);
	vis[a][b]=fill;
	while(a=pop_a())
	{
		b=pop_b();
		a--;b--;
		c=a;d=b;
		for(k=0;k<6;k++)
		{
			 c=a+x[k];
			  d=b+y[k];
			if(c>=0&&c<n&&d>=0&&d<n&&!vis[c][d]&&grid[c][d]==ch)
			{
		    	push_a(c+1);
				push_b(d+1);
			    vis[c][d]=fill;
				if(fu==1&&d==n-1)
					return 1;
				else if(fu==2&&c==n-1)
					return 1;

			}
		}
	}
	return 0;
}
long dfs(long a,long b,char im)
{
	long c,d,k,count=0;
	push_a(0);
	push_b(0);
	push_a(a+1);
	push_b(b+1);
	vis[a][b]=1;
	while(a=pop_a())
	{
		count++;
		b=pop_b();
		a--;b--;
		c=a;d=b;
		for(k=0;k<8;k++)
		{
			 c=a+x[k];
			  d=b+y[k];
			if(c>=0&&c<m&&d>=0&&d<n&&!vis[c][d]&&grid[c][d]==im)
			{
		    	push_a(c+1);
				push_b(d+1);
			    vis[c][d]=1;
			  
			}
			 
			  
		}
	}
	return count;
}
예제 #4
0
파일: push_swap.c 프로젝트: C00kie-/42
t_list_i	*push_swap(t_list_i *list)
{
	t_list_i	*pile_b;
	t_list_i	*last_item;

	pile_b = NULL;
	last_item = NULL;
	while ((test_sort_croissant(list) == 0) || pile_b)
	{
		last_item = can_we_rotate(list);
		if (last_item && list->next && list->next->next)
			list = rotate_a(list);
		if (list->c < list->next->c)
			list = swap_a(list);
		if (list->c > list->next->c && (test_sort_croissant(list) == 0)
			&& list->next && list->next->next != NULL)
			pile_b = push_b(&list, pile_b);
		if (pile_b && pile_b->next && pile_b->c > pile_b->next->c)
			pile_b = swap_b(pile_b);
		if (pile_b && pile_b->next && test_sort_decroissant(pile_b) &&
			test_sort_croissant(list))
			list = push_a(list, &pile_b);
		if (test_sort_croissant(list) && pile_b && pile_b->next == NULL)
			list = push_a(list, &pile_b);
	}
	return (list);
}
예제 #5
0
파일: sort_3.c 프로젝트: mdambrev/42
void				tri_basic(t_content *axx)
{
	TMP_A(4) = LIST_A(3);
	LIST_I(NB_LIST + 4, 0);
	norm(axx);
	while (check_list(axx, 0) != 0)
	{
		TMP_A(3) = LIST_A(0);
		LIST_I(NB_LIST + 3, 1);
		TMP_A(8) = LIST_A(0);
		LIST_I(NB_LIST + 8, 0);
		LIST_I(NB_LIST + 8, 0);
		if (VALUE_IN(0, 0) < VALUE_IN(0, 1))
		{
			op(axx, 5);
		}
		else if (TMP_IN(3, 1) < VALUE_IN(0, 1))
		{
			op(axx, 0);
		}
		else
		{
			push_b(axx, pos_x(axx, TMP_I(4, 0)));
			LIST_I(NB_LIST + 4, 0);
		}
	}
}
예제 #6
0
static void			order_a(t_push **lista, t_push **listb, t_action **actions)
{
	while (!checkorder(*lista))
	{
		if (get_last(*lista) > get_before_last(*lista))
			swap_a(lista, listb, actions);
		else
			push_b(lista, listb, actions);
		order_b(lista, listb, actions);
	}
}
예제 #7
0
void	decroissant(t_all *i)
{
	while (i->a->len > 2)
	{
		push_b(i);
		if (i->b->len > 1)
			rotate_b(i);
	}
	swap_a(i);
	while (i->b->len > 0)
		push_a(i);
}
예제 #8
0
파일: algo.c 프로젝트: Galathorn/Project
void			basic_sort(t_box *box)
{
	while (box->size_a != 0)
	{
		box->locat = little_number(A, box->size_a, &box->val_min);
		if (A[0] > A[1] && A[1] < A[2])
			swap_a(box);
		if (check_sort(A, box->size_a))
			break ;
		if (box->locat < (box->size / 2))
			while (A[0] != box->val_min)
				rot_a(box);
		else
			while (A[0] != box->val_min)
				revrot_a(box);
		push_b(box);
	}
	while (box->size_b != 0)
		push_a(box);
}
예제 #9
0
void			sort_pile(t_pushswap *data)
{
	while (42)
	{
		first_pile_check(data);
		put_smallest_last(data);
		if (check_pile(data->pile_a, data->length_a))
		{
			if (data->length_b == 0)
				break ;
			else if (data->length_b == 1)
				push_a(data, 1);
			else
			{
				while (data->length_b > 0)
					push_a(data, 0);
			}
		}
		else
			push_b(data, 0);
	}
}
예제 #10
0
파일: vm.c 프로젝트: shun-honda/ScriptC
long vm_execute(VMContext ctx, ScriptCInstruction inst) {
  static const void *table[] = {
#define DEFINE_TABLE(NAME) &&OP_##NAME,
    IR_EACH(DEFINE_TABLE)
#undef DEFINE_TABLE
  };

  if(inst == NULL) {
    return (long)table;
  }

  register ScriptCInstruction pc = inst+1;

  goto *GET_ADDR(pc);

  OP(exit) {
    return 0;
  }
  OP(call) {
    ctx = createVMContext(ctx, pc-inst+1);
    JUMP(inst + pc->call_point);
  }
  OP(ret) {
    long retPoint = ctx->retPoint;
    Type top = pop_sp(ctx);
    VMContext next = call_back(ctx);
    if(top->type == TYPE_INT) {
      push_i(next, top->int_val);
    } else if(top->type == TYPE_FLOAT) {
      push_d(next, top->double_val);
    } else if(top->type == TYPE_STRING) {
      push_s(next, top->string);
    } else if(top->type == TYPE_BOOL) {
      push_b(next, top->bool_val);
    } else {
      fprintf(stderr, "type error of return statement\n");
      return 1;
    }
    disposeVMContext(ctx);
    ctx = next;
    JUMP(inst + retPoint);
  }
  OP(ret_void) {
    long retPoint = ctx->retPoint;
    VMContext next = call_back(ctx);
    disposeVMContext(ctx);
    ctx = next;
    JUMP(inst + retPoint);
  }
  OP(iconst) {
    push_i(ctx, pc->int_val);
    DISPATCH_NEXT;
  }
  OP(dconst) {
    push_d(ctx, pc->double_val);
    DISPATCH_NEXT;
  }
  OP(sconst) {
    push_s(ctx, pc->string);
    DISPATCH_NEXT;
  }
  OP(bconst) {
    push_b(ctx, pc->bool_val);
    DISPATCH_NEXT;
  }
  OP(jump) {
    JUMP(inst + pc->jump);
  }
  OP(ifcmp) {
    Type top = pop_sp(ctx);
    if(top->type != TYPE_BOOL) {
      fprintf(stderr, "type error of ifcmp\n");
    }
    if(!top->bool_val) {
      JUMP(inst + pc->jump);
    }
    DISPATCH_NEXT;
  }
  OP(gt) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_b(ctx, left->int_val > right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_b(ctx, left->double_val > right->double_val);
    } else {
      fprintf(stderr, "type error of gt expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(ge) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_b(ctx, left->int_val >= right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_b(ctx, left->double_val >= right->double_val);
    } else {
      fprintf(stderr, "type error of ge expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(lt) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_b(ctx, left->int_val < right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_b(ctx, left->double_val < right->double_val);
    } else {
      fprintf(stderr, "type error of lt expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(le) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_b(ctx, left->int_val <= right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_b(ctx, left->double_val <= right->double_val);
    } else {
      fprintf(stderr, "type error of le expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(eq) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_b(ctx, left->int_val == right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_b(ctx, left->double_val == right->double_val);
    } else if(right->type == TYPE_STRING && left->type == TYPE_STRING) {
      push_b(ctx, !strcmp(left->string, right->string));
    } else if(right->type == TYPE_BOOL && left->type == TYPE_BOOL) {
      push_b(ctx, left->bool_val == right->bool_val);
    } else {
      fprintf(stderr, "type error of le expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(ne) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_b(ctx, left->int_val != right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_b(ctx, left->double_val != right->double_val);
    } else if(right->type == TYPE_STRING && left->type == TYPE_STRING) {
      push_b(ctx, strcmp(left->string, right->string));
    } else if(right->type == TYPE_BOOL && left->type == TYPE_BOOL) {
      push_b(ctx, left->bool_val != right->bool_val);
    } else {
      fprintf(stderr, "type error of le expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(add) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_i(ctx, left->int_val + right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_d(ctx, left->double_val + right->double_val);
    } else if(right->type == TYPE_STRING && left->type == TYPE_STRING) {
      strcat(left->string, right->string);
      push_s(ctx, left->string);
    } else {
      fprintf(stderr, "type error of add expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(sub) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_i(ctx, left->int_val - right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_d(ctx, left->double_val - right->double_val);
    } else {
      fprintf(stderr, "type error of sub expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(mul) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_i(ctx, left->int_val * right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_d(ctx, left->double_val * right->double_val);
    } else {
      fprintf(stderr, "type error of mul expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(div) {
    Type right = pop_sp(ctx);
    Type left = pop_sp(ctx);
    if(right->type == TYPE_INT && left->type == TYPE_INT) {
      push_i(ctx, left->int_val / right->int_val);
    } else if(right->type == TYPE_FLOAT && left->type == TYPE_FLOAT) {
      push_d(ctx, left->double_val / right->double_val);
    } else {
      fprintf(stderr, "type error of div expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(minus) {
    Type left = pop_sp(ctx);
    if(left->type == TYPE_INT) {
      push_i(ctx, -left->int_val);
    } else if(left->type == TYPE_FLOAT) {
      push_d(ctx, -left->double_val);
    } else {
      fprintf(stderr, "type error of add expression\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(loadl) {
    Type val = ctx->var_list+pc->var_id;
    if(val->type == TYPE_INT) {
      push_i(ctx, val->int_val);
    } else if(val->type == TYPE_FLOAT) {
      push_d(ctx, val->double_val);
    } else if(val->type == TYPE_STRING) {
      push_s(ctx, val->string);
    } else if(val->type == TYPE_BOOL) {
      push_b(ctx, val->bool_val);
    } else {
      fprintf(stderr, "type error of loadl\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(storea) {
    Type val = ctx->var_list+pc->var_id;
    Type top = pop_sp(ctx->prev);
    if(top->type == TYPE_INT) {
      val->int_val = top->int_val;
    } else if(top->type == TYPE_FLOAT) {
      val->double_val = top->double_val;
    } else if(top->type == TYPE_STRING) {
      val->string = top->string;
    } else if(top->type == TYPE_BOOL) {
      val->bool_val = top->bool_val;
    } else {
      fprintf(stderr, "type error of storel\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(storel) {
    Type val = ctx->var_list+pc->var_id;
    Type top = pop_sp(ctx);
    if(top->type == TYPE_INT) {
      val->int_val = top->int_val;
      val->type = TYPE_INT;
    } else if(top->type == TYPE_FLOAT) {
      val->double_val = top->double_val;
      val->type = TYPE_FLOAT;
    } else if(top->type == TYPE_STRING) {
      val->string = top->string;
      val->type = TYPE_STRING;
    } else if(top->type == TYPE_BOOL) {
      val->bool_val = top->bool_val;
      val->type = TYPE_BOOL;
    } else {
      fprintf(stderr, "type error of storel\n");
      return 1;
    }
    DISPATCH_NEXT;
  }
  OP(write) {
    Type val = pop_sp(ctx);
    if(val->type == TYPE_INT) {
      printf("%d\n", val->int_val);
    } else if(val->type == TYPE_FLOAT) {
      printf("%f\n", val->double_val);
    } else if(val->type == TYPE_STRING) {
      printf("%s\n", val->string);
    } else if(val->type == TYPE_BOOL) {
      if(val->bool_val) {
        printf("true\n");
      } else {
        printf("false\n");
      }
    }
    DISPATCH_NEXT;
  }

  return 0;
}