Ejemplo n.º 1
0
/* compares operator priority for infix mode */
int higher_priority(int i, int j)
{
     int p1, p2;
     p1=op_priority(i); p2=op_priority(j);
     if (p1>p2) return 1 ;
     else if (p1<p2) return -1 ;
     else return 0;
}
Ejemplo n.º 2
0
Archivo: expr.c Proyecto: ShijianXu/ICS
int find_domiop(int p,int q)
{
	int domiop_ip=0;
	int count=0;
	int i;
	for(i=p;i<=q;i++)
	 {
		if(tokens[i].type==VAR || tokens[i].type==NUMBER || tokens[i].type==HEX || tokens[i].type==REG)
			continue;
		else if(tokens[i].type=='(')
			count++;
		else if(tokens[i].type==')')
			count--;
		else if(domiop_ip==0 && count==0)
			domiop_ip=i;
		else if(op_priority(i)>=op_priority(domiop_ip) && count==0)
				domiop_ip=i;
	} 
	return domiop_ip;
}
Ejemplo n.º 3
0
int main(int argc, char** argv)
{
	/*
	 * These counters will be used as offsets for messages when using pipes
	 * i.e. write(stack + stack_pos, strlen(stack + stack_pos))
	 */
	int stack_pos = 0;
	int input_pos = 1;

	char input[MAX_BUF_SIZE] = "";
	char stack[MAX_BUF_SIZE] = "";
	char result[MAX_BUF_SIZE] = "";

	fgets(input, MAX_BUF_SIZE - 1, stdin);
	fgets(stack, MAX_BUF_SIZE - 1, stdin);
	fgets(result, MAX_BUF_SIZE - 1, stdin);

	input[strcspn(input, "\n")] = 0;
	stack[strcspn(stack, "\n")] = 0;
	result[strcspn(result, "\n")] = 0;

	if ((strlen(input) == 0)) // End of input, clearing stack
	{
		while (stack_pos < strlen(stack))
		{
				write_to_result(result, ' ');
				write_to_result(result, stack[stack_pos++]);
		}

		if (debug)
			fprintf(stderr, "pid: %d end of input! result: %s \n",
				getpid(), result
			);

		printf("%s", result);
		return 0;
	}

	if (debug)
		fprintf(stderr, "pid: %d input[0]: %c stack: %s result: %s \n",
			getpid(), input[0], stack, result
		);

	switch (input[0])
	{
		case '-': /* Odp.: Przez liczbę całkowitą należy rozumież liczbę całkowitą bez znaku */
		case '^':
		case '*':
		case '/':
		case '+':
			while (stack_pos < strlen(stack))
			{
				/* treating every op as left-associative */
				if (op_priority(input[0]) <= op_priority(stack[stack_pos]))
				{
					write_to_result(result, ' ');
					write_to_result(result, stack[stack_pos++]);
				}
				else break;
			}

			write_to_result(result, ' ');

		case '(':
			write_to_stack(stack, input[0], &stack_pos);
			break;
		case ')':
			while(stack_pos <= strlen(stack))
			{
				if (stack[stack_pos] != '(')
				{
					write_to_result(result, ' ');
					write_to_result(result, stack[stack_pos++]);
				}
				else
				{
					 stack_pos++;
					 break;
				}
			}
		case ' ':
		case '\n':
			break;
		default:
			add_space_to_result(result);
			/*
			 * We only checked first character of expression (input[0])
			 * Now let's check if there are other characters in expression
			 */
			for (int i = 0; i < strlen(input); i++)
			{
				/* When there's no space around parenthesis
				 * easier testing
				 */
				if (!isspace(input[i]) && (input[i] != ')'))
				{
					write_to_result(result, input[i]);
				}
				else
				{
					input_pos = i + ((input[i] != ')') ? (1) : (0));
					break;
				}
			}
			break;
	}

	/* Moving offset to first nonwhite character in input */
	while ((input_pos < strlen(input)) && (isspace(input[input_pos]))) input_pos++;

	spawn_worker(input + input_pos, stack + stack_pos, result);

	return 0;
}