Created
April 22, 2025 14:27
-
-
Save fabianbaechli/964fb2af90d9abfdd2ea3face1e77c39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool needsParentheses(struct node* p, bool rgt) { | |
return ( | |
// (a + b) / c | |
!rgt && precedence(p) < precedence(p->lft)) || | |
// case where right operation has precedence | |
// for example: a + (b * c) | |
(rgt && precedence(p) < precedence(p->rgt)) || | |
// case where the precedence is equal | |
// for example: a - (b - c) | |
( | |
rgt && precedence(p) == precedence(p->rgt) && | |
(p->val == "-" || p->val == "/") | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment