Ejemplo n.º 1
0
void infixtoprefix(char a[],char x[])
    {
    char y[1000];
    int i,n=strlen(a),topx=-1,topy=-1;
    for(i=n-1;i>=0;i--)
        {
        if((a[i]>=48&&a[i]<=57)||(a[i]>=65&&a[i]<=90)||(a[i]>=97&&a[i]<=122))
            {
            pushc(y,&topy,a[i]);
        }
        else
            {
                while((topx!=-1)&&(priority(pushtopc(x,&topx))>priority(a[i])))
                    {
                    pushc(y,&topy,popc(x,&topx));
                }
               pushc(x,&topx,a[i]);
        }
    }
    while(topx!=-1)
            {
            pushc(y,&topy,popc(x,&topx));
        }
   while(topy!=-1)
       {
       pushc(x,&topx,popc(y,&topy));
   }
   pushc(x,&topx,'\0');
}
Ejemplo n.º 2
0
void add(struct oper op, char finish_rem, int curline)
{
	if (finish_rem == 'f')
	{
		if (op.type == LEFT)
			pusho(&o, op);
		else if (sizeo(&o) == 0)
			pusho(&o, op);
		else
		{
			while (sizeo(&o) > 0 && topo(&o).type != LEFT && priority[topo(&o).type] >= priority[op.type])
			{
				struct command* newcom = (struct command*)malloc(sizeof(struct command));
				newcom->input = newcom->output = NULL;
				newcom->type = types[topo(&o).type];
				if (sizec(&s) < 2)
					printError(curline);
				newcom->u.command[1] = topc(&s); popc(&s);
				newcom->u.command[0] = topc(&s); popc(&s);
				pushc(&s, newcom);
				popo(&o);
			}
			if (op.type == RIGHT && topo(&o).type == LEFT)
				popo(&o);
			else
				pusho(&o, op);
			if (op.type == RIGHT)
			{
				struct command* ncom = (struct command*)malloc(sizeof(struct command));
				ncom->input = ncom->output = NULL;
				ncom->type = SUBSHELL_COMMAND;
				ncom->u.subshell_command = topc(&s); popc(&s);
				pushc(&s, ncom);
			}
		}
	}
	else if (finish_rem == 't')
	{
		while (sizeo(&o) > 0)
		{
			struct command* newcom = (struct command*)malloc(sizeof(struct command));
			newcom->input = newcom->output = NULL;
			newcom->type = types[topo(&o).type];
			if (sizec(&s) < 2)
				printError(curline);
			newcom->u.command[1] = topc(&s); popc(&s);
			newcom->u.command[0] = topc(&s); popc(&s);
			pushc(&s, newcom);
			popo(&o);
		}

	}
}
Ejemplo n.º 3
0
unsigned get_arg(void)
{
    SCRATCH int c;
    SCRATCH unsigned a, u;

    a = NUM;  u = 0;  bad = FALSE;
    switch (lex() -> attr & TYPE) {
	case SEP:   exp_error('S');  a = NONE;  break;

	case EOL:   unlex();  a = NONE;  break;

	case REG:   a = token.valu;  break;

	case OPR:   if (token.valu == '(') {
			if ((lex() -> attr & TYPE) == REG) {
			    a = token.valu;  lex();
			    switch (a) {
				case C:	    a = C_IND;  break;

				case BC:
				case DE:
				case SP:    a += SP_IND - SP;  break;

				case HL:    a = HL_IND;  break;

				case IX:    a = IX_IND;  goto do_ix_ind;

				case IY:    a = IY_IND;
do_ix_ind:				    if ((token.attr & TYPE) == OPR &&
						(token.valu == '+' ||
						token.valu == '-')) {
						unlex();
						if ((u = eval(LPREN)) > 0x7f
						    && u < 0xff80)
						    exp_error('V');
					    }
					    break;

				default:    exp_error('R');  break;
			    }
			    if ((token.attr & TYPE) != OPR ||
				token.valu != ')') exp_error(')');
			    break;
			}
			else {
			    unlex();  u = eval(LPREN);
			    trash();  pushc(c = popc());
			    if (c == ',' || c == '\n') {
				a = NUM_IND;  break;
			    }
			    token.attr = VAL;  token.valu = u;
			}
		    }

	case VAL:
	case STR:   unlex();  u = eval(START);  unlex();  break;
    }
    arg.valu = bad ? 0 : u;  return arg.attr = a;
}
Ejemplo n.º 4
0
void pof_pf()
{
char postfix[20],prefix[20],ch[2],temp1[2],temp2[2];	
int i,len;	
	printf("Enter the postfix expression: ");
	scanf("%s",postfix);
	len=strlen(postfix);
	prefix[0]='\0';
	for(i=0;i<len;i++)
	{
	
		ch[0]=postfix[i];
		ch[1]='\0';
		switch(ch[0])
		{
			case '+':
			case '-':
			case '*':
			case '/':
			case '$':
				      temp1[0]=popc();
				      temp2[0]=popc();
				      temp1[1]='\0';
				      temp2[1]='\0';
				    strcat(prefix,ch);
				    //printf("%s",prefix);
					strcat(prefix,temp2);
					strcat(prefix,temp1);  
				    break;
			default:
			      pushc(ch[0]);
			      break;
		}
	}
	printf("The prefix expression is : %s",prefix);
	
}
Ejemplo n.º 5
0
int 
main(unsigned long long id) {
  vector unsigned int x = get_vector_param_3();
  vector unsigned int count  = (vector unsigned int){0,0,0,0};
  vector unsigned int result = (vector unsigned int){0,0,0,0};

  spu_ready();

  count  = popc(x);
  result = reduce_word(count);
  
  spu_write_out_mbox(spu_extract(result, 0));

  return SPU_SUCCESS;
}
Ejemplo n.º 6
0
inline size_t count_if_with_ballot(InputIterator first,
                                   InputIterator last,
                                   Predicate predicate,
                                   command_queue &queue)
{
    size_t count = iterator_range_size(first, last);
    size_t block_size = 32;
    size_t block_count = count / block_size;
    if(block_count * block_size != count){
        block_count++;
    }

    const ::boost::compute::context &context = queue.get_context();

    ::boost::compute::vector<uint_> counts(block_count, context);

    ::boost::compute::detail::nvidia_popcount<uint_> popc;
    ::boost::compute::detail::nvidia_ballot<uint_> ballot;

    meta_kernel k("count_if_with_ballot");
    k <<
        "const uint gid = get_global_id(0);\n" <<

        "bool value = false;\n" <<
        "if(gid < count)\n" <<
        "    value = " << predicate(first[k.var<const uint_>("gid")]) << ";\n" <<

        "uint bits = " << ballot(k.var<const uint_>("value")) << ";\n" <<

        "if(get_local_id(0) == 0)\n" <<
            counts.begin()[k.var<uint_>("get_group_id(0)") ]
                << " = " << popc(k.var<uint_>("bits")) << ";\n";

    k.add_set_arg<const uint_>("count", count);

    k.exec_1d(queue, 0, block_size * block_count, block_size);

    uint_ result;
    ::boost::compute::reduce(
        counts.begin(),
        counts.end(),
        &result,
        queue
    );
    return result;
}
Ejemplo n.º 7
0
void toBack(char str[]){
    int stillint = 0;
    int str_len = getlength(str);
    int num = 0;
    int i = 0;
    for(i = 0; i < str_len; i++){
        char c = str[i];
        if(isint(c)){
            if(stillint){
                if((i+1) != str_len){
                    if(isint(str[i+1])){
                        poly_exp[num] = c;
                        num++;
                    }
                    else{
                        stillint = 0;
                        poly_exp[num] = c;
                        num++;
                        poly_exp[num] = '~';
                        num++;
                    }
                }
                else{
                    poly_exp[num] = c;
                    num++;
                }
            }
            else{
                poly_exp[num] = c;
                num++;
                stillint = 1;
            }
        }
        else{
            if(stillint){
                poly_exp[num] = '~';
                num++;
                stillint = 0;
            }
            if(c == '('){
                pushc(c);
            }else{
                if(c == ')'){
                    while(ctop() != '('){
                        poly_exp[num] = ctop();
                        num++;
                        popc();
                    }
                    popc();
                }else{
                    if(empytc()){
                        pushc(c);
                    }else{
                        if(getType(c) > getType(ctop())){
                            pushc(c);
                        }else{
                            while((1 - empytc()) && getType(c) <= getType(ctop())){
                                poly_exp[num] = popc();
                                num++;
                            }
                            pushc(c);
                        }
                    }
                }
            }
        }
    }
    while(empytc() == 0){
        poly_exp[num] = ctop();
        num++;
        popc();
    }
}
Ejemplo n.º 8
0
int main()
{
	char infx[MAX];
	int c, j, i, a, b;
	start:
        printf("\n1.Convert Infix to Postfix\n2.Evaluate Postfix\n3.Exit\n : ");
        scanf("%d", &c);
        if(c == 1)
	{
		top = topc = -1;
		pofx[0] = '\0';
		printf("\nEnter an infix expression : ");
		scanf("%s", infx);
		strcat(infx, ")");
		pushc('(');
		for(i = 0; infx[i] != '\0'; i++)
		{
			if(infx[i] == '(')
				pushc('(');
			else if(isalnum(infx[i]))
			{
				pofx[j = strlen(pofx)] = infx[i];
				pofx[j + 1] = '\0';
			}
			else if(infx[i] == ')')
			{
				while(peepc() != '(')
				{
					pofx[j = strlen(pofx)] = popc();
					pofx[j+ 1] = '\0';
				}
				popc();
			}
			else if(ispunct(infx[i]))
			{
				while(prec(peepc(), infx[i]) >= 0)
				{
					pofx[j = strlen(pofx)] = popc();
     	                        	pofx[j+ 1] = '\0';
				}
				pushc(infx[i]);
			}
		}
		printf("\nPostfix Expression is : ");
		for(i = 0; pofx[i] != '\0' ;i++)
			printf("%c ", pofx[i]);
	}
	else if(c == 2)
	{
		top = topc = -1;
		printf("\nEnter Postfix expression : ");
		scanf("%s", pofx);
		strcat(pofx, "#");
		for(i = 0; pofx[i] != '#'; i++)
		{
			if(isdigit(pofx[i]))
				push(pofx[i] - '0');
			else
			{
				a = pop();
				b = pop();
				if(pofx[i] == '+')
					push(b + a);
				else if(pofx[i] == '-')
					push(b - a);
				else if(pofx[i] == '*')
					push(b * a);
				else if(pofx[i] == '/')
					push(b / a);
				else if(pofx[i] == '^')
					push(pow(b, a));
			}
		}
		printf("\nAnswer is : %d\n", pop());
	}
	else if(c == 3)
		return 0;
	else
		printf("\nWrong Choice!\n");
	goto start;
}
Ejemplo n.º 9
0
command_stream_t
make_command_stream(int(*getbyte) (void *), void *arg)
{
  initStackc(&s);
  initStacko(&o);
  struct command_stream *command_stream_t = (struct command_stream *)malloc(sizeof(struct command_stream));
  initStream(command_stream_t);
  struct command*com = (struct command*)malloc(sizeof(struct command));
  com->type = SIMPLE_COMMAND;
  com->input = com->output = NULL;
  struct oper o;
  char c;
  bool extra = false;
  bool done = false;
  int csize = 10;
  com->u.word = (char**)malloc(10 * sizeof(char*));
  int count = 0;
  int length = 0;
  int mlength = 12;
  char* word = (char*)malloc(12 * sizeof(char));
  c = getbyte(arg);
  int curline = 1;
  skipws(getbyte, arg, &c, &curline);
  while (c >= 0)
    {
      done = false;
      if (!isValidChar(c))
	printError(curline);
      extra = false;
      if (isvalid(c))
	{
	  word[length] = c;
	  length++;
	  if (length >= mlength)
	    {
	      mlength += 5;
	      word = (char*)realloc(word, mlength*sizeof(char));
	    }
	}
      else
	{
	  if (c == '#')
	    {
	      while ((c = (char)getbyte(arg)) >= 0 && c != '\n');
	    }
	  else if (c == '\\')
	    {
	      while (c == '\\')
		{
		  c = (char)getbyte(arg);
		  c = (char)getbyte(arg);
		}
	      continue;
	    }
	  else if (c == ' ')
	    {
	      if (length > 0)
		{
		  addWord(&com, &count, &length, word);
		  if (count == csize - 1)
		    {
		      csize += 5;
		      com->u.word = (char**)realloc(com->u.word, csize*sizeof(int));
		    }
		}
	      else
		{
		  c = (char)getbyte(arg);
		  continue;
		}
	    }

	  else if (c == '|' || c == '&' || c == ';' || c == '\n' || c == '(' || c == ')')
	    {
	      /*if (com->type == SUBSHELL_COMMAND)
		{
		com = (struct command*)malloc(sizeof(struct command));
		com->type = SIMPLE_COMMAND;
		com->u.word = (char**)malloc(10 * sizeof(char*));
		com->input = com->output = NULL;
		count = 0;
		csize = 10;
		}
	      */
	      addWord(&com, &count, &length, word);
	      if (c == '|')
		{
		  c = (char)getbyte(arg);
		  if (c == '|')
		    {
		      o.type = OR;
		    }
		  else
		    {
		      o.type = PIPE;
		    }
		}
	      else if (c == '&')
		{
		  o.type = AND;
		  c = (char)getbyte(arg);
		  if (c != '&')
		    printError(curline);
		}
	      else if (c == ';')
		{
		  o.type = SEQ;
		  c = (char)getbyte(arg);
		}
	      else if (c == '(')
		{
		  o.type = LEFT;
		}
	      else if (c == ')')
		{
		  o.type = RIGHT;
		}
	      else if (c == '\n')
		{
		  curline += 1;
		  while ((c = (char)getbyte(arg)) == ' ');
		  if (c < 0) break;
		  while (c == '\\')
		    {
		      c = (char)getbyte(arg);
		      c = (char)getbyte(arg);
		    }
		  if (c == '#')
		    {
		      while ((c = getbyte(arg)) >= 0 && c != '\n');
		    }      
		  if (c != '\n' && (com->u.word != NULL || com->u.subshell_command != NULL))
		    {
		      o.type = SEQ;
		      extra = true;
		    }
		  else
		    {
		      extra = false;
		      if (count > 0)
			{
			  com->u.word[count] = '\0';
			  pushc(&s, com);
			}
		      o.type = SEQ;
		      add(o, 't', curline);
		      insert(command_stream_t, topc(&s));
		      popc(&s);
		      done = true;
		      reset(&com, &word, &length, &count, &csize, &mlength, 'f');
		      c = (char)getbyte(arg);
		      skipws(getbyte, arg, &c, &curline);
		      continue;
		    }
		}
	    
	  if (o.type != PIPE && o.type != SEQ)
	    {
	      c = (char)getbyte(arg);
	    }
	  if (count > 0)
	    {
	      com->u.word[count] = '\0';
	      pushc(&s, com);
	    }
	  add(o, 'f', curline);
	  if (o.type == RIGHT)
	    reset(&com, &word, &length, &count, &csize, &mlength, 't');
	  else
	    reset(&com, &word, &length, &count, &csize, &mlength, 'f');
	  if (o.type != RIGHT)
	    {
	      skipws(getbyte, arg, &c,&curline);
	    }
	  extra = true;
	}
      else if (c == '>' || c == '<')
	{
	  char* ptr;
	  int insize = 12;
	  ptr = (char*)malloc(insize * sizeof(char));
	  int i = 0;
	  char t = c;
	  while ((c = (char)getbyte(arg)) == ' ');
	  if (!isvalid(c))
	    printError(curline);
	  while ((c >= 0))
	    {
	      if (!isvalid(c))
		{
		  extra = true;
		  break;
		}
	      ptr[i] = c;
	      i++;
	      if (i == insize - 1)
		{
		  insize += 5;
		  ptr = (char*)realloc(ptr, insize*sizeof(char));
		}
	      c = (char)getbyte(arg);
	    }
	  ptr[i] = '\0';
	  if (t == '<')
	    com->input = ptr;
	  else com->output = ptr;
	}
    }

  if (!extra)
    c = (char)getbyte(arg);
}
if (!done)
  {
    addWord(&com, &count, &length, word);
    if (count > 0)
      {
	com->u.word[count] = '\0';
	pushc(&s, com);
      }
    o.type = SEQ;
    add(o, 't', curline);
    if (sizec(&s) != 1)
      printError(curline);
    insert(command_stream_t, topc(&s));
    popc(&s);
  }
return command_stream_t;

}
Ejemplo n.º 10
0
int
proc_content2str(core_content_t content, char *buf, size_t size)
{
	int nonecnt, defcnt, allcnt;
	core_content_t mask, bit;
	int first;
	uint_t index;
	size_t n, tot = 0;

	if (content == 0)
		return ((int)strlcpy(buf, "none", size));

	if (content & ~CC_CONTENT_ALL)
		return ((int)strlcpy(buf, "<invalid>", size));

	nonecnt = popc(content);
	defcnt = 1 + popc(content ^ CC_CONTENT_DEFAULT);
	allcnt = 1 + popc(content ^ CC_CONTENT_ALL);

	if (defcnt <= nonecnt && defcnt <= allcnt) {
		mask = content ^ CC_CONTENT_DEFAULT;
		first = 0;
		tot += (n = strlcpy(buf, "default", size));
		if (n > size)
			n = size;
		buf += n;
		size -= n;
	} else if (allcnt < nonecnt) {
		mask = content ^ CC_CONTENT_ALL;
		first = 0;
		tot += (n = strlcpy(buf, "all", size));
		if (n > size)
			n = size;
		buf += n;
		size -= n;
	} else {
		mask = content;
		first = 1;
	}

	while (mask != 0) {
		bit = mask ^ (mask & (mask - 1));

		if (!first) {
			if (size > 1) {
				*buf = (bit & content) ? '+' : '-';
				buf++;
				size--;
			}

			tot++;
		}
		index = popc(bit - 1);
		tot += (n = strlcpy(buf, content_str[index], size));
		if (n > size)
			n = size;
		buf += n;
		size -= n;

		mask ^= bit;
		first = 0;
	}

	return ((int)tot);
}