The user enters a formula to calculate the value. Only simple arithmetic operations and integers are allowed in the string. Addition, subtraction, multiplication and division. C ++ language. Examples: "1 + 1", "10 + 5 * 100/5555" and the like. I saw a long time ago that this can be implemented via Boost.Python, but I have no clue.

Closed due to the fact that Nicolas Chabanovsky’s participant ♦ 15 Dec '16 at 6:18 is incomprehensible.

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Read about the reverse Polish post - Anton Shchyrov
  • one
    Well, take and write a full-fledged parser. As a good job course. - VladD

1 answer 1

Well, here there is such a ready code:

#include <stdio.h> #include <stdlib.h> #include <math.h> #define BUF_SIZE 1024 // <Ρ†ΠΈΡ„Ρ€Π°> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' // <число> ::= <Ρ†ΠΈΡ„Ρ€Π°> { <Ρ†ΠΈΡ„Ρ€Π°> } [ '.' <Ρ†ΠΈΡ„Ρ€Π°> { <Ρ†ΠΈΡ„Ρ€Π°> } ] // // <Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅> ::= <слагаСмоС> [ ( '+' | '-' ) <слагаСмоС> ] // <слагаСмоС> ::= <ΠΌΠ½ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒ> [ ( '*' | '/' ) <ΠΌΠ½ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒ> ] // <ΠΌΠ½ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒ> ::= ( <число> | '(' <Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅> ')' ) [ '^' <ΠΌΠ½ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒ> ] double eval(char *str); double number(char *, unsigned *); double expr(char *, unsigned *); double term(char *, unsigned *); double factor(char *, unsigned *); int main() { char str[BUF_SIZE]; printf("Enter expression: "); fgets(str, BUF_SIZE, stdin); printf("Result: %lf\n", eval(str)); return 0; } double eval(char *str) { unsigned i = 0; return expr(str, &i); } double number(char *str, unsigned *idx) { double result = 0.0; double div = 10.0; int sign = 1; if (str[*idx] == '-') { sign = -1; ++*idx; } while (str[*idx] >= '0' && str[*idx] <= '9') { result = result * 10.0 + (str[*idx] - '0'); ++*idx; } if (str[*idx] == '.') { ++*idx; while (str[*idx] >= '0' && str[*idx] <= '9') { result = result + (str[*idx] - '0') / div; div *= 10.0; ++*idx; } } return sign * result; } double expr(char *str, unsigned *idx) { double result = term(str, idx); while (str[*idx] == '+' || str[*idx] == '-') { switch (str[*idx]) { case '+': ++*idx; result += term(str, idx); break; case '-': ++*idx; result -= term(str, idx); break; } } return result; } double term(char *str, unsigned *idx) { double result = factor(str, idx); double div; while (str[*idx] == '*' || str[*idx] == '/') { switch (str[*idx]) { case '*': ++*idx; result *= factor(str, idx); break; case '/': ++*idx; div = factor(str, idx); if (div != 0.0) { result /= div; } else { printf("Division by zero!\n"); exit(-1); } break; } } return result; } double factor(char *str, unsigned *idx) { double result; int sign = 1; if (str[*idx] == '-') { sign = -1; ++*idx; } if (str[*idx] == '(') { ++*idx; result = expr(str, idx); if (str[*idx] != ')') { printf("Brackets unbalanced!\n"); exit(-2); } ++*idx; } else result = number(str, idx); if (str[*idx] == '^') { ++*idx; result = pow(result, factor(str, idx)); } return sign * result; } 

If you do not want to write everything by hand - there are such things as YACC or Bison.