c - How to properly parse numbers in an arithmetic expression, differentiating positive and negative ones? -


i have assignment in data structures class in have program calculator solves arithmetic expressions 4 basic operations , parenthesis, input done via stdin buffer , same output.

it easy @ beginning, teacher provided algorithms (how transform expression infix postfix , how evaluate it) , goal implement our own stack , use it, calculator not work quite well, , think because of parser.

this algorithm, , code, used parse numbers, operators , parenthesis while putting them array store expression in way easier evaluate later.

// saida array of pairs of integers, first value of pair value of info (the number or ascii value of operator) // second value indicator of whether number or operator (i = 0; < exp_size; i++) {     c = expression[i];      // if current char digit, store helper string , keep going until non-digit found     // atoi() used transform string int , store it.     if (c >= '0' && c <= '9') {         j = 1, k = i+1;         tempint[0] = c;         while(expression[k] >= '0' && expression[k] <= '9') {             tempint[j++] = expression[k];             k++;         }         tempint[j] = '\0';         saida[saidaindex][0] = atoi(tempint);         saida[saidaindex++][1] = 0;         = k-1;     }      // if character operator, algorithm followed.     else if (c == '+' || c == '-' || c == '*' || c == '/') {         while(pilha->size > 0 && isopbigger( stack_top(pilha), c )) {             saida[saidaindex][0] = stack_pop(pilha);             saida[saidaindex++][1] = 1;         }         stack_push(c, pilha);     }     else if (c == '(') stack_push(c, pilha);     else if (c == ')') {         j = stack_pop(pilha);         while(j != '(') {             saida[saidaindex][0] = j;             saida[saidaindex++][1] = 1;             j = stack_pop(pilha);         }     } } 

the problem is, in code can't tell if minus sign indicates subtraction operator or negative number (i know minus operator sum negative number, didn't helped me solving problem), thought of following, none success:

  • every time there minus sign, store plus sign instead , let next number * -1. wouldn't work in case next number negative 1 or if minus sign before expression parenthesis 1 + 2 - (3 * 4).
  • if there space after minus sign, operator, if not, negative number. wouldn't work because there not rules on input.

i have no experience whatsoever in interpreters , don't know how proceed. code works valid expressions without negative numbers, not weird ones () + 3 - (), that's problem.

thanks help.

that problem called "unary minus" , can in case (no variables) solved substitution.

the operator - unary minus if is

  • preceded left parenthesis
  • preceded operator
  • the first character of input

now instead of storing - store different character like, m , assign higher precedence other operators (or same exponentiation operator if have one).

another tip: don't use spaces indicate anything, arithmetic expression must work without spaces or not correct.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -