Ejemplo n.º 1
0
int doOp(Stack *s, token op)
{
	token roperand = (token)stackPop(s);
	token loperand = (token)stackPop(s);
	number lside = buildNumber(loperand);
	number rside = buildNumber(roperand);
	number ret;
	switch(*op)
	{
		case '^':
			{
				ret = pow(lside, rside);
			}
			break;
		case '*':
			{
				ret = lside * rside;
			}
			break;
		case '/':
			{
				if(rside == 0)
				{
					raise(divZero);
					return -1;
				}
				else
					ret = lside / rside;
			}
			break;
		case '%':
			{
				if(rside == 0)
				{
					raise(divZero);
					return -1;
				}
				else
				{
					ret = (int)(lside / rside);
					ret = lside - (ret * rside);
				}
			}
			break;
		case '+':
			{
				ret = lside + rside;
			}
			break;
		case '-':
			{
				ret = lside - rside;
			}
			break;
	}
	stackPush(s, num2Str(ret));
	return 0;
}
Ejemplo n.º 2
0
QString FLUtil::roundFieldValue(const QVariant &n, const QString &table, const QString &field)
{
  FLTableMetaData *tmd = FLSqlConnections::database()->manager()->metadata(table);
  if (!tmd)
    return 0;
  FLFieldMetaData *fmd = tmd->field(field);
  if (!fmd)
    return 0;
  return buildNumber(n, 'f', fmd->partDecimal());
}
Ejemplo n.º 3
0
void Node::buildStateNumber()
{
    unsigned lessers[NUM_OF_TILES];


    for (unsigned i = 0; i < NUM_OF_TILES; ++i)
    {
        lessers[i] = i;
    }


    stateNumber = buildNumber(tile, NUM_OF_TILES, lessers);
}
Ejemplo n.º 4
0
unsigned Node::buildNumber(unsigned* A, unsigned n, unsigned lessers[])
{
    if (!n)
    {
        return 0;
    }


    unsigned i;


    for (i = *A + 1; i < NUM_OF_TILES; ++i)
    {
        --lessers[i];
    }


    return ((lessers[*A] * factorial(n - 1)) + buildNumber((A + 1), (n - 1), lessers));
}
Ejemplo n.º 5
0
token doFunc(Stack *s, token function)
{
	if (stackSize(s) == 0)
	{
		raise(inputMissing);
		return "NaN";
	}
	else if (stackSize(s) == 1 && strcmp(stackTop(s), FUNCTIONSEPARATOR) == 0)
	{
		stackPop(s);
		raise(inputMissing);
		return "NaN";
	}
	token input = (token)stackPop(s);
	number num = buildNumber(input);
	number result = num;
	number counter = 0;

	if(strncmp(function, "abs", 3) == 0)
		result = fabs(num);
	else if(strncmp(function, "floor", 5) == 0)
		result = floor(num);
	else if(strncmp(function, "ceil", 4) == 0)
		result = ceil(num);
	else if(strncmp(function, "sin", 3) == 0)
		result = !prefs.mode.degrees ? sin(num) : sin(toRadians(num));
	else if(strncmp(function, "cos", 3) == 0)
		result = !prefs.mode.degrees ? cos(num) : cos(toRadians(num));
	else if(strncmp(function, "tan", 3) == 0)
		result = !prefs.mode.degrees ? tan(num) : tan(toRadians(num));
	else if(strncmp(function, "arcsin", 6) == 0
		 || strncmp(function, "asin", 4) == 0)
		result = !prefs.mode.degrees ? asin(num) : toDegrees(asin(num));
	else if(strncmp(function, "arccos", 6) == 0
		 || strncmp(function, "acos", 4) == 0)
		result = !prefs.mode.degrees ? acos(num) : toDegrees(acos(num));
	else if(strncmp(function, "arctan", 6) == 0
		 || strncmp(function, "atan", 4) == 0)
		result = !prefs.mode.degrees ? atan(num) : toDegrees(atan(num));
	else if(strncmp(function, "sqrt", 4) == 0)
		result = sqrt(num);
	else if(strncmp(function, "cbrt", 4) == 0)
		result = cbrt(num);
	else if(strncmp(function, "log", 3) == 0)
		result = log(num);
	else if(strncmp(function, "exp", 3) == 0)
		result = exp(num);
	else if(strncmp(function, "min", 3) == 0)
	{
		while (stackSize(s) > 0 && strcmp(stackTop(s), FUNCTIONSEPARATOR) != 0)
		{
			input = (token)stackPop(s);
			num = buildNumber(input);
			if (num < result)
				result = num;
		}
	}
	else if(strncmp(function, "max", 3) == 0)
	{
		while (stackSize(s) > 0 && strcmp(stackTop(s), FUNCTIONSEPARATOR) != 0)
		{
			input = (token)stackPop(s);
			num = buildNumber(input);
			if (num > result)
				result = num;
		}
	}
	else if(strncmp(function, "sum", 3) == 0)
	{
		while (stackSize(s) > 0  && strcmp(stackTop(s), FUNCTIONSEPARATOR) != 0)
		{
			input = (token)stackPop(s);
			num = buildNumber(input);
			result += num;
		}
	}
	else if(strncmp(function, "avg", 3) == 0 ||
			strncmp(function, "mean", 4) == 0)
	{
		// Result already initialized with first number
		counter = 1;
		while (stackSize(s) > 0  && strcmp(stackTop(s), FUNCTIONSEPARATOR) != 0)
		{
			input = (token)stackPop(s);
			num = buildNumber(input);
			result += num;
			counter++;
		}
		result /= counter;
	}
	else if(strncmp(function, "median", 6) == 0)
	{
		// needed for sorting
		Stack tmp, safe;
		// Result already initialized with first number
		counter = 1;
		stackInit(&tmp, (stackSize(s) > 0 ? stackSize(s) : 1));
		stackInit(&safe, (stackSize(s) > 0 ? stackSize(s) : 1));
		// add first value to the later sorted stack
		stackPush(&tmp, input);
		while (stackSize(s) > 0  && strcmp(stackTop(s), FUNCTIONSEPARATOR) != 0)
		{
			input = (token)stackPop(s);
			num = buildNumber(input);
			// save all numbers larger as the stack value
			while (stackSize(&tmp) > 0 && buildNumber(stackTop(&tmp)) < num)
			{
				stackPush(&safe, stackPop(&tmp));
			}
			// push value on the sorted stack
			stackPush(&tmp, input);
			// push all saved numbers back on the sorted stack
			while (stackSize(&safe) > 0)
			{
				stackPush(&tmp, stackPop(&safe));
			}
			counter++;
		}
		stackFree(&safe);
		// calculate the median index
		counter = (number)(((int)counter+1)/2);
		// pop all numbers until median index
		while (counter > 1)
		{
			stackPop(&tmp);
			counter--;
		}
		result = buildNumber(stackPop(&tmp));
		// pop the remaining sorted stack
		while (stackSize(&tmp) > 0)
		{
			stackPop(&tmp);
		}
		stackFree(&tmp);
	}
	else if(strncmp(function, "var", 3) == 0)
	{
		Stack tmp;
		counter = 1;
		// second stack to store values during calculation of mean
		stackInit(&tmp, (stackSize(s) > 0 ? stackSize(s) : 1));
		// push first value to temporary stack
		stackPush(&tmp, input);
		number mean = result;
		while (stackSize(s) > 0  && strcmp(stackTop(s), FUNCTIONSEPARATOR) != 0)
		{
			input = (token)stackPop(s);
			// push value to temporary stack
			stackPush(&tmp, input);
			num = buildNumber(input);
			mean += num;
			counter++;
		}
		// calculate mean
		mean /= counter;
		result = 0;
		// calculate sum of squared differences
		while (stackSize(&tmp) > 0)
		{
			input = (token)stackPop(&tmp);
			num = buildNumber(input)-mean;
			result += pow(num,2);
		}
		// determine variance
		result /= counter;
		stackFree(&tmp);
	}
	if (strcmp(stackTop(s), FUNCTIONSEPARATOR) == 0)
		stackPop(s);
	stackPush(s, num2Str(result));
	return 0;
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
{
	char* str = NULL;
	token* tokens = NULL;
	int numTokens = 0;
	Stack expr;
	int i;
	int ch, rflag = 0;
	prefs.precision = DEFAULTPRECISION;
	prefs.maxtokenlength = MAXTOKENLENGTH;

	while ((ch = getopt(argc, argv, "rm:")) != -1) {
		switch (ch) {
			case 'r':
				rflag = 1;
				break;
			case 'm':
				prefs.maxtokenlength = atoi(optarg);
		}
	}
	str = ufgets(stdin);
	while(str != NULL && strcmp(str, "quit") != 0)
	{
		if (strlen(str) == 0)
		{
			free(str);
			goto get_new_string;
		}
		if(type(*str) == text)
		{
			// Do something with command
			if (!execCommand(str))
				goto no_command;

			free(str);
			str = NULL;
		}
		else
		{
no_command:
			numTokens = tokenize(str, &tokens);
			free(str);
			str = NULL;

			if(prefs.display.tokens)
			{
				printf("\t%d tokens:\n", numTokens);
				for(i = 0; i < numTokens; i++)
				{
					printf("\t\"%s\"", tokens[i]);
					if(tokenType(tokens[i]) == value)
						printf(" = %f", buildNumber(tokens[i]));
					printf("\n");
				}
			}

			// Convert to postfix
			stackInit(&expr, numTokens);
			if(prefs.display.postfix)
				printf("\tPostfix stack:\n");
			postfix(tokens, numTokens, &expr);
			//stackReverse(&expr);
			/*printf("\tReversed postfix stack:\n\t");
			for(i = 0; i < stackSize(&expr); i++)
			{
				printf("%s ", (token)(expr.content[i]));
			}
			printf("\n");*/
			if(stackSize(&expr) != 1)
			{
				printf("\tError evaluating expression\n");
			}
			else
			{
				if (!rflag)
					printf("\t= ");
				printf("%s\n", (char*)stackTop(&expr));
				for (i=0; i< numTokens; i++)
				{
					if (tokens[i] == stackTop(&expr))
						tokens[i] = NULL;
				}
				free(stackPop(&expr));
			}

			for(i = 0; i < numTokens; i++)
			{
				if (tokens[i] != NULL)
					free(tokens[i]);
			}
			free(tokens);
			tokens = NULL;
			numTokens = 0;
			stackFree(&expr);
		}
get_new_string:
		str = ufgets(stdin);
	}

	free(str);
	str = NULL;


	return EXIT_SUCCESS;
}