feat: add support for exponentiation operator (^) in calculator
This commit is contained in:
@@ -38,14 +38,31 @@ static double parse_factor(Parser *p) {
|
||||
return val;
|
||||
}
|
||||
|
||||
static double parse_term(Parser *p) {
|
||||
static double parse_exponent(Parser *p) {
|
||||
double left = parse_factor(p);
|
||||
skip_ws(p);
|
||||
if (p->buffer[p->pos] == '^') {
|
||||
p->pos++;
|
||||
double right = parse_exponent(p);
|
||||
double old = left;
|
||||
left = pow(old, right);
|
||||
|
||||
char step[256];
|
||||
snprintf(step, sizeof(step), "<div>%g ^ %g = <b>%g</b></div>", old, right,
|
||||
left);
|
||||
strncat(logic_log, step, sizeof(logic_log) - strlen(logic_log) - 1);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
static double parse_term(Parser *p) {
|
||||
double left = parse_exponent(p);
|
||||
while (1) {
|
||||
skip_ws(p);
|
||||
char op = p->buffer[p->pos];
|
||||
if (op == '*' || op == '/') {
|
||||
p->pos++;
|
||||
double right = parse_factor(p);
|
||||
double right = parse_exponent(p);
|
||||
double old = left;
|
||||
left = (op == '*') ? left * right : left / right;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user