Exemplo n.º 1
0
/* Set the passed DeviceGray image (which should be a PS style image
 * dictionary) as a soft mask.
 */
Bool pdf_setGrayImageAsSoftMask(PDFCONTEXT *pdfc, OBJECT image)
{
  Group *group = NULL ;
  Bool result = FALSE ;

  PDF_CHECK_MC(pdfc);

#define return DO_NOT_RETURN_-_SET_result_INSTEAD!
  /* We need to save the current gstate before we install the DeviceGray color
     space. */
  if ( gs_gpush(GST_GROUP) ) {
    int32 gid = gstackptr->gId ;

    /* Set the colorspace to device gray and initialise the constant alpha. */
    if ( gsc_setcolorspacedirect(gstateptr->colorInfo, GSC_FILL, SPACE_DeviceGray)) {
      OBJECT gray = OBJECT_NOTVM_NAME(NAME_DeviceGray, LITERAL) ;

      tsSetConstantAlpha(gsTranState(gstateptr), FALSE, 1, gstateptr->colorInfo);

      /* Open a group to capture the image. */
      if ( groupOpen(pdfc->corecontext->page, gray, TRUE /*I*/, FALSE /*K*/,
                     TRUE /*Banded*/, NULL /*bgcolor*/, NULL /*xferfn*/,
                     NULL /*patternTA*/, GroupLuminositySoftMask, &group)) {
        if ( gs_gpush(GST_GSAVE) ) {
          OBJECT psDict = OBJECT_NOTVM_NOTHING;

          /* Copy the pdf dictionary into PostScript memory as we're calling a
             PostScript function (we don't want to risk having a mixed pdf/ps
             memory dictionary). */
          if ( pdf_copyobject(NULL, &image, &psDict)) {
            PDF_IMC_PARAMS* imc;

            /* Get hold of the PDF stack. */
            PDF_GET_IMC(imc);

            /* Draw the image. */
            if ( push(&psDict, &imc->pdfstack) ) {
              if ( gs_image(pdfc->corecontext, &imc->pdfstack)) {
                result = TRUE ;
              } else {
                pop(&imc->pdfstack) ;
              }
            }
          } else /* Some routes through pdf_copyobject don't set errors. */
            result = error_handler(VMERROR);
        }

        /* Close the group. */
        if ( !groupClose(&group, result) )
          result = FALSE ;
      }
    }

    /* Restore gstate. */
    if ( !gs_cleargstates(gid, GST_GROUP, NULL) )
      result = FALSE;

    /* Install the soft mask. */
    if ( result )
      result = tsSetSoftMask(gsTranState(gstateptr), LuminositySoftMask,
                             groupId(group), gstateptr->colorInfo);
  }

#undef return
  return result;
}
Exemplo n.º 2
0
uint8_t PinMock::next_digital_value(uint8_t pin) {
  return pop(digital_values[pin]);
}
void main()
{
	int ch;
	clrscr();
	while(ch!=3)
	{
		printf("\nEnter Your Choice: \n 1. For PUSH \n 2. For Pattern check \n 3. for Exit from the Window");
		scanf("%d",&ch);
		if(ch==1)
		{
			int n=0,s=0;
			int f=1;
			while(n!= -1 && f)
			{
				printf("Enter the no to Push\t for exit -1 ");
				scanf("%d",&n);
				if(stop== -1)
				{
					push(n);
					s=n;
				}
				else
				{
					if( n == s+1 )
					{
						s=n;
						push(n);
					}
					else
					{
						printf("Invalid PUSH %d ",n);
						break;
					}
				}

			}
		}
		else if(ch==2)
		{
			int i=0,temp=0,j=0,len;
			char s[20];
			printf("\n Enter the Pattern");
			scanf("%s",s);
			len=strlen(s);
			while(i<len)
			{
				if(i==0)
				{
					for(j=0;j<= s[0]-'0' ;j++)
					{
						push(j);
					}
					temp=pop();
					printf("\%d",temp);
					temp=pop();
					i++;
				}
				else
				{
					if(temp == s[i]-'0')
					{
						printf("\n%d",temp);
						temp=pop();
						i++;
					}
					else if(temp < s[i]-'0')
					{
						
						int c=0,k;
						while(j<= s[i]-'0')
						{
							push(j);
							j++;
							c++;
						}
						for(k=0;k<c;k++)
						{
						if(temp!=-1)
						{printf("\n%d",temp);}
						temp=pop(); }
						i++;
					}
					else
					{	break; }
				}

			}
			if(temp==-1)
			{
				printf("\nValid Pattern");
			}
			else
			{
				printf("\n Invalid Pattern %d",temp);

			}
		}
Exemplo n.º 4
0
tnode* generateTree(int s, char str[])
{
	tnode* root = getNode(0,'o');
	tnode* tmp;
	queue* q = newQueue();
	push(root, q);
	
	int i,t = pow(2,s)-1;

	for (i=0; i<t; i++)
	{
		tmp = pop(q);
		int a = tmp->data;
		char b = reverse(tmp->orand);
		tmp->lchild = getNode(2*a+1,b);
		tmp->lchild->parent = tmp;
		push(tmp->lchild, q);
		tmp->rchild = getNode(2*a+2,b);
		tmp->rchild->parent = tmp;
		push(tmp->rchild, q);
	}

	/*------NOW EVALUATING All NODES-------*/

	t++;
	int j=0;
	for (i=0; i<t; i++)
	{
		tmp = pop(q);
		if((tmp->data)%2 != 0)
		{
			tmp->data = evaluate(str,j,s);
			// printf("--%d",tmp->data);
			j++;	
		}
		else
		{
			tmp->data = evaluate(str,j,s);
			// printf("--%d",tmp->data);
			j++;
			push(tmp->parent, q);
		}
	}

	while(!isEmpty(q))
	{
		tmp = pop(q);
		if((tmp->data)%2 != 0 || tmp->data == 0)
		{
			if(tmp->orand == 'o')
				tmp->data = max(tmp->lchild->data, tmp->rchild->data);
			else
				tmp->data = min(tmp->lchild->data, tmp->rchild->data);
		}
		else
		{
			if(tmp->orand == 'o')
				tmp->data = max(tmp->lchild->data, tmp->rchild->data);
			else
				tmp->data = min(tmp->lchild->data, tmp->rchild->data);

			push(tmp->parent, q);
		}
			
	}
	return root;
}
//把栈置空,先将各节点pop出去,内存释放,直到s = NULL。 若要将栈销毁,再free(s)即可。
//最简单的方法是s=NULL;但是没有释放有效节点,浪费内存。
void SetEmpty(PStack s)
{
    while(s != NULL)
        pop(s);
}
Exemplo n.º 6
0
void
op(int what)
{
    mnumber a, b, c, *spval;
    char *lv;
    int tp = type[what];

    if (errflag)
	return;
    if (sp < 0) {
	zerr("bad math expression: stack empty");
	return;
    }

    if (tp & (OP_A2|OP_A2IR|OP_A2IO|OP_E2|OP_E2IO)) {
	/* Make sure anyone seeing this message reports it. */
	DPUTS(sp < 1, "BUG: math: not enough wallabies in outback.");
	b = pop(0);
	a = pop(what == EQ);
	if (errflag)
	    return;

	if (tp & (OP_A2IO|OP_E2IO)) {
	    /* coerce to integers */
	    if (a.type & MN_FLOAT) {
		a.type = MN_INTEGER;
		a.u.l = (zlong)a.u.d;
	    }
	    if (b.type & MN_FLOAT) {
		b.type = MN_INTEGER;
		b.u.l = (zlong)b.u.d;
	    }
	} else if (a.type != b.type && what != COMMA &&
		   (a.type != MN_UNSET || what != EQ)) {
	    /*
	     * Different types, so coerce to float.
	     * It may happen during an assignment that the LHS
	     * variable is actually an integer, but there's still
	     * no harm in doing the arithmetic in floating point;
	     * the assignment will do the correct conversion.
	     * This way, if the parameter is actually a scalar, but
	     * used to contain an integer, we can write a float into it.
	     */
	    if (a.type & MN_INTEGER) {
		a.type = MN_FLOAT;
		a.u.d = (double)a.u.l;
	    }
	    if (b.type & MN_INTEGER) {
		b.type = MN_FLOAT;
		b.u.d = (double)b.u.l;
	    }
	}

	if (noeval) {
	    c.type = MN_INTEGER;
	    c.u.l = 0;
	} else {
	    /*
	     * type for operation: usually same as operands, but e.g.
	     * (a == b) returns int.
	     */
	    c.type = (tp & OP_A2IR) ? MN_INTEGER : a.type;

	    switch(what) {
	    case AND:
	    case ANDEQ:
		c.u.l = a.u.l & b.u.l;
		break;
	    case XOR:
	    case XOREQ:
		c.u.l = a.u.l ^ b.u.l;
		break;
	    case OR:
	    case OREQ:
		c.u.l = a.u.l | b.u.l;
		break;
	    case MUL:
	    case MULEQ:
		if (c.type == MN_FLOAT)
		    c.u.d = a.u.d * b.u.d;
		else
		    c.u.l = a.u.l * b.u.l;
		break;
	    case DIV:
	    case DIVEQ:
		if (!notzero(b))
		    return;
		if (c.type == MN_FLOAT)
		    c.u.d = a.u.d / b.u.d;
		else
		    c.u.l = a.u.l / b.u.l;
		break;
	    case MOD:
	    case MODEQ:
		if (!notzero(b))
		    return;
		c.u.l = a.u.l % b.u.l;
		break;
	    case PLUS:
	    case PLUSEQ:
		if (c.type == MN_FLOAT)
		    c.u.d = a.u.d + b.u.d;
		else
		    c.u.l = a.u.l + b.u.l;
		break;
	    case MINUS:
	    case MINUSEQ:
		if (c.type == MN_FLOAT)
		    c.u.d = a.u.d - b.u.d;
		else
		    c.u.l = a.u.l - b.u.l;
		break;
	    case SHLEFT:
	    case SHLEFTEQ:
		c.u.l = a.u.l << b.u.l;
		break;
	    case SHRIGHT:
	    case SHRIGHTEQ:
		c.u.l = a.u.l >> b.u.l;
		break;
	    case LES:
		c.u.l = (zlong)
		    (a.type == MN_FLOAT ? (a.u.d < b.u.d) : (a.u.l < b.u.l));
		break;
	    case LEQ:
		c.u.l = (zlong)
		    (a.type == MN_FLOAT ? (a.u.d <= b.u.d) : (a.u.l <= b.u.l));
		break;
	    case GRE:
		c.u.l = (zlong)
		    (a.type == MN_FLOAT ? (a.u.d > b.u.d) : (a.u.l > b.u.l));
		break;
	    case GEQ:
		c.u.l = (zlong)
		    (a.type == MN_FLOAT ? (a.u.d >= b.u.d) : (a.u.l >= b.u.l));
		break;
	    case DEQ:
		c.u.l = (zlong)
		    (a.type == MN_FLOAT ? (a.u.d == b.u.d) : (a.u.l == b.u.l));
		break;
	    case NEQ:
		c.u.l = (zlong)
		    (a.type == MN_FLOAT ? (a.u.d != b.u.d) : (a.u.l != b.u.l));
		break;
	    case DAND:
	    case DANDEQ:
		c.u.l = (zlong)(a.u.l && b.u.l);
		break;
	    case DOR:
	    case DOREQ:
		c.u.l = (zlong)(a.u.l || b.u.l);
		break;
	    case DXOR:
	    case DXOREQ:
		c.u.l = (zlong)((a.u.l && !b.u.l) || (!a.u.l && b.u.l));
		break;
	    case COMMA:
		c = b;
		break;
	    case POWER:
	    case POWEREQ:
		if (c.type == MN_INTEGER && b.u.l < 0) {
		    /* produces a real result, so cast to real. */
		    a.type = b.type = c.type = MN_FLOAT;
		    a.u.d = (double) a.u.l;
		    b.u.d = (double) b.u.l;
		}
		if (c.type == MN_INTEGER) {
		    for (c.u.l = 1; b.u.l--; c.u.l *= a.u.l);
		} else {
		    if (b.u.d <= 0 && !notzero(a))
			return;
		    if (a.u.d < 0) {
			/* Error if (-num ** b) and b is not an integer */
			double tst = (double)(zlong)b.u.d;
			if (tst != b.u.d) {
			    zerr("imaginary power");
			    return;
			}
		    }
		    c.u.d = pow(a.u.d, b.u.d);
		}
		break;
	    case EQ:
		c = b;
		break;
	    }
	}
	if (tp & (OP_E2|OP_E2IO)) {
	    struct mathvalue *mvp = stack + sp + 1;
	    lv = stack[sp+1].lval;
	    c = setmathvar(mvp, c);
	    push(c, mvp->lval, 0);
	} else
	    push(c,NULL, 0);
	return;
    }
Exemplo n.º 7
0
/* infix_to_postfix:
   routine that will be called with parameter:
   char *in characters of infix notation (algebraic formula)
   returns: char * pointer to string of returned postfix */
static char *
infix_to_postfix( char *infix ) {
	INTDBG("ENTER: in: %s, size: %zu\n", infix, strlen(infix));
	static char postfix[2*PAPI_HUGE_STR_LEN];	// output
        unsigned int index;
        int postfixlen;
        char token;
        if ( strlen(infix) > PAPI_HUGE_STR_LEN ) 
            PAPIERROR("A infix string (probably in user-defined presets) is too big (max allowed %d): %s", PAPI_HUGE_STR_LEN, infix );

        // initialize stack
	memset( &stack, 0, 2*PAPI_HUGE_STR_LEN );
	stacktop = -1; 
	push('#');
        /* initialize output string */
	memset(&postfix,0,2*PAPI_HUGE_STR_LEN);
        postfixlen = 0;

	for( index=0; index<strlen(infix); index++ ) {
                token = infix[index];
                INTDBG("INTDBG: in: %s, length: %zu, index: %d token %c\n", infix, strlen( infix ), index, token);
		switch( token ) {
		case '(':
			push( token );
			break;
		case ')':
                        if (postfix[postfixlen-1]!='|') postfix[postfixlen++] = '|';
                        while ( stack[stacktop] != '(' ) {
                                postfix[postfixlen++] = pop();
                                postfix[postfixlen++] = '|';
                        }
                        token = pop();  /* pop the '(' character */
			break;
		case '+':
		case '-':
		case '*':
		case '/':
		case '%':
		case '^':       /* if an operator */
                        if (postfix[postfixlen-1]!='|') postfix[postfixlen++] = '|';
                        while ( priority(stack[stacktop]) > priority(token) ) {
                                postfix[postfixlen++] = pop();
                                postfix[postfixlen++] = '|';
                        }
                        push( token ); /* save current operator */
                        break;
		default: // if alphanumeric character which is not parenthesis or an operator
                        postfix[postfixlen++] = token;
			break;
		} // end switch symbol
	} // end while

        /* Write any remaining operators */
        if (postfix[postfixlen-1]!='|') postfix[postfixlen++] = '|';
        while ( stacktop>0 ) {
                postfix[postfixlen++] = pop();
                postfix[postfixlen++] = '|';
        }
        postfix[postfixlen++] = '\0';
	stacktop = -1; 

	INTDBG("EXIT: postfix: %s, size: %zu\n", postfix, strlen(postfix));
	return (postfix);
} // end infix_to_postfix
Exemplo n.º 8
0
int do_assemble2(program *program, program2 * program2)
{
  int pc = 0;
  int nextpc = 0;
  long long counter = 0;
  instruction2 ist;
  int iname, arg1, arg2, arg3;
    
  while (1) {
    pc = nextpc;

    
    ist = program2->insts[pc];
    iname = ist.name[0];
    arg1 = ist.name[1];
    arg2 = ist.name[2];
    arg3 = ist.name[3];

    /*
      ++counter;

      if(counter > 575600 && counter < 575719)
      {
      print_label_from_index(program, pc);
      print_instruction(program->insts[pc]);
      print_register();
      }
    */
	
    if (iname == 0) {
      // printf("this is nop\n");
    }
    DO_INST_1(1, +)
      DO_INST_1(2, -)
      DO_INST_1(3, *)
      DO_INST_1(4, &)
      DO_INST_1(5, |)
      /*ALU命令 */
    else if (iname == 6) {
      regist[ist.name[1]] = ~(regist[ist.name[2]] | regist[ist.name[3]]);
    }
    else if (iname == 7) {
      regist[ist.name[1]] = regist[ist.name[2]] ^ regist[ist.name[3]];
    }
    else if (iname == 8) {
      regist[ist.name[1]] = regist[ist.name[2]] + ist.name[3];
    }
    else if (iname == 9) {
      regist[ist.name[1]] = regist[ist.name[2]] - ist.name[3];
    }
    else if (iname == 10) {
      regist[ist.name[1]] = regist[ist.name[2]] * ist.name[3];
    }
    else if (iname == 11) {
      regist[ist.name[1]] = regist[ist.name[2]] & ist.name[3];
    }
    else if (iname == 12) {
      regist[ist.name[1]] = regist[ist.name[2]] | ist.name[3];
    }
    else if (iname == 13) {
      regist[ist.name[1]] = ~(regist[ist.name[2]] | ist.name[3]);
    }
    else if (iname == 14) {
      regist[ist.name[1]] = regist[ist.name[2]] ^ ist.name[3];
    }
    /*FPU命令 */
    else if (iname == 15) {
      freg[ist.name[1]] = freg[ist.name[2]] + freg[ist.name[3]];
    }
    else if (iname == 16) {
      freg[ist.name[1]] = freg[ist.name[2]] - freg[ist.name[3]];
    }
    else if (iname == 17) {
      freg[ist.name[1]] = freg[ist.name[2]] * freg[ist.name[3]];
    }
    else if (iname == 18) {
      freg[ist.name[1]] = freg[ist.name[2]] / freg[ist.name[3]];
    }
    else if (iname == 19) {
      freg[ist.name[1]] = 1 / freg[ist.name[2]];
    }
    else if (iname == 20) {
      freg[ist.name[1]] = sqrt(freg[ist.name[2]]);
    }
    else if (iname == 21) {
      freg[ist.name[1]] = floor(freg[ist.name[2]]);
    }
    else if (iname == 22) {
      freg[ist.name[1]] = (float) (regist[ist.name[2]]);
    }


    /*MEM ACSESS命令 */
    else if (iname == 23) {
      //memory_check
      if (check_memory(regist[ist.name[2]] + ist.name[3]) == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	//	 printf("%d\n", iname);
	//printf("%lld\n", counter);
	exit(1);
      }
      regist[ist.name[1]] = memory[regist[ist.name[2]] + ist.name[3]].i;
    }

    else if (iname == 24) {
      //memory_check
      if (check_memory(regist[ist.name[2]] + ist.name[3]) == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	//printf("%d\n", iname);
	exit(1);
      }
      memory[regist[ist.name[2]] + ist.name[3]].i =  regist[ist.name[1]];
    }

    else if (iname == 25) {
      //memory_check
      if (check_memory(regist[ist.name[2]] + ist.name[3])
	  == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	//printf("%d\n", iname);
	exit(1);
      }
      freg[ist.name[1]] =
	memory[regist[ist.name[2]] + ist.name[3]].d;
    }

    else if (iname == 26) {
      //memory_check
      if (check_memory(regist[ist.name[2]] + ist.name[3])
	  == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	// printf("%d\n", iname);
	exit(1);
      }
      memory[regist[ist.name[2]] + ist.name[3]].d =
	freg[ist.name[1]];
    }


    /*BRANCH命令 */
    else if (iname == 27) {
      if (regist[ist.name[1]] == regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 28) {
      if (regist[ist.name[1]] != regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }
    else if (iname == 29) {
      if (regist[ist.name[1]] > regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 30) {
      if (regist[ist.name[1]] < regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 31) {
      if (regist[ist.name[1]] >= regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 32) {
      if (regist[ist.name[1]] <= regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }

    } else if (iname == 33) {
      if (freg[ist.name[1]] == freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 34) {
      if (freg[ist.name[1]] != freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 35) {
      if (freg[ist.name[1]] > freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 36) {
      if (freg[ist.name[1]] < freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 37) {
      if (freg[ist.name[1]] >= freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 38) {
      if (freg[ist.name[1]] <= freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }


    /*JUMP命令 */
    else if (iname == 39) {
      if (ist.name[1] == -100) {
	freg[2] = atanf(freg[2]);
	nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -200) {
	freg[2] = sqrtf(freg[2]);
	nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -300) {
	regist[4] = read_int();
	printf("read_int: %d\n", regist[4]);
	nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -400) {
	freg[2] = read_float();
	printf("read_float: %f\n", freg[2]);
	nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -500) {
	printf("aaa\n");
	if(ppp < freg[2])
	  {
	    ppp = freg[2];
	  }
	if(nnn > freg[2])
	  {
	    nnn = freg[2];
	  }
	//print_register();
	nextpc = label_trans(program->insts[pc].name[1], program) - 1;
	//freg[2] = sinf(freg[2]);
	//printf("read_float: %f\n", freg[2]);
	//nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -600) {
	freg[2] = cosf(freg[2]);
	//printf("read_float: %f\n", freg[2]);
	nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -700) {
	//print_int
	print_int();
	nextpc = pop(&call_stack) - 1;
      } else if (ist.name[1] == -800) {
	print_float();
	nextpc = pop(&call_stack) - 1;
      }
      else
	{
	  nextpc = ist.name[1] - 1;
	}
      /*
	if(strncmp(program->insts[pc].name[1] , "L_main", 6) == 0)
	{
	print_memory();
	}
      */
    }


    else if (iname == 40) {
          
      nextpc = ist.name[1] - 1;
      /*特殊ラベル*/
      if (ist.name[1] == -100) {
	freg[2] = atanf(freg[2]);
	nextpc = pc;
      } else if (ist.name[1] == -200) {
	freg[2] = sqrtf(freg[2]);
	nextpc = pc;
      } else if (ist.name[1] == -300) {
	regist[4] = read_int();
	printf("read_int: %d\n", regist[4]);
	nextpc = pc;
      } else if (ist.name[1] == -400) {
	freg[2] = read_float();
	printf("read_float: %f\n", freg[2]);
	nextpc = pc;
      } else if (ist.name[1] == -500) {
	printf("aab\n");
	kkk = 1;
	if(ppp < freg[2])
	  {
	    ppp = freg[2];
	  }
	if(nnn > freg[2])
	  {
	    nnn = freg[2];
	  }
	push(&call_stack, (pc + 1));
	nextpc = label_trans(program->insts[pc].name[1], program) - 1;
	
	//freg[2] = sinf(freg[2]);
	//printf("read_float: %f\n", freg[2]);
	//nextpc = pc;
      } else if (ist.name[1] == -600) {
	freg[2] = cosf(freg[2]);
	//printf("read_float: %f\n", freg[2]);
	nextpc = pc;
      } else if (ist.name[1] == -700) {
	//print_int
	print_int();
	nextpc = pc;
      } else if (ist.name[1] == -800) {
	print_float();
	nextpc = pc;
      } else {
	push(&call_stack, (pc + 1));
      }


    }

    else if (iname == 41) {
      nextpc = pop(&call_stack) - 1;
    }

    else if (iname = 42) {
      print_register();
      print_memory();
    }
    //命令が存在しなかった場合error parseでやっているのでいらない。
    //printf("ist = %d\n",iname);

    nextpc++;


    /*命令がラストの行まで行けば処理を終了する */
    if (nextpc >= program2->inst_count) {
      break;
    }
  }

  return 0;
}
Exemplo n.º 9
0
int do_assemble(program * program)
{
  int pc = 0;
  int nextpc = 0;
  int stop = 0, i, j, k, l;
  char *iname;
  instruction ist;
  char *arg1, *arg2, *arg3;
  int count = 0;
  int r3;
  union umemory tmp;
  int a;
  float b;
  int cnt[100];

  for (i = 0; i < 100; ++i) {
    cnt[i] = 0;
  }

  while (1) {
    pc = nextpc;

    /*
      途中で操作を止める際のコード 
      print_register();
      printf("pc = %d\n",pc);
      if(stop>=100){exit(1);}else{stop++;}
    */

    r3 = regist[3];
    ++count;
    ist = program->insts[pc];
    iname = ist.name[0];
    arg1 = ist.name[1];
    arg2 = ist.name[2];
    arg3 = ist.name[3];

    /*ALU命令 */
    if (strcmp(iname, "nop") == 0) {
      ++cnt[0];
    }

    else if (strcmp(iname, "add") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] +
	regist[int_of_register(ist.name[3])];

      ++cnt[1];
    }

    else if (strcmp(iname, "sub") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] -
	regist[int_of_register(ist.name[3])];

      ++cnt[2];
    }

    else if (strcmp(iname, "mul") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] *
	regist[int_of_register(ist.name[3])];

      ++cnt[3];
    }

    else if (strcmp(iname, "and") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] &
	regist[int_of_register(ist.name[3])];

      ++cnt[4];
    }

    else if (strcmp(iname, "or") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] |
	regist[int_of_register(ist.name[3])];

      ++cnt[5];
    }

    else if (strcmp(iname, "nor") == 0) {
      regist[int_of_register(ist.name[1])] =
	~(regist[int_of_register(ist.name[2])] |
	  regist[int_of_register(ist.name[3])]);

      ++cnt[6];
    }

    else if (strcmp(iname, "xor") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] ^
	regist[int_of_register(ist.name[3])];

      ++cnt[7];
    }

    else if (strcmp(iname, "addi") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] + atoi(ist.name[3]);

      ++cnt[8];
    }

    else if (strcmp(iname, "subi") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] - atoi(ist.name[3]);

      ++cnt[9];
    }

    else if (strcmp(iname, "muli") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] * atoi(ist.name[3]);

      ++cnt[10];
    }

    else if (strcmp(iname, "andi") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] & atoi(ist.name[3]);

      ++cnt[11];
    }

    else if (strcmp(iname, "ori") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] | atoi(ist.name[3]);
      ++cnt[12];
    }

    else if (strcmp(iname, "nori") == 0) {
      regist[int_of_register(ist.name[1])] =
	~(regist[int_of_register(ist.name[2])] |
	  atoi(ist.name[3]));
      ++cnt[13];
    }

    else if (strcmp(iname, "xori") == 0) {
      regist[int_of_register(ist.name[1])] =
	regist[int_of_register(ist.name[2])] ^ atoi(ist.name[3]);

      ++cnt[14];
    }


    /*FPU命令 */
    else if (strcmp(iname, "fadd") == 0) {
      freg[int_of_register(ist.name[1])] =
	freg[int_of_register(ist.name[2])] +
	freg[int_of_register(ist.name[3])];

      ++cnt[15];
    }

    else if (strcmp(iname, "fsub") == 0) {
      freg[int_of_register(ist.name[1])] =
	freg[int_of_register(ist.name[2])] -
	freg[int_of_register(ist.name[3])];

      ++cnt[16];
    }

    else if (strcmp(iname, "fmul") == 0) {
      freg[int_of_register(ist.name[1])] =
	freg[int_of_register(ist.name[2])] *
	freg[int_of_register(ist.name[3])];

      ++cnt[17];
    }

    else if (strcmp(iname, "fdiv") == 0) {
      freg[int_of_register(ist.name[1])] =
	freg[int_of_register(ist.name[2])] /
	freg[int_of_register(ist.name[3])];

      ++cnt[18];
    }

    else if (strcmp(iname, "finv") == 0) {
      freg[int_of_register(ist.name[1])] =
	1.0 / freg[int_of_register(ist.name[2])];
      ++cnt[19];
    }

    else if (strcmp(iname, "fsqrt") == 0) {
      freg[int_of_register(ist.name[1])] =
	sqrt(freg[int_of_register(ist.name[2])]);
      ++cnt[20];
    }

    else if (strcmp(iname, "floor") == 0) {
      /*
	i = int_of_register(ist.name[1]);
	j = int_of_register(ist.name[2]);
	if (i == 2 && j == 2) {
	printf("%f\n", freg[2]);
	}
      */
      freg[int_of_register(ist.name[1])] =
	floor(freg[int_of_register(ist.name[2])]);
      ++cnt[21];
    }

    else if (strcmp(iname, "foi") == 0) {
      freg[int_of_register(ist.name[1])] =
	(float) (regist[int_of_register(ist.name[2])]);
      ++cnt[22];
    }

    else if (strcmp(iname, "iof") == 0) {
      a = (int) (freg[int_of_register(ist.name[2])]);
      b = ((float) a) + 0.5;
      if (freg[int_of_register(ist.name[2])] >= b) {
	++a;
      }
      //printf("%f %d\n", freg[int_of_register(ist.name[2])], a);
      regist[int_of_register(ist.name[1])] = a;
      ++cnt[23];
    }




    /*MEM ACSESS命令 */
    else if (strcmp(iname, "load") == 0) {
      //      fprintf(stderr, "%d\n", regist[4]);
      regist[int_of_register(ist.name[1])] =
	memory[regist[int_of_register(ist.name[2])]
	       + atoi(ist.name[3])].i;
      ++cnt[24];
    }

    else if (strcmp(iname, "store") == 0) {
      memory[regist[int_of_register(ist.name[2])] +
	     atoi(ist.name[3])].i =
	regist[int_of_register(ist.name[1])];

      if ((regist[int_of_register(ist.name[2])]
	   + atoi(ist.name[3])) == 6144) {
	//printf("store %d\n", regist[int_of_register(ist.name[1])]);
	//printf("at %d\n", pc);
      }
      ++cnt[25];
    }

    else if (strcmp(iname, "fload") == 0) {
      //    fprintf(stdout, "aa\n");
      //            fprintf(stdout, "%d\n", tmp.i = regist[int_of_register(ist.name[2])] + atoi(ist.name[3]));
      //    fprintf(stdout, "%f\n", tmp.d);
      freg[int_of_register(ist.name[1])] =
	memory[regist[int_of_register(ist.name[2])] +
	       atoi(ist.name[3])].d;
      //      fprintf(stdout, "bb\n");

      ++cnt[26];
    }

    else if (strcmp(iname, "fstore") == 0) {
      memory[regist[int_of_register(ist.name[2])] +
	     atoi(ist.name[3])].d =
	freg[int_of_register(ist.name[1])];

      ++cnt[27];
    }


    /*BRANCH命令 */
    else if (strcmp(iname, "beq") == 0) {
      if (regist[int_of_register(ist.name[1])] ==
	  regist[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }

      ++cnt[28];
    }

    else if (strcmp(iname, "bne") == 0) {
      if (regist[int_of_register(ist.name[1])] !=
	  regist[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }

      ++cnt[29];
    }

    else if (strcmp(iname, "bgt") == 0) {
      if (regist[int_of_register(ist.name[1])] >
	  regist[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }

      ++cnt[30];
    }

    else if (strcmp(iname, "blt") == 0) {
      if (regist[int_of_register(ist.name[1])] <
	  regist[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }

      ++cnt[31];
    }

    else if (strcmp(iname, "bge") == 0) {
      if (regist[int_of_register(ist.name[1])] >=
	  regist[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }

      ++cnt[32];
    }

    else if (strcmp(iname, "ble") == 0) {
      if (regist[int_of_register(ist.name[1])] <=
	  regist[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[33];

    } else if (strcmp(iname, "fbeq") == 0) {
      if (freg[int_of_register(ist.name[1])] ==
	  freg[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[34];

    }

    else if (strcmp(iname, "fbne") == 0) {
      if (freg[int_of_register(ist.name[1])] !=
	  freg[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[35];

    }

    else if (strcmp(iname, "fbgt") == 0) {
      if (freg[int_of_register(ist.name[1])] >
	  freg[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[36];

    }

    else if (strcmp(iname, "fblt") == 0) {
      if (freg[int_of_register(ist.name[1])] <
	  freg[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[37];

    }

    else if (strcmp(iname, "fbge") == 0) {
      if (freg[int_of_register(ist.name[1])] >=
	  freg[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[38];

    }

    else if (strcmp(iname, "fble") == 0) {
      if (freg[int_of_register(ist.name[1])] <=
	  freg[int_of_register(ist.name[2])]) {
	nextpc =
	  pc + label_trans_soutai(ist.name[3], program, pc) - 1;
      }
      ++cnt[39];

    }


    /*JUMP命令 */
    else if (strcmp(iname, "jump") == 0) {
      nextpc = label_trans(ist.name[1], program) - 1;

      ++cnt[40];
    }


    else if (strcmp(iname, "call") == 0) {
      nextpc = label_trans(ist.name[1], program) - 1;


      ++cnt[41];
      /*
	if (strncmp(ist.name[1], "L_sin", 5) == 0) {
	//printf("%f\n", freg[2]);
	freg[2] = sinf(freg[2]);
	nextpc = pc;
	} else if (strncmp(ist.name[1], "L_cos", 5) == 0) {
	//printf("%f\n", freg[2]);
	freg[2] = cosf(freg[2]);
	nextpc = pc;
      */
      /*} else */ if (strncmp(ist.name[1], "L_atan", 6) == 0) {
	//printf("%f\n", freg[2]);
	freg[2] = atanf(freg[2]);
	nextpc = pc;

      } else if (strncmp(ist.name[1], "L_sqrt", 6) == 0) {
	//printf("%f\n", freg[2]);
	freg[2] = sqrtf(freg[2]);
	nextpc = pc;

      } else if (strcmp(ist.name[1], "min_caml_read_int") == 0) {
	regist[4] = read_int();
	//printf("read_int: %d\n", regist[4]);
	nextpc = pc;
      } else if (strcmp(ist.name[1], "min_caml_read_float") == 0) {
	freg[2] = read_float();
	//printf("read_float: %f\n", freg[2]);
	nextpc = pc;
      } else {
	push(&call_stack, (pc + 1));
      }
    }


    else if (strcmp(iname, "return") == 0) {
      nextpc = pop(&call_stack) - 1;

    }
    //命令が存在しなかった場合error
    else {
      printf("Error:this is legal instruction!:%s\n",
	     program->insts[pc].name[0]);
      exit(1);
    }

    //printf("%d\n", regist[3]);

    nextpc++;
    /*
      if(count > 817757)
      {
      for(j = 0; j < program->label_count; ++j)
      {
      if(program->labels[j]->index == pc)
      {
      printf("%s:\n", program->labels[j]->name);
      }
      }

      printf("%d", pc);
      print_instruction(ist);
      //                                                                                                                                                                                                                                                      printf("%d\n", count);
      printf("%d\n", memory[4062].i);
      print_register();
      } */
    /*
      if((count % 1000000) == 0)
      {
      printf("%d\n", count);
      }
    */


    /*命令がラストの行まで行けば処理を終了する */
    if (nextpc >= program->inst_count) {
      break;
    }

  }

  for (i = 0; i < 100; ++i) {
    printf("%d: %d\n", i, cnt[i]);
  }



  return 0;
}
Exemplo n.º 10
0
int segmentImage(frame_t *frame, frame_t *res, int *largestLabel)  {
    //segment the image (label each connected component a different label)
    if (thresholdImage(frame, res) != 0) {
        printf("segmentImage: thresholdImage failure code\n");
        return 1;
    }

    // Segmentation code here - watershed
    //      START LABELS AT 2 (non-labeled remains at 0)
    int i, j, pValW, pValH, label = 2;
    int rWidth = res->image->width;
    int rHeight = res->image->height;
    
    pixel_t *P;
    int x, y;
    createStack();
    for (i = 0; i < rHeight; i++) {
        for (j = 0; j < rWidth; j++) {
        pValH = i;
        pValW = j;
        // Using pVal, we'll segment surrounding pixels with the same label.
        if (res->image->data[pValH*rWidth + pValW].L == 0) {
            // Pixel did not have a value
            //LOG_ERR("segmentImage: Continuing with seeds, pixel off at (w,h) -> (%d, %d)\n", pValW, pValH);
        } else if (res->image->data[pValH*rWidth + pValW].L > 1) {
            //LOG_ERR("segmentImage: Continuing with seeds, pixel already labeled at (w,h) -> (%d, %d)\n", pValW, pValH);
        } else {
            LOG_ERR("segmentImage: Labeling connected pixels starting at (w,h) -> (%d, %d)\n", pValW, pValH);
            // Add pixels to stack 
            push(&res->image->data[pValH * rWidth + pValW], pValW, pValH); 
            while(isEmpty() != 0) {
                P = pop(&x, &y);
                if (P->L == 1) {
                    P->L = label;
                    // Add neighboring pixels within the bounds to the stack
                    if (y-1 >= 0) {
                        if(res->image->data[(y-1)*rWidth+x].L == 1){
                            push(&res->image->data[(y-1)*rWidth+x], x, y-1);
                        }
                    }
                    if (y+1 < rHeight) {
                        if (res->image->data[(y+1)*rWidth+x].L == 1) {
                            push(&res->image->data[(y+1)*rWidth+x], x, y+1);
                        }   
                    }   
                    if (x-1 >= 0) {
                        if(res->image->data[y*rWidth+(x-1)].L == 1) {
                            push(&res->image->data[y*rWidth+(x-1)], x-1, y);
                        }
                    }
                    if (x+1 < rWidth) {
                        if(res->image->data[y*rWidth+(x+1)].L == 1) {
                            push(&res->image->data[y*rWidth+(x+1)], x+1, y);
                        }
                    }
                }
            }
        }

        label++;
        }
    }

// Other method of labelling pixels - sequential algo
#if 0
    // segment remaining pixels by looking for neighbor nearby or creating new label
    int val1, val2, val3, val4; 
    for (i = 0; i <rHeight; i++){
        for (j = 0; j < rWidth; j++) {
            val1 = 0;
            val2 = 0;
            val3 = 0;
            val4 = 0;
            // pixel has not been labelled yet
            if (res->image->data[i*rWidth+j].L == 1) {
                // give the current pixel the label of its neighbor or new label
                if (i-1 >= 0) 
                    val1 = res->image->data[(i-1)*rWidth+j].L;
                if (i+1 < rHeight)
                    val2 = res->image->data[(i+1)*rWidth+j].L;
                if (j-1 >= 0)
                    val3 = res->image->data[i*rWidth+(j-1)].L;
                if (j+1 < rWidth)
                    val4 = res->image->data[i*rWidth+(j+1)].L;
                if (val1 > 1){
                    res->image->data[i*rWidth+j].L = val1;
                } else if (val2 > 1) {
                    res->image->data[i*rWidth+j].L = val2;
                } else if (val3 > 1) {
                    res->image->data[i*rWidth+j].L = val3;
                } else if (val4 > 1) {
                    res->image->data[i*rWidth+j].L = val4;
                } else {
                    res->image->data[i*rWidth+j].L = label;
                    label++;
                }
            }
        }
    }
#endif

    *largestLabel = label;
    return 0;
}
Exemplo n.º 11
0
void Stack_Node::makeEmpty()
{
	while (!isEmpty())
		pop();
}
Exemplo n.º 12
0
void test_to_push_and_pop_together(){
	int value = 2;
	stack=create(2);
	ASSERT(push(stack, &value));
	ASSERT(&value ==  pop(stack));
}
Exemplo n.º 13
0
void test_to_pop_from_an_empty_stack(){
	stack=create(2);
	ASSERT(false==pop(stack));
}
Exemplo n.º 14
0
address InterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {

  // rbx,: methodOop
  // rcx: scratrch
  // r13: sender sp

  if (!InlineIntrinsics) return NULL; // Generate a vanilla entry

  address entry_point = __ pc();

  // These don't need a safepoint check because they aren't virtually
  // callable. We won't enter these intrinsics from compiled code.
  // If in the future we added an intrinsic which was virtually callable
  // we'd have to worry about how to safepoint so that this code is used.

  // mathematical functions inlined by compiler
  // (interpreter must provide identical implementation
  // in order to avoid monotonicity bugs when switching
  // from interpreter to compiler in the middle of some
  // computation)
  //
  // stack: [ ret adr ] <-- rsp
  //        [ lo(arg) ]
  //        [ hi(arg) ]
  //

  // Note: For JDK 1.2 StrictMath doesn't exist and Math.sin/cos/sqrt are
  //       native methods. Interpreter::method_kind(...) does a check for
  //       native methods first before checking for intrinsic methods and
  //       thus will never select this entry point. Make sure it is not
  //       called accidentally since the SharedRuntime entry points will
  //       not work for JDK 1.2.
  //
  // We no longer need to check for JDK 1.2 since it's EOL'ed.
  // The following check existed in pre 1.6 implementation,
  //    if (Universe::is_jdk12x_version()) {
  //      __ should_not_reach_here();
  //    }
  // Universe::is_jdk12x_version() always returns false since
  // the JDK version is not yet determined when this method is called.
  // This method is called during interpreter_init() whereas
  // JDK version is only determined when universe2_init() is called.

  // Note: For JDK 1.3 StrictMath exists and Math.sin/cos/sqrt are
  //       java methods.  Interpreter::method_kind(...) will select
  //       this entry point for the corresponding methods in JDK 1.3.
  // get argument

  if (kind == Interpreter::java_lang_math_sqrt) {
    __ sqrtsd(xmm0, Address(rsp, wordSize));
  } else {
    __ fld_d(Address(rsp, wordSize));
    switch (kind) {
      case Interpreter::java_lang_math_sin :
          __ trigfunc('s');
          break;
      case Interpreter::java_lang_math_cos :
          __ trigfunc('c');
          break;
      case Interpreter::java_lang_math_tan :
          __ trigfunc('t');
          break;
      case Interpreter::java_lang_math_abs:
          __ fabs();
          break;
      case Interpreter::java_lang_math_log:
          __ flog();
          break;
      case Interpreter::java_lang_math_log10:
          __ flog10();
          break;
      default                              :
          ShouldNotReachHere();
    }

    // return double result in xmm0 for interpreter and compilers.
    __ subptr(rsp, 2*wordSize);
    // Round to 64bit precision
    __ fstp_d(Address(rsp, 0));
    __ movdbl(xmm0, Address(rsp, 0));
    __ addptr(rsp, 2*wordSize);
  }


  __ pop(rax);
  __ mov(rsp, r13);
  __ jmp(rax);

  return entry_point;
}
Exemplo n.º 15
0
static void undefine(node s){
     assertpos(issym(s),s);
     /* if (debug) fprintf(stderr,"undefining %s\n",tostring(s)); */
     pop(s->body.symbol.name->body.unique_string.symbol_list);
     }
Exemplo n.º 16
0
int main() {

  char input[101]; // max input is 100 plus 1 for '\0' char
  bool done = false;

  initStack();

  while (!done) {
    // the "%100s" tells scanf we want no more than 100 chars
    if (scanf("%100s", input) == EOF) {
      // End of input, assume the user wants to exit
      done = true;
    } else if (isnumber(input)) {
      // push number on stack
      push(atoi(input));
    } else if (strcmp(input, "+") == 0) {
      // integer add
      if (length() < 2) {
        smallStackError(input);
      } else {
        push(pop() + pop());
      }
    } else if (strcmp(input, "-") == 0) {
      // integer subtract
      if (length() < 2) {
        smallStackError(input);
      } else {
        // subtract is non-commutative so we can't inline the pops
        int a = pop();
        int b = pop();
        push(b - a);
      }
    } else if (strcmp(input, "*") == 0) {
      // integer multiply
      if (length() < 2) {
        smallStackError(input);
      } else {
        push(pop() | pop());
      }
    } else if (strcmp(input, "/") == 0) {
      // integer divide
      if (length() < 2) {
        smallStackError(input);
      } else {
        // divide is non-commutative so we can't inline the pops
        int a = pop();
        int b = pop();
        if (a == 0) {
          printf("Ignoring illegal division by zero\n");
          push(b);
          push(a);
        } else {
          push(b / a);
        }
      }
    } else if (strcmp(input, "|") == 0) {
      // bitwise or
      if (length() < 2) {
        smallStackError(input);
      } else {
        push(pop() | pop());
      }
    } else if (strcmp(input, "p") == 0) {
      // print whole stack
      printStack(false);
      printStack(true);
      printf("\n");
    } else if (strcmp(input, "t") == 0) {
      // print top of stack
      if (length() < 1) {
        smallStackError(input);
      } else {
        printf("%d\n", peek());
      }
    } else if (strcmp(input, "w") == 0) {
      // wipe stack
      while (!isEmpty()) {
        pop();
      }
    } else if (strcmp(input, "q") == 0) {
      // quit
      done = true;
    } else {
      printf("Ignoring unknown command: %s\n", input);
    }
  }

  return 0;
}
Exemplo n.º 17
0
int main()
{
	int i,j,flag,curr,count,n;
	int **arr;
	FILE *ptr;
	top=-1;
	ptr=fopen("diji.txt","r");
	fscanf(ptr,"%d",&n);
	arr=(int**)malloc(sizeof(int)*n*n);
	for(i=0;i<n;i++)
		*(arr+i)=(int*)malloc(sizeof(int)*n);
		
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			{
				fscanf(ptr,"%d",&arr[i][j]);
			}
	}
	fclose(ptr);
	//disp
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			printf("%d   ",arr[i][j]);
		printf("\n");	
	}
	count=0;	
	printf("\nenter the source::\t");
	scanf("%d",&curr);
	curr--;
	while(1)
	{	flag=0;
		push(curr);
		printf("%c",65+curr);
		for(i=0;i<n;i++)
					arr[i][curr]=0;
		for(i=0;i<n;i++)
		{
				if(arr[curr][i]>0)
				{
					flag=1;
					break;
				}
		}
		if(flag==0)
		{
			pop();
			if(top==-1)
				exit(1);
			else
			{
				curr=stack[top];
				count++;
			}
		}
		else
		{
			curr=i;
			count++;
		}
		if(count==n)
			exit(1);
		if(count==n*n)
					exit(1);
	}
	return 0;	
}
Exemplo n.º 18
0
void merge(struct node *root1,struct node *root2)
{

  //Initializing
  struct snode *s1 = NULL;
  struct node *current1 = root1;

  struct snode *s2 = NULL;
  struct node *current2 = root2;


  //Base cases
  if(root1==NULL)
  {
    inorder(root2);
    return;
  }

  if(root2==NULL)
  {
    inorder(root1);
    return;
  }

  while(current1!= NULL || !isEmpty(s1) || current2!=NULL || !isEmpty(s2))
  {

    if(current1!=NULL || current2!=NULL)
    {
      if(current1!=NULL)
      {

        push(&s1,current1);
        current1 = current1->left;

      }

      if(current2!=NULL)
      {

        push(&s2,current2);
        current2 = current2->left;

      }
    }

    else {

      if(isEmpty(s1))
      {
        while(!isEmpty(s2))
        {
          current2 = pop(&s2);
          current2->left = NULL;
          inorder(current2);


        }
        return;
      }
      if(isEmpty(s2))
      {

        while(!isEmpty(s1))
        {
          current1 = pop(&s1);
          current1->left = NULL;
          inorder(current1);

        }
        return;


      }

      current1 = pop(&s1);
      current2 = pop(&s2);

      if(current1->data < current2->data)
      {
          printf("%d ",current1->data);
          current1=current1->right;
          push(&s2,current2);
          current2= NULL;

      }
      else {
        printf("%d ",current2->data);
          current2=current2->right;
          push(&s1,current1);
          current1= NULL;

      }

      }

    }

  }
Exemplo n.º 19
0
/*注意输入的时候,需要用空格作为间断*/
main()
{
    int type;
    double temp; /*储存弹出的临时数据*/
    char s[MAXOP];
    printf("----------RPN calculator----------\n");
    printf("*      use ctrl+z to exit        *\n");
    printf("*      use space to split        *\n");
    printf("----------------------------------\n");
    printf("please input:");

    while((type=getop(s))!=EOF)
    {
        switch (type)
        {
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop()+pop());
            break;
        case '*':
            push(pop()*pop());
            break;
        case '-':
            temp=pop();
            push(pop()-temp);
            break;
        case '/':
            temp=pop();
            if(temp!=0.0)
                push(pop()/temp);
            else
                printf("error:zero divisor\n");
            break;
        case '%':/*求模运算*/
            temp=pop();
            if(temp!=0.0)
                push(fmod(pop(),temp));
            else
                printf("error:zero divisor\n");
            break;
        case '\n':
            printf("result:\t%.8g\n",pop());
            break;
        default:
            printf("error:unknown command %s\n",s);
            break;
        }
    }
    return 0;
}
Exemplo n.º 20
0
/**
 * \brief Traverses the tree and calls t_coffee to produce the alignments
 * \param root The root of the tree.
 * \param vecs The vector set.
 * \param seq_set The sequence set.
 * \param out_f The output file.
 * \param n_core The number of cores to use (OPEN-MP for kmeans/fork in T-Coffee)
 * \param gapopen The gapopening costs
 * \param gapext The gapextension costs
 * \param method The method to use in the alignment
 */
void
traverse_km_tree(KM_node* root, int *vecs, const SeqSet *seq_set, char *out_f, int n_cores, int gapopen, int gapext, char *method)
{
	Stack *to_do =Stack_init();
	Node_pair *tmp = (Node_pair*)my_malloc(sizeof(Node_pair));
	tmp->node=root;
	tmp->id = 0;
	push(to_do, tmp);

	KM_node* current;
	size_t child;
	size_t j;
// 	size_t pos;
	char command[1000];
	while (to_do->size != 0)
	{

		current = ((Node_pair*)to_do->last)->node;
		child = ((Node_pair*)to_do->last)->id;
		if (current->n_children==0)
		{
			if(current->end-current->start > 2)
			{
				sprintf(command, "%li",current->id);
				write_files(current, vecs, seq_set, command);
// 				sprintf(command,"clustalo --guidetree-out co_tree.dnd -i %li --force >/dev/null 2>/dev/null", current->id);
// 				if (system(command))
// 				{
// 					printf("%s\n",command);
// 					exit(1);
// 				}
				if (current->id!=0)
					sprintf(command,"t_coffee -in %li -output fasta_aln -outfile %li.fa -n_core %i -gapopen %i -gapext %i -method %s -quiet >/dev/null 2>/dev/null", current->id, current->id, n_cores, gapopen, gapext, method);
				else
					sprintf(command,"t_coffee -in %li -output fasta_aln -outfile %s -n_core %i -gapopen %i -gapext %i -method %s -quiet >/dev/null 2>/dev/null ",  current->id, out_f, n_cores, gapopen, gapext, method);
				if (system(command))
				{
					printf("ERROR when running: %s\n",command);
					exit(1);
				}
			}
			else
				if(current->end-current->start > 1)
			{
				sprintf(command, "%li",current->id);
				write_files(current, vecs, seq_set, command);
				if (current->id!=0)
					sprintf(command,"t_coffee -in %li -output fasta_aln -outfile %li.fa -n_core %i -gapopen %i -gapext %i -method %s -quiet >/dev/null 2>/dev/null", current->id, current->id, n_cores, gapopen, gapext, method);
				else
					sprintf(command,"t_coffee -in %li -output fasta_aln -outfile %s -n_core %i -gapopen %i -gapext %i -method %s -quiet >/dev/null 2>/dev/null ",  current->id, out_f, n_cores, gapopen, gapext, method);

				printf("%s\n", command);
				if (system(command))
				{
					printf("ERROR when running: %s\n",command);
					exit(1);
				}
			}
			else
			{
				sprintf(command, "%li.fa",current->id);
				write_files(current, vecs, seq_set, command);
			}
			tmp =(Node_pair*) pop(to_do);
		}
		else
		{
			if (child == current->n_children)
			{
				if (current->id!=0)
					sprintf(command, "t_coffee -output fasta_aln -method %s -quiet -outfile %li.fa -n_core %i -gapopen %i -profile FILE::prf.fa ", method, current->id,n_cores, gapopen);
				else
					sprintf(command, "t_coffee -output fasta_aln -method %s -quiet -outfile %s -n_core %i -gapopen %i   -profile FILE::prf.fa ", method, out_f, n_cores, gapopen);
				FILE *prf_F = my_fopen("prf.fa", "w");
				for(j=0; j<current->n_children;++j)
				{
					fprintf(prf_F, "%li.fa\n", current->children[j]->id);
				}
				fclose(prf_F);

				printf("%s\n", command);
				if (system(command))
				{

					printf("ERROR when running: %s\n",command);
					exit(1);
				}
				pop(to_do);
			}
			else
			{
				++((Node_pair*)to_do->last)->id;
				tmp = (Node_pair*)my_malloc(sizeof(Node_pair));
				tmp->node=current->children[child];
				tmp->id = 0;
				push(to_do, tmp);
			}
		}
	}
	exit(1);
}
Exemplo n.º 21
0
main()
{
	int type;
	double op2;
	char s[MAXOP];

	while((type=getop(s))!=EOF){
		switch(type){
			case NUMBER:
					push(atof(s));
					break;
			case '+':
					push(pop()+pop());
					break;
			case '*':
					push(pop()*pop());
					break;
			case '-':
					op2=pop();
					push(pop()-op2);
					break;
			case '/':
					op2=pop();
					if(op2!=0.0)
						push(pop()/op2);
					else
						printf("error:zero divisor\n");
					break;
			case '%':
					op2=pop();
					push((long)pop()%(long)op2);
					break;
			case '\n':
					printf("\t%.8g\n",pop());
					break;
			default:
					printf("error:unknown command %s\n",s);
					break;
		}
	}
	return 0;
}
int main(void)
{
	int action, x, y, top, stack[LIMIT];
	top = 0;
	while(1)
	{
		printf("\n現在のデータ数:%d\n", top);
		printf("(1) push (2) pop (3) finish (4) CALC: ");
		scanf("%d", &action);

		switch(action)
		{
		case 1:
			printf("input data: ");
			scanf("%d", &x);
			if (push(stack, &top, x) == OVERFLOW) printf("stack overflow\n");
			break;

		case 2:
			x = pop(stack, &top);
			if (x == UNDERFLOW) printf("stack underflow\n");
			else printf("poped data: %d", x);
			break;

		case 3:
			return 0;

		case 4:
			x = pop(stack, &top);
			printf("CALC: ");
			getchar();
			y = getchar();
			switch(y)
			{
			case '+':
				push(stack, &top, pop(stack, &top) + x);
				break;

			case '-':
				push(stack, &top, pop(stack, &top) - x);
				break;

			case '*':
				push(stack, &top, pop(stack, &top) * x);
				break;

			case '/':
				push(stack, &top, pop(stack, &top) / x);
				break;

			case '%':
				push(stack, &top, pop(stack, &top) % x);
				break;

			default:
				printf("please input +, -, *, / or %%");
				push(stack, &top, x);
				break;
			}
			break;

		default:
			puts("please input 1, 2, 3 or 4");
			break;
		}
	}
}
Exemplo n.º 23
0
static Lisp_Object plusib(Lisp_Object a, Lisp_Object b)
/*
 * Add a fixnum to a bignum, returning a result as a fixnum or bignum
 * depending on its size.  This seems much nastier than one would have
 * hoped.
 */
{
    int32_t len = bignum_length(b)-CELL, i, sign = int_of_fixnum(a), s;
    Lisp_Object c, nil;
    len = len/4;         /* This is always 4 because even on a 64-bit */
                         /* machine where CELL=8 I use 4-byte B-digits */
    if (len == 1)
    {   int32_t t;
/*
 * Partly because it will be a common case and partly because it has
 * various special cases I have special purpose code to cope with
 * adding a fixnum to a one-word bignum.
 */
        s = (int32_t)bignum_digits(b)[0] + sign;
        t = s + s;
        if (top_bit_set(s ^ t))  /* needs to turn into two-word bignum */
        {   if (s < 0) return make_two_word_bignum(-1, clear_top_bit(s));
            else return make_two_word_bignum(0, s);
        }
        t = s & fix_mask;    /* Will it fit as a fixnum? */
        if (t == 0 || t == fix_mask) return fixnum_of_int(s);
        /* here the result is a one-word bignum */
        return make_one_word_bignum(s);
    }
/*
 * Now, after all the silly cases have been handled, I have a calculation
 * which seems set to give a multi-word result.  The result here can at
 * least never shrink to a fixnum since subtracting a fixnum can at
 * most shrink the length of a number by one word.  I omit the stack-
 * check here in the hope that code here never nests enough for trouble.
 */
    push(b);
    c = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+4*len);
    pop(b);
    errexit();
    s = bignum_digits(b)[0] + clear_top_bit(sign);
    bignum_digits(c)[0] = clear_top_bit(s);
    if (sign >= 0) sign = 0; else sign = 0x7fffffff; /* extend the sign */

    len--;
    for (i=1; i<len; i++)
    {   s = bignum_digits(b)[i] + sign + top_bit(s);
        bignum_digits(c)[i] = clear_top_bit(s);
    }
    /* Now just the most significant digit remains to be processed  */
    if (sign != 0) sign = -1;
    {   s = bignum_digits(b)[i] + sign + top_bit(s);
        if (!signed_overflow(s))         /* did it overflow? */
        {
/*
 * Here the most significant digit did not produce an overflow, but maybe
 * what we actually had was some cancellation and the MSD is now zero
 * or -1, so that the number should shrink...
 */
            if ((s == 0 && (bignum_digits(c)[i-1] & 0x40000000) == 0) ||
                (s == -1 && (bignum_digits(c)[i-1] & 0x40000000) != 0))
            {   /* shrink the number */
                numhdr(c) -= pack_hdrlength(1L);
                if (s == -1) bignum_digits(c)[i-1] |= ~0x7fffffff;
/*
 * Now sometimes the shrinkage will leave a padding word, sometimes it
 * will really allow me to save space. As a jolly joke with a 64-bit
 * system I need padding if there have been an odd number of (32-bit)
 * words of bignum data while with a 32-bit system the header word is
 * 32-bits wide and I need padding if there are ar even number of additional
 * data words.
 */
                if ((SIXTY_FOUR_BIT && ((i & 1) != 0)) ||
                    (!SIXTY_FOUR_BIT && ((i & 1) == 0)))
                {   bignum_digits(c)[i] = 0;   /* leave the unused word tidy */
                    return c;
                }
/*
 * Having shrunk the number I am leaving a doubleword of unallocated space
 * in the heap.  Dump a header word into it to make it look like an
 * 8-byte bignum since that will allow the garbage collector to handle it.
 * It I left it containing arbitrary junk I could wreck myself. The
 * make_bighdr(2L) makes a header for a number that fills 2 32-bit words
 * in all.
 */
                *(Header *)&bignum_digits(c)[i] = make_bighdr(2L);
                return c;
            }
            bignum_digits(c)[i] = s;  /* length unchanged */
            return c;
        }
/*
 * Here the result is one word longer than the input-bignum.
 * Once again SOMTIMES this will not involve allocating more store,
 * but just encroaching into the previously unused word that was padding
 * things out to a multiple of 8 bytes.
 */
        if ((SIXTY_FOUR_BIT && ((i & 1) == 0)) ||
            (!SIXTY_FOUR_BIT && ((i & 1) == 1)))
        {   bignum_digits(c)[i++] = clear_top_bit(s);
            bignum_digits(c)[i] = top_bit_set(s) ? -1 : 0;
            numhdr(c) += pack_hdrlength(1L);
            return c;
        }
        push(c);
/*
 * NB on the next line there is a +8. One +4 is because I had gone len--
 * somewhere earlier. The other +4 is to increase the length of the number
 * by one word.
 */
        b = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+8+4*len);
        pop(c);
        errexit();
        for (i=0; i<len; i++)
            bignum_digits(b)[i] = bignum_digits(c)[i];
/*
 * I move the top digit across by hand since if the number is negative
 * I must lost its top bit
 */
        bignum_digits(b)[i++] = clear_top_bit(s);
/* Now the one-word extension to the number */
        bignum_digits(b)[i++] = top_bit_set(s) ? -1 : 0;
/*
 * Finally because I know that I expanded into a new doubleword I should
 * tidy up the second word of the newly allocated pair.
 */
        bignum_digits(b)[i] = 0;
        return b;
    }
}
Exemplo n.º 24
0
/*--------------------------------------------------------------------*/
int ICACHE_FLASH_ATTR
jsonparse_next(struct jsonparse_state *state) {
	char c;
	char s;

	skip_ws(state);
	c = state->json[state->pos];
	s = jsonparse_get_type(state);
	state->pos++;

	switch (c) {
	case '{':
		push(state, c);
		return c;
	case '}':
		if (s == ':' && state->vtype != 0) {
			/*       printf("Popping vtype: '%c'\n", state->vtype); */
			pop(state);
			s = jsonparse_get_type(state);
		}
		if (s == '{') {
			pop(state);
		} else {
			state->error = JSON_ERROR_SYNTAX;
			return JSON_TYPE_ERROR;
		}
		return c;
	case ']':
		if (s == '[') {
			pop(state);
		} else {
			state->error = JSON_ERROR_UNEXPECTED_END_OF_ARRAY;
			return JSON_TYPE_ERROR;
		}
		return c;
	case ':':
		push(state, c);
		return c;
	case ',':
		/* if x:y ... , */
		if (s == ':' && state->vtype != 0) {
			pop(state);
		} else if (s == '[') {
			/* ok! */
		} else {
			state->error = JSON_ERROR_SYNTAX;
			return JSON_TYPE_ERROR;
		}
		return c;
	case '"':
		if (s == '{' || s == '[' || s == ':') {
			atomic(state, c = (s == '{' ? JSON_TYPE_PAIR_NAME : c));
		} else {
			state->error = JSON_ERROR_UNEXPECTED_STRING;
			return JSON_TYPE_ERROR;
		}
		return c;
	case '[':
		if (s == '{' || s == '[' || s == ':') {
			push(state, c);
		} else {
			state->error = JSON_ERROR_UNEXPECTED_ARRAY;
			return JSON_TYPE_ERROR;
		}
		return c;
	default:
		if (s == ':' || s == '[') {
			if (c <= '9' && c >= '0') {
				atomic(state, JSON_TYPE_NUMBER);
				return JSON_TYPE_NUMBER;
			}
		}
	}
	return 0;
}
Exemplo n.º 25
0
int main(){

	int input, eof, insuf_args = 1;
	int *previous;
	char *token;

	printf("RPN Calculator - Enter the expression to evaluate or Ctrl-D to quit.\n");

prompt:
	for(; fgets(line, LINE_SIZE, stdin)!= NULL; insuf_args = 1, top = stack) {
		line[strlen(line) - 1] = '\0';		/* cut the \n from the string */
		if((token = strtok (line, " " "\t")) == NULL) {
			continue;
		}

		eof = sscanf(token, "%d", &input);	/* eof = 1 means there was a number in the token */

		if(eof)
			push(input);
		else{
			printf("Weird Arguments!\n");
			goto prompt;
		}

		while ((token = strtok (NULL, " " "\t")) != NULL) {
			eof = sscanf(token, "%d", &input);

			if(eof){
				insuf_args = 0;
				previous = top;
				push(input);
			}
			else{
				if(!insuf_args){
					if (!(strcmp (token, "+")))
						*previous += *top;
					else if (!(strcmp (token, "-")))
						*previous -= *top;
					else if (!(strcmp (token, "*")))
						*previous *= *top;
					else if (!(strcmp (token, "/"))){
						if(*top)
							*previous /= *top;
						else{
							printf("Division by Zero!\n");
							goto prompt;
						}
					}
					else{
						printf("Wrong Operator!\n");
						goto prompt;
					}
					pop();
					if(previous != base)
						previous--;
				}
			}
		}

		if(insuf_args || previous != base || top != previous)
			printf("Malformed Expression!\n");
		else if(under_or_overflow)
			under_or_overflow = 0;
		else
			printf("Result : %d\n", *previous);
	}

	return 0;
}
void
base_quicksort7(unsigned int a[], int N)
{
    int l, r;
    int i;
    int m;
    int il, ir; /* names follow pl, pm, and pn from bently/mcilroy. used ir instead of in */

    stackinit(N);

    describe_predictor(&global_predictor[0], "i");
    describe_predictor(&global_predictor[1], "j");
    describe_predictor(&global_predictor[2], "partition end");
    describe_predictor(&global_predictor[3], "insertion");
    describe_predictor(&global_predictor[4], "median");
    /*	describe_predictor(&global_predictor[4], "median of 7 ab"); */
    describe_predictor(&global_predictor[5], "median of 7 bc");
    describe_predictor(&global_predictor[6], "median of 7 ac");
    describe_predictor(&global_predictor[7], "median of 7 cb");
    describe_predictor(&global_predictor[8], "median of 7 ca");
    describe_predictor(&global_predictor[9], "median of 7 ab2");
    describe_predictor(&global_predictor[10], "median of 7 bc2");
    describe_predictor(&global_predictor[11], "median of 7 ac2");
    describe_predictor(&global_predictor[12], "median of 7 cb2");
    describe_predictor(&global_predictor[13], "median of 7 ca2");
    describe_predictor(&global_predictor[14], "median of 3 cmp1");
    describe_predictor(&global_predictor[15], "median of 3 cmp2");
    describe_predictor(&global_predictor[16], "median of 3 cmp3");


    r = N-1;
    l = 0;

    while(1)
    {
        int n = r - l;
        int n6 = n/6;
        int n3 = n/3;
        if (r - l <= THRESHHOLD)
        {
            if (stackempty())
                break;

            l = pop();
            r = pop();
            continue;
        }

        /* pseudo - Median of 7 partitioning*/
        m = (l+r)/2;
        if (n > 40)
        {

            il = med3(a, l, l + n6, l + n3);

            /* the 2 is for seperate branch predictors, as it's inlined */
            ir = med3_2(a, r - n3, r - n6, r);

            exch(a[l], a[il]);
            exch(a[r], a[ir]);
            exch(a[m], a[r-1]);
        }


        pred_compexch(a[l], a[r-1], 14);
        pred_compexch(a[l], a[r], 15);
        pred_compexch(a[r-1], a[r], 16);

        i = partition(a,l+1,r-1);

        /* here is the bug */
        /* then key is being copied more times than necessary. the reason for this is that it is not being removed when it is taken as the key */
        /* instead, it is being put in place more than once */
        /* example: i == 1, j == 10; key = a[1]; key < pivot, so key is swapped with a[2], the key is now in a[1] and a[2]. uh oh  */
        if (i-l > r-i)
        {
            push(i-1,l);
            l = i+1;
        }
        else
        {
            push(r,i+1);
            r = i-1;
        }
    }

    stackclear();
    /* the +1 isnt immediately obvious. its because THRESHHOLD is the difference between l and r up above */
    if (2*THRESHHOLD > N) insertion_sentinel(a,N);
    else insertion_sentinel(a,2*THRESHHOLD);

    insertion(a, N);

    /* add the predictors up */
    add_predictor(&global_predictor[4], &global_predictor[5]);
    add_predictor(&global_predictor[4], &global_predictor[6]);
    add_predictor(&global_predictor[4], &global_predictor[7]);
    add_predictor(&global_predictor[4], &global_predictor[8]);
    add_predictor(&global_predictor[4], &global_predictor[9]);
    add_predictor(&global_predictor[4], &global_predictor[10]);
    add_predictor(&global_predictor[4], &global_predictor[11]);
    add_predictor(&global_predictor[4], &global_predictor[12]);
    add_predictor(&global_predictor[4], &global_predictor[13]);
    add_predictor(&global_predictor[4], &global_predictor[14]);
    add_predictor(&global_predictor[4], &global_predictor[15]);
    add_predictor(&global_predictor[4], &global_predictor[16]);
    init_predictor(&global_predictor[5]);
    init_predictor(&global_predictor[6]);
    init_predictor(&global_predictor[7]);
    init_predictor(&global_predictor[8]);
    init_predictor(&global_predictor[9]);
    init_predictor(&global_predictor[10]);
    init_predictor(&global_predictor[11]);
    init_predictor(&global_predictor[12]);
    init_predictor(&global_predictor[13]);
    init_predictor(&global_predictor[14]);
    init_predictor(&global_predictor[15]);
    init_predictor(&global_predictor[16]);
}
Exemplo n.º 27
0
uint8_t PinMock::next_analog_value(uint8_t pin) {
  return pop(analog_values[pin]);
}
Exemplo n.º 28
0
/*
 * Tree Depth First Search implementation
 * Implements iterative search using a Stack
 * Checks for a match before adding a node to Stack
 *
 * PARAMS
 * ------
 * root: pointer to the root node pointer of a tree
 * val : value to search
 * stop: stop if val is found
 */
bool search_DFS(tree_pp root, int val, bool stop)
{
	int ret = false;
	tree_p node = NULL;
	stack_p stack = NULL;

	if (!root || !*root) {
		log(ERROR, "tree or root node is NULL.\n");
		return false;
	}

	/* Check for a match in root node */
	node = *root;
	if (node->data == val) {
		log(INFO, "FOUND %d\n", val);

		if (stop)
			return true;
	}

	stack = get_stack();

	/* Add root node to Stack */
	if (!push(stack, *root)) {
		log(ERROR, "push failed!\n");
		destroy_stack(stack);
		return false;
	}

	/* Process all valid nodes */
	while (node) {
		log(INFO, "tracking...\n");

		/* Match and add complete
		   left subtree to Stack */
		while (node->left) {
			if (node->left->data == val) {
				log(INFO, "FOUND %d\n", val);
				ret = true;

				if (stop) {
					node = NULL;
					break;
				}
			}

			/* Add node to stack */
			if (!push(stack, node->left)) {
				log(ERROR, "push failed!\n");
				destroy_stack(stack);
				return false;
			}

			node = node->left;
		}

		while ((node = pop(stack)) != NULL) {
			/* Process right child of node */
			if (node->right) {
				if (node->right->data == val) {
					log(INFO, "FOUND %d\n", val);
					ret = true;

					if (stop) {
						node = NULL;
						break;
					}
				}

				/* Add right child to Stack */
				if (!push(stack, node->right)) {
					log(ERROR, "push failed!\n");
					destroy_stack(stack);
					return false;
				}

				node = node->right;
				/* Break inner loop if there's a right node */
				break;
			}
		}
	}

	/* Report if no match was found */
	if (!ret)
		log(INFO, "NOT FOUND\n");

	destroy_stack(stack);

	return ret;
}
Exemplo n.º 29
0
int main()
{
    int type;
    double op1, op2;
    char s[MAXOP];

    while ((type = getop(s)) != EOF) {
        switch (type) {
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop() + pop());
            break;
        case '*':
            push(pop() * pop());
            break;
        case '-':
            op2 = pop();
            op1 = pop();
            if (op1 == 0.0)
                push(op2 * -1);
            else
                push(op1 - op2);
            break;
        case '/':
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("error: zero divisor\n");
            break;
        case '%':
            op2 = pop();
            push(fmod(pop(), op2));
            break;
        case 'c':
            op2 = pop();
            op1 = pop();
/**********************************************/
            v = op2;
/**********************************************/
            printf("%f\n", op2);
            push(op2);
            push(op1);
            break;
        case 's':
            push(sin(pop())); 
            break;
        case 'e':
            push(exp(pop()));
            break;
        case 'p':
            op2 = pop();
            push(pow(pop(), op2));
            break;
/*****************************************************/
        case 'v':
            push(v);
            break;
        case '\n':
            v = pop();
            printf("\t%.8g\n", v);
            break;
/*****************************************************/
        default:
            printf("error: unknown command %s\n", s);
            break;
        }
    }
    return 0;
}
Exemplo n.º 30
0
int main(void)
{
	itemS_t* file = NULL;
	STACK_t* myFiles = NULL;
	char userInput = '\0';
	bool success = false;
	scanf(" %c", &userInput);

	while(userInput != 'q')
	{
		file = NULL;
		myFiles = NULL;
		
		switch(userInput)
		{
			success = false;
			case '1':
				myFiles = search("", false, false);
				break;
			case '2':
				myFiles = search("", false, true);
				break;
			case '3':
				myFiles = search("..", true, false);
				break;
			case '4':
				myFiles = search("..", true, true);
				break;
			case '5':
				myFiles = search("../../../../..", true, false);
				break;
			case '6': 
				myFiles = search("EmptyFolder", false, false);
				break;
			case '7': 
				myFiles = search("/", false, false);
			default:
				break;
		}
		
		if(myFiles != NULL)
		{
			file = pop(myFiles);
			success = true;
		}
		else file = NULL;
		while(file != NULL)
		{
			free(file->keyValue);
			free(file);
			file = pop(myFiles);
		}
		free(myFiles);

		success ? printf("Success!\n") : printf("Fail!\n");
		
		scanf(" %c", &userInput);
	}
  
	return 0;
}