int parentheses(int n, int l, int r, char *output) { int rvl = 0; if (l == n) { while (n - r) { output[l + r] = ')'; r++; } rvl += 1; int i; printf("%s, ", output); } else { output[l + r] = '('; rvl += parentheses(n, l + 1, r, output); if (l > r) { output[l + r] = ')'; rvl += parentheses(n, l, r + 1, output); } } return rvl; }
bool Parser::atomic(std::string::iterator& iter, double& val) { auto localIter = iter; double localValue = 0; try { // Try to evaluate a number if (number(localIter, localValue)) { iter = localIter; val = localValue; return true; } // If there is no number then try to evaluate an expression if (parentheses(localIter, localValue)) { iter = localIter; val = localValue; return true; } return false; } // If there was an exception in parentheses function then rethrow it catch (std::exception& e) { throw e; } }
int main() { int n = 3; char *output = malloc(sizeof(char)*(1+n)); output[n] = '\0'; int ret = parentheses(n, 0, 0, output); printf("\n# of parentheses = %d\n", ret); return 0; }