| 1 | //: |
| 2 | // \file |
| 3 | // \brief arbitrary precision calculator |
| 4 | // Accepts a C-style numeric expression that is to be parsed (preferably within |
| 5 | // quotes to escape spaces, brackets and other special symbols from the shell); |
| 6 | // it may contain the following: |
| 7 | // optional spaces (are ignored) |
| 8 | // integer and floating point numbers, possibly with scientific notation |
| 9 | // but without decimal point |
| 10 | // operators: + - * / % ! |
| 11 | // brackets () |
| 12 | // the function calls abs(), floor(), ceil(), round(), min(,), max(,), binom(,), pow(,). |
| 13 | // The normal operator precedence applies: (5) brackets(), (4) function call, |
| 14 | // (3) unary + and -, (2) *, / and %, (1) + and -. |
| 15 | // On the same level, the order is left-to-right. |
| 16 | // |
| 17 | // Usage examples: |
| 18 | // calculate '(1e99-1)/9' |
| 19 | // calculate << EOF |
| 20 | // 123! |
| 21 | // 10 * 11 / 12 |
| 22 | // binom(100,50) |
| 23 | // EOF |
| 24 | // echo '33333333333333333333333333333e-5%12345' | calculate |
| 25 | // |
| 26 | // Uses the class vnl_decnum to do the I/O and the arithmetic. |
| 27 | // |
| 28 | // \author Peter Vanroose, ABIS Leuven |
| 29 | // \date August 2011 |
| 30 | // History: Originally written for double precision arithmetic (1996). |
| 31 | |
| 32 | #include <iostream> |
| 33 | #include "vnl/vnl_decnum.h" |
| 34 | |
| 35 | const vnl_decnum zero("0"); |
| 36 | const vnl_decnum one("1"); |
| 37 | const vnl_decnum half("5e-1"); |
| 38 | |
| 39 | vnl_decnum |
| 40 | round(vnl_decnum const & a) |
| 41 | { |
| 42 | return floor(a + half); |
| 43 | } |
| 44 | vnl_decnum |
| 45 | min(vnl_decnum const & a, vnl_decnum const & b) |
| 46 | { |
| 47 | return (a < b) ? a : b; |
| 48 | } |
| 49 | vnl_decnum |
| 50 | max(vnl_decnum const & a, vnl_decnum const & b) |
| 51 | { |
| 52 | return (a < b) ? b : a; |
| 53 | } |
| 54 | vnl_decnum |
| 55 | unaryminus(vnl_decnum const & a) |
| 56 | { |
| 57 | return -a; |
| 58 | } |
| 59 | vnl_decnum |
| 60 | prod(vnl_decnum const & a, vnl_decnum const & b) |
| 61 | { |
| 62 | return a * b; |
| 63 | } |
| 64 | vnl_decnum |
| 65 | quot(vnl_decnum const & a, vnl_decnum const & b) |
| 66 | { |
| 67 | return a / b; |
| 68 | } |
| 69 | vnl_decnum |
| 70 | modulo(vnl_decnum const & a, vnl_decnum const & b) |
| 71 | { |
| 72 | return a % b; |
| 73 | } |
| 74 | vnl_decnum |
| 75 | sum(vnl_decnum const & a, vnl_decnum const & b) |
| 76 | { |
| 77 | return a + b; |
| 78 | } |
| 79 | vnl_decnum |
| 80 | diff(vnl_decnum const & a, vnl_decnum const & b) |
| 81 | { |
| 82 | return a - b; |
| 83 | } |
| 84 | vnl_decnum |
| 85 | fac(vnl_decnum const & a) |
| 86 | { |
| 87 | if (a <= one) |
| 88 | return one; |
| 89 | else |
| 90 | return a * fac(a - one); |
| 91 | } |
| 92 | vnl_decnum |
| 93 | binom(vnl_decnum const & a, vnl_decnum const & b) |
| 94 | { |
| 95 | if (a < zero || b < zero || a < b) |
| 96 | return zero; |
| 97 | return fac(a) / fac(a - b) / fac(b); |
| 98 | } |
| 99 | |
| 100 | using fptr1 = vnl_decnum (*)(const vnl_decnum &); |
| 101 | using fptr2 = vnl_decnum (*)(const vnl_decnum &, const vnl_decnum &); |
| 102 | using fptr3 = vnl_decnum (*)(const vnl_decnum &, unsigned long); |
| 103 | |
| 104 | class node |
| 105 | { |
| 106 | public: |
| 107 | fptr1 func1{ nullptr }; |
| 108 | fptr2 func2{ nullptr }; |
| 109 | fptr3 func3{ nullptr }; |
| 110 | void * param1{ nullptr }; |
| 111 | void * param2{ nullptr }; |
| 112 | node() = default; |
| 113 | }; |
| 114 | |
| 115 | void |
| 116 | ErrorExit(std::string const & expr, char const * t, unsigned long s) |
| 117 | { |
| 118 | std::cerr << "Error parsing expression -- " << t << ":\n" << expr << '\n'; |
| 119 | while (s--) |
| 120 | std::cerr << ' '; |
| 121 | std::cerr << "^\n"; |
| 122 | std::exit(1); |
| 123 | } |
| 124 | |
| 125 | //: find end of argument, i.e., comma or ')' |
| 126 | int |
| 127 | arglength(std::string const & expr, unsigned long s, int l) |
| 128 | { |
| 129 | int i = 0; |
| 130 | int level = 0; |
| 131 | while (i < l) |
| 132 | { |
| 133 | if (expr[s + i] == '(') |
| 134 | ++level; |
| 135 | if (level == 0 && (expr[s + i] == ',' || expr[s + i] == ')')) |
| 136 | break; |
| 137 | if (expr[s + i] == ')') |
| 138 | --level; |
| 139 | ++i; |
| 140 | } |
| 141 | if (i >= l) |
| 142 | ErrorExit(expr, "argument not finished here", s + i); |
| 143 | return i; |
| 144 | } |
| 145 | |
| 146 | //: find matching bracket or other end of block |
| 147 | int |
| 148 | blocklength(std::string const & expr, unsigned long s, int l) |
| 149 | { |
| 150 | int i = 0; |
| 151 | if (expr[s] == '-' || expr[s] == '+') // unary minus or plus |
| 152 | return 1 + blocklength(expr, s + 1, l - 1); |
| 153 | if (expr[s] == '!') |
| 154 | return 1; |
| 155 | if (expr[s] == '(') |
| 156 | { // bracketed subsequence |
| 157 | int level = 1; |
| 158 | while (++i < l) |
| 159 | { |
| 160 | if (expr[s + i] == '(') |
| 161 | ++level; |
| 162 | else if (expr[s + i] == ')') |
| 163 | --level; |
| 164 | if (level == 0) |
| 165 | break; |
| 166 | } |
| 167 | if (i >= l) |
| 168 | ErrorExit(expr, "no matching closing bracket", s); |
| 169 | return i + 1; |
| 170 | } |
| 171 | if (expr[s] >= 'a' && expr[s] <= 'z') |
| 172 | { // function call |
| 173 | for (i = 1; i < l && ((expr[s + i] >= 'a' && expr[s + i] <= 'z') || (expr[s + i] >= '0' && expr[s + i] <= '9')); |
| 174 | ++i) |
| 175 | ; |
| 176 | if (i >= l || expr[s + i] != '(') |
| 177 | ErrorExit(expr, "function call without parameters", s + i); |
| 178 | return i + blocklength(expr, s + i, l - i); |
| 179 | } |
| 180 | if (expr[s] >= '0' && expr[s] <= '9') |
| 181 | { // a number |
| 182 | for (i = 0; i < l && expr[s + i] >= '0' && expr[s + i] <= '9'; ++i) |
| 183 | ; |
| 184 | if (i < l && expr[s + i] == 'e') |
| 185 | { // optional exponent |
| 186 | ++i; |
| 187 | if (i < l && (expr[s + i] == '-' || expr[s + i] == '+')) |
| 188 | ++i; |
| 189 | if (i >= l) |
| 190 | ErrorExit(expr, "Exponent missing", s + i - 1); |
| 191 | while (i < l && expr[s + i] >= '0' && expr[s + i] <= '9') |
| 192 | ++i; |
| 193 | } |
| 194 | return i; |
| 195 | } |
| 196 | if (expr[s] == ')') |
| 197 | { |
| 198 | std::cerr << "Warning: length of closing bracket!\n"; |
| 199 | return 0; |
| 200 | } |
| 201 | std::cerr << "Warning: length 1 returned on " << expr[s] << '\n'; |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | //: put one pair of brackets around expr[s]--expr[s+l-1], except if already bracketed |
| 206 | // THIS IS THE ONLY ROUTINE THAT INSERTS SOMETING (namely a pair of brackets) INTO expr !! |
| 207 | int |
| 208 | enbracket(std::string & expr, unsigned long s, int l) |
| 209 | { |
| 210 | if (l <= 0) |
| 211 | return l; |
| 212 | if (expr[s] == '(' && blocklength(expr, s, l) == l) |
| 213 | return l; |
| 214 | expr.insert(s + l, 1, ')'); |
| 215 | expr.insert(s, 1, '('); |
| 216 | #ifdef DEBUG |
| 217 | std::cerr << "--> " << expr << '\n'; |
| 218 | #endif |
| 219 | return l + 2; |
| 220 | } |
| 221 | |
| 222 | int |
| 223 | isoperator(char c) |
| 224 | { // true if c is not a-z or 0-9. |
| 225 | return (c < 'a' || c > 'z') && (c < '0' || c > '9'); |
| 226 | } |
| 227 | |
| 228 | //: add all level brackets into expr[s]--expr[s+l-1] |
| 229 | // REMARK: expr should not contain spaces or uppercase letters |
| 230 | int |
| 231 | put_brackets(std::string & expr, unsigned long s, int l) |
| 232 | { |
| 233 | int k; |
| 234 | if (l <= 0) |
| 235 | return l; // do nothing on empty string |
| 236 | if (expr[s] == ')') |
| 237 | ErrorExit(expr, "string starts with closing bracket", s); |
| 238 | int j = blocklength(expr, s, l); |
| 239 | if (expr[s] == '(') |
| 240 | { |
| 241 | k = 2 + put_brackets(expr, s + 1, j - 2); // put brackets inside |
| 242 | l += k - j; |
| 243 | j = k; |
| 244 | if (j < l && !isoperator(expr[s + j])) |
| 245 | ErrorExit(expr, "expecting an operator here", s + j); |
| 246 | k = put_brackets(expr, s + j + 1, l - j - 1); // put brackets after |
| 247 | return k + j + 1; |
| 248 | } |
| 249 | |
| 250 | // remove unary +: |
| 251 | if (expr[s] == '+') |
| 252 | expr.erase(s, 1L); |
| 253 | |
| 254 | // process unary minus: |
| 255 | if (expr[s] == '-' && l < 2) |
| 256 | ErrorExit(expr, "nothing following unary minus", s); |
| 257 | if (l < 1) |
| 258 | ErrorExit(expr, "something missing", s); |
| 259 | if (expr[s] == '-' && expr[s + 1] != '(') |
| 260 | { |
| 261 | if (expr[s + 1] == ')') |
| 262 | ErrorExit(expr, "closing bracket after unary -", s + 1); |
| 263 | j = blocklength(expr, s + 1, l - 1); |
| 264 | l += enbracket(expr, s + 1, j) - j; |
| 265 | } |
| 266 | if (expr[s] == '-') |
| 267 | { // now, expr[s+1] == '(' |
| 268 | j = blocklength(expr, s + 1, l - 1); |
| 269 | k = 2 + put_brackets(expr, s + 2, j - 2); // put brackets inside |
| 270 | l += k - j; |
| 271 | j = k; |
| 272 | if (j + 1 < l && !isoperator(expr[s + j + 1])) |
| 273 | ErrorExit(expr, "expecting an operator here", s + j + 1); |
| 274 | k = put_brackets(expr, s + j + 2, l - j - 2); // put brackets after |
| 275 | return k + j + 2; |
| 276 | } |
| 277 | |
| 278 | j = blocklength(expr, s, l); |
| 279 | |
| 280 | // process numbers: are always put between brackets, so scanning is only to be done once |
| 281 | if (expr[s] >= '0' && expr[s] <= '9') |
| 282 | { |
| 283 | k = enbracket(expr, s, j); |
| 284 | l += k - j; |
| 285 | j = k; |
| 286 | if (j < l && !isoperator(expr[s + j])) |
| 287 | ErrorExit(expr, "expecting an operator here", s + j); |
| 288 | k = put_brackets(expr, s + j + 1, l - j - 1); // put brackets after |
| 289 | return k + j + 1; |
| 290 | } |
| 291 | |
| 292 | // process function calls |
| 293 | if (expr[s] >= 'a' && expr[s] <= 'z') |
| 294 | { |
| 295 | for (k = 1; k < j; ++k) |
| 296 | if ((expr[s + k] < 'a' || expr[s + k] > 'z') && (expr[s + k] < '0' || expr[s + k] > '9')) |
| 297 | break; |
| 298 | if (expr[s + k] != '(') |
| 299 | ErrorExit(expr, "opening bracket missing", s + k); |
| 300 | while (k < j - 1) |
| 301 | { // bracket inside each argument |
| 302 | int i = k + 1 + arglength(expr, s + k + 1, j - k - 1); |
| 303 | k += 1 + put_brackets(expr, s + k + 1, i - k - 1); |
| 304 | l += k - i; |
| 305 | j += k - i; |
| 306 | } |
| 307 | if (j < l && !isoperator(expr[s + j])) |
| 308 | ErrorExit(expr, "expecting an operator here", s + j); |
| 309 | k = put_brackets(expr, s + j + 1, l - j - 1); // put brackets after |
| 310 | return k + j + 1; |
| 311 | } |
| 312 | |
| 313 | ErrorExit(expr, "invalid symbol", s); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | //: add operator hierarchy brackets into expr[s]--expr[s+l-1] |
| 318 | // REMARK: expr should already been bracketed by put_brackets() |
| 319 | int |
| 320 | hierarchy_brackets(std::string & expr, unsigned long s, int l) |
| 321 | { |
| 322 | int i, previ, bl, strength = 0; |
| 323 | |
| 324 | if (expr[s] == ')') |
| 325 | ErrorExit(expr, "substring starts with closing bracket", s); |
| 326 | int j = blocklength(expr, s, l); |
| 327 | int is = 0; |
| 328 | while (l > 0 && expr[s] == '(' && j == l && (expr[s + 1] < '0' || expr[s + 1] > '9')) |
| 329 | { |
| 330 | if (expr[s + (++is)] == ')') |
| 331 | ErrorExit(expr, "empty () brackets", s + is); |
| 332 | j = blocklength(expr, s + is, l - 2 * is); |
| 333 | } |
| 334 | if (l - 2 * is <= 0) |
| 335 | return l; // do nothing on empty string |
| 336 | |
| 337 | // find highest precedence operator on outer level |
| 338 | int first = -2, second = -2; |
| 339 | for (previ = -1, i = is; i < l - is; previ = i, i += 1 + bl) |
| 340 | { |
| 341 | if (expr[s + i] == ')') |
| 342 | ErrorExit(expr, "closing bracket after operator", s + i); |
| 343 | bl = blocklength(expr, s + i, l - is - i); |
| 344 | if (previ >= 0) |
| 345 | { |
| 346 | if (strength < 6 && (expr[s + i - 1] == '*' || expr[s + i - 1] == '/' || expr[s + i - 1] == '%')) |
| 347 | strength = 6, second = i, first = previ; |
| 348 | if (strength < 5 && (expr[s + i - 1] == '+' || expr[s + i - 1] == '-')) |
| 349 | strength = 5, second = i, first = previ; |
| 350 | } |
| 351 | if (expr[s + i] == '(' && (expr[s + i + 1] < '0' || expr[s + i + 1] > '9')) |
| 352 | { // no number |
| 353 | int k = hierarchy_brackets(expr, s + i, bl); |
| 354 | l += k - bl; |
| 355 | bl = k; |
| 356 | } |
| 357 | if (expr[s + i] >= 'a' && expr[s + i] <= 'z') |
| 358 | { // function call |
| 359 | int m = i; |
| 360 | while (expr[s + m] != '(' && m < bl + i) |
| 361 | ++m; |
| 362 | ++m; |
| 363 | while (m < bl + i) |
| 364 | { |
| 365 | j = arglength(expr, s + m, bl + i - m); |
| 366 | int k = hierarchy_brackets(expr, s + m, j); |
| 367 | bl += k - j; |
| 368 | l += k - j; |
| 369 | m += 1 + k; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | if (strength <= 0) |
| 374 | return l; // no operators in outer level |
| 375 | if (first == is && second == previ) |
| 376 | return l; // only one prefix operator |
| 377 | if (expr[s + second] == ')') |
| 378 | ErrorExit(expr, "closing bracket after operator", s + second); |
| 379 | second += blocklength(expr, s + second, l - is - second); |
| 380 | l += enbracket(expr, s + first, second - first) - (second - first); |
| 381 | l = hierarchy_brackets(expr, s, l); |
| 382 | return l; |
| 383 | } |
| 384 | |
| 385 | node |
| 386 | build_tree(std::string const & expr, unsigned long s, int l) |
| 387 | { |
| 388 | int i, j; |
| 389 | node n; |
| 390 | // expr[s]--expr[s+l-1] is any of: |
| 391 | // (number) |
| 392 | // function() |
| 393 | // (-()) |
| 394 | // (() operator ()) |
| 395 | if (expr[s] == ')') |
| 396 | ErrorExit(expr, "string begins with closing bracket", s); |
| 397 | while (l > 0 && expr[s] == '(' && blocklength(expr, s, l) == l) |
| 398 | ++s, l -= 2; |
| 399 | if (l <= 0) |
| 400 | ErrorExit(expr, "empty block", s); |
| 401 | if (expr[s] == ')') |
| 402 | ErrorExit(expr, "empty ()", s); |
| 403 | j = blocklength(expr, s, l); |
| 404 | if (j == l && expr[s] >= '0' && expr[s] <= '9') |
| 405 | { // number |
| 406 | n.func1 = nullptr; |
| 407 | n.func2 = nullptr; |
| 408 | n.func3 = nullptr; |
| 409 | n.param2 = nullptr; |
| 410 | n.param1 = (void *)(new vnl_decnum(expr.substr(s, l))); |
| 411 | return n; |
| 412 | } |
| 413 | if (j == l && expr[s] >= 'a' && expr[s] <= 'z') |
| 414 | { // function |
| 415 | for (j = 0; j < l; ++j) |
| 416 | if (expr[s + j] == '(') |
| 417 | break; |
| 418 | ++j; |
| 419 | if (j >= l) |
| 420 | ErrorExit(expr, "missing parameter list for function", s); |
| 421 | else if (expr.substr(s, j) == "abs(") |
| 422 | n.func1 = &vnl_math::abs, n.func2 = nullptr, n.func3 = nullptr; |
| 423 | else if (expr.substr(s, j) == "floor(") |
| 424 | n.func1 = &floor, n.func2 = nullptr, n.func3 = nullptr; |
| 425 | else if (expr.substr(s, j) == "ceil(") |
| 426 | n.func1 = &ceil, n.func2 = nullptr, n.func3 = nullptr; |
| 427 | else if (expr.substr(s, j) == "round(") |
| 428 | n.func1 = &round, n.func2 = nullptr, n.func3 = nullptr; |
| 429 | else if (expr.substr(s, j) == "min(") |
| 430 | n.func1 = nullptr, n.func2 = &min, n.func3 = nullptr; |
| 431 | else if (expr.substr(s, j) == "max(") |
| 432 | n.func1 = nullptr, n.func2 = &max, n.func3 = nullptr; |
| 433 | else if (expr.substr(s, j) == "binom(") |
| 434 | n.func1 = nullptr, n.func2 = &binom, n.func3 = nullptr; |
| 435 | else if (expr.substr(s, j) == "pow(") |
| 436 | n.func1 = nullptr, n.func2 = nullptr, n.func3 = &pow; |
| 437 | else |
| 438 | ErrorExit(expr, "unknown function call", s); |
| 439 | n.param1 = new node(); |
| 440 | n.param2 = nullptr; |
| 441 | for (i = j; i < l - 1; ++i) |
| 442 | if (expr[s + i] == ',') |
| 443 | break; |
| 444 | if (n.func2) |
| 445 | { |
| 446 | n.param2 = new node(); |
| 447 | if (i > l - 2) |
| 448 | ErrorExit(expr, "function needs two arguments", s); |
| 449 | } |
| 450 | *(node *)(n.param1) = build_tree(expr, s + j, i - j); |
| 451 | j = i + 1; |
| 452 | if (n.param2 == nullptr) |
| 453 | { |
| 454 | if (j < l) |
| 455 | ErrorExit(expr, "function needs only one argument", s); |
| 456 | return n; |
| 457 | } |
| 458 | for (i = j; i < l - 1; ++i) |
| 459 | if (expr[s + i] == ',') |
| 460 | break; |
| 461 | if (i < l - 1) |
| 462 | ErrorExit(expr, "function needs only two arguments", s); |
| 463 | *(node *)(n.param2) = build_tree(expr, s + j, i - j); |
| 464 | return n; |
| 465 | } |
| 466 | if (expr[s] == '-') |
| 467 | { // unary minus |
| 468 | n.func1 = &unaryminus; |
| 469 | n.param1 = new node(build_tree(expr, s + 1, l - 1)); |
| 470 | n.param2 = nullptr; |
| 471 | return n; |
| 472 | } |
| 473 | |
| 474 | // now, expr[s]-- should be of the form "BLOCK operator BLOCK" or "BLOCK !" |
| 475 | n.param1 = new node(build_tree(expr, s, j)); |
| 476 | if (expr[s + j] == '!') |
| 477 | { |
| 478 | n.func1 = &fac; |
| 479 | return n; |
| 480 | } |
| 481 | n.param2 = new node(build_tree(expr, s + j + 1, l - j - 1)); |
| 482 | if (expr[s + j] == '*') |
| 483 | n.func1 = nullptr, n.func2 = &prod, n.func3 = nullptr; |
| 484 | else if (expr[s + j] == '/') |
| 485 | n.func1 = nullptr, n.func2 = ", n.func3 = nullptr; |
| 486 | else if (expr[s + j] == '%') |
| 487 | n.func1 = nullptr, n.func2 = &modulo, n.func3 = nullptr; |
| 488 | else if (expr[s + j] == '+') |
| 489 | n.func1 = nullptr, n.func2 = &sum, n.func3 = nullptr; |
| 490 | else if (expr[s + j] == '-') |
| 491 | n.func1 = nullptr, n.func2 = &diff, n.func3 = nullptr; |
| 492 | else |
| 493 | ErrorExit(expr, "unknown operator", s + j); |
| 494 | return n; |
| 495 | } |
| 496 | |
| 497 | //: Return str, but after removing spaces, making lowercase, and adding () |
| 498 | std::string |
| 499 | simplify(char const * str) |
| 500 | { |
| 501 | std::string expr = ""; |
| 502 | // remove all spaces and tabs: |
| 503 | for (int i = 0; str[i]; ++i) |
| 504 | if (str[i] != ' ' && str[i] != '\t') |
| 505 | expr.push_back(str[i]); |
| 506 | int l = expr.length(); |
| 507 | // make lowercase |
| 508 | for (int i = 0; i < l; ++i) |
| 509 | { |
| 510 | if (expr[i] >= 'A' && expr[i] <= 'Z') |
| 511 | expr[i] += 'a' - 'A'; |
| 512 | } |
| 513 | l = put_brackets(expr, 0L, l); // all grouping except operator precedence |
| 514 | // EXAMPLE: |
| 515 | // -2 * abs (32*pow(32,2)) + 7*32 -(8-32) |
| 516 | // --> (-(2))*abs(32*pow(32,(2)))+(7)*32-((8)-32) |
| 517 | hierarchy_brackets(expr, 0L, l); // put brackets according to o.p. |
| 518 | // EXAMPLE: |
| 519 | // (-(2))*abs(32*pow(32,(2)))+(7)*32-((8)-32) |
| 520 | // --> (((-(2))*abs(32*pow(32,(2))))+((7)*32))-((8)-32) |
| 521 | return expr; |
| 522 | } |
| 523 | |
| 524 | vnl_decnum |
| 525 | calc(node * n) |
| 526 | { |
| 527 | if (!n) |
| 528 | ErrorExit("", "This should not happen...", 0L); |
| 529 | else if (!n->func1 && !n->func2 && !n->func3) |
| 530 | return *(vnl_decnum *)(n->param1); |
| 531 | else if (n->func3 && n->param1 && n->param2) |
| 532 | return n->func3(calc((node *)n->param1), (unsigned long)calc((node *)n->param2)); |
| 533 | else if (n->param1 && n->param2) |
| 534 | return n->func2(calc((node *)n->param1), calc((node *)n->param2)); |
| 535 | else if (n->param1 && n->func1) |
| 536 | return n->func1(calc((node *)n->param1)); |
| 537 | else if (n->param2 && n->func1) |
| 538 | return n->func1(calc((node *)n->param2)); |
| 539 | else |
| 540 | ErrorExit("", "This should not happen!", 0L); |
| 541 | return vnl_decnum("0"); // never reached... |
| 542 | } |
| 543 | |
| 544 | int |
| 545 | main(int argc, char * argv[]) |
| 546 | { |
| 547 | if (argc > 2) |
| 548 | { |
| 549 | std::cerr << "Syntax: calculate expression\n"; |
| 550 | return 1; |
| 551 | } |
| 552 | if (argc == 2) |
| 553 | { |
| 554 | std::string expression = simplify(argv[1]); // bracketed and cleaned up version of argv[1] |
| 555 | node root = build_tree(expression, 0L, expression.length()); |
| 556 | vnl_decnum result = calc(&root); |
| 557 | std::cout << result; |
| 558 | unsigned long len = result.data().length(); |
| 559 | result.compactify(); |
| 560 | if (result.data().length() < len) |
| 561 | std::cout << " = " << result; |
| 562 | result += 1L; |
| 563 | result -= 1L; |
| 564 | if (result.data().length() > len) |
| 565 | std::cout << " = " << result; |
| 566 | std::cout << std::endl; |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | char s[4000]; |
| 571 | while (true) |
| 572 | { |
| 573 | s[0] = '\0'; |
| 574 | std::cin.getline(s, 4000); |
| 575 | if (!s[0]) |
| 576 | return 0; // empty line, or EOF |
| 577 | std::string expression = simplify(s); |
| 578 | node root = build_tree(expression, 0L, expression.length()); |
| 579 | vnl_decnum result = calc(&root); |
| 580 | std::cout << s << "\t=\t" << result; |
| 581 | result.compactify(); |
| 582 | unsigned long len = result.data().length(); |
| 583 | result.compactify(); |
| 584 | if (result.data().length() < len) |
| 585 | std::cout << " = " << result; |
| 586 | result += 1L; |
| 587 | result -= 1L; |
| 588 | if (result.data().length() > len) |
| 589 | std::cout << " = " << result; |
| 590 | std::cout << std::endl; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |