[gcalctool/gcalctool-new-parser] Get percentages working again
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gcalctool/gcalctool-new-parser] Get percentages working again
- Date: Sun, 16 Oct 2011 05:55:35 +0000 (UTC)
commit e0f094bb1de52ca2a6d1fbf2e5c4ffe97445a289
Author: Robert Ancell <robert ancell canonical com>
Date: Sun Oct 16 16:55:27 2011 +1100
Get percentages working again
src/mp-equation.c | 41 +++++++++++++++++++++++++++++++++++------
1 files changed, 35 insertions(+), 6 deletions(-)
---
diff --git a/src/mp-equation.c b/src/mp-equation.c
index 46fd8c1..fa0e7da 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -264,6 +264,7 @@ typedef enum
{
TOKEN_NONE,
TOKEN_NUMBER,
+ TOKEN_PERCENTAGE,
TOKEN_SUPER_NUMBER,
TOKEN_SUB_NUMBER,
TOKEN_ADD,
@@ -275,7 +276,6 @@ typedef enum
TOKEN_ROOT,
TOKEN_CUBE_ROOT,
TOKEN_FOURTH_ROOT,
- TOKEN_PERCENTAGE,
TOKEN_FACTORIAL,
TOKEN_BOOLEAN_AND,
TOKEN_BOOLEAN_OR,
@@ -408,8 +408,6 @@ parse(MPEquationOptions *options, const gchar *expression, GList **tokens)
current_token = TOKEN_CUBE_ROOT;
else if (c == 0x221C /* â */)
current_token = TOKEN_FOURTH_ROOT;
- else if (c == '%')
- current_token = TOKEN_PERCENTAGE;
else if (c == '!')
current_token = TOKEN_FACTORIAL;
else if (c == 0x2227 /* â */)
@@ -468,6 +466,24 @@ parse(MPEquationOptions *options, const gchar *expression, GList **tokens)
}
else if (g_unichar_isxdigit(c) || unichar_issubdigit(c) || unichar_isfraction(c)) {
}
+ else if (c == '%') {
+ Token *t;
+ gchar *string;
+
+ current_token = TOKEN_NONE;
+ t = token_new(TOKEN_PERCENTAGE, token_start, i + 1);
+ string = token_get_string(t);
+ string[strlen (string) - 1] = '\0';
+ if (!mp_set_from_string(string, options->base, &t->value))
+ {
+ mp_divide_integer (&t->value, 100, &t->value);
+ *tokens = g_list_append(*tokens, t);
+ }
+ else
+ return PARSER_ERR_INVALID;
+
+ g_free(string);
+ }
else {
Token *t;
gchar *string;
@@ -670,7 +686,7 @@ replace_block(GList **first, GList **last, GList *start, GList *end, MPNumber *r
static MPErrorCode
has_value (Token *token)
{
- return token->type == TOKEN_EXPRESSION || token->type == TOKEN_NUMBER || token->type == TOKEN_VARIABLE;
+ return token->type == TOKEN_EXPRESSION || token->type == TOKEN_NUMBER || token->type == TOKEN_PERCENTAGE || token->type == TOKEN_VARIABLE;
}
@@ -795,10 +811,23 @@ do_operation (MPEquationOptions *options, GList **first, GList **last, GList *op
switch (o->type) {
case TOKEN_ADD:
- mp_add(&a->value, &b->value, &result);
+ if (b->type == TOKEN_PERCENTAGE)
+ {
+ mp_multiply(&a->value, &b->value, &result);
+ mp_add(&result, &a->value, &result);
+ }
+ else
+ mp_add(&a->value, &b->value, &result);
break;
case TOKEN_SUBTRACT:
- mp_subtract(&a->value, &b->value, &result);
+ if (b->type == TOKEN_PERCENTAGE)
+ {
+ mp_multiply(&a->value, &b->value, &result);
+ mp_invert_sign(&result, &result);
+ mp_add(&result, &a->value, &result);
+ }
+ else
+ mp_subtract(&a->value, &b->value, &result);
break;
case TOKEN_MULTIPLY:
mp_multiply(&a->value, &b->value, &result);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]